CultriX commited on
Commit
86ef4a9
·
verified ·
1 Parent(s): 550ae51

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -30
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import qrcode
 
2
  from PIL import Image
3
  import gradio as gr
4
  import tempfile
@@ -20,51 +21,41 @@ def generate_qr(data):
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
  )
@@ -72,7 +63,7 @@ def create_gradio_interface():
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.",
@@ -86,14 +77,8 @@ def create_gradio_interface():
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
 
 
1
  import qrcode
2
+ import cv2
3
  from PIL import Image
4
  import gradio as gr
5
  import tempfile
 
21
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
22
  img.save(temp_file.name, format="PNG")
23
  temp_file.close() # Ensure file is written to disk
24
+ return temp_file.name, img # Return the file path and the PIL image
25
 
 
26
 
27
  # Function to read a QR code
28
+ def read_qr(img):
29
  # Convert PIL image to a NumPy array
30
+ img = np.array(img)
 
31
 
32
  # Convert RGB to BGR as OpenCV expects
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
  return data if data else "No QR code found."
40
 
 
 
 
 
 
41
 
42
  # Gradio Interface
43
  def create_gradio_interface():
44
  # QR Code Generator with Display and Downloadable Link
45
  def generate_qr_interface(data):
46
+ qr_file, qr_image = generate_qr(data)
47
+ return qr_image, qr_file # Show image and provide download link
 
48
 
49
  # QR Code Reader with Copy-to-Clipboard Button
50
+ def read_qr_interface(img):
51
+ decoded_data = read_qr(img)
52
  return decoded_data # Return decoded text
53
 
54
  # QR Code Generator Tab
55
  generate_interface = gr.Interface(
56
  fn=generate_qr_interface,
57
  inputs=gr.Textbox(placeholder="Enter text or URL here...", label="Data to Encode"),
58
+ outputs=[gr.Image(label="Generated QR Code"), gr.File(label="Download QR Code")],
 
 
 
59
  title="Generate QR Code",
60
  description="Quickly create a QR code from any text or URL.",
61
  )
 
63
  # QR Code Reader Tab
64
  read_interface = gr.Interface(
65
  fn=read_qr_interface,
66
+ inputs=gr.Image(type="pil", label="Upload QR Code Image"),
67
  outputs=gr.Textbox(label="Decoded Data"),
68
  title="Read QR Code",
69
  description="Upload an image with a QR code to decode the embedded data.",
 
77
  with gr.Tab("Read QR Code"):
78
  qr_text = gr.Textbox(label="Decoded Data") # Single box for decoded data
79
  copy_button = gr.Button("Copy to Clipboard")
80
+ read_interface.render() # Corrected placement
81
+ copy_button.click(None, qr_text, None, _js="() => {navigator.clipboard.writeText(document.querySelector('input[type=text]').value); return 'Copied to Clipboard!'}")
 
 
 
 
 
 
82
 
83
  demo.launch(share=True)
84