Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,89 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import tempfile
|
| 3 |
import qrcode
|
|
|
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
-
import cv2
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
print(f"Error generating QR code: {e}")
|
| 26 |
-
return None, None
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
img = np.array(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
detector = cv2.QRCodeDetector()
|
| 31 |
data, _, _ = detector.detectAndDecode(img)
|
|
|
|
| 32 |
return data if data else "No QR code found."
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
generate_interface = gr.Interface(
|
| 44 |
-
fn=
|
| 45 |
inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
|
| 46 |
-
outputs=
|
| 47 |
-
gr.Image(label="Generated QR Code"), # Display the QR code image
|
| 48 |
-
gr.File(label="Download QR Code"), # Downloadable link for the QR code file
|
| 49 |
-
],
|
| 50 |
title="Generate QR Code",
|
| 51 |
description="Quickly create a QR code from any text or URL.",
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
read_interface = gr.Interface(
|
| 55 |
-
fn=
|
| 56 |
inputs=gr.Image(type="pil", label="Upload QR Code Image"),
|
| 57 |
outputs=gr.Textbox(label="Decoded Data"),
|
| 58 |
title="Read QR Code",
|
| 59 |
description="Upload an image with a QR code to decode the embedded data.",
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
-
gr.
|
| 67 |
-
|
| 68 |
-
generate_interface
|
| 69 |
-
with gr.Tab("Read QR Code"):
|
| 70 |
-
with gr.Row():
|
| 71 |
-
qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
|
| 72 |
-
copy_button = gr.Button("Copy to Clipboard")
|
| 73 |
-
read_interface
|
| 74 |
-
copy_button.click(
|
| 75 |
-
fn=copy_to_clipboard,
|
| 76 |
-
inputs=qr_text,
|
| 77 |
-
outputs=None,
|
| 78 |
-
)
|
| 79 |
|
| 80 |
demo.launch(share=True)
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import qrcode
|
| 2 |
+
import cv2
|
| 3 |
from PIL import Image
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
+
# Function to generate a QR code
|
| 10 |
+
def generate_qr(data):
|
| 11 |
+
qr = qrcode.QRCode(
|
| 12 |
+
version=1,
|
| 13 |
+
error_correction=qrcode.constants.ERROR_CORRECT_L,
|
| 14 |
+
box_size=10,
|
| 15 |
+
border=4,
|
| 16 |
+
)
|
| 17 |
+
qr.add_data(data)
|
| 18 |
+
qr.make(fit=True)
|
| 19 |
+
img = qr.make_image(fill="black", back_color="white")
|
| 20 |
|
| 21 |
+
# Save to a temporary file and return the file path
|
| 22 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 23 |
+
img.save(temp_file.name, format="PNG")
|
| 24 |
+
temp_file.close() # Close the file to flush contents to disk
|
| 25 |
+
return temp_file.name
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
# Function to read a QR code using OpenCV
|
| 28 |
+
def read_qr(img):
|
| 29 |
+
# Convert PIL image to a NumPy array
|
| 30 |
img = np.array(img)
|
| 31 |
+
|
| 32 |
+
# Convert RGB to BGR format as OpenCV expects BGR
|
| 33 |
+
if img.ndim == 3:
|
| 34 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 35 |
+
|
| 36 |
+
# Initialize OpenCV QR code detector
|
| 37 |
detector = cv2.QRCodeDetector()
|
| 38 |
data, _, _ = detector.detectAndDecode(img)
|
| 39 |
+
|
| 40 |
return data if data else "No QR code found."
|
| 41 |
|
| 42 |
+
# Custom CSS styling as HTML for dark mode
|
| 43 |
+
custom_css = """
|
| 44 |
+
<style>
|
| 45 |
+
body {background-color: #1e1e2f; font-family: Arial, sans-serif; color: #e0e0e0;}
|
| 46 |
+
.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);}
|
| 47 |
+
h1, h2 {text-align: center; color: #f0f0f0; font-weight: 600;}
|
| 48 |
+
.gr-button {background-color: #3a3a4f; color: #e0e0e0; padding: 10px 20px; border-radius: 5px; font-size: 15px; border: none;}
|
| 49 |
+
.gr-button:hover {background-color: #505068;}
|
| 50 |
+
input, textarea, .gr-box {background-color: #3a3a4f; border: 1px solid #555; border-radius: 5px; padding: 10px; font-size: 14px; color: #e0e0e0;}
|
| 51 |
+
.gr-box:hover, input:hover, textarea:hover {border-color: #777;}
|
| 52 |
+
</style>
|
| 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=generate_qr,
|
| 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=read_qr,
|
| 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],
|
| 78 |
+
["Generate QR Code", "Read QR Code"]
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
# Launch interface with custom HTML for CSS styling
|
| 82 |
with gr.Blocks() as demo:
|
| 83 |
+
gr.HTML(custom_css) # Embed the custom CSS
|
| 84 |
+
interface.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
demo.launch(share=True)
|
| 87 |
|
| 88 |
+
# Run the Gradio interface
|
| 89 |
+
create_gradio_interface()
|