Spaces:
Running
Running
File size: 3,363 Bytes
352e30c 2cad3b3 352e30c 8b06480 2cad3b3 352e30c 9f0ef40 352e30c 2cad3b3 6084325 2cad3b3 352e30c 2cad3b3 784d954 352e30c 2cad3b3 352e30c 2cad3b3 352e30c 2cad3b3 8102829 352e30c 8102829 6084325 352e30c 9e1cd24 2cad3b3 352e30c 2cad3b3 6084325 352e30c 6084325 2cad3b3 6084325 2cad3b3 550ae51 352e30c 86ef4a9 550ae51 2cad3b3 6084325 2cad3b3 7df5306 1893f38 7df5306 1893f38 7df5306 1893f38 7df5306 1893f38 550ae51 056d44f |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import os
import tempfile
import cv2
import numpy as np
from PIL import Image
import gradio as gr
import pyperclip
def generate_qr_code(data):
"""
Generate a QR code from the given data.
Args:
data (str): The data to encode in the QR code.
Returns:
str: The path to the generated QR code image.
"""
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill="black", back_color="white")
# Save to a temporary file and return the file path
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
img.save(temp_file.name, format="PNG")
return temp_file.name
def read_qr_code(img):
"""
Read a QR code from the given image.
Args:
img (PIL.Image): The image containing the QR code.
Returns:
str: The decoded data from the QR code.
"""
# Convert PIL image to a NumPy array
img_array = np.array(img)
# Convert RGB to BGR format as OpenCV expects BGR
if img_array.ndim == 3:
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
# Initialize OpenCV QR code detector
detector = cv2.QRCodeDetector()
data, _, _ = detector.detectAndDecode(img_array)
return data if data else "No QR code found."
def download_qr_code(qr_image):
"""
Download the generated QR code image.
Args:
qr_image (str): The path to the generated QR code image.
"""
# Implement download logic here
pass
def copy_to_clipboard(decoded_text):
"""
Copy the decoded text to the clipboard.
Args:
decoded_text (str): The decoded text to copy.
"""
pyperclip.copy(decoded_text)
def create_gradio_interface():
"""
Create the Gradio interface for generating and reading QR codes.
"""
# QR Code Generator Interface
generate_interface = gr.Interface(
fn=generate_qr_code,
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
outputs=gr.Image(label="Generated QR Code"),
title="Generate QR Code",
description="Quickly create a QR code from any text or URL.",
)
# QR Code Reader Interface
read_interface = gr.Interface(
fn=read_qr_code,
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
outputs=gr.Textbox(label="Decoded Data"),
title="Read QR Code",
description="Upload an image with a QR code to decode the embedded data.",
)
# Combine interfaces into a single tabbed layout
with gr.Blocks() as demo:
gr.HTML(custom_css) # Embed the custom CSS
with gr.Tab("Generate QR Code"):
generate_interface.render()
qr_code_output = generate_interface.outputs[0]
download_button = gr.Button("Download QR Code")
download_button.click(fn=download_qr_code, inputs=[qr_code_output], outputs=None)
with gr.Tab("Read QR Code"):
read_interface.render()
decoded_data_output = read_interface.outputs[0]
copy_button = gr.Button("Copy to Clipboard")
copy_button.click(fn=copy_to_clipboard, inputs=[decoded_data_output], outputs=None)
demo.launch(share=True)
|