Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,20 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
def read_barcodes(image):
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
|
|
|
|
|
|
|
|
|
| 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 =
|
| 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 =
|
| 24 |
|
| 25 |
-
# Call the function to read barcodes
|
| 26 |
results = read_barcodes(img)
|
| 27 |
|
| 28 |
if len(results) == 0:
|
| 29 |
-
return "No barcode
|
| 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
|
| 42 |
-
description="Upload an image to extract text from barcodes
|
|
|
|
| 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()
|