import cv2 import pylibdmtx.pylibdmtx as pylibdmtx import gradio as gr def read_barcodes(image): # Load the image img = cv2.imdecode(image, cv2.IMREAD_GRAYSCALE) # Decode DataMatrix barcodes barcodes = pylibdmtx.decode(img) # Process each barcode found results = [] for barcode in barcodes: # Extract the data (text) from the barcode barcode_data = barcode.data.decode("utf-8") barcode_type = "DataMatrix" # Store the barcode type and data results.append((barcode_type, barcode_data)) return results def process_image(image): # Convert the image data to a numpy array img = image.reshape((image.shape[0], image.shape[1])) # Call the function to read barcodes from the image results = read_barcodes(img) if len(results) == 0: return "No barcode found." else: output = "" for barcode_type, barcode_data in results: output += f"Type: {barcode_type}, Data: {barcode_data}\n" return output.strip() # Define the input and output interfaces using Gradio inputs = gr.inputs.Image(label="Input Image") output = gr.outputs.Textbox(label="Extracted Text") # Create the Gradio interface gr.Interface(fn=process_image, inputs=inputs, outputs=output, title="Barcode Reader", description="Upload an image to extract text from DataMatrix barcodes.").launch()