SettingCam.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from .ui_py.SettingCam import Ui_SettingCam
  2. from PySide6.QtWidgets import QMainWindow
  3. from PySide6.QtCore import Qt, Slot
  4. from PySide6.QtMultimedia import QMediaDevices
  5. from module.WorkWithCam import WorkWithCam
  6. from module.WorkWithDB import *
  7. class SettingCam(QMainWindow, Ui_SettingCam):
  8. def __init__(self, parent=None) -> None:
  9. super(SettingCam, self).__init__(parent)
  10. self.setAttribute(Qt.WA_DeleteOnClose)
  11. self.setupUi(self)
  12. self.load_cams()
  13. self.face_list_cam.currentTextChanged.connect(
  14. self.print_selected_face_cam)
  15. self.pasport_list_cam.currentTextChanged.connect(
  16. self.print_selected_pasport_cam)
  17. self.chacked_cam_face.clicked.connect(self._open_cam_face)
  18. self.stop_cam_face.clicked.connect(self._close_cam_face)
  19. self.checked_cam_pasport.clicked.connect(self._open_cam_pasport)
  20. self.stop_cam_pasport.clicked.connect(self._close_cam_pasport)
  21. self.save_setting_cam.clicked.connect(self._save_setting_cam)
  22. def load_cams(self) -> None:
  23. for device in list(QMediaDevices.videoInputs()):
  24. self.face_list_cam.addItem(device.description())
  25. self.pasport_list_cam.addItem(device.description())
  26. self.load_cams_from_db()
  27. @Slot()
  28. def _save_setting_cam(self) -> None:
  29. self.save_cams(1, self.face_list_cam.currentIndex())
  30. self.save_cams(2, self.pasport_list_cam.currentIndex())
  31. @Slot()
  32. def _open_cam_face(self) -> None:
  33. self.cam_photo.setHidden(False)
  34. self._cam_face = WorkWithCam(
  35. self.cam_photo, self.face_list_cam.currentIndex())
  36. self._cam_face.start_cam()
  37. @Slot()
  38. def _open_cam_pasport(self) -> None:
  39. self.cam_pasport.setHidden(False)
  40. self._cam_pasport = WorkWithCam(
  41. self.cam_pasport, self.pasport_list_cam.currentIndex())
  42. self._cam_pasport.start_cam()
  43. @Slot()
  44. def _close_cam_face(self) -> None:
  45. if self._cam_face:
  46. self._cam_face.stop_cam()
  47. self.cam_photo.setHidden(True)
  48. del (self._cam_face)
  49. @Slot()
  50. def _close_cam_pasport(self) -> None:
  51. if self._cam_pasport:
  52. self._cam_pasport.stop_cam()
  53. self.cam_pasport.setHidden(True)
  54. del (self._cam_pasport)
  55. @Slot()
  56. def print_selected_face_cam(self, value) -> None:
  57. print(F"face_list_cam selected item {value}")
  58. @Slot()
  59. def print_selected_pasport_cam(self, value) -> None:
  60. print(F"pasport_list_cam selected item {value}")
  61. def load_cams_from_db(self):
  62. with connect() as conn:
  63. result = conn.execute(cam_setting.select()).all()
  64. if result:
  65. self.face_list_cam.setCurrentIndex(result[0].selected_cam)
  66. self.pasport_list_cam.setCurrentIndex(result[1].selected_cam)
  67. def save_cams(self, index: int, index_cam: int) -> None:
  68. query = None
  69. with connect() as conn:
  70. select_cam = cam_setting.select().where(cam_setting.c.id == index)
  71. result = conn.execute(select_cam)
  72. if not result.all() or any([x for x in result if x.id in (1, 2)]):
  73. query = cam_setting.insert().values(selected_cam=index_cam)
  74. else:
  75. query = cam_setting.update().where(
  76. cam_setting.c.id == index).values(selected_cam=index_cam)
  77. result = conn.execute(query)