Ivan000 commited on
Commit
675cb03
·
verified ·
1 Parent(s): b67a798

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -1,40 +1,43 @@
1
  # app.py
2
  # =============
3
- # This is a complete app.py file for a QR code scanner using Gradio.
4
 
5
  import gradio as gr
 
6
  from pyzbar.pyzbar import decode
7
  from PIL import Image
8
- import io
9
 
10
- def decode_qr_code(image):
11
  """
12
- Decode the QR code from the given image.
13
 
14
  Args:
15
- image (PIL.Image.Image): The input image containing the QR code.
16
 
17
  Returns:
18
- str: The decoded text from the QR code.
19
  """
20
- # Decode the QR code
21
- decoded_objects = decode(image)
 
22
 
 
 
23
  if decoded_objects:
24
- # Extract the data from the first decoded object
25
- return decoded_objects[0].data.decode('utf-8')
26
- else:
27
- return "No QR code found in the image."
28
 
29
- # Create the Gradio interface
30
  iface = gr.Interface(
31
- fn=decode_qr_code,
32
- inputs=gr.Image(type="pil"),
33
- outputs="text",
34
  title="QR Code Scanner",
35
- description="Upload an image containing a QR code to decode its content."
36
  )
37
 
38
- # Launch the interface
39
  if __name__ == "__main__":
40
- iface.launch()
 
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 (PIL.Image.Image): The uploaded image.
17
 
18
  Returns:
19
+ str: The decoded QR code data.
20
  """
21
+ # Convert PIL image to OpenCV format
22
+ open_cv_image = np.array(image)
23
+ open_cv_image = open_cv_image[:, :, ::-1].copy()
24
 
25
+ # Decode QR code
26
+ decoded_objects = decode(open_cv_image)
27
  if decoded_objects:
28
+ for obj in decoded_objects:
29
+ return obj.data.decode('utf-8')
30
+ return "No QR code found"
 
31
 
32
+ # Define the Gradio interface
33
  iface = gr.Interface(
34
+ fn=scan_qr_code,
35
+ inputs=gr.inputs.Image(type="pil"),
36
+ outputs=gr.outputs.Textbox(),
37
  title="QR Code Scanner",
38
+ description="Upload an image containing a QR code to decode it."
39
  )
40
 
41
+ # Launch the Gradio app
42
  if __name__ == "__main__":
43
+ iface.launch()