opencv 如何修复我的相机,使图像不会保持静止?

ozxc1zmp  于 2023-02-05  发布在  其他
关注(0)|答案(1)|浏览(126)

我正在做一个项目,我必须使用秒表,同时打开相机和检测手,我已经有两个代码分别,但当我把它们放在一起,相机不工作,它保持静态,只有计时器工作,你能帮我:(
手部检测代码:

import cv2
from cvzone.HandTrackingModule import HandDetector

frame = cv2.VideoCapture(0)
#frame.set(3, 1280)
#frame.set(4, 720)

# initialize hand detector module with some confidence
handDetector = HandDetector(detectionCon=0.8)

# loop
while True:
    # Read the frames from webcam
    res, img = frame.read()
    # detect the hands, by default it will detect two hands
    hands = handDetector.findHands(img)
    
    
    # show the output
    cv2.imshow("Hands", img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
            break
frame.release()
cv2.destroyAllWindows()

秒表代码:

import tkinter as tk
from datetime import datetime

class stopwatch(tk.Frame):
    def __init__(self,window=None):
        super().__init__(window)
        self.window = window
        self.new_time = ''
        self.running= False
        self.total_hours = 0
        self.total_minutes = 0
        self.total_seconds = 0
        self.pack()
        self.features()
    
    def features(self):
        self.stopwatch_label = tk.Label(self,text='00:00:00',background="black",foreground="white",font=('arial',85,"bold"))
        self.stopwatch_label.pack()

        self.start_time_button = tk.Button(self,text="START",height=5,width=7,font=('arial',19,"bold"),background="green", command=self.start_time)
        self.start_time_button.pack(side=tk.LEFT)

        self.stop_time_button = tk.Button(self,text="STOP",height=5,width=7,font=('arial',19,"bold"),background="red", command=self.pause_time)
        self.stop_time_button.pack(side=tk.LEFT)

        self.reset_time_button = tk.Button(self,text="RESET",height=5,width=7,font=('arial',19,"bold"),background="yellow",command=self.reset_time)
        self.reset_time_button.pack(side=tk.LEFT)

        self.quit_button =tk.Button(self,text="QUIT",height=5, width=7,font=('arial',19,"bold"),background="blue",command=self.window.quit)
        self.quit_button.pack(side=tk.LEFT)

        self.window.title('STOPWATCH')
    
    def start_time(self):
        if not self.running:
            self.stopwatch_label.after(1000)
            self.change()
            self.running = True
    
    def pause_time(self):
        if self.running:
            self.stopwatch_label.after_cancel(self.new_time)
            self.running == False
    
    def reset_time(self):
        if self.running:
            self.stop_time_button.after_cancel(self.new_time)
            self.running = False
            self.total_hours, self.total_minutes, self.total_seconds = 0,0,0
            self.stopwatch_label.config(text="00:00:00")
    
    def change(self):
        self.total_seconds += 1
        if self.total_seconds == 60:
            self.total_minutes += 1
            self.total_seconds = 0
        if self.total_minutes == 60:
            self.total_hours += 1
            self.total_minutes = 0

        total_hours_string = f"{self.total_hours}" if self.total_hours>9 else f'0{self.total_hours}'
        total_minutes_string = f"{self.total_minutes}" if self.total_minutes>9 else f'0{self.total_minutes}'
        total_seconds_string = f"{self.total_seconds}" if self.total_seconds>9 else f'0{self.total_seconds}'
        
        self.stopwatch_label.config(text=total_hours_string + ':' + total_minutes_string+ ':' + total_seconds_string)
  
        self.new_time = self.stopwatch_label.after(100,self.change)

root = tk.Tk()
obj = stopwatch(window = root)
obj.mainloop()

下面是我的最终代码:

import tkinter as tk
from datetime import datetime
import cv2
from cvzone.HandTrackingModule import HandDetector

frame = cv2.VideoCapture(0)
handDetector = HandDetector(detectionCon=0.8)
# Read the frames from webcam
while True:
    res, img = frame.read()
    hands = handDetector.findHands(img)
    
    class stopwatch(tk.Frame):
        def __init__(self,window=None):
            super().__init__(window)
            self.window = window
            self.new_time = ''
            self.running= False
            self.total_hours = 0
            self.total_minutes = 0
            self.total_seconds = 0
            self.pack()
            self.features()
        
        def features(self):
            self.stopwatch_label = tk.Label(self,text='00:00:00',background="black",foreground="white",font=('arial',85,"bold"))
            self.stopwatch_label.pack()
            
            self.start_time_button = tk.Button(self,text="START",height=5,width=7,font=('arial',19,"bold"),background="green", command=self.start_time)
            self.start_time_button.pack(side=tk.LEFT)
            
            self.stop_time_button = tk.Button(self,text="STOP",height=5,width=7,font=('arial',19,"bold"),background="red", command=self.pause_time)
            self.stop_time_button.pack(side=tk.LEFT)
            
            self.reset_time_button = tk.Button(self,text="RESET",height=5,width=7,font=('arial',19,"bold"),background="yellow",command=self.reset_time)
            self.reset_time_button.pack(side=tk.LEFT)
            
            self.quit_button =tk.Button(self,text="QUIT",height=5, width=7,font=('arial',19,"bold"),background="blue",command=self.window.quit)
            self.quit_button.pack(side=tk.LEFT)
            
            self.window.title('STOPWATCH')
        
        def start_time(self):
            if not self.running:
                self.stopwatch_label.after(1000)
                self.change()
                self.running = True
        
        def pause_time(self):
            if self.running:
                self.stopwatch_label.after_cancel(self.new_time)
                self.running == False
        
        def reset_time(self):
            if self.running:
                self.stop_time_button.after_cancel(self.new_time)
                self.running = False
                self.total_hours, self.total_minutes, self.total_seconds = 0,0,0
                self.stopwatch_label.config(text="00:00:00")
        
        def change(self):
            self.total_seconds += 1
            if self.total_seconds == 60:
                self.total_minutes += 1
                self.total_seconds = 0
            if self.total_minutes == 60:
                self.total_hours += 1
                self.total_minutes = 0
            
            total_hours_string = f"{self.total_hours}" if self.total_hours>9 else f'0{self.total_hours}'
            total_minutes_string = f"{self.total_minutes}" if self.total_minutes>9 else f'0{self.total_minutes}'
            total_seconds_string = f"{self.total_seconds}" if self.total_seconds>9 else f'0{self.total_seconds}'
            
            self.stopwatch_label.config(text=total_hours_string + ':' + total_minutes_string+ ':' + total_seconds_string)
            self.new_time = self.stopwatch_label.after(100,self.change)
    
    root = tk.Tk()
    obj = stopwatch(window = root)
    cv2.waitKey(1)
    cv2.imshow("Hands", img)
    obj.mainloop()

我已经尝试了不同的方式teh代码把它放在一起,但最后相机仍然不能很好地工作

noj0wjuj

noj0wjuj1#

不建议在tkinter应用程序中运行while循环,因为它会阻止tkinter mainloop处理挂起的事件和更新。
建议改为在线程中运行摄像头任务:

from datetime import datetime
import threading
import tkinter as tk
import cv2
from cvzone.HandTrackingModule import HandDetector

# define the camera task in a function that to be called in a thread
def hand_detection(e):
    frame = cv2.VideoCapture(0)

    # initialize hand detector module with some confidence
    handDetector = HandDetector(detectionCon=0.8)

    # loop
    while not e.is_set():
        # Read the frames from webcam
        res, img = frame.read()
        # detect the hands, by default it will detect two hands
        hands = handDetector.findHands(img)
        # show the output
        cv2.imshow("Hands", img)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    frame.release()
    cv2.destroyAllWindows()

class stopwatch(tk.Frame):
    def __init__(self,window=None):
        super().__init__(window)
        self.window = window
        self.new_time = ''
        self.running= False
        self.total_hours = 0
        self.total_minutes = 0
        self.total_seconds = 0
        self.pack()
        self.features()
    
    def features(self):
        self.stopwatch_label = tk.Label(self,text='00:00:00',background="black",foreground="white",font=('arial',85,"bold"))
        self.stopwatch_label.pack()

        self.start_time_button = tk.Button(self,text="START",height=5,width=7,font=('arial',19,"bold"),background="green", command=self.start_time)
        self.start_time_button.pack(side=tk.LEFT)

        self.stop_time_button = tk.Button(self,text="STOP",height=5,width=7,font=('arial',19,"bold"),background="red", command=self.pause_time)
        self.stop_time_button.pack(side=tk.LEFT)

        self.reset_time_button = tk.Button(self,text="RESET",height=5,width=7,font=('arial',19,"bold"),background="yellow",command=self.reset_time)
        self.reset_time_button.pack(side=tk.LEFT)

        self.quit_button =tk.Button(self,text="QUIT",height=5, width=7,font=('arial',19,"bold"),background="blue",command=self.window.quit)
        self.quit_button.pack(side=tk.LEFT)

        self.window.title('STOPWATCH')
    
    def start_time(self):
        if not self.running:
            self.stopwatch_label.after(1000)
            self.change()
            self.running = True
    
    def pause_time(self):
        if self.running:
            self.stopwatch_label.after_cancel(self.new_time)
            self.running == False
    
    def reset_time(self):
        if self.running:
            self.stop_time_button.after_cancel(self.new_time)
            self.running = False
            self.total_hours, self.total_minutes, self.total_seconds = 0,0,0
            self.stopwatch_label.config(text="00:00:00")
    
    def change(self):
        self.total_seconds += 1
        if self.total_seconds == 60:
            self.total_minutes += 1
            self.total_seconds = 0
        if self.total_minutes == 60:
            self.total_hours += 1
            self.total_minutes = 0

        total_hours_string = f"{self.total_hours}" if self.total_hours>9 else f'0{self.total_hours}'
        total_minutes_string = f"{self.total_minutes}" if self.total_minutes>9 else f'0{self.total_minutes}'
        total_seconds_string = f"{self.total_seconds}" if self.total_seconds>9 else f'0{self.total_seconds}'
        
        self.stopwatch_label.config(text=total_hours_string + ':' + total_minutes_string+ ':' + total_seconds_string)
  
        self.new_time = self.stopwatch_label.after(100,self.change)

root = tk.Tk()
obj = stopwatch(window = root)

# start the hand detection in a thread
e = threading.Event()
t = threading.Thread(target=hand_detection, args=(e,))
t.start()

obj.mainloop()

# kill the hand detection thread
e.set()
t.join()

相关问题