{"id":21802493,"url":"https://github.com/qengineering/multithread-camera-opencv","last_synced_at":"2025-08-17T05:05:46.650Z","repository":{"id":165981866,"uuid":"624440235","full_name":"Qengineering/Multithread-Camera-OpenCV","owner":"Qengineering","description":"OpenCV Universal Multi thread video Interface with neglectable latency.","archived":false,"fork":false,"pushed_at":"2024-03-21T09:07:20.000Z","size":35,"stargazers_count":28,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-13T18:55:15.446Z","etag":null,"topics":["cpp","ffmpeg","gstreamer","jetson-nano","multi-camera","multithreading","no-lag","opencv","raspberry-pi","rtsp","rtsp-client","surveillance","surveillance-camera","tcp","tcp-client","udp","udp-client"],"latest_commit_sha":null,"homepage":"https://qengineering.eu/install-gstreamer-1.18-on-raspberry-pi-4.html","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Qengineering.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-04-06T13:25:16.000Z","updated_at":"2025-01-30T18:02:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"f3ac88a2-1f32-40c2-bd5d-b6faf9cef4ec","html_url":"https://github.com/Qengineering/Multithread-Camera-OpenCV","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Qengineering/Multithread-Camera-OpenCV","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FMultithread-Camera-OpenCV","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FMultithread-Camera-OpenCV/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FMultithread-Camera-OpenCV/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FMultithread-Camera-OpenCV/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Qengineering","download_url":"https://codeload.github.com/Qengineering/Multithread-Camera-OpenCV/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FMultithread-Camera-OpenCV/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270807934,"owners_count":24649346,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","ffmpeg","gstreamer","jetson-nano","multi-camera","multithreading","no-lag","opencv","raspberry-pi","rtsp","rtsp-client","surveillance","surveillance-camera","tcp","tcp-client","udp","udp-client"],"created_at":"2024-11-27T11:29:05.258Z","updated_at":"2025-08-17T05:05:46.628Z","avatar_url":"https://github.com/Qengineering.png","language":"C++","readme":"# Multithread cameras in OpenCV (with neglectable latency)\n![output image]( https://qengineering.eu/github/MultiCamCodeBlocks.webp )\u003cbr\u003e\n### RTSP - UDP - TCP streams \nIt is a known issue with RTSP streams and time-consuming algorithms such as deep learning frameworks. You're getting out of sync if individual frames take longer than your stream's frame rate to process. There is an increasing delay between reality and the captured images.\u003cbr\u003e\u003cbr\u003e\nThere are two possible solutions to this problem.\u003cbr\u003e\n* You could determine the missed images and skipping them before grabbing a new frame. This solution is [implemented here](https://github.com/Qengineering/RTSP-with-OpenCV).\u003cbr\u003e\n* Or you can continuously grabbing images in a seperated thread. This way, it is possible to connect more than one camera to your application.\u003cbr\u003e\u003cbr\u003e\n\nThe code speaks for itself. You can either use GStreamer of FFmpeg to open the stream.\u003cbr\u003e\nThe number of cameras connected is not limited. It could be even just one camera.\u003cbr\u003e\nHowever, the more cameras, the more CPU time is needed to visit all seperate threads.\u003cbr\u003e\n\n------------\n\n## Dependencies.\nTo run the application, you have to:\n- OpenCV installed [32-bit](https://qengineering.eu/install-opencv-4.5-on-raspberry-pi-4.html) or [64-bit](https://qengineering.eu/install-opencv-4.5-on-raspberry-64-os.html) OS\u003cbr/\u003e\n\n------------\n\n## Build project\nCreate build path and build project with cmake as follows:\n```\nrm -rf build \u0026\u0026 mkdir -p build \u0026\u0026 cd build\ncmake .. \u0026\u0026 make\n```\n\nAlternatively, you can also build the project using Code::Blocks to which you need to install it (```$ sudo apt-get install codeblocks```).\n\n------------\n\n## Tips.\nUse only a ***wired*** Ethernet connection for your RTSP stream. Wi-Fi can be unstable.\u003cbr\u003e\nBecause the RTSP protocol is sensitive to even just one missing frame, the stream can easily crash.\u003cbr\u003e\u003cbr\u003e\nIf you are using the stream in a deep learning app, adjust your resolution and frame rate to the requirements of the deep learning model.\u003cbr\u003e\nIt is not a good idea to send a 1280x960 stream at 30 FPS if your model has a 416x416 input and takes 200 mSec to process a single frame. It only costs extra memory and processing power.\u003cbr\u003e\u003cbr\u003e\nMake sure all cameras have the same resolution. In the example, the OpenCV concatenate algorithm is sensitive to different resolutions. It throws an exception when it faces different widths or heights.\u003cbr\u003e\u003cbr\u003e\nPlease note that the cameras are not synchronized with each other by the threads. They may have slightly different lags.\u003cbr\u003e\u003cbr\u003e\nIf you want to stream UDP or TCP, are sure the streams work with the command line prompt beforehand. If not, they certainly won't work in OpenCV. Often errors are caused by the coding in the pipeline, the addresses or missing modules. If you need to install additional GStreamer modules, you'll need to rebuild your OpenCV also! For more information, see our [website](https://qengineering.eu/install-gstreamer-1.18-on-raspberry-pi-4.html).\n\n------------\n\n## Running the app.\nTo run the application load the project file MultiThread.cbp in Code::Blocks.\u003cbr\u003e\u003cbr\u003e \nIf you are using a **Jetson Nano**, you have to change the location where OpenCV has stored its header files to `/usr/include/opencv4`\u003cbr\u003e\u003cbr\u003e\nAt line 23 in main.cpp the stream is defined.\n#### RTSP\n```\nGrb1-\u003eInit(\"rtsp://192.168.178.129:8554/test/\");\n```\n#### UDP\n```\nGrb1-\u003eInit(\"udpsrc port=5200 ! application/x-rtp, media=video, clock-rate=90000, payload=96 ! rtpjpegdepay ! jpegdec ! videoconvert ! appsink\", cv::CAP_GSTREAMER);\n```\nSender: RaspiCam with Raspberry Pi **Buster** OS\u003cbr\u003e\n`gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=-1 ! video/x-raw, width=640, height=480, framerate=30/1 ! videoconvert ! jpegenc ! rtpjpegpay ! udpsink host=192.168.178.84 port=5200`\u003cbr\u003e\u003cbr\u003e\nSender: RaspiCam with Raspberry Pi **Bullseye** OS\u003cbr\u003e\n`gst-launch-1.0 -v libcamerasrc ! video/x-raw, width=640, height=480, framerate=30/1 ! videoconvert ! jpegenc ! rtpjpegpay ! udpsink host=192.168.178.84 port=5200`\u003cbr\u003e\u003cbr\u003e\nNote, `host=192.168.178.84` being the IP address of the receiver.\n\n#### TCP\n```\nGrb1-\u003eInit(\"tcpclientsrc host=192.168.178.129 port=5000 ! jpegdec ! videoconvert ! appsink\", cv::CAP_GSTREAMER);\n```\nSender: RaspiCam with Raspberry Pi **Buster** OS\u003cbr\u003e\n`gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=-1 ! video/x-raw,width=640,height=480, framerate=30/1 ! videoconvert ! jpegenc ! tcpserversink  host=192.168.178.32 port=5000`\u003cbr\u003e\u003cbr\u003e\nSender: RaspiCam with Raspberry Pi **Bullseye** OS\u003cbr\u003e\n`gst-launch-1.0 -v libcamerasrc ! video/x-raw,width=640,height=480, framerate=30/1 ! videoconvert ! jpegenc ! tcpserversink  host=192.168.178.32 port=5000`\u003cbr\u003e\u003cbr\u003e\nNote, `host=192.168.178.32` being the IP address of the sender.\n\n#### RaspiCam (Bullseye)\n```\nGrb1-\u003eInit(\"libcamerasrc ! video/x-raw, width=640, height=480, framerate=30/1 ! videoconvert ! videoscale ! video/x-raw, width=640, height=480 ! appsink\", cv::CAP_GSTREAMER);\n```\n#### RaspiCam (Buster)\n```\nGrb1-\u003eInit(\"v4l2src device=/dev/video0 ! video/x-raw, width=640, height=480, framerate=30/1 ! videoconvert ! videoscale ! video/x-raw, width=640, height=480 ! appsink\", cv::CAP_GSTREAMER);\n```\n#### RaspiCam (only Buster or Bullseye legacy stack)\n```\nGrb1-\u003eInit(0);\n```\n#### Webcam (only Buster OS or Bullseye legacy stack)\n```\nGrb1-\u003eInit(0); //if RaspiCam is not connected\nGrb2-\u003eInit(1); //if RaspiCam is connected\n```\n------------\n[![paypal](https://qengineering.eu/images/TipJarSmall4.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=CPZTM5BB3FCYL) \n","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=CPZTM5BB3FCYL"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqengineering%2Fmultithread-camera-opencv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqengineering%2Fmultithread-camera-opencv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqengineering%2Fmultithread-camera-opencv/lists"}