Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import zxingcpp
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
def scan_barcode(image: np.ndarray) -> dict:
|
| 7 |
+
"""Scan barcode from image array"""
|
| 8 |
+
# Convert RGB to BGR for OpenCV
|
| 9 |
+
bgr_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 10 |
+
|
| 11 |
+
# Scan barcodes
|
| 12 |
+
results = zxingcpp.read_barcodes(bgr_image)
|
| 13 |
+
|
| 14 |
+
if results:
|
| 15 |
+
# Return first barcode found
|
| 16 |
+
return {
|
| 17 |
+
"status": "success",
|
| 18 |
+
"text": results[0].text,
|
| 19 |
+
"format": str(results[0].format)
|
| 20 |
+
}
|
| 21 |
+
return {"status": "error", "message": "No barcode detected"}
|
| 22 |
+
|
| 23 |
+
# Create Gradio interface
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=scan_barcode,
|
| 26 |
+
inputs=gr.Image(label="Barcode Image", type="numpy"),
|
| 27 |
+
outputs=gr.JSON(label="Scan Result"),
|
| 28 |
+
title="📦 Barcode Scanner API",
|
| 29 |
+
description="Upload an image to scan barcode",
|
| 30 |
+
allow_flagging="never"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# Launch the application
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
iface.launch(
|
| 36 |
+
server_name="0.0.0.0",
|
| 37 |
+
server_port=7860,
|
| 38 |
+
show_error=True
|
| 39 |
+
)
|