DigitDev.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. # self.dev = cv2.VideoCapture(0)
  11. print("dev = " + str(self.dev))
  12. def run(self):
  13. while not self.is_over:
  14. # 图像抓取
  15. status, image = self.dev.read()
  16. print("image = " + str(image))
  17. # 状态判定
  18. if not status:
  19. self.dev.release()
  20. self.exit(0)
  21. # 如果抓取图像成功,发送
  22. shape = image.shape
  23. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  24. self.signal_video.emit(shape[0], shape[1], shape[2], image.tobytes())
  25. # 视觉暂停
  26. QThread.usleep(100000)
  27. def close(self):
  28. self.is_over = True
  29. while self.isRunning():
  30. pass
  31. # 释放设备
  32. if self.dev.isOpened():
  33. self.dev.release()