识别人脸时如何使用 OpenCV 拍摄快照?

时间:2021-02-13 11:17:32

标签: c++ opencv

如何在成功检测到人脸时组织自动创建屏幕截图? OpenCV 成功选择了要看到的人脸。现在我想让它截取脸部的截图。

这是我的人脸识别代码:


int main(int argc,char** argv)
{
    // Load Face Detector
    CascadeClassifier faceDetector("haarcascade_frontalface_alt2.xml");
    // Create an instance of Facemark
    Ptr<Facemark> facemark = FacemarkLBF::create();
    // Load landmark detector
    facemark->loadModel("untitled/lbfmodel.yaml");
    // Set up webcam for video capture
    VideoCapture cam("myvideo.mp4");
    // Variable to store a video frame and its grayscale
    Mat frame, gray;
    // Read a frame
    while(cam.read(frame))
    {
      // Find face
      vector<Rect> faces;
      // Convert frame to grayscale because
      // faceDetector requires grayscale image.
      cvtColor(frame, gray, COLOR_BGR2GRAY);

      // Detect faces
      faceDetector.detectMultiScale(gray, faces);
      // Variable for landmarks.
      // Landmarks for one face is a vector of points
      // There can be more than one face in the image. Hence, we
      // use a vector of vector of points.
      vector< vector<Point2f> > landmarks;
      // Run landmark detector
      bool success = facemark->fit(frame,faces,landmarks);
      if(success)
      {
        // If successful, render the landmarks on the face
        for(int i = 0; i < landmarks.size(); i++)
        {
          drawLandmarks(frame, landmarks[i]);
          frame.imwrite("smile_aa.jpg");
        }
      }

      // Display results
      imshow("Facial Landmark Detection", frame);
      // Exit loop if ESC is pressed
      if (waitKey(1) == 27) break;
    }
    return 0;
}

0 个答案:

没有答案