-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (104 loc) · 6.29 KB
/
main.py
File metadata and controls
138 lines (104 loc) · 6.29 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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import cv2
import numpy
from CountsPerSec import CountsPerSec
from VideoGet import VideoGet
from VideoShow import VideoShow
from Process import Package, Image, Cache
# Initialize cache for contain QRcode query data
cache = Cache()
# Function for prin Iteration rate in frame
def putIterationsPerSec(frame, iterations_per_sec):
"""
Add iterations per second text to lower-left corner of a frame.
"""
cv2.putText(frame, "{:.0f} iterations/sec".format(iterations_per_sec), (10, 450), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255))
return frame
# check that qrcode is in the box
def QRisinBox(box, qr):
# check if (min box.x < qr.x < max box.x) and (min box.y < qr.y < max box.y)
qrpt, boxpt = [item[0] for item in qr.get('pts')], box.get('pts')
# zip coordinate to list of x and y
qrzip, boxzip = tuple(zip(qrpt[0], qrpt[1], qrpt[2], qrpt[3])), tuple(zip(boxpt[0], boxpt[1], boxpt[2], boxpt[3]))
# Get min and max value of x.
minx, maxx = min(boxzip[0]), max(boxzip[0])
# Get min and max value of y.
miny, maxy = min(boxzip[1]), max(boxzip[1])
# return true if all of qrcode's point is in range of min, max of x and y
if all(x in range(minx, maxx) for x in qrzip[0]) and all(y in range(miny, maxy) for y in qrzip[1]):
return True
else:
return False
# value from this func is in centimeter unit
def get_depth(x:int, y:int) -> float:
'''value from this func is in centimeter unit'''
return (depth_frame[(int(y), int(x))]) / 10
# main function
def main(source = 0):
# start cv2.Videocapture's thread
video_getter = VideoGet(source).start()
# start cv2.imshow's thread
video_shower = VideoShow(video_getter.color_frame).start()
# start time counter for calculate a iteration rate
cps = CountsPerSec().start()
# loop for stream frame
while True:
# check if doesn't has any frame or User press 'Q'
if video_getter.stopped or video_shower.stopped:
video_shower.stop() # stop cv2.VideoCapture's thread
video_getter.stop() # stop cv2.imshow's thread
break
# init frame as object
image = Image(video_getter.color_frame, video_getter.depth_frame)
# for loop for mactching qrcode and box
for qr in image.qrlist:
for box in image.boxlist:
# check qr is in the box
if QRisinBox(box, qr):
# init qrcode to an object and pass parameters with id and cache for append to cache
package = Package(qr, box, cache)
# qrcode is activated and registeratered
if package.IsActivatedIsRegistered:
# get the pixel height and width of qrcode and average them
dimension = (package.QRheight + package.QRwidth) / 2
# calculate the ratio for turn a value in pixel unit to centemeter unit. 5 represent to the size of qrcode in centemeter units.
ratio = 5 / dimension
# get a width and height of the box and round it to decimal point.
width, height = package.BOXwidth * ratio, package.BOXheight * ratio
width, height, depth, weight = round(width,2), round(height,2), image.get_depth(package.QRpts[0][0]), 0
print(width, height, depth, weight)
# draw the rectangle around the box and qrcode
cv2.polylines(image.frame, [package.QRpts], True, (255, 0 ,0), 3)
cv2.polylines(image.frame, [package.BOXpts], True, (0, 111 ,111), 3)
# put text that contain the ID of qrcode and its dimension
cv2.putText(image.frame, f"id: {qr.get('id')}", package.BOXpts[0][0], cv2.FONT_HERSHEY_PLAIN, 1, (100, 200, 0), 2)
cv2.putText(image.frame, f"width: {width}, height: {height}", (package.BOXpts[0][0], package.BOXpts[0][1] + 35), cv2.FONT_HERSHEY_PLAIN, 1, (100, 200, 0), 2)
# write the dimension of the box to the database
package.AddDimension(width, height, depth, weight)
# qrcode is not activated
elif package.NotActivated:
# draw rectangle around qrcode and put status text
cv2.polylines(image.frame, [package.QRpts], True, (255, 0 ,0), 3)
cv2.polylines(image.frame, [package.BOXpts], True, (0, 111 ,111), 3)
cv2.putText(image.frame, f"Isn't activated", package.BOXpts[0], cv2.FONT_HERSHEY_PLAIN, 1, (100, 200, 0), 2)
# draw rectangle around qrcode and put status text
elif package.IsActivatedIsRegistered:
cv2.polylines(image.frame, [package.QRpts], True, (255, 0 ,0), 3)
cv2.polylines(image.frame, [package.BOXpts], True, (0, 111 ,111), 3)
cv2.putText(image.frame, f"Isn't Registered", package.BOXpts[0], cv2.FONT_HERSHEY_PLAIN, 1, (100, 200, 0), 2)
# draw rectangle around qrcode and put status text
elif package.IsAddDimension:
cv2.polylines(image.frame, [package.QRpts], True, (255, 0 ,0), 3)
cv2.polylines(image.frame, [package.BOXpts], True, (0, 111 ,111), 3)
cv2.putText(image.frame, f"Is added dimension", package.BOXpts[0], cv2.FONT_HERSHEY_PLAIN, 1, (100, 200, 0), 2)
# break the loop because we found the box that match with this qrcode already.
break
# put the iteration rate to frame.
image.frame = putIterationsPerSec(image.frame, cps.countsPerSec())
# display frame
video_shower.frame = image.frame
# clear cache to 10 items if it has more than 10 items.
cache.Clear()
# iteration counter
cps.increment()
if __name__ == "__main__":
main()