123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import os, sys
- from PySide6.QtMultimediaWidgets import QVideoWidget
- from PySide6.QtCore import Slot
- from PySide6.QtGui import QImage
- from PySide6.QtWidgets import QLabel
- from PySide6.QtMultimedia import (
- QMediaDevices, QCamera, QImageCapture, QMediaCaptureSession)
- from itertools import groupby
- from logger import logger
- from module import create_filename
- from widgets import ImageViewer
- if not os.environ.get("PHOTO_DIR"):
- logger.error("Не задана локальная переменная PHOTO_DIR")
- raise ValueError(
- "не задана локальная переменная PHOTO_DIR"
- )
- class USBCam:
- _capture_session: QMediaCaptureSession
- _camera: QCamera
- _camera_info: list[QMediaDevices]
- _image_capture: QImageCapture
- _current_preview: QImage = QImage()
- _label: QLabel
- _video_widget: QVideoWidget
-
- def __init__(self, video_widget: QVideoWidget, name_cam: str) -> None:
- self._create_dirs()
- self._video_widget = video_widget
- self._camera_info = get_object_cam_by_name(name_cam)
- if not self._camera_info:
- logger.error(
- "Не нашли камеру убидитесь, что у вас есть камера")
- sys.exit(0)
- def start_cam(self):
- self._camera = QCamera(self._camera_info)
- self._camera.errorOccurred.connect(self._camera_error)
- self._image_capture = QImageCapture(self._camera)
- self._image_capture.imageCaptured.connect(self.image_captured)
- self._image_capture.imageSaved.connect(self.image_saved)
- self._image_capture.errorOccurred.connect(self._capture_error)
- self._capture_session = QMediaCaptureSession()
- self._capture_session.setCamera(self._camera)
- self._capture_session.setImageCapture(self._image_capture)
- if self._camera and self._camera.error() == QCamera.NoError:
- self._capture_session.setVideoOutput(self._video_widget)
- self._camera.start()
- else:
- logger.error("Camera unavailable")
- def stop_cam(self) -> None:
- try:
- if self._camera and self._camera.isActive():
- self._camera.stop()
- except RuntimeError as err:
- logger.error(err)
- def __del__(self) -> None:
- self.stop_cam()
- def cupture_image(self, image_viewer: ImageViewer) -> str:
- self._image_viewer = image_viewer
- self._file_name = create_filename()
- self._image_capture.captureToFile(self._file_name)
- logger.info(F"Создаем файл {self._file_name}")
- return self._file_name
- @Slot(int, QImageCapture.Error, str)
- def _capture_error(self, id, error, error_string):
- logger.error(error_string)
- @Slot(QCamera.Error, str)
- def _camera_error(self, error, error_string):
- logger.error(error_string)
- def _create_dirs(self):
- if not os.path.exists(os.environ.get('PHOTO_DIR')):
- os.mkdir(os.environ.get('PHOTO_DIR'))
- @Slot(int, QImage)
- def image_captured(self, id, previewImage):
- self._current_preview = previewImage
- @Slot(int, str)
- def image_saved(self, id, fileName):
- self._image_viewer.setImage(fileName)
- self.stop_cam()
- return fileName
- def get_first_cam() -> str:
- try:
- return QMediaDevices.videoInputs()[0].description()
- except IndexError:
- pass
- def get_list_name_cam() -> list[str]:
- return [x for x, _ in groupby(
- [x.description() for x in QMediaDevices.videoInputs()]
- )]
- def get_object_cam_by_name(name_cam: str) -> QMediaDevices:
- return [x for x in QMediaDevices.videoInputs() if x.description() == name_cam][0]
|