Skip to content

Commit d19d161

Browse files
VideoCapture dependency injection.
1 parent dbfd27e commit d19d161

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

app_tools/my_video_capture.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import cv2
2+
import logging
23

34

45
class MyVideoCapture:
56
def __init__(self, video_source=0):
7+
self.logger = logging.getLogger(f'{self.__class__.__name__}', )
68
self.video_source = video_source
79
self.vid = None
810
self.width = None
@@ -13,7 +15,7 @@ def init(self):
1315
# Open the video source
1416
self.vid = cv2.VideoCapture(self.video_source)
1517
if not self.vid.isOpened():
16-
print("Unable to open video source")
18+
self.logger.error("Unable to open video source")
1719
raise ValueError("Unable to open video source", self.video_source)
1820

1921
self.width = self.vid.get(cv2.CAP_PROP_FRAME_WIDTH)
@@ -26,22 +28,19 @@ def get_frame(self):
2628
# Return a boolean success flag and the current frame converted to BGR
2729
return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
2830
else:
29-
print("Ret not found")
31+
self.logger.error("Ret not found")
3032
return (ret, None)
3133
else:
32-
print("get_frame Not opened")
34+
self.logger.error("get_frame Not opened")
3335
return (None)
3436

3537
# Release the video source when the object is destroyed
3638
def __del__(self):
37-
print("Camera capture released")
39+
self.logger.info("Camera capture released")
3840
if self.vid.isOpened():
3941
self.vid.release()
4042

4143
def release_camera(self):
42-
print("Camera capture released")
44+
self.logger.info("Camera capture released")
4345
if self.vid.isOpened():
4446
self.vid.release()
45-
46-
47-
DEFAULT_VIDEO_CAPTURE = MyVideoCapture(0)

csc-manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from app_tools.logger import stdout_redirector
99
from di.containers import MyContainer
10+
import ui
1011

1112

1213
def main():
@@ -21,5 +22,5 @@ def main():
2122
container = MyContainer()
2223
container.init_resources()
2324
container.config.from_ini('config.ini')
24-
container.wire(modules=[sys.modules[__name__]])
25+
container.wire(modules=[sys.modules[__name__]], packages=[ui])
2526
main()

di/containers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
from dependency_injector import containers, providers
66

7+
from app_tools.my_video_capture import MyVideoCapture
8+
79

810
class MyContainer(containers.DeclarativeContainer):
911

@@ -14,4 +16,6 @@ class MyContainer(containers.DeclarativeContainer):
1416
fname='logging.ini',
1517
)
1618

19+
default_video_capture = providers.Singleton(MyVideoCapture)
20+
1721

ui/camera_widget.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11

22
import PIL.Image
33
import PIL.ImageTk
4+
from dependency_injector.wiring import Provide
45

6+
from app_tools.my_video_capture import MyVideoCapture
57
from app_tools.qr_code_scaner import QrCodeScanner
6-
from app_tools.my_video_capture import MyVideoCapture, DEFAULT_VIDEO_CAPTURE
78
import tkinter
89
import imutils
910

1011

1112
class CameraWidget:
12-
def __init__(self, camera_frame, width, height, on_qr_scanned_callback=None, paused=False):
13+
def __init__(self, camera_frame, width, height, on_qr_scanned_callback=None, paused=False,
14+
video_capture: MyVideoCapture = Provide['default_video_capture']
15+
):
16+
17+
self.video_capture = video_capture
1318
self.camera_frame = camera_frame
1419
self.qr_code_scanner = QrCodeScanner()
1520
self.width = width
@@ -22,7 +27,7 @@ def __init__(self, camera_frame, width, height, on_qr_scanned_callback=None, pau
2227
self.canvas = tkinter.Canvas(self.camera_frame, width=self.width, height=self.height)
2328
self.canvas.pack()
2429

25-
self.vid = DEFAULT_VIDEO_CAPTURE
30+
self.vid = self.video_capture
2631
self.delay = 40
2732

2833
self.camera_frame.after(1000, self.update_barcode_frame)
@@ -47,12 +52,12 @@ def update_barcode_frame(self):
4752
self.camera_frame.after(self.delay * 100, self.update_barcode_frame)
4853

4954
def pause(self):
50-
DEFAULT_VIDEO_CAPTURE.release_camera()
55+
self.video_capture.release_camera()
5156
self.paused = True
5257

5358
def resume(self):
5459
if self.paused:
55-
DEFAULT_VIDEO_CAPTURE.init()
60+
self.video_capture.init()
5661
self.paused = False
5762
if self.camera_frame is not None:
5863
self.camera_frame.after(100, self.update_barcode_frame)

ui/recovery_widget.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
from controller.recovery_controller import RecoveryController
55
from ui.recovery.header_widget import HeaderWidget
66
from ui.recovery.footer_widget import FooterWidget
7+
import logging
78

89
from tkinter import messagebox
910

1011

1112
class RecoveryWidget:
1213
def __init__(self, recovery_frame):
1314
self.recovery_frame = recovery_frame
15+
self.logger = logging.getLogger(f'{self.__class__.__name__}', )
16+
1417
top_frame = tkinter.Frame(self.recovery_frame, borderwidth=3)
1518

1619
currencies = coinFactory.get_available_currencies()
@@ -60,11 +63,11 @@ def show_coin_details_info(self, private_key, snip, address):
6063
self.footer_widget.show_coin_details_info(private_key, snip, address)
6164

6265
def init_camera(self):
63-
print("init recovery camera")
66+
self.logger.info("init recovery camera")
6467
self.footer_widget.camera_widget.resume()
6568

6669
def release_camera(self):
67-
print("release recovery camera")
70+
self.logger.info("release recovery camera")
6871
self.footer_widget.camera_widget.pause()
6972

7073
@staticmethod

0 commit comments

Comments
 (0)