OpenCV take snapshot when face recognation

Hello. Please tell me how to organize the creation of screenshots in my code when a face is successfully detected in c ++. I am new to this, so please help. The recognition code I am using is:

#include <opencv2/opencv.hpp>
#include <opencv2/face.hpp>
#include <opencv2/imgcodecs.hpp>
#include "drawLandmarks.hpp"
using namespace std;
using namespace cv;
using namespace cv::face;

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
    // 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
      // 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 successful, render the landmarks on the face
        for(int i = 0; i < landmarks.size(); i++)
          drawLandmarks(frame, landmarks[i]);
      // Display results
      imshow("Facial Landmark Detection", frame);
      // Exit loop if ESC is pressed
      if (waitKey(1) == 27) break;
    return 0;


why do you say “screenshots” and “snapshots”? those words don’t make sense in this context.

your code is from a tutorial or other source. which source is it?

how is this post related to the other one?

In my understanding, I do not see the difference between these terms

if you want to save an image to file, use cv::imwrite.