bonrix commited on
Commit
c658e07
·
1 Parent(s): 0bd1aec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -1,17 +1,20 @@
1
- from pyzbar import pyzbar
 
2
  import gradio as gr
3
- import numpy as np
4
 
5
  def read_barcodes(image):
6
- # Find and decode barcodes and QR codes
7
- barcodes = pyzbar.decode(image)
8
 
9
- # Process each barcode/QR code found
 
 
 
10
  results = []
11
  for barcode in barcodes:
12
  # Extract the data (text) from the barcode
13
  barcode_data = barcode.data.decode("utf-8")
14
- barcode_type = barcode.type
15
 
16
  # Store the barcode type and data
17
  results.append((barcode_type, barcode_data))
@@ -20,13 +23,13 @@ def read_barcodes(image):
20
 
21
  def process_image(image):
22
  # Convert the image data to a numpy array
23
- img = np.array(image, dtype=np.uint8)
24
 
25
- # Call the function to read barcodes/QR codes from the image
26
  results = read_barcodes(img)
27
 
28
  if len(results) == 0:
29
- return "No barcode or QR code found."
30
  else:
31
  output = ""
32
  for barcode_type, barcode_data in results:
@@ -38,5 +41,5 @@ inputs = gr.inputs.Image(label="Input Image")
38
  output = gr.outputs.Textbox(label="Extracted Text")
39
 
40
  # Create the Gradio interface
41
- gr.Interface(fn=process_image, inputs=inputs, outputs=output, title="Barcode/QR Code Reader",
42
- description="Upload an image to extract text from barcodes or QR codes.").launch()
 
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))
 
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:
 
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()