Ivan000 commited on
Commit
48c8169
·
verified ·
1 Parent(s): 75a253f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -24
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
- # 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)
@@ -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(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."
 
 
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