forked from sightsdev/SIGHTSVision
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqr_read_rotate.py
More file actions
98 lines (76 loc) · 2.77 KB
/
qr_read_rotate.py
File metadata and controls
98 lines (76 loc) · 2.77 KB
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
import cv2
from pyzbar.pyzbar import decode
import imutils
from imutils.video import VideoStream
import os
# constants
angle_increase = 30
def getNumber(line, prop):
numbers = []
count = 0
found_equal = False
if prop in line:
for character in line:
if found_equal == True:
numbers.append(line[count])
if character == 'b':
found_equal = True
if character == "'" and line[count + 1] == ",":
return numbers
count = count + 1
return numbers
def dataArrayToString(line, prop):
number_string = ""
number = getNumber(line, prop)
if number == None:
return ""
for num in number:
number_string += num
return number_string
# save codes
def detectionFeed(robot_camera, ip):
# grab stream
if not robot_camera:
source = VideoStream(src=0).start()
else:
source = VideoStream(src=ip).start()
codes = {}
while True:
# read front camera
im = source.read()
# convert to grayscale
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
# increase contrast
contrast_image = cv2.equalizeHist(im)
# loop over all rotations
for angle in range(0, 360, angle_increase):
# rotate contrast_image by angle
rows, cols = contrast_image.shape
M = cv2.getRotationMatrix2D((cols/2, rows/2), angle, 1)
dst = cv2.warpAffine(contrast_image, M, (cols, rows))
#cv2.imshow('test', dst)
# decode QR code
decoded = decode(dst)
decodedString = dataArrayToString(str(decoded), "data=")
# if theres something in the output of decoded()
if len(decoded) == 1:
# do the region
[x, y, w, h] = decoded[0].rect
[x1, y1, x2, y2] = [x, y, x+w, y+h]
region = dst[y1:y2, x1:x2]
#cv2.rectangle(contrast_image, (x1, y1), (x2, y2), (255), 3)
#cv2.rectangle(contrast_image, (x1, y1), (x2, y2), (0), 1)
# print to console, if there is not nothing (= if there is something)
if (decodedString != ""):
print(decodedString)
# add the string and the region to the codes dict
codes[decodedString] = region
# draw keypoints window
#cv2.imshow("Keypoints", im)
# draw histogram window
# cv2.imshow("Camera", contrast_image)
# _, jpeg = cv2.imencode('.jpg', contrast_image)
# bytes = jpeg.tobytes()
# yield (b'--frame\r\n'
# b'Content-Type: image/jpeg\r\n\r\n' + bytes + b'\r\n\r\n')
yield bytes(decodedString)