計算機視覺實例應用
基于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;
}