IPCam.py 2.7 KB

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