Skip to content

Instantly share code, notes, and snippets.

@ThakurSarveshGit
Last active May 2, 2024 17:19
Show Gist options
  • Save ThakurSarveshGit/c5399fa3f363e1b2f8fa763dab9a0329 to your computer and use it in GitHub Desktop.
Save ThakurSarveshGit/c5399fa3f363e1b2f8fa763dab9a0329 to your computer and use it in GitHub Desktop.
Boiler plate code for Multithreading camera(s) for faster FPS. Check writeup here:
/*
* Boilerplate code for handling cameras for computer vision tasks.
* Date Created: 2/4/2020
* Author: Sarvesh Thakur
*/
#include "Interaction.h"
int main() {
// Create instance of Interaction class.
Interaction* interact = new Interaction("ConfigurationFile.txt"); // @args: PATH_CONFIG file
// Use this section for multithreading camera(s)
std::thread c1(&Interaction::Acquisition, interact, true); // @args: (bool)checkFPS
std::thread p1(&Interaction::Processing, interact);
c1.join();
p1.join();
return 0;
}
CAM_ID 0
RES_X 1024
RES_Y 576
CAP_PROP_BRIGHTNESS -64
CAP_PROP_SATURATION 10
CAP_PROP_HUE 0
CAP_PROP_FRAME_WIDTH 1024
CAP_PROP_FRAME_HEIGHT 576
CAP_PROP_GAIN 0
CAP_PROP_FPS 60
CAP_PROP_AUTO_EXPOSURE 0
CAP_PROP_EXPOSURE -6
MJPEG 1
FPS_THRESHOLD 100
/*
* Interaction.cpp : Initializes the camera, starts the capturing and the processing algorithm.
* Date Created: 2/4/2020
* Author: Sarvesh Thakur
*/
#include "Interaction.h"
Interaction::Interaction(std::string configFile) {
this->configFilePath = configFile;
this->readConfigurationFile();
this->initializeCameras(paramDict["CAM_ID"]);
}
void Interaction::readConfigurationFile() {
std::ifstream file(this->configFilePath);
std::string parameterName = "";
int parameterValue = 0;
while (file >> parameterName >> parameterValue) {
paramDict.insert({ parameterName, parameterValue });
}
std::cout << "[INTERACTION] : [readConfigurationFile] : " << paramDict.size() << " parameters read for param Dictionary." << std::endl;
}
void Interaction::initializeCameras(int ID) {
this->newFrameAvailable = false;
this->camera.open(ID, cv::CAP_DSHOW);
if (this->paramDict["MJPEG"] == 1) {
camera.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('m', 'j', 'p', 'g'));
camera.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'));
std::cout << "[INTERACTION] : [initializeCameras] : CAM Frame read in MJPEG format : (MJPEG = " << paramDict["MJPEG"] << ")" << std::endl;
}
else {
std::cout << "[INTERACTION] : [initializeCameras] : CAM Frame read in YUY2 format : (MJPEG = " << paramDict["MJPEG"] << ")" << std::endl;
}
camera.set(cv::CAP_PROP_BRIGHTNESS, this->paramDict["CAP_PROP_BRIGHTNESS"]);
camera.set(cv::CAP_PROP_SATURATION, this->paramDict["CAP_PROP_SATURATION"]);
camera.set(cv::CAP_PROP_HUE, this->paramDict["CAP_PROP_HUE"]);
camera.set(cv::CAP_PROP_FRAME_WIDTH, this->paramDict["RES_X"]);
camera.set(cv::CAP_PROP_FRAME_HEIGHT, this->paramDict["RES_Y"]);
camera.set(cv::CAP_PROP_GAIN, this->paramDict["CAP_PROP_GAIN"]);
//this->camera.set(cv::CAP_PROP_FPS, this->paramDict["CAP_PROP_FPS"]); // In opencv api for windows, this does not seems to work. In linux, it works fine.
camera.set(cv::CAP_PROP_EXPOSURE, this->paramDict["CAP_PROP_EXPOSURE"]);
std::cout << "[INTERACTION] : [initializeCameras] : CAM Initialized : " << ID << std::endl;
}
void Interaction::Acquisition(bool checkFPS) {
if (!(camera.isOpened())) {
this->newFrameAvailable = false;
std::cout << "[INTERACTION] : [Acquisition] : Cannot open camera"<< std::endl;
system("pause");
std::exit(EXIT_FAILURE);
}
time(&currentTime);
while (camera.isOpened()) {
camera.read(frame);
this->newFrameAvailable = true;
this->framesCaptured++;
if (checkFPS && this->framesCaptured % paramDict["FPS_THRESHOLD"] == 0) {
time_t timeNow;
time(&timeNow);
std::cout << "FPS: " << (float)paramDict["FPS_THRESHOLD"] / (float)difftime(timeNow, currentTime) << "\n";
time(&currentTime);
}
}
}
void Interaction::Processing() {
while (true) {
if (this->newFrameAvailable) {
this->newFrameAvailable = false;
cv::imshow("Frame", frame);
cv::waitKey(1);
}
}
}
/*
* Interaction.h : Initializes the camera, starts the capturing and the processing algorithm.
* Date Created: 2/4/2020
* Author: Sarvesh Thakur
*/
#pragma once
#include <string>
#include <fstream>
#include <cstdlib>
#include <iostream>
#include <unordered_map>
#include <windows.h>
#include <opencv2/opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
class Interaction{
public:
Interaction(std::string configFile);
void readConfigurationFile();
void initializeCameras(int ID);
void Acquisition(bool checkFPS);
void Processing();
private:
cv::VideoCapture camera;
cv::Mat frame;
bool newFrameAvailable;
int framesCaptured;
std::string configFilePath;
std::unordered_map<std::string, int> paramDict;
time_t currentTime;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment