Adityadn commited on
Commit
8b3c2b9
·
verified ·
1 Parent(s): b215fe0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -20
app.py CHANGED
@@ -1,37 +1,33 @@
1
  import cv2
2
  import numpy as np
3
  import gradio as gr
4
- from pyzbar.pyzbar import decode
5
  from PIL import Image
6
 
7
- def read_qr_barcode(image):
8
- """Reads QR Code or Barcode from an uploaded image."""
9
  try:
10
- # Convert Gradio image to OpenCV format
11
- image = np.array(image.convert('RGB'))
12
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
13
-
14
- # Decode using pyzbar
15
- decoded_objects = decode(gray)
16
-
17
- if not decoded_objects:
18
- return "No QR Code or Barcode found."
19
-
20
- results = []
21
- for obj in decoded_objects:
22
- results.append(f"Type: {obj.type} | Data: {obj.data.decode('utf-8')}")
23
-
24
- return "\n".join(results)
25
 
26
  except Exception as e:
27
  return f"Error: {str(e)}"
28
 
29
  # Gradio Interface
30
  interface = gr.Interface(
31
- fn=read_qr_barcode,
32
- inputs=gr.Image(type="pil", label="Upload QR/Barcode Image"),
33
  outputs=gr.Textbox(label="Scan Result"),
34
- description="Upload an image containing a QR Code or Barcode to scan and retrieve its data.",
35
  css="footer {visibility: hidden}"
36
  )
37
 
 
1
  import cv2
2
  import numpy as np
3
  import gradio as gr
 
4
  from PIL import Image
5
 
6
+ def read_qr_code(image):
7
+ """Reads QR Code from an uploaded image using OpenCV."""
8
  try:
9
+ # Convert image to OpenCV format
10
+ image = np.array(image.convert("RGB"))
11
  gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
12
+
13
+ # Initialize OpenCV QR Code detector
14
+ detector = cv2.QRCodeDetector()
15
+ data, _, _ = detector.detectAndDecode(gray)
16
+
17
+ if data:
18
+ return f"QR Code Data: {data}"
19
+ else:
20
+ return "No QR Code found."
 
 
 
21
 
22
  except Exception as e:
23
  return f"Error: {str(e)}"
24
 
25
  # Gradio Interface
26
  interface = gr.Interface(
27
+ fn=read_qr_code,
28
+ inputs=gr.Image(type="pil", label="Upload QR Code Image"),
29
  outputs=gr.Textbox(label="Scan Result"),
30
+ description="Upload an image containing a QR Code to scan and retrieve its data.",
31
  css="footer {visibility: hidden}"
32
  )
33