Thursday, November 14, 2019

Frame Extraction From Video @x fps (Frame Per Second) In Python

Video is collection of Frames . In this article we will know how to extract frames from video @x fps(Frame per second) using Python . 

A frame is one of the many still images which compose the complete moving picture . When the moving picture is displayed, each frame is flashed on a screen for a short time (nowadays, usually 1/24, 1/25 or 1/30 of a second) and then immediately replaced by the next one

The frame is also sometimes used as a unit of time, so that a momentary event might be said to last six frames, the actual duration of which depends on the frame rate of the system, which varies according to the video or film standard in use. In North America and Japan, 30 frames per second (fps) is the broadcast standard, with 24 frames/s now common in production for high-definition video shot to look like film. In much of the rest of the world, 25 frames/s is standard.

Below code is extracting frame from video @ 2 fps .

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 14 09:47:58 2019

@author: Jo
"""
#Doing with open cv2
#Import Cv2 library
import cv2,os
from moviepy.editor import VideoFileClip

inputfilepath="Dataset/vid1.mov"

#For saving output 
if not os.path.exists('Frames'):
    os.makedirs('Frames')

#location of video file
video=cv2.VideoCapture(inputfilepath)

#For Getting Clip duration
clip = VideoFileClip(inputfilepath)
print(clip.duration,"seconds")

def extractframe(sec):
    # cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000) is responsible for
    #skipping directly to the sec in the video (sec*1000th millisecond)
    video.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
    #reading frame
    hasframes,image = video.read()
       
    if hasframes:
        #Write to location , increasing the count to avoid name conflict of images
        #
        cv2.imwrite("Frames/image"+str(count)+".jpg", image)  # save frame as JPG file
      
    return hasframes

#starting from 0th second
sec = 0

#Setting fps , here it will capture image in each 0.5 second , 2fps
frameRate = 0.5
count=1

#to check whether frame are their in video or not.
success = extractframe(sec)
while success:
    #increasing counter to name conflick
    count = count + 1
    #setting sec
    sec = sec + frameRate
    sec = round(sec, 2)
    print(sec)
    success = extractframe(sec)

      
Code at Github
*********************************************
Next Article :
Frame Extraction From Multiple Videos @x fps (Frame Per Second) In Python

1 comment:

  1. Do you realize there's a 12 word sentence you can say to your man... that will induce intense feelings of love and impulsive appeal to you deep inside his chest?

    Because deep inside these 12 words is a "secret signal" that triggers a man's instinct to love, treasure and care for you with his entire heart...

    ====> 12 Words Will Trigger A Man's Desire Instinct

    This instinct is so hardwired into a man's brain that it will make him work harder than ever before to make your relationship as strong as it can be.

    Matter of fact, fueling this dominant instinct is so binding to getting the best possible relationship with your man that the moment you send your man a "Secret Signal"...

    ...You'll instantly notice him expose his heart and mind to you in a way he's never expressed before and he'll perceive you as the only woman in the world who has ever truly understood him.

    ReplyDelete

Behavior Recognition System Based on Convolutional Neural Network

Our this article is on this  research paper .  Credit : Bo YU What we will do ? We build a set of human behavior recognition syste...