Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,14 @@
|
|
1 |
-
import
|
2 |
-
import tempfile
|
3 |
import cv2
|
4 |
-
import numpy as np
|
5 |
from PIL import Image
|
6 |
import gradio as gr
|
7 |
-
import
|
8 |
-
|
9 |
-
|
10 |
-
"""
|
11 |
-
Generate a QR code from the given data.
|
12 |
|
13 |
-
Args:
|
14 |
-
data (str): The data to encode in the QR code.
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
"""
|
19 |
qr = qrcode.QRCode(
|
20 |
version=1,
|
21 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
@@ -26,87 +19,70 @@ def generate_qr_code(data):
|
|
26 |
qr.make(fit=True)
|
27 |
img = qr.make_image(fill="black", back_color="white")
|
28 |
|
29 |
-
# Save to a temporary file
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
33 |
|
34 |
-
def read_qr_code(img):
|
35 |
-
"""
|
36 |
-
Read a QR code from the given image.
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
Returns:
|
42 |
-
str: The decoded data from the QR code.
|
43 |
-
"""
|
44 |
# Convert PIL image to a NumPy array
|
45 |
-
|
46 |
|
47 |
-
# Convert RGB to BGR
|
48 |
-
if
|
49 |
-
|
50 |
|
51 |
# Initialize OpenCV QR code detector
|
52 |
detector = cv2.QRCodeDetector()
|
53 |
-
data, _, _ = detector.detectAndDecode(
|
54 |
-
|
55 |
return data if data else "No QR code found."
|
56 |
|
57 |
-
def download_qr_code(qr_image):
|
58 |
-
"""
|
59 |
-
Download the generated QR code image.
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
pass
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
pyperclip.copy(decoded_text)
|
75 |
|
76 |
-
def
|
77 |
-
|
78 |
-
|
79 |
-
"""
|
80 |
-
# QR Code Generator Interface
|
81 |
-
generate_interface = gr.Interface(
|
82 |
-
fn=generate_qr_code,
|
83 |
-
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
84 |
-
outputs=gr.Image(label="Generated QR Code"),
|
85 |
-
title="Generate QR Code",
|
86 |
-
description="Quickly create a QR code from any text or URL.",
|
87 |
-
)
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
title="Read QR Code",
|
95 |
-
description="Upload an image with a QR code to decode the embedded data.",
|
96 |
-
)
|
97 |
|
98 |
-
# Combine interfaces into a single tabbed layout
|
99 |
-
with gr.Blocks() as demo:
|
100 |
-
gr.HTML(custom_css) # Embed the custom CSS
|
101 |
-
with gr.Tab("Generate QR Code"):
|
102 |
-
generate_interface.render()
|
103 |
-
qr_code_output = generate_interface.outputs[0]
|
104 |
-
download_button = gr.Button("Download QR Code")
|
105 |
-
download_button.click(fn=download_qr_code, inputs=[qr_code_output], outputs=None)
|
106 |
with gr.Tab("Read QR Code"):
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
demo.launch(share=True)
|
|
|
|
|
|
|
|
|
|
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 |
+
import shutil # For cleaning up temporary files
|
|
|
|
|
8 |
|
|
|
|
|
9 |
|
10 |
+
# Function to generate a QR code
|
11 |
+
def generate_qr(data):
|
|
|
12 |
qr = qrcode.QRCode(
|
13 |
version=1,
|
14 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
|
19 |
qr.make(fit=True)
|
20 |
img = qr.make_image(fill="black", back_color="white")
|
21 |
|
22 |
+
# Save to a temporary file
|
23 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
24 |
+
img.save(temp_file.name, format="PNG")
|
25 |
+
temp_file.close() # Ensure file is written to disk
|
26 |
+
return temp_file.name, img # Return the file path and the PIL image
|
27 |
|
|
|
|
|
|
|
28 |
|
29 |
+
# Function to read a QR code
|
30 |
+
def read_qr(img):
|
|
|
|
|
|
|
|
|
31 |
# Convert PIL image to a NumPy array
|
32 |
+
img = np.array(img)
|
33 |
|
34 |
+
# Convert RGB to BGR as OpenCV expects
|
35 |
+
if img.ndim == 3:
|
36 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
37 |
|
38 |
# Initialize OpenCV QR code detector
|
39 |
detector = cv2.QRCodeDetector()
|
40 |
+
data, _, _ = detector.detectAndDecode(img)
|
|
|
41 |
return data if data else "No QR code found."
|
42 |
|
|
|
|
|
|
|
43 |
|
44 |
+
# Gradio Interface
|
45 |
+
def create_gradio_interface():
|
46 |
+
with gr.Blocks() as demo:
|
47 |
+
gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
|
|
|
48 |
|
49 |
+
with gr.Tab("Generate QR Code"):
|
50 |
+
with gr.Row():
|
51 |
+
data_input = gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode")
|
52 |
+
generate_button = gr.Button("Generate QR Code")
|
53 |
|
54 |
+
with gr.Row():
|
55 |
+
qr_image = gr.Image(label="Generated QR Code")
|
56 |
+
qr_file = gr.File(label="Download QR Code")
|
|
|
57 |
|
58 |
+
def generate_qr_interface(data):
|
59 |
+
qr_file_path, qr_image_pil = generate_qr(data)
|
60 |
+
return qr_image_pil, qr_file_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
generate_button.click(
|
63 |
+
generate_qr_interface,
|
64 |
+
inputs=data_input,
|
65 |
+
outputs=[qr_image, qr_file],
|
66 |
+
)
|
|
|
|
|
|
|
67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
with gr.Tab("Read QR Code"):
|
69 |
+
with gr.Row():
|
70 |
+
image_input = gr.Image(type="pil", label="Upload QR Code Image")
|
71 |
+
decode_button = gr.Button("Decode QR Code")
|
72 |
+
|
73 |
+
decoded_data = gr.Textbox(label="Decoded Data")
|
74 |
+
|
75 |
+
def read_qr_interface(img):
|
76 |
+
return read_qr(img)
|
77 |
+
|
78 |
+
decode_button.click(
|
79 |
+
read_qr_interface,
|
80 |
+
inputs=image_input,
|
81 |
+
outputs=decoded_data,
|
82 |
+
)
|
83 |
|
84 |
demo.launch(share=True)
|
85 |
+
|
86 |
+
|
87 |
+
# Run the Gradio interface
|
88 |
+
create_gradio_interface()
|