Saturday 29 January 2022

Ubuntu18 cannot play h264 video using Videos?

sudo apt install x264 x265 libx264-dev libx265-dev 

sudo apt-get install ubuntu-restricted-extras

sudo apt-get install libavcodec54 libav-tools ffmpeg

Tuesday 25 January 2022

How to use the rotatedRect in opencv c++?

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char* argv){

    cv::Mat src = cv::imread("../tony.jpg");
    // rect is the RotatedRect (I got it from a contour...)
    cv::RotatedRect rect(cv::Point2f(453, 127), cv::Size(230,140), 45);
        // matrices we'll use
    cv::Mat M, rotated, cropped;
        // get angle and size from the bounding box
        float angle = rect.angle;
    cv::Size rect_size = rect.size;
        if (rect.angle < -45.) {
            angle += 90.0;
        cv::swap(rect_size.width, rect_size.height);
        }
        // get the rotation matrix
        M = cv::getRotationMatrix2D(rect.center, angle, 1.0);
        // perform the affine transformation
    cv::warpAffine(src, rotated, M, src.size(), cv::INTER_CUBIC);
        // crop the resulting image
    cv::getRectSubPix(rotated, rect_size, rect.center, cropped);

    cv::imshow("cropped", cropped);
    cv::waitKey(0);
    return 0;

}

ref: https://gist.github.com/steven2358/ba153c642fe2bb1e47485962df07c730
https://ffmpeg.org/ffmpeg-all.html#Video-Options

FFmpeg cheat sheet

A list of useful commands for the ffmpeg command line tool.

Download FFmpeg: https://www.ffmpeg.org/download.html

Full documentation: https://www.ffmpeg.org/ffmpeg.html

Basic conversion

ffmpeg -i in.mp4 out.avi

Remux an MKV file into MP4

ffmpeg -i in.mkv -c:v copy -c:a copy out.mp4

High-quality encoding

Use the crf (Constant Rate Factor) parameter to control the output quality. The lower crf, the higher the quality (range: 0-51). The default value is 23, and visually lossless compression corresponds to -crf 18. Use the preset parameter to control the speed of the compression process. Additional info: https://trac.ffmpeg.org/wiki/Encode/H.264

ffmpeg -i in.mp4 -preset slower -crf 18 out.mp4

Trimming

Without re-encoding:

ffmpeg -ss [start] -i in.mp4 -t [duration] -c copy out.mp4
  • -ss specifies the start time, e.g. 00:01:23.000 or 83 (in seconds)
  • -t specifies the duration of the clip (same format).
  • Recent ffmpeg also has a flag to supply the end time with -to.
  • -c copy copies the first video, audio, and subtitle bitstream from the input to the output file without re-encoding them. This won't harm the quality and make the command run within seconds.

With re-encoding:

If you leave out the -c copy option, ffmpeg will automatically re-encode the output video and audio according to the format you chose. For high quality video and audio, read the x264 Encoding Guide and the AAC Encoding Guide, respectively.

For example:

ffmpeg -ss [start] -i in.mp4 -t [duration] -c:v libx264 -c:a aac -strict experimental -b:a 128k out.mp4

Mux video and audio from another video

To copy the video from in0.mp4 and audio from in1.mp4:

ffmpeg -i in0.mp4 -i in1.mp4 -c copy -map 0:0 -map 1:1 -shortest out.mp4

Concat demuxer

First, make a text file.

file 'in1.mp4'
file 'in2.mp4'
file 'in3.mp4'
file 'in4.mp4'

Then, run ffmpeg:

ffmpeg -f concat -i list.txt -c copy out.mp4

Delay audio/video

Delay video by 3.84 seconds:

ffmpeg -i in.mp4 -itsoffset 3.84 -i in.mp4 -map 1:v -map 0:a -vcodec copy -acodec copy out.mp4

Delay audio by 3.84 seconds:

ffmpeg -i in.mp4 -itsoffset 3.84 -i in.mp4 -map 0:v -map 1:a -vcodec copy -acodec copy out.mp4

Burn subtitles

Use the libass library (make sure your ffmpeg install has the library in the configuration --enable-libass).

First convert the subtitles to .ass format:

ffmpeg -i sub.srt sub.ass

Then add them using a video filter:

ffmpeg -i in.mp4 -vf ass=sub.ass out.mp4

Extract the frames from a video

To extract all frames from between 1 and 5 seconds, and also between 11 and 15 seconds:

ffmpeg -i in.mp4 -vf select='between(t,1,5)+between(t,11,15)' -vsync 0 out%d.png

To extract one frame per second only:

ffmpeg -i in.mp4 -fps=1 -vsync 0 out%d.png

Rotate a video

Rotate 90 clockwise:

ffmpeg -i in.mov -vf "transpose=1" out.mov

For the transpose parameter you can pass:

0 = 90CounterCLockwise and Vertical Flip (default)
1 = 90Clockwise
2 = 90CounterClockwise
3 = 90Clockwise and Vertical Flip

Use -vf "transpose=2,transpose=2" for 180 degrees.

Download "Transport Stream" video streams

  1. Locate the playlist file, e.g. using Chrome > F12 > Network > Filter: m3u8
  2. Download and concatenate the video fragments:
ffmpeg -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4

If you get a "Protocol 'https not on whitelist 'file,crypto'!" error, add the protocol_whitelist option:

ffmpeg -protocol_whitelist "file,http,https,tcp,tls" -i "path_to_playlist.m3u8" -c copy -bsf:a aac_adtstoasc out.mp4

Mute some of the audio

To replace the first 90 seconds of audio with silence:

ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='lte(t,90)':volume=0" out.mp4

To replace all audio between 1'20" and 1'30" with silence:

ffmpeg -i in.mp4 -vcodec copy -af "volume=enable='between(t,80,90)':volume=0" out.mp4

Deinterlace

Deinterlacing using "yet another deinterlacing filter".

ffmpeg -i in.mp4 -vf yadif out.mp4

Create a video slideshow from images

Parameters: -r marks the image framerate (inverse time of each image); -vf fps=25 marks the true framerate of the output.

ffmpeg -r 1/5 -i img%03d.png -c:v libx264 -vf fps=25 -pix_fmt yuv420p out.mp4

Extract images from a video

  • Extract all frames: ffmpeg -i input.mp4 thumb%04d.jpg -hide_banner
  • Extract a frame each second: ffmpeg -i input.mp4 -vf fps=1 thumb%04d.jpg -hide_banner
  • Extract only one frame: ffmpeg -i input.mp4 -ss 00:00:10.000 -vframes 1 thumb.jpg

Metadata: Change the title

ffmpeg -i in.mp4 -map_metadata -1 -metadata title="My Title" -c:v copy -c:a copy out.mp4

Wednesday 19 January 2022

How to make a simple OpenCV C++ program CMakeLists.txt?

cmake_minimum_required(VERSION 3.10)
project(main)
set(CMAKE_PREFIX_PATH /data/opencv-4.4.0/distribute/)
find_package(OpenCV REQUIRED)
include_directories(/data/opencv-4.4.0/distribute/include)
link_directories(/data/opencv-4.4.0/distribute/lib)
add_executable(main main.cpp)
target_link_libraries(main opencv_core opencv_imgproc opencv_highgui opencv_videoio)

Friday 14 January 2022

How to calculate the module time in C++ OpenCV?

method1

         double t = (double)cv::getTickCount();
        bool status = weightedMovingVariance(frame, output, count);
        t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
        std::cout << "Times passed in seconds: " << t << std::endl;


method2:

cv::TickMeter tm;
tm.start();
// do something ...
tm.stop();
std::cout << "Total time: " << tm.getTimeSec() << std::endl;

How to do smart indent in vim for C++ code?

 gg then =G

Wednesday 12 January 2022

How to run spawn using a bash script?

/data/build/main1.sh &
P1=$!
/data/build/main2.sh &
P2=$!
wait $P1 $P2

Friday 7 January 2022

How to fix corrupted pip?

wget https://bootstrap.pypa.io/pip/3.5/get-pip.py 

python get-pip.py

triton - how to set the batch size: dynamic versus predefined

predefined:-

name: "kps"
platform: "tensorrt_plan"
max_batch_size: 16
dynamic_batching {
  preferred_batch_size: [ 1,2,4,8,16 ]
  max_queue_delay_microseconds: 50
}

instance_group [
    {
      count: 16
      kind: KIND_GPU
      gpus: [ 0 ]
    }
]

input [
  {
    name: "input"
    data_type: TYPE_FP32
    format: FORMAT_NCHW
    dims: [ 3, 64, 64 ]
    reshape { shape: [ 3, 64, 64 ] }
  }
]
output [
  {
    name: "output1"
    data_type: TYPE_FP32
    dims: [ 1]
  },
  {
    name: "output2"
    data_type: TYPE_INT32
    dims: [ 1 ]
  },
  {
    name: "output3"
    data_type: TYPE_FP32
    dims: [ 8,64 ]
  }
]



version_policy: { all { } }
 

 

dynamic:-

platform: "tensorrt_plan"
max_batch_size: 0

instance_group [
    {
      count: 16
      kind: KIND_GPU
      gpus: [ 0 ]
    }
]


input [
  {
    name: "images"
    data_type: TYPE_FP32
    dims: [ -1,3, 640, 640]
  }
]
output [
  {
    name: "524"
    data_type: TYPE_FP32
    dims: [ -1,3,80,80, 7 ]
  },
      {
    name: "585"
    data_type: TYPE_FP32
    dims: [ -1,3,40,40, 7 ]
  },
  {
    name: "646"
    data_type: TYPE_FP32
    dims: [ -1,3,20,20, 7 ]
  },
  {
    name: "output"
    data_type: TYPE_FP32
    dims: [ -1, 25200, 7 ]
  }
]
~                                                                                                                                                                                                           
~                                                                                                                                                                                                           
~                                                                                                                                                                                                           
~                                             

Tuesday 4 January 2022

cmake of VisionWorks

 cmake_minimum_required(VERSION 3.10)

#set the project name and version
project(render)

find_package(OpenCV REQUIRED)
find_package(CUDA 10.2 REQUIRED)

set(WITH_CUDA ON)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")

include_directories(${OpenCV_INCLUDE_DIRS})


add_executable(main main_nvgstcamera_capture.cpp)


target_include_directories(main PUBLIC
                -I/usr/local/cuda-10.2/targets/aarch64-linux/include
                /usr/share/visionworks/sources/nvxio/include
                /usr/share/visionworks/sources/nvxio/src/
                /usr/share/visionworks/sources/nvxio/src/NVX/
                /usr/share/visionworks/sources/3rdparty/opengl
                /usr/share/visionworks/sources/3rdparty/glfw3/include
                /usr/share/visionworks/sources/3rdparty/freetype/include
                /usr/share/visionworks/sources/3rdparty/eigen
)


target_link_libraries(main
            -L$(PKG_CONFIG_SYSROOT_DIR)/usr/lib
            /usr/share/visionworks/sources/libs/aarch64/linux/release/libovx.a
            /usr/share/visionworks/sources/3rdparty/freetype/libs/libfreetype.a
            /usr/share/visionworks/sources/3rdparty/glfw3/libs/libglfw3.a
            /usr/lib/aarch64-linux-gnu/tegra-egl/libGLESv2_nvidia.so.2
            -L/usr/lib/aarch64-linux-gnu
            -lEGL
            -lXrandr
            -lXi
            -lXxf86vm
            -lX11
            -lgstpbutils-1.0
            -lgstaudio-1.0
            -lgstvideo-1.0
            -lgstapp-1.0
            -lgstbase-1.0
            -lgstreamer-1.0
            -lgobject-2.0
            -lglib-2.0
            /usr/lib/aarch64-linux-gnu/tegra/libcuda.so
            -L/usr/local/cuda-10.2/targets/aarch64-linux/lib
            -lcudart
            -lvisionworks
                




            
            ${OpenCV_LIBS}
            )

Saturday 1 January 2022

how to loop a string array in c++?

#include "iostream"

#include "string.h"


using namespace std;


int main()

{

    string strNames[] = { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6"};


    for each(string name in strNames)

    {

       printf("%s\n", name.c_str());

    }

    return 0;

}