DigitDev.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from PyQt5.QtCore import QThread, pyqtSignal
  2. import cv2
  3. class DigitDev(QThread):
  4. signal_video = pyqtSignal(int, int, int, bytes)
  5. def __init__(self):
  6. super(DigitDev, self).__init__()
  7. self.is_over = False
  8. # 初始化设备
  9. self.dev = cv2.VideoCapture(0, cv2.CAP_DSHOW)
  10. def run(self):
  11. while not self.is_over:
  12. # 图像抓取
  13. status, image = self.dev.read()
  14. # 状态判定
  15. if not status:
  16. self.dev.release()
  17. self.exit(0)
  18. # 如果抓取图像成功,发送
  19. shape = image.shape
  20. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  21. self.signal_video.emit(shape[0], shape[1], shape[2], image.tobytes())
  22. # 视觉暂停
  23. QThread.usleep(100000)
  24. def close(self):
  25. self.is_over = True
  26. while self.isRunning():
  27. pass
  28. # 释放设备
  29. if self.dev.isOpened():
  30. self.dev.release()