Spaces:
Sleeping
Sleeping
File size: 2,152 Bytes
11fbb91 675cb03 11fbb91 675cb03 75a253f 48c8169 11fbb91 675cb03 11fbb91 675cb03 11fbb91 75a253f 11fbb91 dcc1ffb 11fbb91 75a253f 48c8169 75a253f 48c8169 75a253f 48c8169 75a253f 48c8169 ace09f3 75a253f 11fbb91 75a253f 11fbb91 675cb03 11fbb91 675cb03 48c8169 11fbb91 48c8169 11fbb91 675cb03 11fbb91 48c8169 |
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import gradio as gr
import cv2
from pyzbar.pyzbar import decode
from PIL import Image
import numpy as np
import io
import base64
def scan_qr_code(image):
"""
Scan QR code from the uploaded image.
Args:
image: The uploaded image (can be PIL Image, numpy array, or file path)
Returns:
str: The decoded QR code data with a descriptive message.
"""
try:
if image is None:
return "No image provided"
# Convert to PIL Image if needed
if isinstance(image, np.ndarray):
image = Image.fromarray(image)
elif isinstance(image, str):
try:
if image.startswith('data:image'):
# Handle base64 encoded images
image_data = base64.b64decode(image.split(',')[1])
image = Image.open(io.BytesIO(image_data))
else:
# Handle file paths
image = Image.open(image)
except Exception as e:
return f"Error opening image: {str(e)}"
# Convert to OpenCV format
open_cv_image = np.array(image)
if len(open_cv_image.shape) == 3: # Color image
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
# Decode QR code
decoded_objects = decode(open_cv_image)
if decoded_objects:
for obj in decoded_objects:
decoded_data = obj.data.decode('utf-8')
return f"The code contains the following data: {decoded_data}"
return "No QR code found"
except Exception as e:
return f"Error processing image: {str(e)}"
# Define the Gradio interface
iface = gr.Interface(
fn=scan_qr_code,
inputs=gr.Image(label="Upload QR Code Image"), # Removed type parameter
outputs=gr.Textbox(label="Result"),
title="QR Code Scanner",
description="Upload an image containing a QR code to decode it.",
examples=[], # You can add example images here
cache_examples=False
)
# Launch the Gradio app
if __name__ == "__main__":
iface.launch(share=True) # Added share=True for public access
|