Video analytics

Today , there are billions of  of video cameras in our homes,  phones , ATMs ,  baby monitors , laptops , smart watches , traffic monitoring , IOT devices , bots , you name it. The underlying purpose of most of them  is to capture media streams and optimize the content for further processing.

Stages of video Analytics-

1.Data Acquisition

Data gathered from multiple camera sourced need to be streamed in an aggregated manner to data processing unit in an analytics engine  or for archiving . It may be subjected to overall monitoring such as sunlight hours etc  or detailed low level object identification  such as facial recognition of passengers

2.Transformation to Data Sets

The assimilated data is grouped to operable entities. The identification and classification is done by adding attributes to recognizable shapes ,movements , edges and patterns  .

3.Calculate deviation or compliance

A trained model recognizes normal behavior and differentiation from the same is calculated .

 

Video content Analytics in surveillance 

Considering the use case for monitoring and surveillance cameras  , there is a growing need for realtime video analytics  , to ” detect and determine the temporal and spatial events “. Consider Surveillance cam recordings as forensic evidence or just monitoring incidents and reporting the specific crucial segments of video , both these usecases involve filtering a vast amount of recorded or steaming media to filter out the exact parts that the authorities are looking for. This involves custom identification and recognition of objects in frames .

There is growing research into extracting real time events of interest , minimizing the search time and maximizing accuracy from computer vision .

Consider following use-cases :

  1. Surveillance cam in solar farms or home based setups to predict sun light hours and forecast energy generation value  . Described in greater details here .
  2. Traffic monitoring cameras :
  • Automatic license / number plate recognition – surveillance cams for traffic need to record vehicle plate number to identify and tag the vehicles as they pass by  .
  • Car  dashboard cams for investigative purposes  post accidents and insurance claims
  • Motion tracking – Mapping the vehicle movement to detect any wrong turns , overtakes , parking etc
  • Scan for QR codes and passes at toll gates.
  • Identifying over-speeding vehicles

3. Security and Law enforcement

  • Trigger alarms or lockdowns  on suspicious activity or intrusion into safe facility
  • Virtual fencing and perimeter breach – Map facial identification from known suspects
  • Detection of left items and acceleration of emergency response

Communication based video analytics 

Unified enterprise communication , conferences , meeting , online webcasts , webinars , social messengers , online project demos are extensively using video analytics for building intuitive use cases and boost innovation around their platform . Few examples of vast number of usecases are

  1. Sentiment Analysis : Capturing emotions by mapping key words to ascertain whether the meeting went , happy , positive , productive or  sad , complaining , negative
  2. Augmented Reality for overlaying information such as interactive manual or a image . Areas for current usage include , e-learning and customer support .
  3. Dynamic masking for privacy

Autonomous Robot purpose Video analytics

Self driving drones , cars and even bots extensive use the feed from wide angle / fish eye lens cameras to create a 3D model of their movement in given space of 3 dimensional coordinates system.

Key technologies includes :

  1. Ego-motion estimation – mapping a 3D space with captured camera feed
  2. Deep Learning ( part of AI) from continuous feed from video cameras to find route around obstacles
  3. Remote monitoring for an unmanned vehicle
  4. Sterile monitoring for a unreachable or hazardous area example war-zone , outer  territorial objects as moon , mars , satellites

Bio-metrics based Video analytics 

Often video feed is now used for advanced search, redaction and facial recognition , which leads to features such as

  • unlocking laptop or phone
  • performing click with blink of eyes
  • creating concentration maps of webpage based on where eyes focused

Read more about role of webrtc in bio- metrics here

Video analytics in Industrial and Retail applications 

Application of video analytics in Industrial landscape are manyfold . On one hand it can be for intelligence and  information gathering such as worker foot count . Machine left unattended etc while on the other hand by using specific image optimization techniques can also audit automation testing of engines , machine parts , rotation counts etc .

  1. Flame and Smoke Detection – images from video streams are analysed for color chrominance, flickering ratio, shape, pattern and moving direction to ascertain a fire hazard.
  2. Collect demographics of the area with people counting
  3. Ensure quality control and procedural compliance
  4. Identify tail-gateing or loitering

 

List of few companies focusing on Video Analytics :

  1. Avigilon -http://avigilon.com/products/video-analytics/video-analytics/
  2. 3VR – http://www.3vr.com/products/videoanalytics
  3. Intelli-vision – https://www.intelli-vision.com/
  4. IPsotek – https://www.ipsotek.com
  5. aimetis – http://www.aimetis.com/

 

Edge Analytics

Performing data analytics at application level on the edge whole system architecture instead of at the core or data warehouse level. The advantage to the computation at fringes of network instead of a centralized system are faster response times and standalone off grid functionality support .

The humongous data collected over by IOT devices m machinery  , sensors  , servers , firewalls , routers , switches gateways and all other types of components are increasingly getting analysed and acted upon at the edge ,  independently with machine learning instead of data centers and network operation centers . With the help of feedback loops and deep learning one could add data drive intelligence to how operations are performed at critical machines arts such as autonomous bots or industrial setups.

Error Recovery and streaming

To control the incoming data stream , we divide and classify the content into packets , add custom classification header and stream them down to the server. In the event of data congestion of back pressure , some non significant packets are either dropped or added to a new dead queue .  The system  is thus made stable for high availability and BCP / fail-over recovery .

 

 

Gstreamer

GStreamer ( LGPL )is a media handling library written in C for applications such as streaming, recording, playback, mixing and editing attributes so on. Even enhanced applications such as transcoding , media format conversion , streaming servers for embedded devices ( read more about Gstreamer in RPi in my article here).

It is a pipeline based multimedia framework.It encompas various codecs, filters and is modular with plugins development to enhance its capabilities. Media Streaming application developers use it as part of their framework at either the broadcaster’s end or as a media player.

Pipeline simple example1

gst-launch-1.0 videotestsrc ! videoconvert ! autovideosink

ex2

gst-launch-1.0 filesrc location=file_example_MP4_480_1_5MG.mp4 ! qtdemux ! h264parse ! avdec_h264 ! autovideosink

Key Features of GStreamer include

  1. Modular and Plugin-Based Architecture:
    • allowing developers to extend its functionality by adding new plugins.
    • Plugins provide support for various codecs, formats, filters, and input/output devices.
  2. Pipeline-Based Processing:
    • GStreamer uses a pipeline model where multimedia data flows through a series of interconnected elements (e.g., sources, filters, sinks).
    • Each element performs a specific task, such as decoding, encoding, or rendering.
  3. Cross-Platform Support:
    • GStreamer runs on Linux, Windows, macOS, Android, and other platforms, making it versatile for different environments.
  4. Wide Range of Supported Formats:
    • GStreamer supports a vast array of audio and video formats, including MP3, AAC, H.264, VP9, and more.
    • It can handle both compressed and uncompressed media streams.
  5. Real-Time Streaming:
    • GStreamer is capable of real-time streaming, making it suitable for applications like video conferencing, IP cameras, and live broadcasting.
  6. Customizable and Extensible GStreamer’s API allows for fine-grained control over media processing.
    • C: The native API for GStreamer.
    • Python: Bindings for Python .
    • C++: Bindings for C++.
    • Rust: Bindings for Rust.
  7. Integration with Other Technologies, frameworks and libraries, such as OpenCV for computer vision, PulseAudio for audio, and WebRTC for real-time communication.

Core Concepts:

  • Elements: The basic building blocks of a GStreamer pipeline. Examples include:
    • Source elements: Capture or generate data (e.g., reading from a file or a webcam).
    • Filter elements: Process data (e.g., decoding, encoding, or applying effects).
    • Sink elements: Output data (e.g., rendering video to a screen or writing to a file).
  • Pads: Connection points on elements where data flows in (sink pads) or out (source pads).
  • Pipeline: A container for elements that defines the flow of media data.
  • Bus: A communication channel for messages (e.g., errors, state changes) between the pipeline and the application.

Some Use Cases:

Server-Side Processing: GStreamer can be used for transcoding, mixing, or analyzing media on servers.

Media Players: GStreamer is used in media players like Totem and Rhythmbox.

gst-launch-1.0 playbin uri=file://($pwd)/file_example_MP4_480_1_5MG.mp4

Also you can play with Debug options

GST_DEBUG=2 gst-launch-1.0 filesrc location=yourfile.mp4 ! qtdemux name=demux\ 
demux.video_0 ! queue ! decodebin ! autovideosink \
demux.audio_0 ! queue ! decodebin ! autoaudiosink

Video Editing: It can be used for non-linear video editing and processing.

Streaming: GStreamer is used in applications like IPTV, video conferencing, and live streaming.
One can use any sample source fot testruns

  • videotestsrc: Generates test video patterns.
  • audiotestsrc: Generates test audio tones.
  • v4l2src: Captures video from a webcam (Linux).
  • ximagesrc: Captures the screen (Linux).

Embedded Systems: It is popular in embedded devices for multimedia playback and capture.

GStreamer-1.8.1 rtsp server and client on ubuntu – Install and configuration for a RTSP Streaming server and Client

Streaming / broadcasting Live Video call to non webrtc supported browsers and media players

 Attempts of streaming / broadcasting Live Video WebRTC call to non WebRTC supported browsers and media players such as VLC , ffplay , default video player in Linux etc .

Code Snippets

To list all packages of Gstreamer

pkg-config --list-all | grep gstreamer
  • gstreamer-gl-1.0 GStreamer OpenGL Plugins Libraries – Streaming media framework, OpenGL plugins libraries
  • gstreamer-bad-video-1.0GStreamer bad video library – Bad video library for GStreamer elements
  • gstreamer-tag-1.0 GStreamer Tag Library – Tag base classes and helper functions
  • gstreamer-bad-base-1.0 GStreamer bad base classes – Bad base classes for GStreamer elements
  • gstreamer-net-1.0GStreamer networking library – Network-enabled GStreamer plug-ins and clocking
  • gstreamer-sdp-1.0 GStreamer SDP Library – SDP helper functions
  • gstreamer-1.0 GStreamer – Streaming media framework
  • gstreamer-bad-audio-1.0 GStreamer bad audio library, uninstalled – Bad audio library for GStreamer elements, Not Installedgstreamer-allocators-1.0 GStreamer Allocators Library – Allocators implementation
  • gstreamer-player-1.0 GStreamer Player – GStreamer Player convenience library
  • gstreamer-insertbin-1.0 GStreamer Insert Bin – Bin to automatically and insertally link elements
  • gstreamer-plugins-base-1.0 GStreamer Base Plugins Libraries – Streaming media framework, base plugins libraries
  • gstreamer-vaapi-glx-1.0 GStreamer VA-API (GLX) Plugins Libraries – Streaming media framework, VA-API (GLX) plugins librariesgstreamer-codecparsers-1.0 GStreamer codec parsers – Bitstream parsers for GStreamer elementsgstreamer-base-1.0 GStreamer base classes – Base classes for GStreamer elements
  • gstreamer-app-1.0 GStreamer Application Library – Helper functions and base classes for application integration
  • gstreamer-vaapi-drm-1.0 GStreamer VA-API (DRM) Plugins Libraries – Streaming media framework, VA-API (DRM) plugins librariesgstreamer-check-1.0 GStreamer check unit testing – Unit testing helper library for GStreamer modules
  • gstreamer-vaapi-1.0 GStreamer VA-API Plugins Libraries – Streaming media framework, VA-API plugins libraries
  • gstreamer-controller-1.0 GStreamer controller – Dynamic parameter control for GStreamer elements
  • gstreamer-video-1.0 GStreamer Video Library – Video base classes and helper functions
  • gstreamer-vaapi-wayland-1.0 GStreamer VA-API (Wayland) Plugins Libraries – Streaming media framework, VA-API (Wayland) plugins libraries
  • gstreamer-fft-1.0 GStreamer FFT Library – FFT implementation
  • gstreamer-mpegts-1.0 GStreamer MPEG-TS – GStreamer MPEG-TS support
  • gstreamer-pbutils-1.0 GStreamer Base Utils Library – General utility functions
  • gstreamer-vaapi-x11-1.0 GStreamer VA-API (X11) Plugins Libraries – Streaming media framework, VA-API (X11) plugins libraries
  • gstreamer-rtp-1.0 GStreamer RTP Library – RTP base classes and helper functions
  • gstreamer-rtsp-1.0 GStreamer RTSP Library – RTSP base classes and helper functions
  • gstreamer-riff-1.0 GStreamer RIFF Library – RIFF helper functions
  • gstreamer-audio-1.0 GStreamer Audio library – Audio helper functions and base classes
  • gstreamer-plugins-bad-1.0 GStreamer Bad Plugin libraries – Streaming media framework, bad plugins libraries
  • gstreamer-rtsp-server-1.0 gst-rtsp-server – GStreamer based RTSP server

At the time of writing this article Gstreamer an much early version in 1.X , which was newer than its then stable version 0.x. Since then the library has updated many fold. summarising release highlights for major versions as the blog was updated over time .

Ex1 : Capture video from your webcam and save it as an MP4 file

> v4l2-ctl --list-devices
Integrated_Webcam_HD: Integrate (usb-0000:00:14.0-5):
/dev/video0
gst-launch-1.0 v4l2src ! videoconvert ! x264enc ! mp4mux ! filesink location=webcam_output.mp4

Ex 2: Record Screen and Save as MP4

gst-launch-1.0 ximagesrc ! videoconvert ! x264enc ! mp4mux ! filesink location=screen_record.mp4

After interrupt

Project : Making and IP survillance system using gstreamer and Janus

To build a turn-key easily deployable surveillance solution 

Features :

  1. Paring of Android Mobile with box
  2. Live streaming from Box to Android
  3. Video Recording inside the  box
  4. Auto parsing of recorded video around motion detection 
  5. Event listeners 
  6. 2 way audio
  7. Inbuild Media Control Unit
  8. Efficient use of bandwidth 
  9. Secure session while live-streaming

Modules

  1. Authentication ( OTP / username- password)
  2. Livestreaming on Opus / vp8 
  3. Session Security and keepalives for live-streaming sessions
  4. Sync local videos to cloud storage 
  5. Record and playback with timeline and events 
  6. Parsing and restructuring video ( transcoding may also be required ) 
  7. Coturn server for NAT and ICE
  8. Web platform on box ( user interface )+ NoSQL
  9. Web platform on Cloud server ( Admin interface )+ NoSQL
  10.  REST APIs for third party add-ons ( Node based )
  11. Android demo app for receiving the live stream and feeds

Varrying experiments and working gstreamer commands

Local Network Stream 

To create /dev/video0

modprobe bcm2835-v4l2

To stream on rtspserver using rpicamsrc using h264 parse. Adjust the pipeline based on your specific requirements (e.g., resolution, bitrate, or codec).

./gst-rtsp-server-1.4.4/examples/test-launch --gst-debug=2 '(rpicamsrc num-buffers=5000 ! 'video/x-h264,width=1080,height=720,framerate=30/1' ! h264parse ! rtph264pay name=pay0 pt=96 )'

./test-launch “( tcpclientsrc host=127.0.0.1 port=5000 ! gdpdepay ! rtph264pay name=pay0 pt=96 )”

pipe raspivid to tcpserversink

raspivid -t 0 -w 800 -h 600 -fps 25 -g 5 -b 4000000 -vf -n -o - | gst-launch-1.0 -v fdsrc ! h264parse ! gdppay ! tcpserversink host=127.0.0.1 port=5000;

Stream Video over local Network with 15 fps

raspivid -n -ih -t 0 -rot 0 -w 1280 -h 720 -fps 15 -b 1000000 -o - | nc -l -p 5001

streaming video over local network with 30FPS and higher bitrate

raspivid -n -t 0 -rot 0 -w 1920 -h 1080 -fps 30 -b 5000000 -o - | nc -l -p 5001

Recording

Audio record to file
Using arecord :

arecord -D plughw:1 -c1 -r 48000 -f S16_LE -t wav -v file.wav;

Using pulse :
pulseAudio -D

gst-launch-1.0 -v pulsesrc device=hw:1 volume=8.0 ! audio/x-raw,format=S16LE ! audioconvert ! voaacenc bitrate=48000 ! aacparse ! flvmux ! filesink location = "testaudio.flv";

Video record to file ( mpg)

gst-launch-1.0 -e rpicamsrc bitrate=500000 ! 'video/x-h264,width=640,height=480’ ! mux. avimux name=mux ! filesink location=testvideo2.mpg;

Video record to file ( flv )

gst-launch-1.0 -e rpicamsrc bitrate=500000 ! video/x-h264,width=320,height=240,framerate=10/1 ! h264parse ! flvmux ! filesink location="testvieo.flv";

Video record to file ( h264)
gst-launch-1.0 -e rpicamsrc bitrate=500000 ! filesink location=”raw3.h264″;

Video record to file ( mp4)

gst-launch-1.0 -e rpicamsrc bitrate=500000 ! video/x-h264,width=320,height=240,framerate=10/1 ! h264parse ! mp4mux ! filesink location=video.mp4;

Audio + Video record to file ( flv)

gst-launch-1.0 -e /
rpicamsrc bitrate=500000 ! /
video/x-h264,width=320,height=240,framerate=10/1 ! h264parse ! muxout. /
pulsesrc volume=8.0 ! /
queue ! audioconvert ! voaacenc bitrate=65536 ! aacparse ! muxout. /
flvmux name=muxout streamable=true ! filesink location ='test44.flv';

Audio + Video record to file ( flv) using pulsesrc

gst-launch-1.0 -v --gst-debug-level=3 pulsesrc device="alsa_input.platform-asoc-simple-card.0.analog-stereo" volume=5.0 mute=FALSE ! audio/x-raw,format=S16LE,rate=48000,channels=1 ! audioresample ! audioconvert ! voaacenc ! aacparse ! flvmux ! filesink location="voicetest.flv";

Audio + Video record to file (mp4)

gst-launch-1.0 -e /
rpicamsrc bitrate=500000 ! /
video/x-h264,width=320,height=240,framerate=10/1 !s h264parse ! muxout. /
pulsesrc volume=4.0 ! /
queue ! audioconvert ! voaacenc ! muxout. /
flvmux name=muxout streamable=true ! filesink location = 'test224.mp4';

Streaming

stream raw Audio over RTMP to srtmpsink

gst-launch-1.0 pulsesrc device=hw:1 volume=8.0 ! /
audio/x-raw,format=S24LE ! audioconvert ! voaacenc bitrate=48000 ! aacparse ! flvmux ! rtmpsink location = “rtmp://192.168.0.3:1935/live/test”;

stream AACpparse Audio over RTMP to srtmpsink

gst-launch-1.0 -v --gst-debug-level=3 pulsesrc device="alsa_input.platform-asoc-simple-card.0.analog-stereo" volume=5.0 mute=FALSE ! audio/x-raw,format=S16LE,rate=48000,channels=1 ! audioresample ! audioconvert ! voaacenc ! aacparse ! flvmux ! rtmpsink location="rtmp://www.altani.com:1935/voice/1/test";

stream Video over RTMP

gst-launch-1.0 -e rpicamsrc bitrate=500000 ! /
video/x-h264,width=320,height=240,framerate=6/1 ! h264parse ! /
flvmux ! rtmpsink location = ‘rtmp://52.66.125.31:1935/live/test live=1’;

stream Audio + video over RTMP from rpicamsrc , framerate 10

gst-launch-1.0 rpicamsrc bitrate=500000 ! video/x-h264,width=320,height=240,framerate=10/1 ! h264parse ! muxout. pulsesrc volume=8.0 ! queue ! audioconvert ! voaacenc bitrate=65536 ! aacparse ! muxout. flvmux name=muxout streamable=true ! rtmpsink location ='rtmp://www.altanai.com/live/test44';

stream Audio + video over RTMP from rpicamsrc , framerate 30

gst-launch-1.0 rpicamsrc bitrate=500000 ! video/x-h264,width=1280,height=720,framerate=30/1 ! h264parse ! muxout. pulsesrc ! queue ! audioconvert ! voaacenc bitrate=65536 ! aacparse ! muxout. flvmux name=muxout ! queue ! rtmpsink location ='rtmp://www.altanai.com/live/test44';

VOD ( video On Demand )

Stream h264 file over RTMP

gst-launch-1.0 -e filesrc location="raw3.h264" ! video/x-h264 ! h264p
arse ! flvmux ! rtmpsink location = 'rtmp://www.altanai.com/live/test';

Stream flv file over RTMP

gst-launch-1.0 -e filesrc location=”testvieo.flv” ! /
video/x-h264,width=320,height=240,framerate=10/1 ! h264parse ! /
flvmux ! rtmpsink location = 'rtmp://192.168.0.3:1935/live/test';

Github Repo for Livestreaming

https://github.com/altanai/Livestreaming

Debugging Error while runnign Gstreamer pipeline

Keep Ubuntu updated

Keep Gstreamer and its plugin updated

sudo apt update
sudo apt install gstreamer1.0-tools gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly


References :

https://gstreamer.freedesktop.org