Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,60 @@
|
|
1 |
-
# app.py
|
2 |
-
# =============
|
3 |
-
# This is a complete app.py file for a QR code scanner using Gradio and Hugging Face Transformers.
|
4 |
-
|
5 |
import gradio as gr
|
6 |
import cv2
|
7 |
from pyzbar.pyzbar import decode
|
8 |
from PIL import Image
|
9 |
import numpy as np
|
|
|
|
|
10 |
|
11 |
def scan_qr_code(image):
|
12 |
"""
|
13 |
Scan QR code from the uploaded image.
|
14 |
|
15 |
Args:
|
16 |
-
image
|
17 |
|
18 |
Returns:
|
19 |
str: The decoded QR code data with a descriptive message.
|
20 |
"""
|
21 |
-
|
22 |
-
|
23 |
-
image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
if decoded_objects:
|
32 |
-
for obj in decoded_objects:
|
33 |
-
decoded_data = obj.data.decode('utf-8')
|
34 |
-
return f"The code contains the following data: {decoded_data}"
|
35 |
-
return "No QR code found"
|
36 |
|
37 |
# Define the Gradio interface
|
38 |
iface = gr.Interface(
|
39 |
fn=scan_qr_code,
|
40 |
-
inputs=gr.Image(type="
|
41 |
outputs=gr.Textbox(),
|
42 |
title="QR Code Scanner",
|
43 |
description="Upload an image containing a QR code to decode it."
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import cv2
|
3 |
from pyzbar.pyzbar import decode
|
4 |
from PIL import Image
|
5 |
import numpy as np
|
6 |
+
import base64
|
7 |
+
import io
|
8 |
|
9 |
def scan_qr_code(image):
|
10 |
"""
|
11 |
Scan QR code from the uploaded image.
|
12 |
|
13 |
Args:
|
14 |
+
image: The uploaded image (can be PIL Image, numpy array, or file path)
|
15 |
|
16 |
Returns:
|
17 |
str: The decoded QR code data with a descriptive message.
|
18 |
"""
|
19 |
+
try:
|
20 |
+
# Handle different input types
|
21 |
+
if isinstance(image, np.ndarray):
|
22 |
+
open_cv_image = image
|
23 |
+
elif isinstance(image, Image.Image):
|
24 |
+
open_cv_image = np.array(image)
|
25 |
+
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
26 |
+
elif isinstance(image, str):
|
27 |
+
# Handle base64 encoded images
|
28 |
+
if image.startswith('data:image'):
|
29 |
+
image = Image.open(io.BytesIO(base64.b64decode(image.split(',')[1])))
|
30 |
+
open_cv_image = np.array(image)
|
31 |
+
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
32 |
+
else:
|
33 |
+
# Handle file paths
|
34 |
+
try:
|
35 |
+
image = Image.open(image)
|
36 |
+
open_cv_image = np.array(image)
|
37 |
+
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
38 |
+
except Exception as e:
|
39 |
+
return f"Error opening image: {str(e)}"
|
40 |
+
else:
|
41 |
+
return "Unsupported image format"
|
42 |
|
43 |
+
# Decode QR code
|
44 |
+
decoded_objects = decode(open_cv_image)
|
45 |
+
if decoded_objects:
|
46 |
+
for obj in decoded_objects:
|
47 |
+
decoded_data = obj.data.decode('utf-8')
|
48 |
+
return f"The code contains the following data: {decoded_data}"
|
49 |
+
return "No QR code found"
|
50 |
|
51 |
+
except Exception as e:
|
52 |
+
return f"Error processing image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Define the Gradio interface
|
55 |
iface = gr.Interface(
|
56 |
fn=scan_qr_code,
|
57 |
+
inputs=gr.Image(type="numpy"), # Changed to numpy type
|
58 |
outputs=gr.Textbox(),
|
59 |
title="QR Code Scanner",
|
60 |
description="Upload an image containing a QR code to decode it."
|