Example programs
================
This is a collection of example programs demonstrating how to use Netint ai 
detection solutions through libxcoder.

Compile
================
In Linux, use "make".

###
Building example programs

To build libxcoder, from the directory FFMPEGXCODER/libxcoder/ :
    ./build.sh

To build libxcoder api example programs, from this directory run:
	make clean; make

###
ni_ocr_vqe_transcoder

Supported ffmpeg version: 4.1.3, 4.2.1, 4.3, 4.3.1, 4.4, 5.0
This example application is used to process input frame with ocr and vqe 
and generate detection result
It need *.nb for ai detection, like network_binary_ocr_512.nb, network_binary_vqe.nb
for ocr, it also need characters lib, like characters.txt

Usage exmaple:
# only transcode
sudo ./ni_ocr_vqe_transcoder -i input.h264 -o out.h264 -d h264_ni_quadra_dec -D out=hw \
-f null -e h264_ni_quadra_enc -E RcEnable=1:bitrate=500000 -b 500000 -n 0

# ocr
sudo ./ni_ocr_vqe_transcoder -i input.h264 -o out.h264 -d h264_ni_quadra_dec -D out=hw \
-f null -e h264_ni_quadra_enc -E RcEnable=1:bitrate=500000 -b 500000 -n 0 \
-O -s ./network_binary_ocr_512.nb -m 100 -c characters.txt -r 720:48:48:56,224:48:1392:960

# vqe
sudo ./ni_ocr_vqe_transcoder -i input.h264 -o out.h264 -d h264_ni_quadra_dec -D out=hw \
-f null -e h264_ni_quadra_enc -E RcEnable=1:bitrate=500000 -b 500000 -n 0 \
-V -S ./network_binary_vqe.nb -M 20 -w 10

# ocr & vqe
sudo ./ni_ocr_vqe_transcoder -i input.h264 -o out.h264 -d h264_ni_quadra_dec -D out=hw \
-f null -e h264_ni_quadra_enc -E RcEnable=1:bitrate=500000 -b 500000 -n 0 \
-O -s ./network_binary_ocr_512.nb -m 100 -c characters.txt -r 720:48:48:56,224:48:1392:960 \
-V -S ./network_binary_vqe.nb -M 20 -w 10

# jepg input with encode
sudo ./ni_ocr_vqe_transcoder -i 01.jpg -o out.jpg -d jpeg_ni_quadra_dec -D out=hw \
-e jpeg_ni_quadra_enc -n 0 -O -s ./network_binary_ocr_512.nb -c characters.txt \
-r 720:48:48:56,224:48:1392:960 -V -S ./network_binary_vqe.nb

# jepg input without encode
sudo ./ni_ocr_vqe_transcoder -i 01.jpg -d jpeg_ni_quadra_dec -D out=hw -n 0 \
-O -s ./network_binary_ocr_512.nb -c characters.txt -r 720:48:48:56,224:48:1392:960 \
-V -S ./network_binary_vqe.nb

Usage:
-i | --input             input file path.
-o | --output            output file path.
-d | --decoder           decoder name [h264_ni_quadra_dec | h265_ni_quadra_dec | jpeg_ni_quadra_dec].
-D | --decoder_params    decoder parameters.
-f | --filter_dsc        filter set params.
-e | --encode_name       encoder name [h264_ni_quadra_enc | h265_ni_quadra_enc | jpeg_ni_quadra_enc].
-E | --encode_params     encoder params.
-l | --loglevel          available debug level: warning, info, debug, trace.          
-n | --devid             device id.
-b | --bitrate           set bitrate.
-O | --enable_ocr        enable ocr.
-V | --enable_vqe        enable vqe.
-s | --ocr_nbg_path      ocr model path.
-p | --ocr_model_pixel   ocr model pixel format:-p WdithxHeight.
-m | --ocr_select_frame  select frame to ocr detection.    
-c | --character_file    input character library
-r | --area_box          crop roi box format:-r w:h:x:y,w:h:x:y.
-t | --ocr_output        the path of txt store the detect result of ocr.
-S | --vqe_nbg_path      vqe model path.
-P | --vqe_model_pixel   vqe model pixel format:-p WdithxHeight.
-M | --vqe_select_frame  select frame to vqe detection.
-w | --window_size       Video Quality Estimation window size, require window_size < 120 default:20.
-T | --vqe_output        the path of txt store the detect result of vqe.
-h | --help              print this help information.

###
ni_roi_transcoder

Supported ffmpeg version: 4.2.1, 4.3, 4.3.1, 4.4, 5.0
This example application is used to process input frame with roi 
and draw box and text on the encode frame
It need *.nb for ai detection, like network_binary_yolov4_head.nb, network_binary_vqe.nb
NOTICE:if enable drawtext, must build ffmpeg like ./build_ffmpeg.sh --nidrawtext, and fps will drop

#yolov4
./ni_roi_transcoder -i cr7_1920x1080.h264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -x out=hw \
-s ../../AI_benchmarking_examples/network_binary_yolov4_head.nb -f null -k 416x416 -y 4 -v error

#yolov5 enable select_class and drawtext
./ni_roi_transcoder -i cr7_1920x1080.h264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -x out=hw \
-s ../../AI_benchmarking_examples/network_binary_yolov5s_improved.nb -f null -k 640x640 -y 5 -v error -c -D 

#write roi result into file
./ni_roi_transcoder -i cr7_1920x1080.h264 -o out.h264 -d h264_ni_quadra_dec -n 0 -e h264_ni_quadra_enc \
-p RcEnable=1:bitrate=5000000 -x out=hw -s ../../AI_benchmarking_examples/network_binary_yolov4_head.nb \
-f null -k 416x416 -y 4 -v error -t out.json

Usage:
-y | --yolo_version      yolo version [4|5].
-s | --nbg_path          path of model.
-k | --model_pixel       pixel of model input, 416x416 for yolov4 640x640 for yolov5.
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-x | --decoder_params    decoder parameters.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-f | --filter_dsc        filter set params.
-b | --bitrate           set bitrate.
-e | --encode_name       encode name [h264_ni_dec | h265_ni_dec].
-p | --encode_params     encode params.
-m | --select_frame      select frame to roi.
-c | --select_class      enable to select person and vehicles.
-t | --roi_output        the path of file store the detect result of roi
-D | --drawtext          enable to draw class and prob.
-h | --help              print this help information.

###
ni_roi_detect

Supported ffmpeg version: 4.2.1, 4.3, 4.3.1, 4.4, 5.0
This example application is used to process input frame with roi 
and draw box and text on the encode frame
It need *.nb for ai detection, like network_binary_yolov4_head.nb, network_binary_vqe.nb
If output txt not been specified, if input is input/test.h264, roi info will been put in test.txt

#yolov4 specified output text
./ni_roi_detect -i cr7_1920x1080.h264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -x out=hw \
-s ../../AI_benchmarking_examples/network_binary_yolov4_head.nb -f null -k 416x416 -y 4 -v error -t test.txt

#yolov5 draw roi box 
./ni_roi_detect -i cr7_1920x1080.h264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -x out=hw \
-s ../../AI_benchmarking_examples/network_binary_yolov5s_improved.nb -f null -k 640x640 -y 5 -v error -D
Usage:
-y | --yolo_version      yolo version [4|5].
-s | --nbg_path          path of model.
-k | --model_pixel       pixel of model input, 416x416 for yolov4 640x640 for yolov5.
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-x | --decoder_params    decoder parameters.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-f | --filter_dsc        filter set params.
-b | --bitrate           set bitrate.
-e | --encode_name       encode name [h264_ni_dec | h265_ni_dec].
-p | --encode_params     encode params.
-t | --roi_output        the path of txt store the detect result of roi.
-c | --select_class      enable to select person and vehicles.
-D | --drawbox           enable to draw box.
-h | --help              print this help information.

#ni_ai_transcode
This example application is used to provide a demo for customer to use there own AI model by customer.
The input is h264 file and output is h265, the input of AI engine should be hw frame so that the "out=hw" is necessary.
./ni_ai_transcode -i Dinner_1920x1080p30_300.h264 \
-s 1920x1080  -o test_ai.h265 -d out=hw -n customer_model.nb
For test, the nb file can use ../../AI_benchmarking_examples/ai_pre_process_demo.nb, this nb should use 1080p clip.
Usage:
options:
--------------------------------------------------------------------------------
-h | --help        Show help.
-v | --version     Print version info.
-l | --loglevel    Set loglevel of libxcoder API.
                   [none, fatal, error, info, debug, trace]
                   Default: info
-c | --card        Set card index to use.
                   See `ni_rsrc_mon` for cards on system.
                   (Default: 0)
-i | --input       Input file path.
-r | --repeat      (Positive integer) to Repeat input X times for performance
                   test. (Default: 1)
-s | --size        Resolution of input file in format WIDTHxHEIGHT.
                   (eg. '1920x1080')
-o | --output      Output file path

###
ni_roi_detect_311

Supported ffmpeg version: 3.1.1
This example application is used to process input frame with roi 
and draw box and text on the encode frame
It need *.nb for ai detection, like network_binary_yolov4_head.nb, network_binary_vqe.nb
If output txt not been specified, if input is input/test.h264, roi info will been put in test.txt

#yolov4 specified output text
./ni_roi_detect_311 -i cr7_1920x1080.h264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -x out=hw \
-s ../../AI_benchmarking_examples/network_binary_yolov4_head.nb -f null -k 416x416 -y 4 -v error -t test.txt

#yolov5 draw roi box 
./ni_roi_detect_311 -i cr7_1920x1080.h264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -x out=hw \
-s ../../AI_benchmarking_examples/network_binary_yolov5s_improved.nb -f null -k 640x640 -y 5 -v error -D
Usage:
-y | --yolo_version      yolo version [4|5].
-s | --nbg_path          path of model.
-k | --model_pixel       pixel of model input, 416x416 for yolov4 640x640 for yolov5.
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-x | --decoder_params    decoder parameters.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-f | --filter_dsc        filter set params.
-b | --bitrate           set bitrate.
-e | --encode_name       encode name [h264_ni_dec | h265_ni_dec].
-p | --encode_params     encode params.
-t | --roi_output        the path of txt store the detect result of roi.
-c | --select_class      enable to select person and vehicles.
-D | --drawtext          enable to draw class and prob.
-h | --help              print this help information.

###
ni_defog

This example application is used to provide an end-to-end application including decoding, defog and encoding to demonstrate how the defog model works.
the example command line applies hardware decoding and encoding as follows:
./ni_defog -i 1920x1080p_ParkScene.264 -o ./out0.h264 -d h264_ni_quadra_dec -n 0 \
-e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -b 5000000 -a dark_channel.nb -z dehaze_guided_filter.nb
For test, the dark channel model file can use ../../AI_benchmarking_examples/QUADRA_Defog/AI_model_1.nb
while the dehaze model file can use ../../AI_benchmarking_examples/QUADRA_Defog/AI_model_2.nb.
this nb should use 1080p clip.
Usage:
options:
--------------------------------------------------------------------------------
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-b | --bitrate           set bitrate.
-e | --encode_name       encode name [h264_ni_dec | h265_ni_dec].
-p | --encode_params     encode params.
-a | --dark_chnl_model   dark channel model file path.
-z | --dehaze_model      dehaze model file path.
-h | --help              print this help information.

###
ni_lpr

This example application is used to provide an end-to-end application including decoding, plate recognition to demonstrate how the licence plate recognition models work.
the example command line applies hardware decoding and encoding as follows:
./ni_lpr -i ../../AI_benchmarking_examples/licence_plate.265 -o ./plate_result.txt -d h265_ni_quadra_dec -n 0 \
        -t ../../AI_benchmarking_examples/dectlicense_hybrid.nb \
        -c ../../AI_benchmarking_examples/reclicense_fp16.nb
Usage:
options:
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-b | --bitrate           set bitrate.
-t | --lic_det_model     licence detect model file path.
-c | --lic_rec_model     licence recognize model file path.
-O | --obj_thresh        detection objectness threshold: (0.0,1.0).
-N | --nms_thresh        detection nms threshold: (0.0,1.0).
-h | --help              print this help information.


###
ni_scene_classify

This example application is used to provide an end-to-end application including decoding, scene_classify to demonstrate how the models work and encode.
the example command line applies hardware decoding and encoding as follows:

./ni_scene_classify -i ../../AI_benchmarking_examples/television_studio_Places365_val_00013554.jpg -o out.h264 -d jpeg_ni_quadra_dec -n 0 \
        -e h264_ni_quadra_enc -p RcEnable=1:bitrate=5000000 -x out=hw -s ../../AI_benchmarking_examples/scene_classification.nb -t out.txt

Usage:
-s | --nbg_path          path of model.
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-x | --decoder_params    decoder parameters.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-e | --encode_name       encode name.
-p | --encode_params     encode params.
-t | --res_output        the path of txt store the detect result.
-h | --help              print this help information.


###
ni_yolov8_detect

This example application decodes video and uses the AI engine with the Yolo v8 segmentation model to detect objects
in the video. Boxes are drawn around cars, trucks, and people using the Quadra 2D engine and the video is then
re-encoded.

./ni_yolov8_detect -i cr7_1920x1080.h264 -d h264_ni_quadra_dec -x out=hw -e h264_ni_quadra_enc \
        -p RcEnable=1:bitrate=5000000 -o yolov8.h264 -s ../../AI_benchmarking_examples/yolov8n352x640nchwbgr.nb -D

 Usage:
-i | --input             input file path.
-o | --output_file       output file path.
-d | --decoder           decoder name.
-x | --decoder_params    decoder parameters (you must at least specify out=hw)
-e | --encode_name       encode name [h264_ni_dec | h265_ni_dec].
-p | --encode_params     encode params.
-s | --nbg_path          path of model.
-D | --drawbox           enable to draw box.
-t | --roi_output        the path of txt store the detect result of roi.
-n | --devid             device id.
-v | --loglevel          available debug level: warning, info, debug, trace.
-I | --inference_freq    inference frequency: 1 = every frame, 2 = every 2nd frame, etc.
-h | --help              print this help information.
