Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,8 @@ 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 |
"""
|
|
@@ -17,28 +17,28 @@ def scan_qr_code(image):
|
|
| 17 |
str: The decoded QR code data with a descriptive message.
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
if isinstance(image, np.ndarray):
|
| 22 |
-
|
| 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 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
try:
|
| 35 |
image = Image.open(image)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
| 42 |
|
| 43 |
# Decode QR code
|
| 44 |
decoded_objects = decode(open_cv_image)
|
|
@@ -54,12 +54,14 @@ def scan_qr_code(image):
|
|
| 54 |
# Define the Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=scan_qr_code,
|
| 57 |
-
inputs=gr.Image(
|
| 58 |
-
outputs=gr.Textbox(),
|
| 59 |
title="QR Code Scanner",
|
| 60 |
-
description="Upload an image containing a QR code to decode it."
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
# Launch the Gradio app
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
iface.launch()
|
|
|
|
| 3 |
from pyzbar.pyzbar import decode
|
| 4 |
from PIL import Image
|
| 5 |
import numpy as np
|
|
|
|
| 6 |
import io
|
| 7 |
+
import base64
|
| 8 |
|
| 9 |
def scan_qr_code(image):
|
| 10 |
"""
|
|
|
|
| 17 |
str: The decoded QR code data with a descriptive message.
|
| 18 |
"""
|
| 19 |
try:
|
| 20 |
+
if image is None:
|
| 21 |
+
return "No image provided"
|
| 22 |
+
|
| 23 |
+
# Convert to PIL Image if needed
|
| 24 |
if isinstance(image, np.ndarray):
|
| 25 |
+
image = Image.fromarray(image)
|
|
|
|
|
|
|
|
|
|
| 26 |
elif isinstance(image, str):
|
| 27 |
+
try:
|
| 28 |
+
if image.startswith('data:image'):
|
| 29 |
+
# Handle base64 encoded images
|
| 30 |
+
image_data = base64.b64decode(image.split(',')[1])
|
| 31 |
+
image = Image.open(io.BytesIO(image_data))
|
| 32 |
+
else:
|
| 33 |
+
# Handle file paths
|
|
|
|
| 34 |
image = Image.open(image)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"Error opening image: {str(e)}"
|
| 37 |
+
|
| 38 |
+
# Convert to OpenCV format
|
| 39 |
+
open_cv_image = np.array(image)
|
| 40 |
+
if len(open_cv_image.shape) == 3: # Color image
|
| 41 |
+
open_cv_image = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR)
|
| 42 |
|
| 43 |
# Decode QR code
|
| 44 |
decoded_objects = decode(open_cv_image)
|
|
|
|
| 54 |
# Define the Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=scan_qr_code,
|
| 57 |
+
inputs=gr.Image(label="Upload QR Code Image"), # Removed type parameter
|
| 58 |
+
outputs=gr.Textbox(label="Result"),
|
| 59 |
title="QR Code Scanner",
|
| 60 |
+
description="Upload an image containing a QR code to decode it.",
|
| 61 |
+
examples=[], # You can add example images here
|
| 62 |
+
cache_examples=False
|
| 63 |
)
|
| 64 |
|
| 65 |
# Launch the Gradio app
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
iface.launch(share=True) # Added share=True for public access
|