WorkWithCam.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import sys
  2. import os
  3. from PySide6.QtMultimediaWidgets import QVideoWidget
  4. from PySide6.QtCore import Slot, Qt
  5. from PySide6.QtGui import QImage, QPixmap
  6. from PySide6.QtWidgets import QLabel
  7. from PySide6.QtMultimedia import (
  8. QMediaDevices, QCamera, QImageCapture, QMediaCaptureSession)
  9. from appdirs import user_data_dir
  10. from datetime import datetime
  11. IMAGES_PATH = F"{user_data_dir()}/propusk/photo"
  12. class WorkWithCam:
  13. def __init__(self, q_Video_Widget: QVideoWidget, index_cam: int = 0) -> None:
  14. self._create_dirs()
  15. self._capture_session = None
  16. self._camera = None
  17. self._camera_info = None
  18. self._image_capture = None
  19. self._video_widget = q_Video_Widget
  20. list_cam_device = QMediaDevices.videoInputs()
  21. if list_cam_device[index_cam]:
  22. self._camera_info = list_cam_device[index_cam]
  23. else:
  24. raise IndexError(
  25. f"Не нашли камеру QMediaDevices.videoInputs(). убидитесь, что у вас есть камера")
  26. def start_cam(self):
  27. self._camera = QCamera(self._camera_info)
  28. self._camera.errorOccurred.connect(self._camera_error)
  29. self._image_capture = QImageCapture(self._camera)
  30. self._image_capture.imageCaptured.connect(self.image_captured)
  31. self._image_capture.imageSaved.connect(self.image_saved)
  32. self._image_capture.errorOccurred.connect(self._capture_error)
  33. self._capture_session = QMediaCaptureSession()
  34. self._capture_session.setCamera(self._camera)
  35. self._capture_session.setImageCapture(self._image_capture)
  36. if self._camera and self._camera.error() == QCamera.NoError:
  37. # print()
  38. name = self._camera_info.description()
  39. self._capture_session.setVideoOutput(self._video_widget)
  40. self._camera.start()
  41. else:
  42. print("Camera unavailable")
  43. def stop_cam(self) -> None:
  44. if self._camera and self._camera.isActive():
  45. self._camera.stop()
  46. def __del__(self) -> None:
  47. self.stop_cam()
  48. @Slot(int, QImageCapture.Error, str)
  49. def _capture_error(self, id, error, error_string):
  50. print(error_string, file=sys.stderr)
  51. self.show_status_message(error_string)
  52. @Slot(QCamera.Error, str)
  53. def _camera_error(self, error, error_string):
  54. print(error_string, file=sys.stderr)
  55. self.show_status_message(error_string)
  56. @staticmethod
  57. def get_filename() -> str:
  58. return F"{IMAGES_PATH}/propusk_{datetime.now().timestamp()}.jpg"
  59. def _create_dirs(self):
  60. if not os.path.exists(IMAGES_PATH):
  61. os.mkdir(IMAGES_PATH)
  62. @Slot(int, QImage)
  63. def image_captured(self, id, previewImage):
  64. self._current_preview = previewImage
  65. @Slot(int, str)
  66. def image_saved(self, id, fileName):
  67. ...
  68. def show_status_message(self, message):
  69. print(message, 5000)
  70. def load_image(qlabel: QLabel, path_file: str) -> None:
  71. qlabel.setPixmap(QPixmap(QImage(path_file)).scaled(
  72. qlabel.width(),
  73. qlabel.height(),
  74. Qt.AspectRatioMode.KeepAspectRatio,
  75. Qt.TransformationMode.FastTransformation
  76. ))