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 16:18:57 浏览: 18
可以通过调用Cease_stop方法来停止self.windows_display。在Cease_stop方法中,将self.bool设置为False,这会导致self.windows_display停止递归调用。同时,删除当前self.label并清空self.canvas2。最后,将button_id的文本和命令设置为打开摄像头,并将其命令设置为self.Firing_run(button_id)。
相关问题

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.32, rely=0.50, anchor='nw') self.queue = Queue() # 创建一个队列 def Firing_run(self, button_id): self.bool = True self.label.destroy() 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 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: image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=photo, anchor=tk.NW) self.canvas2.update() self.canvas2.after(15, self.windows_display())用多线程怎么进行优化

可以考虑使用多线程来优化程序,将图像采集和画面显示的任务放到两个不同的线程中执行,避免两个任务之间的阻塞和竞争。具体实现可以按照以下步骤: 1. 创建两个线程,一个用于采集图像数据,一个用于显示图像数据。 2. 采集线程中,使用一个循环不断读取摄像头采集到的图像数据,并将其放入队列中。 3. 显示线程中,从队列中获取图像数据,进行处理和显示,并在一定时间后再次获取队列中的数据进行更新。 4. 在程序运行过程中,可以通过控制标志位的值来控制线程的开启和关闭。 下面是一个使用多线程进行优化的示例代码: ``` import threading import cv2 import tkinter as tk from queue import Queue from PIL import Image, ImageTk 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 = tk.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.32, rely=0.50, anchor='nw') self.queue = Queue() # 创建一个队列 self.bool = False # 控制线程开启和关闭的标志位 def Firing_run(self, button_id): self.bool = True self.label.destroy() self.cap = cv2.VideoCapture(0) t1 = threading.Thread(target=self.capture_image) t2 = threading.Thread(target=self.show_image) t1.start() t2.start() button_id.config(text='关闭摄像头', command=lambda: self.Cease_stop(button_id)) def Cease_stop(self, button_id): self.bool = False button_id.config(text='打开摄像头', command=lambda: self.Firing_run(button_id)) def capture_image(self): while self.bool: ref, frame = self.cap.read() if ref: self.queue.put(frame) def show_image(self): while self.bool: if not self.queue.empty(): frame = self.queue.get() image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image1 = Image.fromarray(image) image2 = image1.resize((self.w, self.h), Image.ANTIALIAS) photo = ImageTk.PhotoImage(image=image2) self.canvas2.create_image(0, 0, image=photo, anchor=tk.NW) self.canvas2.update() else: continue self.canvas2.after(15) ``` 在上面的代码中,我们创建了两个线程,一个用于采集图像数据(capture_image函数),一个用于显示图像数据(show_image函数)。通过队列来进行线程间通信,采集线程从摄像头中读取图像数据,将其放入队列中;显示线程从队列中获取图像数据,并将其进行处理和显示。同时,我们使用了标志位来控制线程的开启和关闭。

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.32, rely=0.50, anchor='nw') def Firing_run(self, button_id): self.bool = True self.label.destroy() self.cap = cv2.VideoCapture(0) t = threading.Thread(target=self.windows_display) t.start() print("1") button_id.config(text='关闭摄像头', command=lambda: self.Cease_stop(button_id)) def Cease_stop(self, button_id): self.bool = False button_id.config(text='打开摄像头', command=lambda: self.Firing_run(button_id)) def windows_display(self): while self.bool: ref, frame = self.cap.read() if ref: 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) self.canvas2.update() self.canvas2.after(10)它这段代码在画布上显示了摄像头画面但是会一闪一闪的怎么修改

这个闪烁的问题可能是由于在`windows_display()`函数中更新画面时,画布的刷新频率和摄像头采集图像的帧率不一致导致的。你可以尝试将`self.canvas2.after(10)`的参数调大一些,比如改成`self.canvas2.after(30)`或者更大的值,这样可以让画布的刷新频率和摄像头采集图像的帧率更接近,减少闪烁的情况。 如果仍然存在闪烁的问题,可以尝试使用双缓冲机制,即在画布上绘制图像之前,先将图像绘制到一个临时的缓冲区,等到图像完全绘制完成后再将缓冲区中的图像显示在画布上。这可以通过在创建画布时添加`doublebuffer=1`参数来实现,即将`self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h, highlightthickness=2, highlightbackground='Black')`改为`self.canvas2 = Canvas(self.top, bg='LightSkyBlue', width=self.w, height=self.h, highlightthickness=2, highlightbackground='Black', doublebuffer=1)`。这样做可以减少闪烁的情况。

相关推荐

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)为什么摄像头不动

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them. Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map. Input The first line contains an integer T (<= 11) which is the number of test cases. For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '', or 'o' which represent Wall, Grass, and Empty, respectively. Output For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.

最新推荐

recommend-type

微软内部资料-SQL性能优化3

Data 2: Index ID; Data 3: Hashed Key Extent (8) Data 1: Extent ID; Data 3: 0. RID (9) Data 1: RID; Data 3: 0. Application (10) Data 1: Application resource name The rsc_bin column of master.....
recommend-type

微信小程序-番茄时钟源码

微信小程序番茄时钟的源码,支持进一步的修改。番茄钟,指的是把工作任务分解成半小时左右,集中精力工作25分钟后休息5分钟,如此视作种一个“番茄”,而“番茄工作法”的流程能使下一个30分钟更有动力。
recommend-type

激光雷达专题研究:迈向高阶智能化关键,前瞻布局把握行业脉搏.pdf

电子元件 电子行业 行业分析 数据分析 数据报告 行业报告
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这