IPCam.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 import create_filename
  7. from module.ImageTool import cupture_face
  8. from logger import logger
  9. class IPCam(Thread):
  10. lnk_connect: str = None
  11. qLabel: QLabel = None
  12. net_work_error: bool = False
  13. def __init__(self, parent=None) -> None:
  14. Thread.__init__(self, parent)
  15. self.status = True
  16. self.cap = True
  17. def run(self) -> None:
  18. logger.info("srarting take image from ip cam")
  19. self.net_work_error = False
  20. try:
  21. self.cap = cv2.VideoCapture()
  22. if 'rtsp' in self.lnk_connect:
  23. self.cap.open(self.lnk_connect)
  24. if not self.cap.isOpened():
  25. raise ConnectionError
  26. while self.status:
  27. if 'http' in self.lnk_connect:
  28. self.cap.open(self.lnk_connect)
  29. if not self.cap.isOpened():
  30. raise ConnectionError
  31. ret, frame = self.cap.read()
  32. if not ret:
  33. continue
  34. color_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  35. _, _, ch = color_frame.shape
  36. img = QImage(color_frame.data, w, h,
  37. ch * w, QImage.Format_RGB888)
  38. label_geometry = self.qLabel.geometry()
  39. h, w = label_geometry.height(), label_geometry.width()
  40. self.__scaled_img = QPixmap.fromImage(
  41. img.scaled(h, w, Qt.KeepAspectRatio))
  42. self.qLabel.setPixmap(self.__scaled_img)
  43. # "http://admin:admin102030@192.168.1.108/cgi-bin/snapshot.cgi?2"\
  44. except ConnectionError:
  45. logger.error("Нет сети или не правильная строка подключения")
  46. logger.info("stop take image from ip cam")
  47. self.status = False
  48. self.qLabel.setText(
  49. 'Нет сети или не правильная строка подключения')
  50. except RuntimeError:
  51. self.status = False
  52. def stop_cam(self):
  53. logger.info("stop take image from ip cam")
  54. self.cap.release()
  55. self.status = False
  56. cv2.destroyAllWindows()
  57. self.join()
  58. def cupture_image(self, qLabel: QLabel) -> str:
  59. name_file = create_filename()
  60. qLabel.setPixmap(self.__scaled_img)
  61. self.__scaled_img.save(name_file, 'jpg')
  62. self.stop_cam()
  63. cupture_face(name_file, F"face_{create_filename()}")
  64. return name_file