video.py 812 B

12345678910111213141516171819202122232425262728293031
  1. from PyQt5.QtCore import QThread, pyqtSignal
  2. import cv2
  3. class DThread(QThread):
  4. signal_video = pyqtSignal(bytes, int, int, int)
  5. def __init__(self):
  6. super(DThread, self).__init__()
  7. self.is_over = False
  8. self.dev = cv2.VideoCapture(0, cv2.CAP_DSHOW)
  9. def run(self):
  10. while not self.is_over:
  11. status, img = self.dev.read()
  12. if status:
  13. # 处理并发送
  14. h, w, c = img.shape
  15. self.signal_video.emit(img.tobytes(), h, w, c)
  16. else:
  17. print("设备出错")
  18. break
  19. QThread.usleep(100000)
  20. def close(self):
  21. self.is_over = True
  22. while self.isRunning():
  23. pass
  24. print("线程正常退出")
  25. self.dev.release()