|
import cv2 |
|
import numpy as np |
|
import gradio as gr |
|
from pyzbar.pyzbar import decode |
|
from PIL import Image |
|
|
|
def read_qr_barcode(image): |
|
"""Reads QR Code or Barcode from an uploaded image.""" |
|
try: |
|
|
|
image = np.array(image.convert('RGB')) |
|
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) |
|
|
|
|
|
decoded_objects = decode(gray) |
|
|
|
if not decoded_objects: |
|
return "No QR Code or Barcode found." |
|
|
|
results = [] |
|
for obj in decoded_objects: |
|
results.append(f"Type: {obj.type} | Data: {obj.data.decode('utf-8')}") |
|
|
|
return "\n".join(results) |
|
|
|
except Exception as e: |
|
return f"Error: {str(e)}" |
|
|
|
|
|
interface = gr.Interface( |
|
fn=read_qr_barcode, |
|
inputs=gr.Image(type="pil", label="Upload QR/Barcode Image"), |
|
outputs=gr.Textbox(label="Scan Result"), |
|
description="Upload an image containing a QR Code or Barcode to scan and retrieve its data.", |
|
css="footer {visibility: hidden}" |
|
) |
|
|
|
interface.launch(share=True) |
|
|