Browse Source

fix bug cupture image

Алексей Сидоров 1 year ago
parent
commit
6b48458ce3
3 changed files with 23 additions and 19 deletions
  1. 1 0
      module/cam/IPCam.py
  2. 19 12
      module/cam/USBCam.py
  3. 3 7
      window/MainWindow.py

+ 1 - 0
module/cam/IPCam.py

@@ -74,4 +74,5 @@ class IPCam(Thread):
         name_file = create_filename()
         qLabel.setPixmap(self.__scaled_img)
         self.__scaled_img.save(name_file, 'jpg')
+        self.stop_cam()
         return name_file

+ 19 - 12
module/cam/USBCam.py

@@ -18,13 +18,15 @@ if not os.environ.get("PHOTO_DIR"):
 
 
 class USBCam:
+    _capture_session: QMediaCaptureSession
+    _camera: QCamera
+    _camera_info: list[QMediaDevices]
+    _image_capture: QImageCapture
+    _current_preview = QImage()
+    _label: QLabel
+    
     def __init__(self, q_Video_Widget: QVideoWidget, name_cam: str) -> None:
         self._create_dirs()
-
-        self._capture_session = None
-        self._camera = None
-        self._camera_info = None
-        self._image_capture = None
         self._video_widget = q_Video_Widget
 
         self._camera_info = get_object_cam_by_name(name_cam)
@@ -51,22 +53,24 @@ class USBCam:
             self._capture_session.setVideoOutput(self._video_widget)
             self._camera.start()
         else:
-            logger.debug("Camera unavailable")
+            logger.error("Camera unavailable")
 
     def stop_cam(self) -> None:
-        if self._camera and self._camera.isActive():
-            self._camera.stop()
+        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, label: QLabel) -> str:
         self._label = label
-        self._current_preview = QImage()
         self._file_name = create_filename()
-
-        logger.info(F"Создаем файл {self._file_name}")
         self._image_capture.captureToFile(self._file_name)
+        logger.info(F"Создаем файл {self._file_name}")
+        
         return self._file_name
 
     @Slot(int, QImageCapture.Error, str)
@@ -87,7 +91,10 @@ class USBCam:
 
     @Slot(int, str)
     def image_saved(self, id, fileName):
-        load_image(self._label, fileName)
+       load_image(self._label, fileName)
+       self.stop_cam()
+
+        
 
 
 

+ 3 - 7
window/MainWindow.py

@@ -76,7 +76,7 @@ class MainWindow(QMainWindow, Ui_MainWindow):
         )
 
         self.capturePhoto.clicked.connect(
-            self.__take_image_face
+            self.__take_image
         )
 
         self.btn_save.clicked.connect(self.__save)
@@ -209,21 +209,17 @@ class MainWindow(QMainWindow, Ui_MainWindow):
             self.__wwc = USBCam(widget.video, cam)
             self.__wwc.start_cam()
 
-    def __take_image_face(self) -> None:
+    def __take_image(self) -> None:
         if self.tabWidget.currentIndex() == 1:
             self.__file_name_document = self.__wwc.cupture_image(
                 self.stacked_document.image)
-            sleep(1)
-            load_image(self.stacked_document.image, self.__file_name_document)
             self.stacked_document.to_image()
         else:
             self.__file_name_face = self.__wwc.cupture_image(
                 self.stacked_face.image)
-            sleep(1)
-            load_image(self.stacked_face.image, self.__file_name_face)
             self.stacked_face.to_image()
 
-        self.__stop_cam()
+        # self.__stop_cam()
 
     def __print(self) -> None:
         if self.data_propusk is None: