videoplay.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from PyQt5.QtCore import QThread
  2. from PyQt5.QtWidgets import QApplication
  3. from PyQt5.QtCore import pyqtSignal
  4. import cv2
  5. from cores.facepcore import FaceFeatures
  6. import os
  7. current_dir = os.path.dirname(__file__)
  8. avi_file = os.path.join(current_dir, "data/huigexuandetu.mp4")
  9. class CameraPlay(QThread):
  10. # 定义信号发送采集的视频图像
  11. sign_show = pyqtSignal(int, int, int , bytes) # 前面三个是图像高度,宽度,深度,最后是图像数据
  12. def __init__(self, dev_id):
  13. super(CameraPlay, self).__init__()
  14. self.face = FaceFeatures()
  15. # 完成摄像头初始化
  16. self.dev = cv2.VideoCapture(avi_file, cv2.CAP_DSHOW) # 避免报一个警告
  17. # self.dev = cv2.VideoCapture(0) # 避免报一个警告
  18. self.dev.open(avi_file)
  19. self.isOver = False
  20. self.is_landmark = False
  21. # 线程运行函数
  22. def run(self):
  23. while not self.isOver:
  24. # 抓取视频
  25. status, img = self.dev.read()
  26. if not status:
  27. print("读取失败, 结束线程!")
  28. self.dev.release()
  29. self.exit(0)
  30. break
  31. # landmark处理
  32. if self.is_landmark:
  33. self.face.landmark(img)
  34. # 发送图像到窗体显示
  35. self.sign_show.emit(img.shape[0], img.shape[1], img.shape[2], img.tobytes())
  36. QThread.usleep(100000)
  37. def close(self):
  38. # 用来释放线程与设备
  39. self.isOver = True
  40. while self.isRunning(): # 避免提前释放设备导致run函数中的操作报错
  41. self.isOver = True
  42. if self.dev.isOpened():
  43. self.dev.release()
  44. def landmark(self, is_landmark):
  45. self.is_landmark = is_landmark