Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,12 @@ from PIL import Image
|
|
3 |
import gradio as gr
|
4 |
import io
|
5 |
import base64
|
|
|
|
|
|
|
6 |
|
7 |
|
8 |
-
# Function to generate a QR code and return
|
9 |
def generate_qr(data):
|
10 |
qr = qrcode.QRCode(
|
11 |
version=1,
|
@@ -21,34 +24,80 @@ def generate_qr(data):
|
|
21 |
buffered = io.BytesIO()
|
22 |
img.save(buffered, format="PNG")
|
23 |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
|
27 |
# Gradio Interface
|
28 |
def create_gradio_interface():
|
29 |
with gr.Blocks() as demo:
|
30 |
-
gr.Markdown("## QR Code Generator and
|
31 |
-
|
32 |
-
#
|
33 |
with gr.Tab("Generate QR Code"):
|
34 |
with gr.Row():
|
35 |
data_input = gr.Textbox(placeholder="Enter text or URL to encode", label="Input Data")
|
36 |
generate_button = gr.Button("Generate QR Code")
|
37 |
|
38 |
qr_code_html = gr.HTML(label="Generated QR Code (Base64 Embedded)")
|
|
|
|
|
39 |
|
40 |
def generate_qr_interface(data):
|
41 |
if not data.strip():
|
42 |
raise ValueError("Input text cannot be empty!")
|
43 |
-
img_base64 = generate_qr(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
# Wrap the base64 string in an <img> tag for display
|
45 |
html_content = f'<img src="{img_base64}" alt="QR Code" style="max-width:300px;">'
|
46 |
-
return html_content
|
47 |
|
48 |
generate_button.click(
|
49 |
generate_qr_interface,
|
50 |
inputs=data_input,
|
51 |
-
outputs=qr_code_html,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
)
|
53 |
|
54 |
demo.launch(share=True)
|
|
|
3 |
import gradio as gr
|
4 |
import io
|
5 |
import base64
|
6 |
+
import numpy as np
|
7 |
+
import cv2
|
8 |
+
import tempfile
|
9 |
|
10 |
|
11 |
+
# Function to generate a QR code and return Base64 and PNG file
|
12 |
def generate_qr(data):
|
13 |
qr = qrcode.QRCode(
|
14 |
version=1,
|
|
|
24 |
buffered = io.BytesIO()
|
25 |
img.save(buffered, format="PNG")
|
26 |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
27 |
+
|
28 |
+
# Save the image temporarily as a PNG file
|
29 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
30 |
+
img.save(temp_file.name, format="PNG")
|
31 |
+
temp_file.close()
|
32 |
+
|
33 |
+
return f"data:image/png;base64,{img_base64}", temp_file.name, img_base64
|
34 |
+
|
35 |
+
|
36 |
+
# Function to decode a QR code from an uploaded image
|
37 |
+
def decode_qr(img):
|
38 |
+
if img is None:
|
39 |
+
return "No image uploaded."
|
40 |
+
|
41 |
+
# Convert PIL image to a NumPy array
|
42 |
+
img_array = np.array(img)
|
43 |
+
|
44 |
+
# Convert RGB to BGR as OpenCV expects
|
45 |
+
if img_array.ndim == 3:
|
46 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
47 |
+
|
48 |
+
# Initialize OpenCV QR code detector
|
49 |
+
detector = cv2.QRCodeDetector()
|
50 |
+
data, _, _ = detector.detectAndDecode(img_array)
|
51 |
+
return data if data else "No QR code found."
|
52 |
|
53 |
|
54 |
# Gradio Interface
|
55 |
def create_gradio_interface():
|
56 |
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown("## QR Code Generator and Decoder")
|
58 |
+
|
59 |
+
# Tab for generating QR codes
|
60 |
with gr.Tab("Generate QR Code"):
|
61 |
with gr.Row():
|
62 |
data_input = gr.Textbox(placeholder="Enter text or URL to encode", label="Input Data")
|
63 |
generate_button = gr.Button("Generate QR Code")
|
64 |
|
65 |
qr_code_html = gr.HTML(label="Generated QR Code (Base64 Embedded)")
|
66 |
+
qr_png_file = gr.File(label="Download QR Code (PNG)")
|
67 |
+
qr_base64_file = gr.File(label="Download Base64 (TXT)")
|
68 |
|
69 |
def generate_qr_interface(data):
|
70 |
if not data.strip():
|
71 |
raise ValueError("Input text cannot be empty!")
|
72 |
+
img_base64, png_path, base64_str = generate_qr(data)
|
73 |
+
|
74 |
+
# Save Base64 string as a .txt file
|
75 |
+
base64_txt_path = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
|
76 |
+
with open(base64_txt_path.name, "w") as f:
|
77 |
+
f.write(base64_str)
|
78 |
+
|
79 |
# Wrap the base64 string in an <img> tag for display
|
80 |
html_content = f'<img src="{img_base64}" alt="QR Code" style="max-width:300px;">'
|
81 |
+
return html_content, png_path, base64_txt_path.name
|
82 |
|
83 |
generate_button.click(
|
84 |
generate_qr_interface,
|
85 |
inputs=data_input,
|
86 |
+
outputs=[qr_code_html, qr_png_file, qr_base64_file],
|
87 |
+
)
|
88 |
+
|
89 |
+
# Tab for decoding QR codes
|
90 |
+
with gr.Tab("Decode QR Code"):
|
91 |
+
with gr.Row():
|
92 |
+
image_input = gr.Image(type="pil", label="Upload QR Code Image")
|
93 |
+
decode_button = gr.Button("Decode QR Code")
|
94 |
+
|
95 |
+
decoded_text = gr.Textbox(label="Decoded Text", interactive=True)
|
96 |
+
|
97 |
+
decode_button.click(
|
98 |
+
decode_qr,
|
99 |
+
inputs=image_input,
|
100 |
+
outputs=decoded_text,
|
101 |
)
|
102 |
|
103 |
demo.launch(share=True)
|