Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from pyzbar.pyzbar import decode
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
def read_qr_barcode(image):
|
| 8 |
+
"""Reads QR Code or Barcode from an uploaded image."""
|
| 9 |
+
try:
|
| 10 |
+
# Convert Gradio image to OpenCV format
|
| 11 |
+
image = np.array(image.convert('RGB'))
|
| 12 |
+
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 13 |
+
|
| 14 |
+
# Decode using pyzbar
|
| 15 |
+
decoded_objects = decode(gray)
|
| 16 |
+
|
| 17 |
+
if not decoded_objects:
|
| 18 |
+
return "No QR Code or Barcode found."
|
| 19 |
+
|
| 20 |
+
results = []
|
| 21 |
+
for obj in decoded_objects:
|
| 22 |
+
results.append(f"Type: {obj.type} | Data: {obj.data.decode('utf-8')}")
|
| 23 |
+
|
| 24 |
+
return "\n".join(results)
|
| 25 |
+
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return f"Error: {str(e)}"
|
| 28 |
+
|
| 29 |
+
# Gradio Interface
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=read_qr_barcode,
|
| 32 |
+
inputs=gr.Image(type="pil", label="Upload QR/Barcode Image"),
|
| 33 |
+
outputs=gr.Textbox(label="Scan Result"),
|
| 34 |
+
description="Upload an image containing a QR Code or Barcode to scan and retrieve its data.",
|
| 35 |
+
css="footer {visibility: hidden}"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
interface.launch(share=True)
|