Peter  Lunk
Published © GPL3+

Webcam Bar-code scanner and product lookup

Scans Bar-code's from products with your webcam and opens a google searh for the product automatically. (ChatGPT helped me create this code.

IntermediateProtip1 hour7
Webcam Bar-code scanner and product lookup

Story

Read more

Code

Webcam Bar-code scanner in python/openCV

Python
This script uses the OpenCV library to capture frames from the user's webcam and the pyzbar library to detect barcodes in the frames. It then converts the frames to grayscale and uses the pyzbar library to decode the barcodes. If a barcode is detected, the script extracts the barcode data, which is the contents of the barcode, and opens a new tab in the default browser to search for the barcode contents on Google. The script also allows the user to quit the webcam by pressing the 'q' key. Finally, the script releases the webcam and closes the script.
import cv2
import webbrowser
import pyzbar.pyzbar as pyzbar

# Create a VideoCapture object to access the webcam
cap = cv2.VideoCapture(0)

while True:
    # Capture a frame from the webcam
    ret, frame = cap.read()
    # Display the webcam image
    cv2.imshow("Webcam", frame)
    
    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Detect barcodes in the frame
    barcodes = pyzbar.decode(gray)

    # Iterate through the detected barcodes
    for barcode in barcodes:
        # Extract the barcode data (i.e. the contents of the barcode)
        barcodeData = barcode.data.decode("utf-8")
        print(barcodeData)
        
        # Open a new tab in the default browser to search for the barcode contents on Google
        webbrowser.open_new_tab("https://www.google.com/search?q=" + barcodeData)

        # Release the webcam and close the script
        cap.release()
        cv2.destroyAllWindows()
        exit()
    # Press 'q' to quit
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release the webcam and close the script
cap.release()
cv2.destroyAllWindows()

Credits

Peter  Lunk
5 projects • 31 followers
Contact

Comments

Please log in or sign up to comment.