In this tutorial, we will take a pre-trained YOLOv5n model, convert it, and run it on the KL720 to detect objects in real-time.
Level: IntermediateTime: 15 mins
1. Download Pre-trained Model
We provide a Kneron-optimized version of YOLOv5n (Nano) that fits within the KL520/720 memory constraints.
2. Pre-processing
YOLO requires images to be resized to 640x640 and normalized (0-1).
# Python Image Prep
img = cv2.imread("dog.jpg")
img = cv2.resize(img, (640, 640))
img = img.astype(np.float32) / 255.03. Convert & Compile
Use the toolchain to compile the ONNX file.
kneron compile yolov5n.onnx --target KL720 --out yolov5n.nef4. Post-processing
The NPU outputs raw anchor boxes. You must apply Non-Maximum Suppression (NMS) on the host CPU.
Note: Hardware NMS is available on KL720 but requires special graph configuration. For this tutorial, we use CPU NMS.
results = monitor.inference(img)
boxes = nms(results, conf_thres=0.4, iou_thres=0.5)
plot_boxes(img, boxes)5. Live Demo
If you have a webcam connected, run the live demo script:
python examples/yolo_webcam.py