sgoldenlab / simba

SimBA (Simple Behavioral Analysis), a pipeline and GUI for developing supervised behavioral classifiers
https://simba-uw-tf-dev.readthedocs.io/
GNU General Public License v3.0
287 stars 140 forks source link

Whenever i extract frame rates from a video dataset folder to a train_1 folder. After extracting frame rates the folder is empty where as pycharm shows it has been done successfully. Any one aware of this issue? My code #83

Closed Qasimster closed 3 years ago

Qasimster commented 3 years ago

Whenever i extract frame rates from a video dataset folder to a train_1 folder. After extracting frame rates the folder is empty where as pycharm shows it has been done successfully. Any one aware of this issue? My code for i in tqdm(range(train.shape[0])): count = 0 videoFile = train['video_name'][i] cap = cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) # capturing the video from the given path frameRate = cap.get(5) #frame rate x=1 while(cap.isOpened()): frameId = cap.get(1) #current frame number ret, frame = cap.read() if (ret != True): break if (frameId % math.floor(frameRate) == 0):

storing the frames in a new folder named train_1

        filename =r"D:\New Projects Pycharm\Videoclassi\tttt/" + videoFile.split('/')[1].split(' ')[0] +"_frame%d.jpg" % count;count+=1
        cv2.imwrite(filename, frame)
cap.release()

Originally posted by @Qasimster in https://github.com/sgoldenlab/simba/issues/8#issuecomment-756070871

sronilsson commented 3 years ago

Hi @Qasimster - what comes to mind is the file path that you write the file to (Videoclassi/ttt/) might not exist. If you write to a path that does not exist it can appear like opencv are saving the frames but you won't find them.

Before your loop, after grabbing the video:

import os

my_path = r"D:\New Projects Pycharm\Videoclassi\tttt"
if not os.path.exists(my_path)
    os.makedirs(my_path)

and in your loop:

filename = 'frame' + str(counter) + '.png'
save_name.= os.path.join(my_path, filename)
cv2.imwrite(save_name, frame)
counter+=1
Qasimster commented 3 years ago

Sir tttt exists and i tried your method too still it isnt working. Have a look

import cv2 # for capturing videos import math # for mathematical operations import matplotlib.pyplot as plt # for plotting the images

%matplotlib inline

import pandas as pd from keras.preprocessing import image # for preprocessing the images import numpy as np # for mathematical operations from keras.utils import np_utils from skimage.transform import resize # for resizing images from sklearn.model_selection import train_test_split import glob from tqdm import tqdm

open the .txt file which have names of training videos

f = open("trainlist01.txt", "r") temp = f.read() videos = temp.split('\n')

creating a dataframe having video names

train = pd.DataFrame() train['video_name'] = videos train = train[:-1] print("Train ",train.head())

open the .txt file which have names of test videos

f = open("testlist01.txt", "r") temp = f.read() videos = temp.split('\n')

creating a dataframe having video names

test = pd.DataFrame() test['video_name'] = videos test = test[:-1] print("Test ",test.head())

print("-----------------------------------------------------------------------------------------------------------")

creating tags for training videos

train_video_tag = [] for i in range(train.shape[0]): train_video_tag.append(train['video_name'][i].split('/')[0])

train['tag'] = train_video_tag

creating tags for test videos

test_video_tag = [] for i in range(test.shape[0]): test_video_tag.append(test['video_name'][i].split('/')[0])

test['tag'] = test_video_tag

print("Train tags ----------------") print(train['tag']) print('---------------------------------------------------------') print("Test tags ------------------") print(test['tag'])

print("-----------------------------------------------------------------------------------------------------------")

storing the frames from training videos

for i in tqdm(range(train.shape[0])): count = 0 videoFile = train['video_name'][i] cap = cv2.VideoCapture('UCF/'+videoFile.split(' ')[0].split('/')[1]) # capturing the video from the given path frameRate = cap.get(5) #frame rate x=1 while(cap.isOpened()): frameId = cap.get(1) #current frame number ret, frame = cap.read() if (ret != True): break if (frameId % math.floor(frameRate) == 0):

storing the frames in a new folder named train_1

        my_path = r"D:\New Projects Pycharm\Videoclassi\tttt"
        if not os.path.exists(my_path):
            os.makedirs(my_path)
        filename = 'frame' + str(counter) + '.png'
        save_name= os.path.join(my_path, filename)
        cv2.imwrite(save_name, frame)
        counter += 1
cap.release()
sronilsson commented 3 years ago

Just before saving the frame in the script, do a:

print(frame) print(save_name)

It's either of those two, or both but most likely the path. What's printed?

Qasimster commented 3 years ago

It displays nothing.

sgoldenlab commented 3 years ago

nothing? does it print an empty string?

Qasimster commented 3 years ago

It just displays nothing not even empty string

sgoldenlab commented 3 years ago

Hmm.. that would mean that the code does not reach the print statement, it gets stuck somewhere before