Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,8 @@
|
|
1 |
import qrcode
|
2 |
-
import cv2
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
import io
|
6 |
import base64
|
7 |
-
import numpy as np
|
8 |
|
9 |
|
10 |
# Function to generate a QR code and return base64 string
|
@@ -23,68 +21,34 @@ def generate_qr(data):
|
|
23 |
buffered = io.BytesIO()
|
24 |
img.save(buffered, format="PNG")
|
25 |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
26 |
-
return img_base64 # Return the base64 string
|
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
|
48 |
-
|
49 |
-
#
|
50 |
with gr.Tab("Generate QR Code"):
|
51 |
with gr.Row():
|
52 |
-
data_input = gr.Textbox(placeholder="Enter text or URL
|
53 |
generate_button = gr.Button("Generate QR Code")
|
54 |
|
55 |
-
|
56 |
-
qr_image = gr.Image(label="Generated QR Code")
|
57 |
-
qr_base64 = gr.Textbox(label="Base64 Encoded String")
|
58 |
|
59 |
def generate_qr_interface(data):
|
60 |
if not data.strip():
|
61 |
raise ValueError("Input text cannot be empty!")
|
62 |
img_base64 = generate_qr(data)
|
63 |
-
|
|
|
|
|
64 |
|
65 |
generate_button.click(
|
66 |
generate_qr_interface,
|
67 |
inputs=data_input,
|
68 |
-
outputs=
|
69 |
-
)
|
70 |
-
|
71 |
-
# Tab for reading QR codes
|
72 |
-
with gr.Tab("Read QR Code"):
|
73 |
-
with gr.Row():
|
74 |
-
image_input = gr.Image(type="pil", label="Upload QR Code Image")
|
75 |
-
decode_button = gr.Button("Decode QR Code")
|
76 |
-
|
77 |
-
decoded_data = gr.Textbox(label="Decoded Data", interactive=True)
|
78 |
-
|
79 |
-
def read_qr_interface(img):
|
80 |
-
if img is None:
|
81 |
-
raise ValueError("Please upload a valid QR code image!")
|
82 |
-
return read_qr(img)
|
83 |
-
|
84 |
-
decode_button.click(
|
85 |
-
read_qr_interface,
|
86 |
-
inputs=image_input,
|
87 |
-
outputs=decoded_data,
|
88 |
)
|
89 |
|
90 |
demo.launch(share=True)
|
|
|
1 |
import qrcode
|
|
|
2 |
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 base64 string
|
|
|
21 |
buffered = io.BytesIO()
|
22 |
img.save(buffered, format="PNG")
|
23 |
img_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
24 |
+
return f"data:image/png;base64,{img_base64}" # Return the base64 string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
|
27 |
# Gradio Interface
|
28 |
def create_gradio_interface():
|
29 |
with gr.Blocks() as demo:
|
30 |
+
gr.Markdown("## QR Code Generator and Viewer")
|
31 |
+
|
32 |
+
# Input to generate QR code
|
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)
|