Python Windows Code Example

You can use the provided Python example code which includes image reading, model loading, model prediction, and output.

You will need a valid DaoAI license to run the code. If you do not have a license, please refer to Software License.

Then, run the following command to execute the Python script:

python example.py

You can change the file paths in example.py to use different images and deep learning models.

model_path = "./model.dwm"
image_path = "./image.png"

Import Libraries

You can also start with a new Python file. First, import the dlsdk library, which is our DaoAI World Python Windows SDK.

import os
import sys
import dlsdk.dlsdk as dlsdk

The following libraries may also be helpful:

import cv2
import numpy as np
import matplotlib.pyplot as plt

Loading a Deep Learning Model

# Initialize the model
dlsdk.initialize()
model_path = "./model.dwm"
model = dlsdk.KeypointDetection(model_path, device=dlsdk.DeviceType.GPU)

Note that each detection task corresponds to a specific object:

# Instance Segmentation
model = dlsdk.InstanceSegmentation(model_path, device=dlsdk.DeviceType.GPU)

# Keypoint Detection
model = dlsdk.KeypointDetection(model_path, device=dlsdk.DeviceType.GPU)

# Image Classification
model = dlsdk.Classification(model_path, device=dlsdk.DeviceType.GPU)

# Object Detection
model = dlsdk.ObjectDetection(model_path, device=dlsdk.DeviceType.GPU)

# Unsupervised Defect Segmentation
model = dlsdk.UnsupervisedDefectSegmentation(model_path, device=dlsdk.DeviceType.GPU)

# Supervised Defect Segmentation
model = dlsdk.SupervisedDefectSegmentation(model_path, device=dlsdk.DeviceType.GPU)

# OCR
model = dlsdk.OCR(model_path, device=dlsdk.DeviceType.GPU)

# Positioning Model (only supported in the industrial version)
model = dlsdk.Positioning(model_path, device=dlsdk.DeviceType.GPU)

# Presence Checking (only supported in the industrial version)
model = dlsdk.PresenceChecking(model_path, device=dlsdk.DeviceType.GPU)

Reading Images

To read images, you can use OpenCV. If you do not have it installed, you can run pip install python-opencv to install it.

image_path = "./kp1.png"  # Path to the image being read
img = cv2.imread(image_path)

daoai_image = dlsdk.Image.from_numpy(img, dlsdk.Image.Type.BGR)  # Create DaoAI Image

Running Deep Learning Model Predictions

Make model predictions and output results as a JSON file.

assert isinstance(daoai_image, dlsdk.Image)
prediction = model.inference(daoai_image, {dlsdk.PostProcessType.CONFIDENCE_THRESHOLD: 0.95})

with open("output.json", "w") as f:
    f.write(prediction.toJSONString())  # Output standard JSON result

with open("outputAnnotation.json", "w") as f:
    f.write(prediction.toAnnotationJSONString())  # Output annotated JSON result

Post-Processing

Model predictions can accept post-processing parameters:

  • dlsdk.PostProcessType.CONFIDENCE_THRESHOLD

    Confidence threshold, which will filter out results with a confidence below the set value.

  • dlsdk.PostProcessType.IOU_THRESHOLD

    IOU threshold, which will filter out results with IOU below the set value.

  • dlsdk.PostProcessType.SENSITIVITY_THRESHOLD

    Used in unsupervised defect segmentation (anomaly detection), it controls the model's sensitivity to defects. The higher the value, the more defects the model will detect, but it may also increase false positives.

prediction = model.inference(daoai_image, {dlsdk.PostProcessType.CONFIDENCE_THRESHOLD: 0.95, dlsdk.PostProcessType.IOU_THRESHOLD: 0.5})

You can also retrieve result information using other methods:

print(prediction.boxes)
print(prediction.class_ids)
print(prediction.class_labels)

Retrieving Prediction Results

The Box represents the bounding box of the predicted results from the model. You can retrieve it as follows:

prediction = model.inference(daoai_image)

prediction.boxes[0].x1()  # Top-left corner X-coordinate
prediction.boxes[0].y1()  # Top-left corner Y-coordinate
prediction.boxes[0].x2()  # Bottom-right corner X-coordinate
prediction.boxes[0].y2()  # Bottom-right corner Y-coordinate

Mask provides the contour information of the target object in the prediction. You can extract the vertices of the polygonal region as follows:

prediction = model.inference(daoai_image)

prediction.masks[0].toPolygons()[0].points[0].x  # X-coordinate of the vertex
prediction.masks[0].toPolygons()[0].points[0].y  # Y-coordinate of the vertex
  • `masks[0]`: Extracts the mask of the first target object.

  • `toPolygons()`: Converts the mask into polygonal contours.

  • `points[0]`: Retrieves the first vertex of the polygon.

By connecting all the vertices sequentially, you can form the complete contour of the target object. This is useful for applications requiring detailed boundary information, such as region analysis or fine-grained annotations.

Generate and save a visualization image that overlays the prediction results on the original image:

result = dlsdk.visualize(daoai_image, prediction)

result.save("output.png")