-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Comments to explain this code for normies
- Loading branch information
1 parent
c71d413
commit 2a461f2
Showing
1 changed file
with
27 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,70 @@ | ||
#include <opencv2/opencv.hpp> | ||
#include <opencv2/dnn.hpp> | ||
#include <iostream> | ||
#include <opencv2/opencv.hpp> // OpenCV library for computer vision | ||
#include <opencv2/dnn.hpp> // OpenCV deep neural networks module | ||
#include <iostream> // Standard input-output library | ||
|
||
using namespace cv; | ||
using namespace cv::dnn; | ||
using namespace std; | ||
using namespace cv; // OpenCV namespace | ||
using namespace cv::dnn; // OpenCV DNN namespace | ||
using namespace std; // Standard C++ namespace | ||
|
||
// Path to the pre-trained model configuration and binary files | ||
const string MODEL_CONFIG = "deploy.prototxt.txt"; | ||
const string MODEL_BINARY = "res10_300x300_ssd_iter_140000.caffemodel"; | ||
|
||
// Function to detect faces in live video | ||
void detectFaces() { | ||
// Load the pre-trained model for face detection | ||
Net net = readNetFromCaffe(MODEL_CONFIG, MODEL_BINARY); | ||
|
||
// Access the default camera for video capture | ||
VideoCapture video(0); | ||
if (!video.isOpened()) { | ||
if (!video.isOpened()) { // Check if the camera couldn't be opened | ||
cerr << "Error: Couldn't open the camera." << endl; | ||
return; | ||
} | ||
|
||
Mat frame; | ||
Mat frame; // Variable to store video frames | ||
|
||
while (true) { | ||
video >> frame; | ||
if (frame.empty()) { | ||
while (true) { // Continuously process video frames | ||
video >> frame; // Capture a frame from the camera | ||
if (frame.empty()) { // Check if the frame is empty | ||
cerr << "Error: Couldn't capture frame." << endl; | ||
break; | ||
} | ||
|
||
// Prepare the frame for neural network input | ||
Mat blob = blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123), false, false); | ||
net.setInput(blob); | ||
Mat detections = net.forward(); | ||
net.setInput(blob); // Set the frame as input to the neural network | ||
Mat detections = net.forward(); // Perform face detection using the neural network | ||
|
||
// Iterate through detected faces and draw rectangles around high-confidence faces (Highly technical stuff) DO NOT TOUCH!!!!!!!!! | ||
for (int i = 0; i < detections.size[2]; ++i) { | ||
float* dataPtr = detections.ptr<float>(0); | ||
float detectionConfidence = dataPtr[i * 7 + 2]; | ||
|
||
if (detectionConfidence > 0.5) { | ||
if (detectionConfidence > 0.5) { // Check face detection confidence threshold | ||
// Extract coordinates for drawing the rectangle around the face of the person | ||
int x1 = static_cast<int>(dataPtr[i * 7 + 3] * frame.cols); | ||
int y1 = static_cast<int>(dataPtr[i * 7 + 4] * frame.rows); | ||
int x2 = static_cast<int>(dataPtr[i * 7 + 5] * frame.cols); | ||
int y2 = static_cast<int>(dataPtr[i * 7 + 6] * frame.rows); | ||
|
||
Rect faceRect(x1, y1, x2 - x1, y2 - y1); | ||
Rect faceRect(x1, y1, x2 - x1, y2 - y1); // Rectangle around the face cordinates | ||
|
||
rectangle(frame, faceRect, Scalar(0, 255, 0), 2); | ||
rectangle(frame, faceRect, Scalar(0, 255, 0), 2); // Draw the rectangle on the frame | ||
} | ||
} | ||
|
||
imshow("Face Detection", frame); | ||
imshow("Face Detection", frame); // Display the processed frame | ||
|
||
if (waitKey(30) == 27) { | ||
if (waitKey(30) == 27) { // Check for 'Esc' key press to exit the program | ||
cout << "Exiting program." << endl; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Main function | ||
int main() { | ||
detectFaces(); | ||
detectFaces(); // Call the function to detect faces in live video | ||
return 0; | ||
} | ||
|