Skip to content

Instantly share code, notes, and snippets.

@fjolublar
Last active June 19, 2020 12:14
Show Gist options
  • Save fjolublar/efac41af0e7f635b0813d2a666b7d3e6 to your computer and use it in GitHub Desktop.
Save fjolublar/efac41af0e7f635b0813d2a666b7d3e6 to your computer and use it in GitHub Desktop.
QR_Code_Detection in Python
# Import the necessary packages
from pyzbar import pyzbar
import cv2
#--------------------------------------------------------------------------------------------------------#
def initCamera(camera_id, frame_width, frame_height): #Function to initialize the CV videocapture.
print('==> Trying to initialize the camera...')
cap = cv2.VideoCapture( int(camera_id) ) #Start VideoCapture with the appropriate camera ID.
if not cap.isOpened(): #Test if the stream is opened.
print("ERROR: Cannot open camera")
exit() #Exit Python.
cap.set(3, int(frame_width)) #Cap set (ID, Number we want) . 3 is the ID for Width.
cap.set(4, int(frame_height)) #Cap set (ID, Number we want) . 4 is the ID for Height.
#cap.set(5, int(15)) #5 is the ID for Framerate.
#print ( cap.get(3) )
#print ( cap.get(4) )
#print ( cap.get(5) )
print('Camera is successfully initialized')
return cap
#--------------------------------------------------------------------------------------------------------#
print("[INFO] starting video stream...")
#found = set() #Initialize the set of barcodes found thus far.
cap = initCamera(0, 500, 500)
while True: #Loop over the frames from the video stream.
ret, frame = cap.read() #Capture a frame. Returning a BOOL and the Frame.
if not ret: #ret = True if the frame is read correctly.
print("Can't receive frame. Exiting ...")
break #Break the stream if a Frame is not read correctly.
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #Convert the image to grayscale.
barcodes = pyzbar.decode (frame) #Find the barcodes in the frame and decode each of the barcodes.
for barcode in barcodes: #Loop over the detected barcodes.
(x, y, w, h) = barcode.rect #Extract the bounding box location of the barcode.
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2) #Draw the box surrounding the barcode on the image.
barcodeData = barcode.data.decode("utf-8") #Convert the byte object (barcode.data) to string.
barcodeType = barcode.type #Get the type of the barcode used.
text = "{} ({})".format(barcodeData, barcodeType) #Set the text parameter to be drawn in the frame.
cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) #Draw the barcode data and barcode type on the image.
#if barcodeData not in found and False: #If the barcode is not in CSV file.
# csv.write("{},{}\n".format(datetime.datetime.now(), barcodeData)) #Write the timestamp + barcode.
# csv.flush()
# found.add(barcodeData) #Update the set.
cv2.imshow("Barcode Scanner", frame) #Show the output frame.
if cv2.waitKey(1) & 0xFF == ord('q'): #If the `q` key was pressed, break from the loop.
break
print("[INFO] cleaning up...")
#csv.close() #Close the output CSV file do a bit of cleanup.
cv2.destroyAllWindows()
print("[INFO] Closed")
pyzbar 0.1.8
https://pypi.org/project/pyzbar/
PyQRCode 1.2.1
https://pypi.org/project/PyQRCode/
An OpenCV barcode and QR code scanner with ZBar
https://www.pyimagesearch.com/2018/05/21/an-opencv-barcode-and-qr-code-scanner-with-zbar/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment