-
Notifications
You must be signed in to change notification settings - Fork 1
/
code2.py
71 lines (51 loc) · 1.89 KB
/
code2.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
import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
import time
# get the webcam:
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
def decode(im) :
# Find barcodes and QR codes
decodedObjects = pyzbar.decode(im)
# Print results
for obj in decodedObjects:
print('Type : ', obj.type)
print('Data : ', obj.data,'\n')
return decodedObjects
font = cv2.FONT_HERSHEY_SIMPLEX
result = []
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
im = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
decodedObjects = decode(im)
for decodedObject in decodedObjects:
points = decodedObject.polygon
# If the points do not form a quad, find convex hull
if len(points) > 4 :
hull = cv2.convexHull(np.array([point for point in points], dtype=np.float32))
# A tuple is a collection which is ordered and unchangeable. In Python tuples ... You can access tuple items by referring to the index number, inside square
hull = list(map(tuple, np.squeeze(hull)))
else :
hull = points;
# Number of points in the convex hull
n = len(hull)
# Draw the convext hull
for j in range(0,n):
cv2.line(frame, hull[j], hull[ (j+1) % n], (255,0,0), 3)
x = decodedObject.rect.left
y = decodedObject.rect.top
a = decodedObject.data.decode("utf-8")
print(a)
barCode = str(decodedObject.data)
cv2.putText(frame, barCode, (x, y), font, 1, (0,255,255), 2, cv2.LINE_AA)
# Display the resulting frame
cv2.imshow('frame',frame)
key = cv2.waitKey(1)
if key & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()