IPCam.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from PySide6.QtWidgets import QLabel
  2. from PySide6.QtCore import Qt
  3. from threading import Thread
  4. from PySide6.QtGui import QImage, QPixmap
  5. import cv2
  6. from module.ImageTool import create_filename
  7. class IPCam(Thread):
  8. lnk_connect: str = None
  9. qLabel: QLabel = None
  10. def __init__(self, parent=None) -> None:
  11. Thread.__init__(self, parent)
  12. self.status = True
  13. self.cap = True
  14. def run(self) -> None:
  15. self.cap = cv2.VideoCapture()
  16. while self.status:
  17. self.cap.open(self.lnk_connect)
  18. ret, frame = self.cap.read()
  19. if not ret:
  20. continue
  21. color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  22. h, w, ch = color_frame.shape
  23. img = QImage(color_frame.data, w, h, ch * w, QImage.Format_RGB888)
  24. self.__scaled_img = QPixmap.fromImage(img.scaled(640, 480, Qt.KeepAspectRatio))
  25. self.qLabel.setPixmap(self.__scaled_img)
  26. # "http://admin:admin102030@192.168.1.108/cgi-bin/snapshot.cgi?2"\
  27. def stop_cam(self):
  28. print("Stopped...")
  29. self.cap.release()
  30. self.status = False
  31. # cv2.destroyAllWindows()
  32. self.join()
  33. def cupture_image(self, qLabel: QLabel) -> str:
  34. name_file = create_filename()
  35. qLabel.setPixmap(self.__scaled_img)
  36. self.__scaled_img.save(name_file, 'jpg')
  37. return name_file