QR Code & Bar Code Detection Using Open CV Python With Source Code
Introduction :
Today We Make a QR Code & Bar Code Detector Using Open CV Python , A Versatile Programming Language . To Make the Detector , We need some modules or Packages from Python Library Like numpy , cv2 , qrcode . Open CV is a Powerfull Computer Vision Library . QR Code & Bar Code Detection is a very essential feature in modern Application . Eg :- In Phonpe app , we can Pay Money by scannig Qr Code . QR Code , which store data in two dimensional matrix . Barcode , which store informations in linear patterns . Open CV Is used to Detecting & Decoding these types of codes and extract their information stored in These Codes .
Language And Interface :-
QR Code & Bar Code Detection Using Python , A Versatile Programming Language With
Source Code .
Required Modules Or Packages:-
You Need The Packages List Given Below :-
1. Numpy :- It is a core library for numerical computation in python and it can handle large arrays of numbers.
2. cv2 :- It is a library to use in Computer Vision task. You will get basic image processing tools while using cv2 for example, Image Reading and Writing .
3. pyzbar.pyzbar :- It is used to Decode the Bar code .
4. qrcode :- It is used to detecting and generating the qr codes .
How To Run The Code :-
Step 1 . First , You Download and Install Visual Studio Code or VS Code In your PC or Laptop by VS Code Official Website .
Step 2 . Now Open CMD As Administrator and install the above packages using pip .
Step 3 . Now Open Visual Studio Code .
Step 4. Now Make The files named as qrbartest.py, qrcodeproject.py .
Step 5 . Now Copy And Paste The Code from the Link Given Below
Step 6 . After pasting The code , Save This & Click On Run Button .
Step 7 . Now You will See The Output .
Code Explanation :-
This Python code is used to Detect the QR Codes and Bar Codes and extract the informations stored in it from Any Image Files format .
Step-by-Step Guide Given Below:-
Imports:
• Numpy :- It is a core library for numerical computation in python and it can handle large arrays of numbers.
• cv2 :- It is a library to use in Computer Vision task. You will get basic image processing tools while using cv2 for example, Image Reading and Writing .
• pyzbar.pyzbar :- It is used to Decode the Bar code .
• qrcode :- It is used to detecting and generating the qr codes .
cv2.VideoCapture(0) : It is used to capture the video , where (0) selects the default webcam . decode from pyzbar : It is used to Detect the QR Codes and Bar Codes .
cv2.polylines : It is used to extract the coordinates of barcode’s corner and convert into
a polygon .
cv2.putText : It is used to overlaiding the decoded data from QR Code on the video frame at
the position of the detected code .
cv2.waitkey(1) : It is Used to smooth Video Playback and updating every mili seconds .
Overall :
This code create a Simple and effective QR Code & Bar code detector . It Displays the
scanned code and decoded code .
Source Code :-
Here Is Your Code Link From Where You Can Access or Copy The Code :-
QrBarTest.py
import cv2
import numpy as np
from pyzbar.pyzbar import decode
#img = cv2.imread('1.png')
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
while True:
success, img = cap.read()
for barcode in decode(img):
myData = barcode.data.decode('utf-8')
print(myData)
pts = np.array([barcode.polygon],np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(255,0,255),5)
pts2 = barcode.rect
cv2.putText(img,myData,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,
0.9,(255,0,255),2)
cv2.imshow('Result',img)
cv2.waitKey(1)
QrCodeProject.py
import cv2
import numpy as np
from pyzbar.pyzbar import decode
#img = cv2.imread('1.png')
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,480)
with open('myDataFile.text') as f:
myDataList = f.read().splitlines()
while True:
success, img = cap.read()
for barcode in decode(img):
myData = barcode.data.decode('utf-8')
print(myData)
if myData in myDataList:
myOutput = 'Authorized'
myColor = (0,255,0)
else:
myOutput = 'Un-Authorized'
myColor = (0, 0, 255)
pts = np.array([barcode.polygon],np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,myColor,5)
pts2 = barcode.rect
cv2.putText(img,myOutput,(pts2[0],pts2[1]),cv2.FONT_HERSHEY_SIMPLEX,
0.9,myColor,2)
cv2.imshow('Result',img)
cv2.waitKey(1)
Output :-
Find More Projects
Build a Quiz Game Using HTML CSS and JavaScript Introduction Hello coders, you might have played various games, but were you aware …
Emoji Catcher Game Using HTML CSS and JavaScript Introduction Hello Coders, Welcome to another new blog. In this article we’ve made a …
Typing Challenge Using HTML CSS and JavaScript Introduction Hello friends, all you developer friends are welcome to our new project. If you …
Breakout Game Using HTML CSS and JavaScript With Source Code Introduction Hello friends, welcome to today’s new blog post. All of you …
Digital and Analog Clock using HTML CSS and JavaScript Introduction : This project is a digital clock and stopwatch system, which allows …
Coffee Shop Website using HTML, CSS & JavaScript Introduction : This project is a website for coffee house business. It uses HTML …