Spaces:
Sleeping
Sleeping
File size: 1,352 Bytes
11fbb91 675cb03 11fbb91 675cb03 11fbb91 675cb03 11fbb91 675cb03 11fbb91 675cb03 11fbb91 059b0b9 11fbb91 dcc1ffb 11fbb91 ace09f3 059b0b9 ace09f3 675cb03 11fbb91 675cb03 11fbb91 675cb03 dcc1ffb 675cb03 11fbb91 675cb03 11fbb91 675cb03 ace09f3 1888e1a 11fbb91 675cb03 11fbb91 675cb03 11fbb91 675cb03 |
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 |
# app.py
# =============
# This is a complete app.py file for a QR code scanner using Gradio and Hugging Face Transformers.
import gradio as gr
import cv2
from pyzbar.pyzbar import decode
from PIL import Image
import numpy as np
def scan_qr_code(image):
"""
Scan QR code from the uploaded image.
Args:
image (PIL.Image.Image or dict): The uploaded image or a dictionary containing the image file path.
Returns:
str: The decoded QR code data with a descriptive message.
"""
# Convert file path to PIL image if necessary
if isinstance(image, dict):
image = Image.open(image['name'])
# Convert PIL image to OpenCV format
open_cv_image = np.array(image)
open_cv_image = open_cv_image[:, :, ::-1].copy()
# 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"
# Define the Gradio interface
iface = gr.Interface(
fn=scan_qr_code,
inputs=gr.Image(type="filepath"),
outputs=gr.Textbox(),
title="QR Code Scanner",
description="Upload an image containing a QR code to decode it."
)
# Launch the Gradio app
if __name__ == "__main__":
iface.launch()
|