Make Barcode Scanner using Python
In this article, we're going to learn, how can we make barcode scanner using python.
in this we're going to use :
- openCV : pip install opencv-python
- pyzbar : pip install pyzbar
- IP Webcam : install on android through play store
So, let's get Started !
firstly ,
we'll import cv2 and pyzbar in our code editor.
- import cv2
- from pyzbar import pyzbar
Now , open ur mobile, and after installing IP Webcam in that,
1. open hotspot
2. Open IP Webcam
3. Click Start Server
Recommended: [ How to connect mobile camera with PC using Python ]and then come in your code editor in computer.
3. url = "192.168.43.1:8080/video"
and then , Start video capturing by mobile camera url
4. cap = cv2.VideoCapture(url)
and under while loop, get started working main part.
read the frame, extract barcode / qrcode from frame and extract its data and type
and show data on cv2 window.
at break loop, print the last barcode scanned data for ensuring data extraction.
Here, it is the full source code ⇨
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 | import cv2 from pyzbar import pyzbar #open IP webcam in mobile and hotspot #then url will be this ::-- url = "192.168.43.1:8080/video" cap = cv2.VideoCapture(url) while True: ret, frame = cap.read() frame = cv2.resize(frame, (0, 0),fx=0.50, fy=0.50) cv2.putText(frame, "Press q to close camera", (10,10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 100), 1) # find the barcodes in the frame and decode each of the barcodes barcodes = pyzbar.decode(frame) # loop over the detected barcodes for barcode in barcodes: # extract the bounding box location of the barcode and draw # the bounding box surrounding the barcode on the image (x, y, w, h) = barcode.rect cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 1) # the barcode data is a bytes object so if we want to draw it # on our output image we need to convert it to a string first barcodeData = barcode.data.decode("utf-8") barcodeType = barcode.type # draw the barcode data and barcode type on the image text = "{} ({})".format(barcodeData, barcodeType) cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1) cv2.imshow("Barcode Scanner",frame) if cv2.waitKey(1)==ord('q'): break cap.release() cv2.destroyAllWindows() |
Watch Full video Illustration 👉
Comments
Post a Comment