hdcoe
Published

Helmet Detection using YOLOv3-Tiny on Kria KV260

Real-time camera helmet detection using YOLOv3-tiny on Kria KV260.

IntermediateFull instructions provided3,535
Helmet Detection using YOLOv3-Tiny on Kria KV260

Things used in this project

Hardware components

Kria KV260 Vision AI Starter Kit
AMD Kria KV260 Vision AI Starter Kit
×1
Webcam
×1

Software apps and online services

Vitis AI

Story

Read more

Code

yolov3tinyHelmet.prototxt

Textile
parameter configuration for DPU
model {
  name: "yolov3tinyHelmet"
  kernel {
     name: "yolov3tinyHelmet"
     mean: 0
     mean: 0
     mean: 0
     scale: 0.25
     scale: 0.25
     scale: 0.25
  }
  model_type : YOLOv3
  yolo_v3_param {
    num_classes: 2
    anchorCnt: 3
    conf_threshold: 0.3
    nms_threshold: 0.45
    layer_name:"yolov3-tinyconvolutional13/BiasAdd/aquant"
    layer_name:"yolov3-tinyconvolutional10/BiasAdd/aquant"
    biases: 10
    biases: 14
    biases: 23
    biases: 27
    biases: 37
    biases: 58
    biases: 81
    biases: 82
    biases: 135
    biases: 169
    biases: 344
    biases: 319
    test_mAP: false
  }
  is_tf : true
}

calibration.py

Python
Create the calibration function for model quantization command
import os
import cv2
import glob
import numpy as np


dataset_path = "Dataset director"
calib_batch_size = 10
inputsize = {'h': 416, 'c': 3, 'w': 416}

def convertimage(img, w, h, c):
    new_img = np.zeros((w, h, c))
    for idx in range(c):
        resize_img = img[:, :, idx]
        resize_img = cv2.resize(resize_img, (w, h), cv2.INTER_AREA)
        new_img[:, :, idx] = resize_img
    return new_img


# This function reads all images in dataset and return all images with the name of inputnode
def calib_input(iter):
    images = []
    #line = open(calibfile).readlines()
    line = glob.glob(dataset_path + "/*.png") #jpg
    for index in range(0, calib_batch_size):
        curline = line[iter * calib_batch_size + index]
        calib_image_name = curline.strip()
        image = cv2.imread(calib_image_name)
        image = convertimage(image, inputsize["w"], inputsize["h"], inputsize["c"])
        image = image / 255.0
        images.append(image)
    return {"yolov3-tinynet1": images}  # first layer

aiinference.json

JSON
Setting dpu core
{
  "xclbin-location":"/lib/firmware/xilinx/kv260-smartcam/kv260-smartcam.xclbin",
  "ivas-library-repo": "/usr/lib/",
  "element-mode":"inplace",
  "kernels" :[
    {
      "library-name":"libivas_xdpuinfer.so",
      "config": {
        "model-name" : "yolov3tinyHelmet",
        "model-class" : "YOLOV3",
        "model-path" : "/home/petalinux",
        "run_time_model" : false,
        "need_preprocess" : false,
        "performance_test" : false,
        "debug_level" : 0
      }
    }
  ]
}

preprocessor.json

JSON
Color correction for DPU
{
  "xclbin-location":"/lib/firmware/xilinx/kv260-smartcam/kv260-smartcam.xclbin",
  "ivas-library-repo": "/opt/xilinx/lib",
  "kernels": [
    {
      "kernel-name": "pp_pipeline_accel:pp_pipeline_accel_1",
      "library-name": "libivas_xpp.so",
      "config": {
        "debug_level" : 1,
        "mean_r": 0,
        "mean_g": 0,
        "mean_b": 0,
        "scale_r": 0.25,
        "scale_g": 0.25,
        "scale_b": 0.25
      }
    }
  ]
}

label.json

JSON
Detection classes
{
  "model-name": "yolov3tinyHelmet",
  "num-labels": 2,
  "labels" :[
    {
      "name": "helmet",
      "label": 0,
      "display_name" : "helmet"
    },
    {
      "name": "head",
      "label": 1,
      "display_name" : "head"
    }
  ]
}

drawresult.json

JSON
display boxes and font
{
  "xclbin-location":"/usr/lib/dpu.xclbin",
  "ivas-library-repo": "/opt/xilinx/lib",
  "element-mode":"inplace",
  "kernels" :[
    {
      "library-name":"libivas_airender.so",
      "config": {
        "fps_interval" : 10,
        "font_size" : 2,
        "font" : 1,
        "thickness" : 2,
        "debug_level" : 0,
        "label_color" : { "blue" : 0, "green" : 0, "red" : 0 },
        "label_filter" : [ "class", "probability" ],
        "classes" : [
                {
                "name" : "helmet",
                "blue" : 255,
                "green" : 0,
                "red" : 0
                },
                {
                "name" : "head",
                "blue" : 0,
                "green" : 255,
                "red" : 0
                }]
      }
    }
  ]
}

Credits

hdcoe

hdcoe

3 projects • 8 followers

Comments