Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import qrcode
|
2 |
-
import cv2
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
import tempfile
|
6 |
import numpy as np
|
7 |
|
8 |
-
|
9 |
# Function to generate a QR code
|
10 |
def generate_qr(data):
|
11 |
qr = qrcode.QRCode(
|
@@ -22,80 +20,83 @@ def generate_qr(data):
|
|
22 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
23 |
img.save(temp_file.name, format="PNG")
|
24 |
temp_file.close() # Ensure file is written to disk
|
25 |
-
return temp_file.name, img # Return the file path and the image
|
26 |
|
|
|
27 |
|
28 |
# Function to read a QR code
|
29 |
-
def read_qr(
|
30 |
# Convert PIL image to a NumPy array
|
31 |
-
img =
|
|
|
32 |
|
33 |
# Convert RGB to BGR as OpenCV expects
|
34 |
-
if
|
35 |
-
|
36 |
|
37 |
# Initialize OpenCV QR code detector
|
38 |
detector = cv2.QRCodeDetector()
|
39 |
-
data, _, _ = detector.detectAndDecode(
|
40 |
return data if data else "No QR code found."
|
41 |
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
#
|
44 |
-
def
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
def read_qr_interface(img):
|
51 |
-
decoded_data = read_qr(img)
|
52 |
-
return decoded_data # Return the decoded text
|
53 |
-
|
54 |
-
|
55 |
-
# Clipboard copy callback
|
56 |
-
def copy_to_clipboard(decoded_text):
|
57 |
-
# Gradio does not support direct clipboard copy; simulate success message
|
58 |
-
return f"Copied: {decoded_text}" if decoded_text else "Nothing to copy!"
|
59 |
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
# Create Gradio Interface
|
62 |
-
def create_gradio_interface():
|
63 |
# QR Code Generator Tab
|
64 |
generate_interface = gr.Interface(
|
65 |
fn=generate_qr_interface,
|
66 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
67 |
outputs=[
|
68 |
-
gr.Image(label="Generated QR Code"), # Display the image
|
69 |
-
gr.File(label="Download QR Code"), #
|
70 |
],
|
71 |
title="Generate QR Code",
|
72 |
description="Quickly create a QR code from any text or URL.",
|
73 |
)
|
74 |
|
75 |
# QR Code Reader Tab
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
qr_input.change(read_qr_interface, inputs=qr_input, outputs=qr_text)
|
86 |
-
copy_button.click(copy_to_clipboard, inputs=qr_text, outputs=copy_feedback)
|
87 |
-
|
88 |
-
# Main interface with tabs
|
89 |
with gr.Blocks() as demo:
|
90 |
-
gr.Markdown("
|
91 |
with gr.Tab("Generate QR Code"):
|
92 |
generate_interface.render()
|
93 |
with gr.Tab("Read QR Code"):
|
|
|
|
|
94 |
read_interface.render()
|
95 |
|
96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
demo.launch(share=True)
|
98 |
|
99 |
|
100 |
-
# Run the Gradio
|
101 |
create_gradio_interface()
|
|
|
1 |
import qrcode
|
|
|
2 |
from PIL import Image
|
3 |
import gradio as gr
|
4 |
import tempfile
|
5 |
import numpy as np
|
6 |
|
|
|
7 |
# Function to generate a QR code
|
8 |
def generate_qr(data):
|
9 |
qr = qrcode.QRCode(
|
|
|
20 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
21 |
img.save(temp_file.name, format="PNG")
|
22 |
temp_file.close() # Ensure file is written to disk
|
|
|
23 |
|
24 |
+
return temp_file.name # Return the file path only, Gradio will handle displaying the image
|
25 |
|
26 |
# Function to read a QR code
|
27 |
+
def read_qr(image_file):
|
28 |
# Convert PIL image to a NumPy array
|
29 |
+
img = Image.open(image_file.name)
|
30 |
+
img_np = np.array(img)
|
31 |
|
32 |
# Convert RGB to BGR as OpenCV expects
|
33 |
+
if img_np.ndim == 3:
|
34 |
+
img_np = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
|
35 |
|
36 |
# Initialize OpenCV QR code detector
|
37 |
detector = cv2.QRCodeDetector()
|
38 |
+
data, _, _ = detector.detectAndDecode(img_np)
|
39 |
return data if data else "No QR code found."
|
40 |
|
41 |
+
# Function to copy text to clipboard (this requires platform-specific implementations)
|
42 |
+
def copy_to_clipboard(text):
|
43 |
+
import pyperclip
|
44 |
+
pyperclip.copy(text)
|
45 |
+
return "Copied to Clipboard!"
|
46 |
|
47 |
+
# Gradio Interface
|
48 |
+
def create_gradio_interface():
|
49 |
+
# QR Code Generator with Display and Downloadable Link
|
50 |
+
def generate_qr_interface(data):
|
51 |
+
qr_file = generate_qr(data) # This will now return the file path directly
|
52 |
+
qr_image = Image.open(qr_file)
|
53 |
+
return [qr_image, qr_file] # Return both image and file for Gradio to display/download
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
# QR Code Reader with Copy-to-Clipboard Button
|
56 |
+
def read_qr_interface(image_file):
|
57 |
+
decoded_data = read_qr(image_file)
|
58 |
+
return decoded_data # Return decoded text
|
59 |
|
|
|
|
|
60 |
# QR Code Generator Tab
|
61 |
generate_interface = gr.Interface(
|
62 |
fn=generate_qr_interface,
|
63 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
64 |
outputs=[
|
65 |
+
gr.Image(label="Generated QR Code"), # Display the QR code image
|
66 |
+
gr.File(label="Download QR Code"), # Downloadable link for the QR code file
|
67 |
],
|
68 |
title="Generate QR Code",
|
69 |
description="Quickly create a QR code from any text or URL.",
|
70 |
)
|
71 |
|
72 |
# QR Code Reader Tab
|
73 |
+
read_interface = gr.Interface(
|
74 |
+
fn=read_qr_interface,
|
75 |
+
inputs=gr.File(type="file", label="Upload QR Code Image"),
|
76 |
+
outputs=gr.Textbox(label="Decoded Data"),
|
77 |
+
title="Read QR Code",
|
78 |
+
description="Upload an image with a QR code to decode the embedded data.",
|
79 |
+
)
|
80 |
+
|
81 |
+
# Clipboard Functionality for "Read QR Code" Tab
|
|
|
|
|
|
|
|
|
82 |
with gr.Blocks() as demo:
|
83 |
+
gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
|
84 |
with gr.Tab("Generate QR Code"):
|
85 |
generate_interface.render()
|
86 |
with gr.Tab("Read QR Code"):
|
87 |
+
qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
|
88 |
+
copy_button = gr.Button("Copy to Clipboard")
|
89 |
read_interface.render()
|
90 |
|
91 |
+
# Connect button click to copy function
|
92 |
+
copy_button.click(
|
93 |
+
fn=copy_to_clipboard,
|
94 |
+
inputs=qr_text,
|
95 |
+
outputs=qr_text,
|
96 |
+
)
|
97 |
+
|
98 |
demo.launch(share=True)
|
99 |
|
100 |
|
101 |
+
# Run the Gradio interface
|
102 |
create_gradio_interface()
|