CultriX commited on
Commit
550ae51
·
verified ·
1 Parent(s): 9e1cd24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -44
app.py CHANGED
@@ -1,11 +1,9 @@
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
 
8
-
9
  # Function to generate a QR code
10
  def generate_qr(data):
11
  qr = qrcode.QRCode(
@@ -22,80 +20,83 @@ def generate_qr(data):
22
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
23
  img.save(temp_file.name, format="PNG")
24
  temp_file.close() # Ensure file is written to disk
25
- return temp_file.name, img # Return the file path and the image
26
 
 
27
 
28
  # Function to read a QR code
29
- def read_qr(img):
30
  # Convert PIL image to a NumPy array
31
- img = np.array(img)
 
32
 
33
  # Convert RGB to BGR as OpenCV expects
34
- if img.ndim == 3:
35
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
36
 
37
  # Initialize OpenCV QR code detector
38
  detector = cv2.QRCodeDetector()
39
- data, _, _ = detector.detectAndDecode(img)
40
  return data if data else "No QR code found."
41
 
 
 
 
 
 
42
 
43
- # Function for generating QR codes with both image and download link
44
- def generate_qr_interface(data):
45
- qr_file, qr_image = generate_qr(data)
46
- return qr_image, qr_file # Return the image and file for download
47
-
48
-
49
- # Function for reading QR codes with decoded text and clipboard update
50
- def read_qr_interface(img):
51
- decoded_data = read_qr(img)
52
- return decoded_data # Return the decoded text
53
-
54
-
55
- # Clipboard copy callback
56
- def copy_to_clipboard(decoded_text):
57
- # Gradio does not support direct clipboard copy; simulate success message
58
- return f"Copied: {decoded_text}" if decoded_text else "Nothing to copy!"
59
 
 
 
 
 
60
 
61
- # Create Gradio Interface
62
- def create_gradio_interface():
63
  # QR Code Generator Tab
64
  generate_interface = gr.Interface(
65
  fn=generate_qr_interface,
66
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
67
  outputs=[
68
- gr.Image(label="Generated QR Code"), # Display the image
69
- gr.File(label="Download QR Code"), # Provide a download link
70
  ],
71
  title="Generate QR Code",
72
  description="Quickly create a QR code from any text or URL.",
73
  )
74
 
75
  # QR Code Reader Tab
76
- with gr.Blocks() as read_interface:
77
- gr.Markdown("### Read QR Code")
78
- with gr.Row():
79
- qr_input = gr.Image(type="pil", label="Upload QR Code Image")
80
- qr_text = gr.Textbox(label="Decoded Data", interactive=False)
81
- copy_button = gr.Button("Copy to Clipboard")
82
- copy_feedback = gr.Textbox(label="Clipboard Status", interactive=False)
83
-
84
- # Define interactions
85
- qr_input.change(read_qr_interface, inputs=qr_input, outputs=qr_text)
86
- copy_button.click(copy_to_clipboard, inputs=qr_text, outputs=copy_feedback)
87
-
88
- # Main interface with tabs
89
  with gr.Blocks() as demo:
90
- gr.Markdown("# QR Code Tool: Generate and Decode with Ease")
91
  with gr.Tab("Generate QR Code"):
92
  generate_interface.render()
93
  with gr.Tab("Read QR Code"):
 
 
94
  read_interface.render()
95
 
96
- # Launch the interface
 
 
 
 
 
 
97
  demo.launch(share=True)
98
 
99
 
100
- # Run the Gradio app
101
  create_gradio_interface()
 
1
  import qrcode
 
2
  from PIL import Image
3
  import gradio as gr
4
  import tempfile
5
  import numpy as np
6
 
 
7
  # Function to generate a QR code
8
  def generate_qr(data):
9
  qr = qrcode.QRCode(
 
20
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
21
  img.save(temp_file.name, format="PNG")
22
  temp_file.close() # Ensure file is written to disk
 
23
 
24
+ return temp_file.name # Return the file path only, Gradio will handle displaying the image
25
 
26
  # Function to read a QR code
27
+ def read_qr(image_file):
28
  # Convert PIL image to a NumPy array
29
+ img = Image.open(image_file.name)
30
+ img_np = np.array(img)
31
 
32
  # Convert RGB to BGR as OpenCV expects
33
+ if img_np.ndim == 3:
34
+ img_np = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
35
 
36
  # Initialize OpenCV QR code detector
37
  detector = cv2.QRCodeDetector()
38
+ data, _, _ = detector.detectAndDecode(img_np)
39
  return data if data else "No QR code found."
40
 
41
+ # Function to copy text to clipboard (this requires platform-specific implementations)
42
+ def copy_to_clipboard(text):
43
+ import pyperclip
44
+ pyperclip.copy(text)
45
+ return "Copied to Clipboard!"
46
 
47
+ # Gradio Interface
48
+ def create_gradio_interface():
49
+ # QR Code Generator with Display and Downloadable Link
50
+ def generate_qr_interface(data):
51
+ qr_file = generate_qr(data) # This will now return the file path directly
52
+ qr_image = Image.open(qr_file)
53
+ return [qr_image, qr_file] # Return both image and file for Gradio to display/download
 
 
 
 
 
 
 
 
 
54
 
55
+ # QR Code Reader with Copy-to-Clipboard Button
56
+ def read_qr_interface(image_file):
57
+ decoded_data = read_qr(image_file)
58
+ return decoded_data # Return decoded text
59
 
 
 
60
  # QR Code Generator Tab
61
  generate_interface = gr.Interface(
62
  fn=generate_qr_interface,
63
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
64
  outputs=[
65
+ gr.Image(label="Generated QR Code"), # Display the QR code image
66
+ gr.File(label="Download QR Code"), # Downloadable link for the QR code file
67
  ],
68
  title="Generate QR Code",
69
  description="Quickly create a QR code from any text or URL.",
70
  )
71
 
72
  # QR Code Reader Tab
73
+ read_interface = gr.Interface(
74
+ fn=read_qr_interface,
75
+ inputs=gr.File(type="file", label="Upload QR Code Image"),
76
+ outputs=gr.Textbox(label="Decoded Data"),
77
+ title="Read QR Code",
78
+ description="Upload an image with a QR code to decode the embedded data.",
79
+ )
80
+
81
+ # Clipboard Functionality for "Read QR Code" Tab
 
 
 
 
82
  with gr.Blocks() as demo:
83
+ gr.Markdown("## QR Code Tool: Generate and Decode with Ease")
84
  with gr.Tab("Generate QR Code"):
85
  generate_interface.render()
86
  with gr.Tab("Read QR Code"):
87
+ qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
88
+ copy_button = gr.Button("Copy to Clipboard")
89
  read_interface.render()
90
 
91
+ # Connect button click to copy function
92
+ copy_button.click(
93
+ fn=copy_to_clipboard,
94
+ inputs=qr_text,
95
+ outputs=qr_text,
96
+ )
97
+
98
  demo.launch(share=True)
99
 
100
 
101
+ # Run the Gradio interface
102
  create_gradio_interface()