-
Notifications
You must be signed in to change notification settings - Fork 0
/
zbar.py
64 lines (44 loc) · 1.75 KB
/
zbar.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
#Install zbar and Pyzbar (pyzbar only if you are using Python version 3 or later)
#!pip install zbar
#!pip install pyzbar
#Import libraries
import cv2 as cv
import numpy as np
import pyzbar.pyzbar as pzb
#from pyzbar.pyzbar import decode
# Create a function to detect Qrcode and Barcode
# Initialize the camara
webcam = cv.VideoCapture(0)
def detector(webcam):
while(True):
# Capture the video frame by frame
video, frame = webcam.read()
# Find barcodes and Qr
for barcode in pzb.decode(frame):
# Print the data got from the barcode lecture
#print(barcode.data)
# Convert de data from bytes to string
mydata = barcode.data.decode('utf-8')
print(mydata)
# Add bounding box to qr codes and barcodes
points = np.array([barcode.polygon], np.int32)
# Print the possition of the qr code or barcode
#print(points)
points = points.reshape (-1,1,2)
cv.polylines(frame, [points], True, (255,0,0),5) #draw a polygon on an image
# Add text over the bounding box
points2 = barcode.rect
cv.putText(frame, mydata, (points2[0], points2[1]), cv.FONT_HERSHEY_SIMPLEX,
0.9, (255,0,0), 2)
# Display the resulting frame
cv.imshow('frame', frame)
#the 'd' button is set as the quitting button
if cv.waitKey(1) & 0xFF == ord('d'):
webcam.release()
cv.destroyAllWindows()
break
#Main
if __name__ == '__main__':
webcam = cv.VideoCapture(0)
#To execute the function run:
#detector(webcam)