This code is the first step towards a project I am working on that attempts to track an agent with a camera mounted to a gimbal. This code was written using C++ in Visual Studio 2019 along with OpenCV. I ran into a few issues with running this configuration, most notably that OpenCV is compatible with x64 based systems but my computer is based on the x86 processor architecture. I think that because of this, the code runs extremely slowly because it must be translated to run on the x86 architecture. Because of this, I think that I will do future computer vision projects in Python because it seems to run better for my specific hardware and software configuration.
This code is extremely simple and basically does the following:
1.) initialize the program and load the appropriate libraries
2.) open the webcam and read the input frame
3.) detect a face in that frame using the pretrained model
4.) draw bounding boxes on the image
5.) output the modified frame to the user
6.) wait 1 ms, then close the frame
7.) iterate from step 3 until the user exits the program
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/objdetect.hpp>
#include <iostream>
using namespace cv;
using namespace std;
//////////////// Webcam Face Tracking /////////////////
void main() {
VideoCapture cap(0);
Mat img;
while (true) {
cap.read(img);
CascadeClassifier faceCascade;
faceCascade.load("Resources/haarcascade_frontalface_default.xml");
vector<Rect> faces;
faceCascade.detectMultiScale(img, faces, 1.1, 10);
for (int i = 0; i < faces.size(); i++) {
rectangle(img, faces[i].tl(), faces[i].br(), Scalar(255, 0, 255), 3);}
imshow("Image", img);
waitKey(1);
}
}