# 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()