-
Notifications
You must be signed in to change notification settings - Fork 2
/
QRCode.py
50 lines (38 loc) · 1.29 KB
/
QRCode.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
from __future__ import print_function
import pyzbar.pyzbar as pyzbar
import numpy as np
import cv2
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
# Display barcode and QR code location
def display(im, decodedObjects):
# Loop over all decoded objects
for decodedObject in decodedObjects:
points = decodedObject.polygon
print(points)
# 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))
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(im, hull[j], hull[(j + 1) % n], (255, 0, 0), 3)
# Display results
cv2.imwrite("images/qr_code_test.JPG", im)
#cv2.imshow("Results", im)
cv2.waitKey(0)
# Read image
# im = cv2.imread('images/qr_code_test8.jpg')
im = cv2.imread('images/qr_code_fullpage_test9.jpg')
decodedObjects = decode(im)
display(im, decodedObjects)