Spaces:
Build error
Build error
Commit ·
1934424
1
Parent(s): 1b8dd75
Add application with streamlit
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""This is a demo for running the barcode QR code reader usng streamlit library"""
|
| 2 |
+
|
| 3 |
+
from dataclasses import dataclass, field
|
| 4 |
+
from typing import Any, Optional
|
| 5 |
+
import asyncio
|
| 6 |
+
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import numpy as np
|
| 10 |
+
import pandas as pd
|
| 11 |
+
|
| 12 |
+
from src.deep_barcode_reader.barcode import Wrapper
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class DemoBarcodeReader:
|
| 17 |
+
"""This is a demo class for barcode/qr code reader using different methods"""
|
| 18 |
+
|
| 19 |
+
image: Optional[Any] = field(init=False, default=None)
|
| 20 |
+
model_option: str = field(init=False, default="opencv")
|
| 21 |
+
model_size: str = field(init=False, default="n")
|
| 22 |
+
|
| 23 |
+
def upload_image(self) -> None:
|
| 24 |
+
"""Upload an image from the streamlit page"""
|
| 25 |
+
uploaded_file = st.file_uploader(
|
| 26 |
+
"Choose an image...", type=["jpg", "png", "jpeg"]
|
| 27 |
+
)
|
| 28 |
+
if uploaded_file is not None:
|
| 29 |
+
self.image = Image.open(uploaded_file)
|
| 30 |
+
else:
|
| 31 |
+
self.image = Image.open("tests/test_data/sample.jpg")
|
| 32 |
+
|
| 33 |
+
st.image(
|
| 34 |
+
self.image, caption="Original/Uploaded Image", use_container_width=True
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
def select_model(self) -> None:
|
| 38 |
+
"""Select a model for barcode/qr code reader"""
|
| 39 |
+
self.model_option = st.selectbox(
|
| 40 |
+
"Choose a reader/decoder model", ["zbar", "opencv", "qrreader"]
|
| 41 |
+
)
|
| 42 |
+
if self.model_option == "qrreader":
|
| 43 |
+
ml_size = st.selectbox(
|
| 44 |
+
"Choose a model size for QRReader method",
|
| 45 |
+
["nano", "small", "medium", "large"],
|
| 46 |
+
)
|
| 47 |
+
self.model_size = (
|
| 48 |
+
"n"
|
| 49 |
+
if ml_size == "nano"
|
| 50 |
+
else "s" if ml_size == "small" else "m" if ml_size == "medium" else "l"
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
def process_image(self) -> None:
|
| 54 |
+
"""Process the image for barcode/qr code reader"""
|
| 55 |
+
if st.button("Read/Decode Barcode/QR Code"):
|
| 56 |
+
reader = Wrapper(model_size=self.model_size, method=self.model_option)
|
| 57 |
+
detections, result_img = asyncio.run(
|
| 58 |
+
reader.method_selection(image=np.array(self.image), result_path="")
|
| 59 |
+
)
|
| 60 |
+
st.markdown("<h3>Detected Results</h3>", unsafe_allow_html=True)
|
| 61 |
+
st.image(result_img, caption="Decoded Result", use_container_width=True)
|
| 62 |
+
results = pd.DataFrame(
|
| 63 |
+
{
|
| 64 |
+
"Barcode/QR Types": detections.decoded_types,
|
| 65 |
+
"Data": detections.decoded_data,
|
| 66 |
+
"Boundary Box": [str(bbx) for bbx in detections.bbox_data],
|
| 67 |
+
}
|
| 68 |
+
)
|
| 69 |
+
st.markdown('<div class="center-container">', unsafe_allow_html=True)
|
| 70 |
+
st.markdown(
|
| 71 |
+
"<h3>Detailed Information of Detections</h3>", unsafe_allow_html=True
|
| 72 |
+
)
|
| 73 |
+
st.table(results)
|
| 74 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
| 75 |
+
|
| 76 |
+
def design_page(self) -> None:
|
| 77 |
+
"""Design the streamlit page for barcode/qr code reader"""
|
| 78 |
+
st.title("Image Barcode/QR Code Reader and Detector")
|
| 79 |
+
self.upload_image()
|
| 80 |
+
self.select_model()
|
| 81 |
+
self.process_image()
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
demo = DemoBarcodeReader()
|
| 85 |
+
demo.design_page()
|