Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,9 @@ import cv2
|
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
import tempfile
|
6 |
-
import os
|
7 |
import numpy as np
|
8 |
|
|
|
9 |
# Function to generate a QR code
|
10 |
def generate_qr(data):
|
11 |
qr = qrcode.QRCode(
|
@@ -18,18 +18,19 @@ def generate_qr(data):
|
|
18 |
qr.make(fit=True)
|
19 |
img = qr.make_image(fill="black", back_color="white")
|
20 |
|
21 |
-
# Save to a temporary file
|
22 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
23 |
img.save(temp_file.name, format="PNG")
|
24 |
-
temp_file.close() #
|
25 |
-
return temp_file.name # Return the file path
|
|
|
26 |
|
27 |
-
# Function to read a QR code
|
28 |
def read_qr(img):
|
29 |
# Convert PIL image to a NumPy array
|
30 |
img = np.array(img)
|
31 |
|
32 |
-
# Convert RGB to BGR
|
33 |
if img.ndim == 3:
|
34 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
35 |
|
@@ -38,58 +39,58 @@ def read_qr(img):
|
|
38 |
data, _, _ = detector.detectAndDecode(img)
|
39 |
return data if data else "No QR code found."
|
40 |
|
41 |
-
|
|
|
42 |
def create_gradio_interface():
|
43 |
-
# QR Code Generator with Downloadable
|
44 |
-
def
|
45 |
-
qr_file = generate_qr(data)
|
46 |
-
return qr_file #
|
47 |
|
48 |
-
# QR Code Reader with Copy-to-Clipboard
|
49 |
-
def
|
50 |
-
decoded_data = read_qr(img)
|
51 |
-
return decoded_data
|
52 |
|
53 |
-
# Generator
|
54 |
generate_interface = gr.Interface(
|
55 |
-
fn=
|
56 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
57 |
-
outputs=
|
|
|
|
|
|
|
58 |
title="Generate QR Code",
|
59 |
description="Quickly create a QR code from any text or URL.",
|
60 |
)
|
61 |
|
62 |
-
# Reader
|
63 |
read_interface = gr.Interface(
|
64 |
-
fn=
|
65 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
66 |
-
outputs=
|
67 |
-
gr.Textbox(label="Decoded Data"), # Decoded QR code text
|
68 |
-
gr.Textbox(visible=False), # Invisible output for clipboard content
|
69 |
-
],
|
70 |
title="Read QR Code",
|
71 |
description="Upload an image with a QR code to decode the embedded data.",
|
72 |
-
live=False,
|
73 |
)
|
74 |
|
75 |
-
#
|
76 |
-
def on_copy(decoded_text):
|
77 |
-
return gr.Textbox.update(value=decoded_text) # Update clipboard content
|
78 |
-
|
79 |
-
# Custom UI with clipboard copy button
|
80 |
with gr.Blocks() as demo:
|
81 |
gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
|
82 |
with gr.Tab("Generate QR Code"):
|
83 |
generate_interface.render()
|
84 |
with gr.Tab("Read QR Code"):
|
85 |
with gr.Row():
|
86 |
-
qr_text = gr.Textbox(label="Decoded Data")
|
87 |
-
|
88 |
-
copy_button = gr.Button("Copy to Clipboard")
|
89 |
read_interface.render()
|
90 |
-
copy_button.click(
|
|
|
|
|
|
|
|
|
91 |
|
92 |
demo.launch(share=True)
|
93 |
|
|
|
94 |
# Run the Gradio interface
|
95 |
create_gradio_interface()
|
|
|
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(
|
|
|
18 |
qr.make(fit=True)
|
19 |
img = qr.make_image(fill="black", back_color="white")
|
20 |
|
21 |
+
# Save to a temporary file
|
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 PIL image
|
26 |
+
|
27 |
|
28 |
+
# Function to read a QR code
|
29 |
def read_qr(img):
|
30 |
# Convert PIL image to a NumPy array
|
31 |
img = np.array(img)
|
32 |
|
33 |
+
# Convert RGB to BGR as OpenCV expects
|
34 |
if img.ndim == 3:
|
35 |
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
36 |
|
|
|
39 |
data, _, _ = detector.detectAndDecode(img)
|
40 |
return data if data else "No QR code found."
|
41 |
|
42 |
+
|
43 |
+
# Gradio Interface
|
44 |
def create_gradio_interface():
|
45 |
+
# QR Code Generator with Display and Downloadable Link
|
46 |
+
def generate_qr_interface(data):
|
47 |
+
qr_file, qr_image = generate_qr(data)
|
48 |
+
return qr_image, qr_file # Show image and provide download link
|
49 |
|
50 |
+
# QR Code Reader with Copy-to-Clipboard Button
|
51 |
+
def read_qr_interface(img):
|
52 |
+
decoded_data = read_qr(img)
|
53 |
+
return decoded_data # Return decoded text
|
54 |
|
55 |
+
# QR Code Generator Tab
|
56 |
generate_interface = gr.Interface(
|
57 |
+
fn=generate_qr_interface,
|
58 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
59 |
+
outputs=[
|
60 |
+
gr.Image(label="Generated QR Code"), # Display the QR code image
|
61 |
+
gr.File(label="Download QR Code"), # Downloadable link for the QR code file
|
62 |
+
],
|
63 |
title="Generate QR Code",
|
64 |
description="Quickly create a QR code from any text or URL.",
|
65 |
)
|
66 |
|
67 |
+
# QR Code Reader Tab
|
68 |
read_interface = gr.Interface(
|
69 |
+
fn=read_qr_interface,
|
70 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
71 |
+
outputs=gr.Textbox(label="Decoded Data"),
|
|
|
|
|
|
|
72 |
title="Read QR Code",
|
73 |
description="Upload an image with a QR code to decode the embedded data.",
|
|
|
74 |
)
|
75 |
|
76 |
+
# Clipboard Functionality for "Read QR Code" Tab
|
|
|
|
|
|
|
|
|
77 |
with gr.Blocks() as demo:
|
78 |
gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
|
79 |
with gr.Tab("Generate QR Code"):
|
80 |
generate_interface.render()
|
81 |
with gr.Tab("Read QR Code"):
|
82 |
with gr.Row():
|
83 |
+
qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
|
84 |
+
copy_button = gr.Button("Copy to Clipboard")
|
|
|
85 |
read_interface.render()
|
86 |
+
copy_button.click(
|
87 |
+
lambda text: gr.Textbox.update(value="Copied to Clipboard!"),
|
88 |
+
qr_text,
|
89 |
+
qr_text,
|
90 |
+
)
|
91 |
|
92 |
demo.launch(share=True)
|
93 |
|
94 |
+
|
95 |
# Run the Gradio interface
|
96 |
create_gradio_interface()
|