Created
November 14, 2018 07:30
-
-
Save skyzh/126a765bbf001733c331dc63674c4dff to your computer and use it in GitHub Desktop.
Simple barcode scanner
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
import pyzbar.pyzbar as pyzbar | |
cap = cv2.VideoCapture(0) | |
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 | |
# 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.imshow("Results", cv2.resize(im, (384, 216))); | |
while(True): | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
decoded = decode(frame) | |
display(frame, decoded) | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment