Opencv3.4.0 realizes the function of saving frames as pictures in video

Time:2019-8-7

Now there is a need to collect faces, with only one person per photo. My solution is to collect and record a video and upload it to the PC. Use OpenCV on PC to save every frame in the picture as a JPG picture.

The following is the code:

import time

import cv2

if __name__ == '__main__':
  # Fill in the Absolute Path of Video
  VideoCapture ('/home/shushi/video/hunting ground.mp4')
  success, image = vidcap.read()
  start_time = time.time()
  print(start_time)

  while success:
    end_time = time.time()
    file_name = str(end_time).replace('.', '')
    # Screen capture every three seconds
    if 3 == int(end_time - start_time):
      start_time = end_time
      # Absolute path to save JGP
      cv2.imwrite('/home/shushi/video/' + file_name + ".jpg", image) # save frame as JPEG file
    success, image = vidcap.read()
    if cv2.waitKey(10) == 27: # exit if Escape is hit
      break

Put the video in the same directory as the Python file, and then run the program, you will get the JPG image corresponding to the video frame:

The editor shares another piece of code for you: OpenCV intercepts video frames by Haku_yyf

#include <opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>
 
 
int main(int argc, char **argv)
 
{
 
 // Open Video File
 
 cv::VideoCapture cap("E:\\New folder\\DSC_3543.MOV");
 
 if (!cap.isOpened())
 {
 Std:: cout < < can't open video file < < std:: endl;
 return -1;
 }
 
 // Play video from 3000ms 
 //cap.set(CV_CAP_PROP_POS_MSEC, 3000);
 // Getting Frame Rate of Video
 
 double fps = cap.get(cv::CAP_PROP_FPS);
 double width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
 double height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
 
 //std::cout << fps << std::endl;
 //std::cout << width << std::endl;
 //std::cout << height << std::endl;
 
 
 cv::VideoWriter wrt("C:\\Users\\Administrator\\Desktop\.avi", CV_FOURCC('M', 'J', 'P', 'G'), fps, cv::Size(width, height));
 int cout = 0;
 while (true)
 
 {
 
 cv::Mat frame;
 
 // Read a frame from a video
 
 bool bSuccess = cap.read(frame);
 ++cout;
 if (!bSuccess)
 
 {
 
  Std:: cout < < can't read frames from video files < < std:: endl;
 
  break;
 
 }
 
 // Display the current frame in the MyVideo window
 If (cout > = FPS * 1 & & cout < = FPS * 56) // Take video content for 1 - 56 seconds
 {
  Std:: cout < < "intercepting..."< < (int)((cout-fps)/(fps * (56-1)))* 100)< < "%"< < std:: endl;
  wrt << frame;
 }
 
 
 if (cout>fps*57)
 {
  std::cout << "ENd!!!" << std::endl;
  break;
 }
 
 }
 system("pause");
 return 0;
 
}

The above is the whole content of this article. I hope it will be helpful to everyone’s study, and I hope you will support developpaer more.