Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Mario Bergerongianluca filippini
Published © Apache-2.0

Supercharge Your ZUBoard with the Hailo-8 AI Accelerator

Supercharge Your ZUBoard: Unleasing Unprecedented Performance with the Hailo-8 AI Accelerator.

IntermediateFull instructions provided4 hours2,701
Supercharge Your ZUBoard with the Hailo-8 AI Accelerator

Things used in this project

Story

Read more

Code

zub1cg_dualcam_hailo8_detection.sh

BatchFile
YoloV5m example script for use with ZUBoard dual camera design.
#!/bin/bash
set -e

CURRENT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

function init_variables() {
    readonly RESOURCES_DIR="${CURRENT_DIR}/resources"
    readonly POSTPROCESS_DIR="/usr/lib/hailo-post-processes"
    readonly DEFAULT_POSTPROCESS_SO="$POSTPROCESS_DIR/libyolo_post.so"
    readonly DEFAULT_NETWORK_NAME="yolov5"
    readonly DEFAULT_VIDEO_SOURCE="/dev/video0"
    readonly DEFAULT_HEF_PATH="${RESOURCES_DIR}/${DEFAULT_NETWORK_NAME}m_yuv.hef"
    readonly DEFAULT_JSON_CONFIG_PATH="$RESOURCES_DIR/configs/yolov5.json" 

    postprocess_so=$DEFAULT_POSTPROCESS_SO
    network_name=$DEFAULT_NETWORK_NAME
    input_source=$DEFAULT_VIDEO_SOURCE
    hef_path=$DEFAULT_HEF_PATH
    json_config_path=$DEFAULT_JSON_CONFIG_PATH 

    print_gst_launch_only=false
    additional_parameters=""
}

init_variables $@

# Script inputs
print_usage() {
    echo "USAGE: zub1cg_dualcam_hailo8_detection.sh [OPTIONS]"
    echo " -m|--mode           mode must be 'dual', 'primary' or 'secondary'"
    echo " -s|--sink           sink must be 'dp', 'window' or 'fake'"
    echo " -f|--format         output format must be 'yuv' or 'rgb'"
    echo " -w|--width          output width"
    echo " -h|--height         output height"
}

while [ "$1" != "" ]; do
    case "$1" in
        -m|--mode)
            case "$2" in
                "dual" | "primary" | "secondary")
                    mode=$2
                ;;
                *)
                    echo "ERROR: Unknown mode specified";
                    print_usage
                    exit 1
                ;;
            esac
            shift
            ;;
        -s|--sink)
            case "$2" in
                "dp" | "fake" | "window")
                    sink=$2
                ;;
                *)
                    echo "ERROR: Unknown sink specified";
                    print_usage
                    exit 1
                ;;
            esac
            shift
            ;;
        -f|--format)
            case "$2" in
                "yuv" | "rgb")
                    format=$2
                ;;
                *)
                    echo "ERROR: Unknown format specified";
                    print_usage
                    exit 1
                ;;
            esac
            shift
            ;;
        -w|--width)
            width=$2
            shift
            ;;
        -h|--height)
            height=$2
            shift
            ;;
        *) # unknown argument
            echo "unknown arg $1"
            print_usage
            exit 1
            ;;
    esac
    shift
done

if [ -z $mode ]
then
    echo "WARNING: mode not set: using default 'dual' mode";
    mode="dual"
fi

if [ -z $sink ]
then
    echo "WARNING: sink not set: using default 'window' mode";
    sink="window"
fi

if [ -z $format ]
then
    echo "WARNING: format not set: using default 'yuv' format";
    format="yuv"
fi

if [ -z $width ] || [ -z $height ]
then
    echo "WARNING: output resolution not set: using default '1280x720' resolution";
    width=1280
    height=720
fi

output_res=${width}x${height}

echo -e "\n\nRun Camera with: mode=$mode, sink=$sink, output resolution=$output_res, format=$format\n"

# Detect MIPI capture pipeline devices
MEDIA_DEV=/dev/$(ls /sys/devices/platform/amba_pl@0/amba_pl@0\:vcap_CAPTURE_PIPELINE_v_proc_ss_scaler_0/ | grep media)
VIDEO_DEV=/dev/$(ls /sys/devices/platform/amba_pl@0/amba_pl@0\:vcap_CAPTURE_PIPELINE_v_proc_ss_scaler_0/video4linux/ | grep video)

CSC_DEV=$(ls /sys/bus/platform/drivers/xilinx-vpss-csc/  | grep v_proc_ss)
SCALER_DEV=$(ls /sys/bus/platform/drivers/xilinx-vpss-scaler/  | grep v_proc_ss)
MIPI_DEV=$(ls /sys/bus/platform/drivers/xilinx-csi2rxss/  | grep mipi_csi2_rx_subsystem)


MODEL=$(tr -d '\0' < $(find /sys/firmware/devicetree -name "sensor,model"))

BUS_DEV=$(ls /sys/bus/i2c/drivers/ap1302 | grep 003c)

AP1302_I2C="${BUS_DEV}"
AP1302_DEV="ap1302.${AP1302_I2C}"
AP1302_SENSOR="${AP1302_I2C}.${MODEL}"

sensor_width_hex=$(xxd -ps -l 4 $(find /sys/firmware/devicetree -name "sensor,resolution"))
sensor_height_hex=$(xxd -ps -l 4 -s 4 $(find /sys/firmware/devicetree -name "sensor,resolution"))
sensor_width=`printf "%d\n" 0x$sensor_width_hex`
sensor_height=`printf "%d\n" 0x$sensor_height_hex`

if [ "$mode" = "dual" ]
then
    CAMERA_RESOLUTION=$((sensor_width*2))x${sensor_height}
else
    CAMERA_RESOLUTION=${sensor_width}x${sensor_height}
fi

if [ "$format" = "yuv" ]
then
    GST_FORMAT="YUY2"
    MEDIA_FORMAT="UYVY8_1X16"
	src_convert=""
else
    GST_FORMAT="BGR"
    MEDIA_FORMAT="RBG24"
	src_convert="videoconvert ! "
fi

# Configure MIPI capture pipeline for RGB
set -x
media-ctl -d ${MEDIA_DEV} -V "'${AP1302_DEV}':2 [fmt:UYVY8_1X16/$CAMERA_RESOLUTION field:none]"
media-ctl -d ${MEDIA_DEV} -V "'${MIPI_DEV}':0 [fmt:UYVY8_1X16/$CAMERA_RESOLUTION field:none]"
media-ctl -d ${MEDIA_DEV} -V "'${MIPI_DEV}':1 [fmt:UYVY8_1X16/$CAMERA_RESOLUTION field:none]"
media-ctl -d ${MEDIA_DEV} -V "'${CSC_DEV}':0 [fmt:UYVY8_1X16/$CAMERA_RESOLUTION field:none]"
media-ctl -d ${MEDIA_DEV} -V "'${CSC_DEV}':1 [fmt:$MEDIA_FORMAT/$CAMERA_RESOLUTION field:none]"
media-ctl -d ${MEDIA_DEV} -V "'${SCALER_DEV}':0 [fmt:$MEDIA_FORMAT/$CAMERA_RESOLUTION field:none]"
media-ctl -d ${MEDIA_DEV} -V "'${SCALER_DEV}':1 [fmt:$MEDIA_FORMAT/$output_res field:none]"
set +x

# Setup Sensors links to AP1302
link1_enable=1
link2_enable=1

if [ "$mode" = "primary" ]
then
    link2_enable=0
fi

if [ "$mode" = "secondary" ]
then
    link1_enable=0
fi
set -x
media-ctl -d ${MEDIA_DEV} -l "'${AP1302_SENSOR}.0':0 -> '${AP1302_DEV}':0[$link1_enable]"
media-ctl -d ${MEDIA_DEV} -l "'${AP1302_SENSOR}.1':0 -> '${AP1302_DEV}':1[$link2_enable]"
set +x

# Turn off AWB for case of AR0144 sensors (monochrome)
if [[ "$MODEL" == "ar0144" ]]; then
	echo "Detected AR0144 - disabling AWB"
	v4l2-ctl --set-ctrl white_balance_auto_preset=0 -d ${VIDEO_DEV}
	echo "Detected AR0144 - setting brightness"
	v4l2-ctl --set-ctrl brightness=256 -d ${VIDEO_DEV}
fi
if [[ "$MODEL" == "ar1335" ]]; then
	echo "Detected AR1335 - enabling AWB"
	v4l2-ctl --set-ctrl white_balance_auto_preset=1 -d ${VIDEO_DEV}
fi

# Setup sink
case "$sink" in
    "dp")
        modetest -D fd4a0000.display -s 43@41:$output_res@RG16 -P 39@41:$output_res@YUYV -w 40:alpha:0 &
        sleep 1
		sink_convert=""
        sink_cmd="fpsdisplaysink video-sink='kmssink plane-id=39 bus-id=fd4a0000.display render-rectangle=\"<0,0,$width,$height>\"' fullscreen-overlay=true sync=false"
    ;;
    "window")
		sink_convert="videoconvert ! "
        sink_cmd="fpsdisplaysink video-sink='autovideosink' text-overlay=false sync=false"
    ;;
    "fake")
		sink_convert=""
        sink_cmd="fpsdisplaysink video-sink='fakevideosink' text-overlay=false sync=false"
    ;;
    *)
        echo "ERROR: Unknown sink specified";
        print_usage
        exit 1
    ;;
esac


# Start Pipeline
set -x
gst-launch-1.0 v4l2src device=${VIDEO_DEV} io-mode="dmabuf" ! \
	"video/x-raw, width=$width, height=$height, format=$GST_FORMAT, framerate=60/1" ! \
	$src_convert \
    queue leaky=downstream max-size-buffers=5 max-size-bytes=0 max-size-time=0 ! \
    hailonet hef-path=$hef_path ! \
    queue leaky=no max-size-buffers=30 max-size-bytes=0 max-size-time=0 ! \
    hailofilter function-name=$network_name config-path=$json_config_path so-path=$postprocess_so qos=false ! \
    queue leaky=no max-size-buffers=30 max-size-bytes=0 max-size-time=0 ! \
    hailooverlay ! \
    queue leaky=downstream max-size-buffers=5 max-size-bytes=0 max-size-time=0 ! \
    $sink_convert \
	$sink_cmd \
	-v
set +x

Credits

Mario Bergeron

Mario Bergeron

48 projects • 277 followers
Mario Bergeron is a Technical Marketing Engineer working at Tria, specializing in embedded vision and machine learning.
gianluca filippini

gianluca filippini

2 projects • 4 followers

Comments