dev2607's picture
Update app.py
5c653f6 verified
raw
history blame
1.17 kB
pip install opencv-contrib-python
source venv/bin/activate
venv\Scripts\activate
pip install opencv-python
import streamlit as st
import cv2
import numpy as np
from pyzbar.pyzbar import decode
from PIL import Image
def decode_barcode(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
barcodes = decode(gray)
results = []
for barcode in barcodes:
barcode_data = barcode.data.decode("utf-8")
barcode_type = barcode.type
results.append(f"Type: {barcode_type}, Data: {barcode_data}")
return results
st.title("Barcode Scanner App")
st.write("Upload an image with a barcode to scan.")
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "png", "jpeg"])
if uploaded_file is not None:
image = Image.open(uploaded_file)
img_np = np.array(image)
img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
st.image(image, caption="Uploaded Image", use_column_width=True)
results = decode_barcode(img_bgr)
if results:
st.success("Barcodes Found:")
for res in results:
st.write(res)
else:
st.warning("No barcode detected. Try another image.")