diff --git a/README.md b/README.md index 7a3577f..35a2f8c 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # Practical Assignment 1 **Dealine**: 27.09.2021 - -Please put your name here: -**Name:** ....... + +**Name: Al-ameen Mawji (Worked with Bishoy Roufael)** ## Problem 1.1 ### Calculate Frames-per-Second (FPS) (Points 30) 1. Fork the current repository diff --git a/src/cascade.cpp b/src/cascade.cpp new file mode 100644 index 0000000..80ad502 --- /dev/null +++ b/src/cascade.cpp @@ -0,0 +1,50 @@ +#include "opencv2/objdetect/objdetect.hpp" +#include "opencv2/highgui/highgui.hpp" +#include "opencv2/imgproc/imgproc.hpp" + +#include +#include + +using namespace std; +using namespace cv; + +int main() +{ + // Load Face cascade (.xml file) + CascadeClassifier face_cascade; + face_cascade.load("haarcascade_frontalface_alt.xml"); + if (!face_cascade.load("haarcascade_frontalface_alt.xml")) + { + cerr << "Error Loading XML file" << endl; + return 0; + } + + VideoCapture capture(0); + if (!capture.isOpened()) + throw "Error when reading file"; + namedWindow("window", 1); + for (;;) + { + Mat image; + capture >> image; + if (image.empty()) + break; + + /* + Detect Faces + */ + std::vector faces; + face_cascade.detectMultiScale(image, faces, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, Size(30, 30)); + + for (int i = 0; i < faces.size(); i++) + { + Point center(faces[i].x + faces[i].width * 0.5, faces[i].y + faces[i].height * 0.5); + ellipse(image, center, Size(faces[i].width * 0.5, faces[i].height * 0.5), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0); + } + + imshow("Detected Face using cascade", image); + waitKey(1); + } + + return 0; +} diff --git a/src/main.cpp b/src/main.cpp index 7b2d524..22b8042 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,18 @@ -#include "types.h" +#include +#include +#include +#include + +using namespace cv; + + +template +T average(std::vector const& v) { + if (v.empty()) { + return 0; + } + return std::accumulate(v.begin(), v.end(), 0.0) / v.size(); +} int main() { VideoCapture camera; @@ -6,13 +20,35 @@ int main() { printf("Can't find a camera\n"); return 1; }; - + // Main loop Mat img; - for(;;) { + std::vector fpsCont; + + long frameCounter = 0; + + std::time_t timeBegin = std::time(0); + int tick = 0; + + for (;;) { camera >> img; imshow("Camera", img); + int key = waitKey(5); + + frameCounter++; + fpsCont.push_back(frameCounter); + + std::time_t timeNow = std::time(0) - timeBegin; + if (timeNow - tick >= 1) + { + tick+=2; + std::cout << "Average Frames per second: " << average(fpsCont) << std::endl; + frameCounter = 0; + fpsCont.clear(); + } + + if (key == 27 || key == 'q') break; } camera.release();