bonrix commited on
Commit
5a24b2d
·
1 Parent(s): d8cc2b1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -40
app.py CHANGED
@@ -1,45 +1,37 @@
1
- import cv2
2
- import pylibdmtx.pylibdmtx as pylibdmtx
3
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def read_barcodes(image):
6
- # Load the image
7
- img = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE)
8
 
9
- # Decode DataMatrix barcodes
10
- barcodes = pylibdmtx.decode(img)
11
 
12
- # Process each barcode found
13
  results = []
14
- for barcode in barcodes:
15
- # Extract the data (text) from the barcode
16
- barcode_data = barcode.data.decode("utf-8")
17
- barcode_type = "DataMatrix"
18
-
19
- # Store the barcode type and data
20
- results.append((barcode_type, barcode_data))
21
-
22
- return results
23
-
24
- def process_image(image):
25
- # Convert the image data to a numpy array
26
- img = image.reshape((image.shape[0], image.shape[1]))
27
-
28
- # Call the function to read barcodes from the image
29
- results = read_barcodes(img)
30
-
31
- if len(results) == 0:
32
- return "No barcode found."
33
- else:
34
- output = ""
35
- for barcode_type, barcode_data in results:
36
- output += f"Type: {barcode_type}, Data: {barcode_data}\n"
37
- return output.strip()
38
-
39
- # Define the input and output interfaces using Gradio
40
- inputs = gr.inputs.Image(label="Input Image")
41
- output = gr.outputs.Textbox(label="Extracted Text")
42
-
43
- # Create the Gradio interface
44
- gr.Interface(fn=process_image, inputs=inputs, outputs=output, title="Barcode Reader",
45
- description="Upload an image to extract text from DataMatrix barcodes.").launch()
 
 
 
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
+
7
+ def qr_barcode_scanner(image):
8
+ # Convert PIL image to numpy array
9
+ image_data = np.array(image)
10
+
11
+ # Convert RGB to grayscale if needed
12
+ if len(image_data.shape) == 3 and image_data.shape[2] == 3:
13
+ image_data = cv2.cvtColor(image_data, cv2.COLOR_RGB2GRAY)
14
 
15
+ # Decode QR codes and barcodes
16
+ decoded_objects = decode(image_data)
 
17
 
18
+ if len(decoded_objects) == 0:
19
+ return "No QR code or barcode found."
20
 
 
21
  results = []
22
+ for obj in decoded_objects:
23
+ data = obj.data.decode("utf-8")
24
+ results.append(f"Type: {obj.type}, Data: {data}")
25
+
26
+ return "\n".join(results)
27
+
28
+ iface = gr.Interface(
29
+ fn=qr_barcode_scanner,
30
+ inputs=gr.inputs.Image(type="pil", label="Select an image containing QR code or barcode."),
31
+ outputs=gr.outputs.Textbox(),
32
+ live=True,
33
+ capture_session=True,
34
+ interpretation="default"
35
+ )
36
+
37
+ iface.launch(share=True)