这就是你问题的答案
import cv2
import time
from threading import Thread
cap = cv2.VideoCapture(0)
threads = []
class WorkerThread(Thread):
def run(self):
print("started")
while True:
ret, frame = cap.read()
cv2.imshow('Face', frame)
k = cv2.waitKey(5) & 0xFF
if k == ord('q'):
break
if __name__ == '__main__':
try:
print("Trying to open camera")
if(cap.isOpened()):
thread = WorkerThread()
thread.start()
threads.append(thread)
time.sleep(0.35)
except KeyboardInterrupt:
for thread in threads:
thread.join()
cap.release()