Spaces:
Sleeping
Sleeping
# 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): The uploaded image. | |
Returns: | |
str: The decoded QR code data with a descriptive message. | |
""" | |
# 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="pil"), | |
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() | |