Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,65 @@
|
|
| 1 |
# app.py
|
| 2 |
-
|
| 3 |
import cv2
|
| 4 |
import zxingcpp
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
def scan_barcode(image
|
| 9 |
-
"""Scan barcode from image
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Create Gradio interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=scan_barcode,
|
| 28 |
-
api_name="scan", # <-- ADD THIS LINE
|
| 29 |
inputs=gr.Image(label="Barcode Image", type="numpy"),
|
| 30 |
outputs=gr.JSON(label="Scan Result"),
|
| 31 |
title="📦 Barcode Scanner API",
|
| 32 |
description="Upload an image to scan barcode",
|
| 33 |
-
|
|
|
|
| 34 |
)
|
| 35 |
|
| 36 |
# Launch the application
|
|
@@ -38,5 +67,6 @@ if __name__ == "__main__":
|
|
| 38 |
iface.launch(
|
| 39 |
server_name="0.0.0.0",
|
| 40 |
server_port=7860,
|
| 41 |
-
show_error=True
|
|
|
|
| 42 |
)
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import cv2
|
| 3 |
import zxingcpp
|
| 4 |
import gradio as gr
|
| 5 |
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import base64
|
| 8 |
+
import io
|
| 9 |
|
| 10 |
+
def scan_barcode(image):
|
| 11 |
+
"""Scan barcode from image"""
|
| 12 |
+
try:
|
| 13 |
+
# Handle different input types
|
| 14 |
+
if isinstance(image, str):
|
| 15 |
+
# If it's a base64 string, decode it
|
| 16 |
+
if image.startswith('data:image'):
|
| 17 |
+
# Remove the data:image/jpeg;base64, prefix
|
| 18 |
+
image_data = image.split(',')[1]
|
| 19 |
+
image_bytes = base64.b64decode(image_data)
|
| 20 |
+
pil_image = Image.open(io.BytesIO(image_bytes))
|
| 21 |
+
image = np.array(pil_image)
|
| 22 |
+
else:
|
| 23 |
+
# Direct base64 without prefix
|
| 24 |
+
image_bytes = base64.b64decode(image)
|
| 25 |
+
pil_image = Image.open(io.BytesIO(image_bytes))
|
| 26 |
+
image = np.array(pil_image)
|
| 27 |
+
|
| 28 |
+
# Ensure image is numpy array
|
| 29 |
+
if not isinstance(image, np.ndarray):
|
| 30 |
+
return {"status": "error", "message": "Invalid image format"}
|
| 31 |
+
|
| 32 |
+
# Convert RGB to BGR for OpenCV if needed
|
| 33 |
+
if len(image.shape) == 3 and image.shape[2] == 3:
|
| 34 |
+
bgr_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
| 35 |
+
else:
|
| 36 |
+
bgr_image = image
|
| 37 |
+
|
| 38 |
+
# Scan barcodes
|
| 39 |
+
results = zxingcpp.read_barcodes(bgr_image)
|
| 40 |
+
|
| 41 |
+
if results:
|
| 42 |
+
# Return first barcode found
|
| 43 |
+
return {
|
| 44 |
+
"status": "success",
|
| 45 |
+
"text": results[0].text,
|
| 46 |
+
"format": str(results[0].format)
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
return {"status": "error", "message": "No barcode detected"}
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return {"status": "error", "message": f"Processing error: {str(e)}"}
|
| 53 |
|
| 54 |
# Create Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=scan_barcode,
|
|
|
|
| 57 |
inputs=gr.Image(label="Barcode Image", type="numpy"),
|
| 58 |
outputs=gr.JSON(label="Scan Result"),
|
| 59 |
title="📦 Barcode Scanner API",
|
| 60 |
description="Upload an image to scan barcode",
|
| 61 |
+
flagging_mode="never", # Updated parameter
|
| 62 |
+
api_name="scan"
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch the application
|
|
|
|
| 67 |
iface.launch(
|
| 68 |
server_name="0.0.0.0",
|
| 69 |
server_port=7860,
|
| 70 |
+
show_error=True,
|
| 71 |
+
share=False # Set to True if you want public URL
|
| 72 |
)
|