Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,8 @@
|
|
1 |
-
import qrcode
|
2 |
-
import cv2
|
3 |
-
from PIL import Image
|
4 |
import gradio as gr
|
5 |
import tempfile
|
6 |
-
import
|
7 |
-
import os
|
8 |
|
9 |
-
|
10 |
-
def generate_qr(data):
|
11 |
qr = qrcode.QRCode(
|
12 |
version=1,
|
13 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
@@ -18,50 +13,37 @@ 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() # 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 |
-
|
37 |
-
# Initialize OpenCV QR code detector
|
38 |
detector = cv2.QRCodeDetector()
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
return qr_image # Show image
|
49 |
|
50 |
-
|
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 |
title="Generate QR Code",
|
61 |
description="Quickly create a QR code from any text or URL.",
|
62 |
)
|
63 |
|
64 |
-
# QR Code Reader Tab
|
65 |
read_interface = gr.Interface(
|
66 |
fn=read_qr_interface,
|
67 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
@@ -70,24 +52,21 @@ def create_gradio_interface():
|
|
70 |
description="Upload an image with a QR code to decode the embedded data.",
|
71 |
)
|
72 |
|
73 |
-
# Clipboard Functionality for "Read QR Code" Tab
|
74 |
with gr.Blocks() as demo:
|
75 |
gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
|
76 |
with gr.Tab("Generate QR Code"):
|
77 |
generate_interface.render()
|
78 |
with gr.Tab("Read QR Code"):
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
82 |
copy_button.click(
|
83 |
-
|
|
|
84 |
qr_text,
|
85 |
-
None,
|
86 |
-
_js="() => {navigator.clipboard.writeText(document.querySelector('input[type=text]').value); return 'Copied to Clipboard!'}"
|
87 |
)
|
88 |
|
89 |
demo.launch(share=True)
|
90 |
|
91 |
-
|
92 |
-
# Run the Gradio interface
|
93 |
create_gradio_interface()
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import tempfile
|
3 |
+
import qrcode
|
|
|
4 |
|
5 |
+
def generate_qr(data: str) -> tuple:
|
|
|
6 |
qr = qrcode.QRCode(
|
7 |
version=1,
|
8 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
|
13 |
qr.make(fit=True)
|
14 |
img = qr.make_image(fill="black", back_color="white")
|
15 |
|
|
|
16 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
17 |
img.save(temp_file.name, format="PNG")
|
18 |
temp_file.close() # Ensure file is written to disk
|
19 |
return temp_file.name, img # Return the file path and the PIL image
|
20 |
|
21 |
+
def read_qr(img: Image.Image) -> str:
|
|
|
|
|
|
|
22 |
img = np.array(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
detector = cv2.QRCodeDetector()
|
24 |
data, _, _ = detector.detectAndDecode(img)
|
25 |
return data if data else "No QR code found."
|
26 |
|
|
|
|
|
27 |
def create_gradio_interface():
|
28 |
+
def generate_qr_interface(data: str):
|
29 |
+
qr_file, qr_image = generate_qr(data)
|
30 |
+
return qr_image, qr_file # Show image and provide download link
|
|
|
31 |
|
32 |
+
def read_qr_interface(img: Image.Image):
|
|
|
33 |
decoded_data = read_qr(img)
|
34 |
return decoded_data # Return decoded text
|
35 |
|
|
|
36 |
generate_interface = gr.Interface(
|
37 |
fn=generate_qr_interface,
|
38 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
39 |
+
outputs=[
|
40 |
+
gr.Image(label="Generated QR Code"), # Display the QR code image
|
41 |
+
gr.File(label="Download QR Code"), # Downloadable link for the QR code file
|
42 |
+
],
|
43 |
title="Generate QR Code",
|
44 |
description="Quickly create a QR code from any text or URL.",
|
45 |
)
|
46 |
|
|
|
47 |
read_interface = gr.Interface(
|
48 |
fn=read_qr_interface,
|
49 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
|
|
52 |
description="Upload an image with a QR code to decode the embedded data.",
|
53 |
)
|
54 |
|
|
|
55 |
with gr.Blocks() as demo:
|
56 |
gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
|
57 |
with gr.Tab("Generate QR Code"):
|
58 |
generate_interface.render()
|
59 |
with gr.Tab("Read QR Code"):
|
60 |
+
with gr.Row():
|
61 |
+
qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
|
62 |
+
copy_button = gr.Button("Copy to Clipboard")
|
63 |
+
read_interface.render()
|
64 |
copy_button.click(
|
65 |
+
lambda text: gr.Textbox.update(value="Copied to Clipboard!"),
|
66 |
+
qr_text,
|
67 |
qr_text,
|
|
|
|
|
68 |
)
|
69 |
|
70 |
demo.launch(share=True)
|
71 |
|
|
|
|
|
72 |
create_gradio_interface()
|