Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,37 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import pylibdmtx.pylibdmtx as pylibdmtx
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
img = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
# Process each barcode found
|
| 13 |
results = []
|
| 14 |
-
for
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
if len(results) == 0:
|
| 32 |
-
return "No barcode found."
|
| 33 |
-
else:
|
| 34 |
-
output = ""
|
| 35 |
-
for barcode_type, barcode_data in results:
|
| 36 |
-
output += f"Type: {barcode_type}, Data: {barcode_data}\n"
|
| 37 |
-
return output.strip()
|
| 38 |
-
|
| 39 |
-
# Define the input and output interfaces using Gradio
|
| 40 |
-
inputs = gr.inputs.Image(label="Input Image")
|
| 41 |
-
output = gr.outputs.Textbox(label="Extracted Text")
|
| 42 |
-
|
| 43 |
-
# Create the Gradio interface
|
| 44 |
-
gr.Interface(fn=process_image, inputs=inputs, outputs=output, title="Barcode Reader",
|
| 45 |
-
description="Upload an image to extract text from DataMatrix barcodes.").launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
from pyzbar.pyzbar import decode
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
def qr_barcode_scanner(image):
|
| 8 |
+
# Convert PIL image to numpy array
|
| 9 |
+
image_data = np.array(image)
|
| 10 |
+
|
| 11 |
+
# Convert RGB to grayscale if needed
|
| 12 |
+
if len(image_data.shape) == 3 and image_data.shape[2] == 3:
|
| 13 |
+
image_data = cv2.cvtColor(image_data, cv2.COLOR_RGB2GRAY)
|
| 14 |
|
| 15 |
+
# Decode QR codes and barcodes
|
| 16 |
+
decoded_objects = decode(image_data)
|
|
|
|
| 17 |
|
| 18 |
+
if len(decoded_objects) == 0:
|
| 19 |
+
return "No QR code or barcode found."
|
| 20 |
|
|
|
|
| 21 |
results = []
|
| 22 |
+
for obj in decoded_objects:
|
| 23 |
+
data = obj.data.decode("utf-8")
|
| 24 |
+
results.append(f"Type: {obj.type}, Data: {data}")
|
| 25 |
+
|
| 26 |
+
return "\n".join(results)
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=qr_barcode_scanner,
|
| 30 |
+
inputs=gr.inputs.Image(type="pil", label="Select an image containing QR code or barcode."),
|
| 31 |
+
outputs=gr.outputs.Textbox(),
|
| 32 |
+
live=True,
|
| 33 |
+
capture_session=True,
|
| 34 |
+
interpretation="default"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|