IPCam.py 2.7 KB

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