-
Notifications
You must be signed in to change notification settings - Fork 24
/
camera_async_api.py
70 lines (54 loc) · 2.28 KB
/
camera_async_api.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
import barcodeQrSDK
import numpy as np
import cv2
import json
g_results = None
def callback(results, elapsed_time):
global g_results
g_results = (results, elapsed_time)
def run():
# set license
barcodeQrSDK.initLicense(
"DLS2eyJoYW5kc2hha2VDb2RlIjoiMjAwMDAxLTE2NDk4Mjk3OTI2MzUiLCJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSIsInNlc3Npb25QYXNzd29yZCI6IndTcGR6Vm05WDJrcEQ5YUoifQ==")
# initialize barcode scanner
scanner = barcodeQrSDK.createInstance()
params = scanner.getParameters()
# Convert string to JSON object
json_obj = json.loads(params)
# json_obj['ImageParameter']['ExpectedBarcodesCount'] = 999
params = json.dumps(json_obj)
ret = scanner.setParameters(params)
scanner.addAsyncListener(callback)
cap = cv2.VideoCapture(0)
while True:
ret, image = cap.read()
if image is not None:
scanner.decodeMatAsync(image)
# scanner.decodeBytesAsync(image.tobytes(), image.shape[1], image.shape[0], image.strides[0], barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)
# results, elapsed_time = scanner.decodeBytes(image.tobytes(), image.shape[1], image.shape[0], image.strides[0], barcodeQrSDK.ImagePixelFormat.IPF_BGR_888)
# results, elapsed_time = scanner.decodeMat(image)
# g_results = (results, elapsed_time)
if g_results != None:
print('Elapsed time: ' + str(g_results[1]) + 'ms')
cv2.putText(image, 'Elapsed time: ' + str(
g_results[1]) + 'ms', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
for result in g_results[0]:
x1 = result.x1
y1 = result.y1
x2 = result.x2
y2 = result.y2
x3 = result.x3
y3 = result.y3
x4 = result.x4
y4 = result.y4
cv2.drawContours(
image, [np.int0([(x1, y1), (x2, y2), (x3, y3), (x4, y4)])], 0, (0, 255, 0), 2)
cv2.putText(image, result.text, (x1, y1),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Barcode QR Code Scanner', image)
ch = cv2.waitKey(1)
if ch == 27:
break
scanner.clearAsyncListener()
if __name__ == '__main__':
run()