def Firing_run(self, button_id): self.bool = True self.cap = cv2.VideoCapture(0) t = threading.Thread(target=self.windows_display) t.start() button_id.config(text='关闭摄像头', command=lambda: self.Cease_stop(button_id)) def Cease_stop(self, button_id): self.bool = False self.label.destroy() self.canvas2.delete("all") self.label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1) self.label.place(relx=0.32, rely=0.50, anchor='nw') button_id.config(text='打开摄像头', command=lambda: self.Firing_run(button_id)) def windows_display(self): if self.bool: ref, frame = self.cap.read() if ref: self.label.destroy() self.canvas2.delete("all") image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) else: self.cap = cv2.VideoCapture(0) self.label.destroy() self.label = tk.Label(self.canvas2, text='未找到摄像头!', font=("黑体", 25), width=15, height=1) self.label.place(relx=0.32, rely=0.50, anchor='nw') self.canvas2.update() self.canvas2.after(15, self.windows_display)怎么停止self.windows_display
时间: 2024-02-14 22:18:57 浏览: 79
可以通过调用Cease_stop方法来停止self.windows_display。在Cease_stop方法中,将self.bool设置为False,这会导致self.windows_display停止递归调用。同时,删除当前self.label并清空self.canvas2。最后,将button_id的文本和命令设置为打开摄像头,并将其命令设置为self.Firing_run(button_id)。
相关问题
import cv2 import tkinter as tk from tkinter import * from PIL import Image, ImageTk#图像控件 from Background.Thread_Demo import Thread_data from Data_demo import data_demo class Camera(): def __init__(self, top): self.top = top self.h = int(data_demo.window_height // 1.5) self.w = int(data_demo.window_width // 1.74) self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h, highlightthickness=2, highlightbackground='Black') self.canvas2.place(relx=0.0, rely=0.032) self.label = tk.Label(self.canvas2, text='摄像头显示区!', font=("黑体", 25), width=15, height=1) self.label.place(relx=0.38, rely=0.45, anchor='nw') # 启动摄像头按钮控件 def Firing_Camera(self): self.label.config(text="等待连接....") self.Firing_Camera1() def Firing_Camera1(self): self.cap = cv2.VideoCapture(0) ret, frame = self.cap.read() if ret: self.running = True self.thread = threading.Thread(target=self.update, args=()) self.thread.start() else: messagebox.showinfo('提示', '请检查摄像头是否接入或状态是否正常') def update(self): while self.running: ret, frame = self.cap.read() if ret: # process the frame here # ... # convert the frame to an RGB image image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(image) # update the tkinter label for widget in self.canvas2.winfo_children(): widget.destroy() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) self.photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=self.photo, anchor=tk.NW) # wait for a fixed amount of time before capturing the next frame self.canvas2.after(15, self.update)为什么摄像头不动
可能是以下原因导致摄像头不动:
1. 摄像头没有接好或者连接错误,需要检查摄像头连接是否正确。可以尝试更换USB接口或者更换摄像头试试。
2. 摄像头的驱动程序没有安装或者不兼容,需要安装或更新摄像头驱动程序。
3. 摄像头被其他程序占用,需要关闭其他占用摄像头的程序,或者重启电脑后再尝试启动摄像头。
4. 摄像头硬件故障,需要更换摄像头或者修理摄像头。
import numpy as np class LIF_Neuron: ''' 一个带漏电流的积分放电模型的类,该类中包含了 a. 模型的偏微分方程描述, b. 单步的数值积分。 ''' def init(self, C,g_leak, E_leak, E_thresh): self.C=C self.g_leak = g_leak self.E_leak =E_leak self.E_thresh = E_thresh def derivative(self, state, inputs=0): v = state # TODO 偏微分方程 Dv = (inputs-self.g_leak * (v - self.E_leak)) / self.C return np.array([Dv]) def step(self, state, dt, inputs=0): state_new = rk4(dt, state, inputs, self.derivative) # TODO 超过阈值后的重置: if state_new[0] >= self.E_thresh: state_new[0] = self.E_leak return state_new 对于上述类注入逐渐增强的电流,观察脉冲发放频率的变化,将结果绘图。
很好,现在我们来注入逐渐增强的电流,并记录神经元的膜电位。具体代码如下:
```python
import matplotlib.pyplot as plt
# 定义模拟时间
t_max = 1000 # 模拟时间,单位为毫秒
dt = 0.001 # 步长,单位为毫秒
time = np.arange(0, t_max, dt)
# 定义模型参数
C = 1.0
g_leak = 0.1
E_leak = -70.0
E_thresh = -54.0
# 定义初始状态和输入电流
v_init = E_leak
I_max = 1.0 # 注入电流的最大值
I_min = 0.0 # 注入电流的最小值
I_step = 0.1 # 注入电流的步长
I = np.arange(I_min, I_max + I_step, I_step) # 生成注入电流的序列
v = np.zeros(len(time)) # 记录神经元的膜电位
# 创建神经元对象
neuron = LIF_Neuron(C, g_leak, E_leak, E_thresh)
# 注入电流并模拟神经元行为
for i in range(len(I)):
state = np.array([v_init])
spikes = 0 # 记录脉冲发放的次数
for j in range(len(time)):
inputs = I[i]
state = neuron.step(state, dt, inputs)
v[j] = state[0]
if v[j] >= E_thresh:
spikes += 1
state[0] = E_leak
print("I = {}, spikes = {}".format(I[i], spikes))
# 绘制脉冲发放频率的变化图
freq = np.array([spikes / t_max * 1000 for spikes in spikes_list]) # 计算每秒的脉冲发放次数
plt.plot(I, freq)
plt.xlabel("Input Current (nA)")
plt.ylabel("Firing Rate (Hz)")
plt.title("LIF Neuron Firing Rate vs. Input Current")
plt.show()
```
这段代码将会注入逐渐增强的电流,并模拟神经元行为。在每一轮注入电流后,我们将会记录神经元膜电位的变化,如果膜电位超过阈值,我们就认为神经元发放了一次脉冲。最后,我们计算每秒的脉冲发放次数,绘制脉冲发放频率的变化图。
阅读全文