Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,13 +1,21 @@
|
|
1 |
-
import
|
|
|
2 |
import cv2
|
|
|
3 |
from PIL import Image
|
4 |
import gradio as gr
|
5 |
-
import
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
|
|
11 |
qr = qrcode.QRCode(
|
12 |
version=1,
|
13 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
@@ -19,59 +27,82 @@ def generate_qr(data):
|
|
19 |
img = qr.make_image(fill="black", back_color="white")
|
20 |
|
21 |
# Save to a temporary file and return the file path
|
22 |
-
|
23 |
-
|
24 |
-
temp_file.close() # Close the file to flush contents to disk
|
25 |
return temp_file.name
|
26 |
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
# Convert PIL image to a NumPy array
|
30 |
-
|
31 |
|
32 |
# Convert RGB to BGR format as OpenCV expects BGR
|
33 |
-
if
|
34 |
-
|
35 |
|
36 |
# Initialize OpenCV QR code detector
|
37 |
detector = cv2.QRCodeDetector()
|
38 |
-
data, _, _ = detector.detectAndDecode(
|
39 |
-
|
40 |
return data if data else "No QR code found."
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
-
# Gradio interface for generating and reading QR codes
|
56 |
def create_gradio_interface():
|
|
|
|
|
|
|
57 |
# QR Code Generator Interface
|
58 |
generate_interface = gr.Interface(
|
59 |
-
fn=
|
60 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
61 |
outputs=gr.Image(label="Generated QR Code"),
|
62 |
title="Generate QR Code",
|
63 |
description="Quickly create a QR code from any text or URL.",
|
64 |
)
|
65 |
|
|
|
|
|
|
|
|
|
66 |
# QR Code Reader Interface
|
67 |
read_interface = gr.Interface(
|
68 |
-
fn=
|
69 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
70 |
outputs=gr.Textbox(label="Decoded Data"),
|
71 |
title="Read QR Code",
|
72 |
description="Upload an image with a QR code to decode the embedded data.",
|
73 |
)
|
74 |
|
|
|
|
|
|
|
|
|
75 |
# Combine interfaces into a single tabbed layout
|
76 |
interface = gr.TabbedInterface(
|
77 |
[generate_interface, read_interface],
|
@@ -85,5 +116,16 @@ def create_gradio_interface():
|
|
85 |
|
86 |
demo.launch(share=True)
|
87 |
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
import cv2
|
4 |
+
import numpy as np
|
5 |
from PIL import Image
|
6 |
import gradio as gr
|
7 |
+
import pyperclip
|
8 |
+
|
9 |
+
def generate_qr_code(data):
|
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 |
+
Returns:
|
17 |
+
str: The path to the generated QR code image.
|
18 |
+
"""
|
19 |
qr = qrcode.QRCode(
|
20 |
version=1,
|
21 |
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
|
|
27 |
img = qr.make_image(fill="black", back_color="white")
|
28 |
|
29 |
# Save to a temporary file and return the file path
|
30 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
31 |
+
img.save(temp_file.name, format="PNG")
|
|
|
32 |
return temp_file.name
|
33 |
|
34 |
+
def read_qr_code(img):
|
35 |
+
"""
|
36 |
+
Read a QR code from the given image.
|
37 |
+
|
38 |
+
Args:
|
39 |
+
img (PIL.Image): The image containing the QR code.
|
40 |
+
|
41 |
+
Returns:
|
42 |
+
str: The decoded data from the QR code.
|
43 |
+
"""
|
44 |
# Convert PIL image to a NumPy array
|
45 |
+
img_array = np.array(img)
|
46 |
|
47 |
# Convert RGB to BGR format as OpenCV expects BGR
|
48 |
+
if img_array.ndim == 3:
|
49 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
50 |
|
51 |
# Initialize OpenCV QR code detector
|
52 |
detector = cv2.QRCodeDetector()
|
53 |
+
data, _, _ = detector.detectAndDecode(img_array)
|
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 |
+
Args:
|
62 |
+
qr_image (str): The path to the generated QR code image.
|
63 |
+
"""
|
64 |
+
# Implement download logic here
|
65 |
+
pass
|
66 |
+
|
67 |
+
def copy_to_clipboard(decoded_text):
|
68 |
+
"""
|
69 |
+
Copy the decoded text to the clipboard.
|
70 |
+
|
71 |
+
Args:
|
72 |
+
decoded_text (str): The decoded text to copy.
|
73 |
+
"""
|
74 |
+
pyperclip.copy(decoded_text)
|
75 |
|
|
|
76 |
def create_gradio_interface():
|
77 |
+
"""
|
78 |
+
Create the Gradio interface for generating and reading QR codes.
|
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 |
+
# Add download button to generated QR code
|
90 |
+
download_button = gr.Button("Download QR Code")
|
91 |
+
download_button.click(fn=lambda x: download_qr_code(x), inputs="Generated QR Code", outputs=None)
|
92 |
+
|
93 |
# QR Code Reader Interface
|
94 |
read_interface = gr.Interface(
|
95 |
+
fn=read_qr_code,
|
96 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
97 |
outputs=gr.Textbox(label="Decoded Data"),
|
98 |
title="Read QR Code",
|
99 |
description="Upload an image with a QR code to decode the embedded data.",
|
100 |
)
|
101 |
|
102 |
+
# Add copy to clipboard button to decoded text
|
103 |
+
copy_button = gr.Button("Copy to Clipboard")
|
104 |
+
copy_button.click(fn=lambda x: copy_to_clipboard(x), inputs="Decoded Data", outputs=None)
|
105 |
+
|
106 |
# Combine interfaces into a single tabbed layout
|
107 |
interface = gr.TabbedInterface(
|
108 |
[generate_interface, read_interface],
|
|
|
116 |
|
117 |
demo.launch(share=True)
|
118 |
|
119 |
+
custom_css = """
|
120 |
+
<style>
|
121 |
+
body {background-color: #1e1e2f; font-family: Arial, sans-serif; color: #e0e0e0;}
|
122 |
+
.gradio-container {max-width: 600px; margin: auto; padding: 20px; background-color: #2c2c3e; border-radius: 10px; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);}
|
123 |
+
h1, h2 {text-align: center; color: #f0f0f0; font-weight: 600;}
|
124 |
+
.gr-button {background-color: #3a3a4f; color: #e0e0e0; padding: 10px 20px; border-radius: 5px; font-size: 15px; border: none;}
|
125 |
+
.gr-button:hover {background-color: #505068;}
|
126 |
+
input, textarea,.gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 5px; padding: 10px; font-size: 14px; color: #e0e0e0;}
|
127 |
+
.gr-box:hover, input:hover, textarea:hover {border-color: #777;}
|
128 |
+
</style>
|
129 |
+
"""
|
130 |
+
|
131 |
+
create_gradio_interface()
|