File size: 1,130 Bytes
0b554f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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:
        # Convert Gradio image to OpenCV format
        image = np.array(image.convert('RGB'))  
        gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
        
        # Decode using pyzbar
        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)}"

# Gradio Interface
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)