-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebCamReader.py
124 lines (88 loc) · 3.2 KB
/
WebCamReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/python
import cv2
import imutils
from pyzbar import pyzbar
from pyzbar.pyzbar import ZBarSymbol
import argparse
import sys
import logging
def measure_time_processing(func): # decorator
from datetime import datetime
def wrapper(*args, **kwargs):
start_time = float(datetime.now().strftime('%S.%f'))
f = func(*args, **kwargs)
print(round(float(datetime.now().strftime('%S.%f')) - start_time, 3))
return f
return wrapper
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-frms', '--number_of_frames', nargs='?', default='1', type=int)
parser.add_argument('-stp', '--step_rotation', nargs='?', default='15', type=int)
parser.add_argument('-rot', '--number_of_rotations', nargs='?', default='5', type=int)
parser.add_argument('-scl', '--scale', nargs='?', default='25', type=int)
namespace = parser.parse_args(sys.argv[1:])
return [namespace.number_of_frames,
namespace.step_rotation,
namespace.number_of_rotations,
namespace.scale]
def start_logger():
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
handler = logging.FileHandler('BCReader.log', mode='a')
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
handler.setFormatter(formatter)
log.addHandler(handler)
return log
@measure_time_processing
def cam_read_and_processing():
frames = []
for i in range(number_of_frames):
_, img = cap.read()
img = frame_scale(scale, img)
frames.append(img)
decode(frames)
def frame_scale(scl: int, frm):
width = int(frm.shape[1] * scl / 100)
height = int(frm.shape[0] * scl / 100)
dim = (width, height)
frm = cv2.resize(frm, dim, interpolation=cv2.INTER_AREA)
return frm
def decode(frames: list):
barcodes = set()
for frame in frames:
frame_copy = frame.copy()
for i in range(number_of_rotations):
angle = i * step_rotation
frame = imutils.rotate_bound(frame_copy, angle)
decoded_objects = pyzbar.decode(frame, symbols=[ZBarSymbol.CODE128])
for obj in decoded_objects:
barcodes.add('<' + bytes.decode(obj.data) + '>')
if len(barcodes) == 0:
print('No read')
logger.info(f'No read')
else:
for bc in barcodes:
print(bc)
logger.info(bc)
if __name__ == '__main__':
[number_of_frames,
step_rotation,
number_of_rotations,
scale] = create_parser()
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
logger = start_logger()
logger.info(f'Start program with arguments: -frms {number_of_frames} -stp {step_rotation} -rot '
f'{number_of_rotations} -scl {scale}')
try:
while True:
trig = input('Trigger: ')
if trig == '1':
cam_read_and_processing()
if trig == '0':
break
except Exception as err:
print(err)
logger.error(err)
finally:
cap.release()
cv2.destroyAllWindows()