Thursday, November 14, 2019

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

Our this article is extension of previous article .In this article we will extract frame from multiple videos having same length duration .

We are assuming the following conditions :

1. All Videos should have same length duration .

In Our Below code you only need to give path location of videos .

#!/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 numpy as np
import cv2,os
from moviepy.editor import VideoFileClip

#location of the input files
inputfilepaths=["Dataset/big1.mp4","Dataset/big2.mp4","Dataset/big3.mp4","Dataset/big4.mp4"
                ,"Dataset/big5.mp4","Dataset/big6.mp4","Dataset/big7.mp4"]

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

video=[]
#location of video file
for x in range(0,len(inputfilepaths)):
    video.append(cv2.VideoCapture(inputfilepaths[x]))
    print(x+1,"Video length ",VideoFileClip(inputfilepaths[0]).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)
  
        #reading frame
    hasframes=np.array([])

    for x in range(0,len(video)):
        video[x].set(cv2.CAP_PROP_POS_MSEC,sec*1000)
        hasimage,images=video[x].read()
        hasframes=np.append(hasframes,hasimage)
     
    if hasframes[len(hasframes)-1]:
        #Write to location , increasing the count to avoid name conflict of immges
        for x in range(0,len(video)):
             hasimage,images=video[x].read()
             cv2.imwrite("Frames/{0}video{1}.jpg".format(x+1,count), images)

    return hasframes[len(hasframes)-1]

#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)
 

*----------------------------------------------------------      
Github Link : 

2 comments:


  1. Thank you very much!
    Your blog is really helpful for us. I was searching for this kind of information. The best information and very Useful in this blog. Are you want to know more about Onlive Server? then click on this link for more information:- https://onliveserver.com/vps-usa/
    Onlive Server provides the magnificent platform of USA VPS Hosting for your website’s benefits in this modern technology world.

    ReplyDelete
  2. Thank you so much!
    Attractive Blog and very helpful! I would like this most information. I achieve this most helpful information from you. I would like the most important information shared with you. I hope that It will be very helpful information for you. Onlive Server provides the latest technology-based UK VPS Hosting plans without any extravagant charges. then you will more get amazing feature-based USA VPS Hosting plans from Onlive Server

    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...