Extract frames from video using Python threads and OpenCV

In my research I wanted to process each frame of a given video individually and also know some details about it. Here is the code I created to achieve it. The code in python will extract all the frames of the video and store it in a folder given as parameter. Im using OpenCV to get the video details and extract the frames and threads to do it quickly.

#!/usr/bin/python

import time
import cv2
import threading
import imutils


class extraction :
      def__init__(self , location , video ):
              self.location = location
              self.cap = cv2.VideoCapture( video )
              self.video_length = int( self.cap.get(cv2.CAP_PROP_FRAME_COUNT )) - 1
              self.video_fps = round( self.cap.get( cv2.CAP_PROP_FPS ),2)
              self.video_time = round(( self.video_length / self.video_fps) ,2)
              self.video_resolution = str( self.cap .get( cv2.CAP_PROP_FRAME_WIDTH )) + "/" + str ( self.cap.get( v2.CAP_PROP_FRAME_HEIGHT ))

      def saveImages(self , imagename , frame ):
             t = threading.Thread( target = cv2.imwrite , args =(imagename ,frame ,))
             t. start()

      def info( self ):
            print("[ INFO ]")
            print(" - Video Reproduction time : %s seconds ") %( self.video_time )
            print(" - Frame Rate : %s/fps ") %( self.video_fps )
            print(" - Number of frames : %s") %( self.video_length )
            print(" - Video Resolution : %s") %( self.video_resolution )

      def extract( self ):
            # Log the time
            time_start = time.time()
            count = 0
            # Start converting the video
            print("[ INFO ]")
            print(" - Starting frames extraction ")
            while self.cap.isOpened():
                  ret , frame = self.cap.read() # Extracting Frames
                  frame_time = round(( self . cap .get ( cv2. CAP _ PROP _ POS _MSEC ) /1000) ,2)
                  frame_name = self.location + " /%#05 d_%s. jpg " % ( count +1, frame _ time )
                  frame_small = imutils.resize(frame , width =640) # Resizing the frame to save space and processing


                  # Calling function to save images in folder
                  self.saveImages( frame_name , frame_small )
                  count = count + 1


                  # If there are no more frames left
                  video_length = int( self.cap.get( cv2.CAP_PROP_FRAME_COUNT )) - 1
                  if ( count > ( video_length - 1)):
                     # Log the time again
                     time_end = time.time ()

 

Usage:


a = extraction(LOCATIONPATH, VIDEOFILE)
a.info()
a.extract()

Uma resposta para “Extract frames from video using Python threads and OpenCV”

  1. […] writing for my master I wrote a series of codes in python to achieve what I wanted, such asslicing the video in small frames, generate the hash for each image and compare the hamming distance of each hach. Here is the piece […]

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *