计算机视觉实例应用
基于C++的人脸识别实例
以下是一些基于C++的人脸识别实例的示例和实现方法,涵盖了多种技术和库的应用。这些例子可以帮助开发者快速上手并实现人脸识别功能。
OpenCV 基础人脸检测
使用OpenCV的预训练模型进行人脸检测是入门级示例。OpenCV自带Haar级联分类器,适合快速实现。
#include <opencv2/opencv.hpp>
using namespace cv;int main() {CascadeClassifier faceCascade;faceCascade.load("haarcascade_frontalface_default.xml");VideoCapture cap(0);Mat frame;while (cap.read(frame)) {std::vector<Rect> faces;faceCascade.detectMultiScale(frame, faces);for (const auto& face : faces) {rectangle(frame, face, Scalar(255, 0, 0), 2);}imshow("Face Detection", frame);if (waitKey(1) == 27) break;}return 0;
}
Dlib 68点人脸特征检测
Dlib库提供了更精确的人脸特征点检测功能,适合需要高精度定位的应用。
#include <dlib/opencv.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing.h>
using namespace dlib;int main() {frontal_face_detector detector = get_frontal_face_detector();shape_predictor predictor;deserialize("shape_predictor_68_face_landmarks.dat") >> predictor;cv::VideoCapture cap(0);cv::Mat frame;while (cap.read(frame)) {cv_image<bgr_pixel> cimg(frame);std::vector<rectangle> faces = detector(cimg);for (const auto& face : faces) {full_object_detection shape = predictor(cimg, face);for (unsigned int i = 0; i < shape.num_parts(); ++i) {cv::circle(frame, cv::Point(shape.part(i).x(), shape.part(i).y()), 2, cv::Scalar(0, 255, 0), -1);}}cv::imshow("Landmark Detection", frame);if (cv::waitKey(1) == 27) break;}return 0;
}
深度学习人脸识别 (OpenCV DNN模块)
使用OpenCV的DNN模块加载深度学习模型(如Caffe或TensorFlow模型)进行人脸识别。
#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace dnn;int main() {Net net = readNetFromCaffe("deploy.prototxt", "res10_300x300_ssd_iter_140000.caffemodel");VideoCapture cap(0);Mat frame;while (cap.read(frame)) {Mat blob = blobFromImage(frame, 1.0, Size(300, 300), Scalar(104, 177, 123));net.setInput(blob);Mat detections = net.forward();for (int i = 0; i < detections.size[2]; ++i) {float confidence = detections.at<float>(0, 0, i, 2);if (confidence > 0.5) {int x1 = static_cast<int>(detections.at<float>(0, 0, i, 3) * frame.cols);int y1 = static_cast<int>(detections.at<float>(0, 0, i, 4) * frame.rows);int x2 = static_cast<int>(detections.at<float>(0, 0, i, 5) * frame.cols);int y2 = static_cast<int>(detections.at<float>(0, 0, i, 6) * frame.rows);rectangle(frame, Point(x1, y1), Point(x2, y2), Scalar(0, 255, 0), 2);}}imshow("Deep Learning Face Detection", frame);if (waitKey(1) == 27) break;}return 0;
}
实时人脸识别 (Face Recognition库)
使用Python的face_recognition库结合C++实现实时人脸识别。
#include <Python.h>
#include <opencv2/opencv.hpp>
using namespace cv;int main() {Py_Initialize();PyRun_SimpleString("import face_recognition");PyRun_SimpleString("import cv2");VideoCapture cap(0);Mat frame;while (cap.read(frame)) {imwrite("temp.jpg", frame);PyRun_SimpleString("image = face_recognition.load_image_file('temp.jpg')");PyRun_SimpleString("face_locations = face_recognition.face_locations(image)");PyObject* face_locations = PyObject_GetAttrString(PyImport_AddModule("__main__"), "face_locations");if (face_locations && PyList_Check(face_locations)) {for (Py_ssize_t i = 0; i < PyList_Size(face_locations); ++i) {PyObject* location = PyList_GetItem(face_locations, i);if (PyTuple_Check(location) && PyTuple_Size(location) == 4) {int top = PyLong_AsLong(PyTuple_GetItem(location, 0));int right = PyLong_AsLong(PyTuple_GetItem(location, 1));int bottom = PyLong_AsLong(PyTuple_GetItem(location, 2));int left = PyLong_AsLong(PyTuple_GetItem(location, 3));rectangle(frame, Point(left, top), Point(right, bottom), Scalar(0, 255, 0), 2);}}}imshow("Face Recognition", frame);if (waitKey(1) == 27) break;}Py_Finalize();return 0;
}
人脸识别与追踪
结合人脸检测和追踪算法,实现高效的人脸追踪功能。
#include <opencv2/opencv.hpp>
#include <opencv2/tracking.hpp>
using namespace cv;int main() {CascadeClassifier faceCascade;faceCascade.load("haarcascade_frontalface_default.xml");Ptr<Tracker> tracker = TrackerKCF::create();VideoCapture cap(0);Mat frame;bool tracking = false;Rect2d faceRect;while (cap.read(frame)) {if (!tracking) {std::vector<Rect> faces;faceCascade.detectMultiScale(frame, faces);if (!faces.empty()) {faceRect = faces[0];tracker->init(frame, faceRect);tracking = true;}} else {tracker->update(frame, faceRect);rectangle(frame, faceRect, Scalar(255, 0, 0), 2);}imshow("Face Tracking", frame);if (waitKey(1) == 27) break;}return 0;
}