安装opencv-python
pip install opencv-python
开启线程
import cv2
import threading
class cameraThread(threading.Thread):
def __init__(self):
super().__init__()
self.working = True
self.cap = cv2.VideoCapture(0)
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH,640)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT,480)
def run(self):
while self.working:
stau, image = self.cap.read()
cv2.imshow('image',image)
cv2.waitKey(1)
def stop(self):
self.working = False
self.cap.release()
cv2.destroyAllWindows()
ct=cameraThread() #实例化线程
ct.start()#启动线程
关闭线程
ct.stop()#关闭线程
|