Update app.py
Browse files
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
|
8 |
-
"""Reads QR Code
|
9 |
try:
|
10 |
-
# Convert
|
11 |
-
image = np.array(image.convert(
|
12 |
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
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=
|
32 |
-
inputs=gr.Image(type="pil", label="Upload QR
|
33 |
outputs=gr.Textbox(label="Scan Result"),
|
34 |
-
description="Upload an image containing a QR Code
|
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 |
|