The place where random ideas get written down and lost in time.

2020-06-05 - OpenCV and JavaCV

Category DEV

JavaCV: Used in Randall’s camera proxy app to capture data from an RTSP h264 / MJPEG feed, and produce jpeg images or publish an MJPEG web (via a Jetty web server).

OpenCV: for the Randall camera proxy project, need basic motion detection.

Some good tutorials here: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html

Example in Python. Java API available here: https://docs.opencv.org/master/javadoc/org/opencv/video/package-summary.html

And in JavaCV it shows how to convert frames between the FFMPEG input and what’s needed by the OpenCV library using “Mat” and an OpenCV converter.

Update 2020-06-09: The “cam-proxy” project is working nicely.

One realisation: I picked up the JavaCV approach as it seemed easier to get started, and that paid off. However it’s worth pointing out that the generated fatJar is an impressive 760 MB!

That is quite a feat.

Looking at the APIs, do I need all of JavaCV, or could I use only OpenCV with its Java bindings? Let’s see which APIs I’m using here:

  • JavaCV.FFMpegFrameGrabber ⇒ there’s an OpenCV specific one.
  • JavaCV.FFMpegFrameRecorder ⇒ to generate the MJPEG stream. Alternative?
  • JavaCV.2dFrameConverters + CanvasFrame ⇒ there’s an OpenCV one.
  • OpenCV core: IplImage, Size, Rect, Mat
  • OpenCV imgproc: resize
  • OpenCV video: cvCreateImage, mediamBlur, createBackgroundSubstractorMOG2.

  • Thus one question is whether it’s possible to generate the output stream (MJPEG or h264) using OpenCV and its Java wrappers, or is FFMpeg needed for that?
  • Uncompressed fatJar size:         2,762,758,664 bytes
  • org/ part of the jar:                 2,227,823,156 bytes
  • Opencv:                                1,065,264,936 bytes
  • lib/ part of the jar:                  629,358,581 bytes
  • Ffmpeg:                                  371,946,457 bytes
  • Javacpp:                                   10,261,365 bytes
  • Javacv:                                      921,733 bytes
  • Jetty part:                              318,8118 bytes

So essentially trying to only use OpenCV would likely not save that much. A huge part of the JavaCV package is indeed OpenCV and a quick look at the final Jar shows that it contains not only java wrappers but also hpp headers, some xml, and most important the jni+si native libs for a lot of architectures (e.g. armhf, armeabi-v7, arm64-v8a, x86, x86_64). If an effort was needed to trim the jar file, it could be by avoiding packaging these in the first place by removing all the arm ones (3 out of 5 archs).


 Generated on 2025-01-03 by Rig4j 0.1-Exp-f2c0035