IPCam.py 2.4 KB

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