Reputation: 203
I want to check for the video file names present in csv file. If name is found video should be skipped else video frames should be extracted
I have the script for frame extraction and video name is also getting written in the csv but i want to loop over the csv data everytime the script is run to check if the video name is present or not
import cv2 import glob from glob import glob import os import shutil import numpy as np import argparse import imutils from keras.preprocessing.image import img_to_array from keras.models import load_model import tensorflow as tf from PIL import Image import json import sys import csv from utils import label_map_util from utils import visualization_utils as vis_util from os.path import isfile,join import pandas as pd
########################################## EXTRACT FRAMES FROM VIDEO###########################
def extractFrames(m):
global vid_name
vid_files=glob(m)
complete_videos = get_completed_videos()
new_vid_files = [x for x in vid_files if x not in complete_videos]
for v_f in range(len(new_vid_files)):
print("path of video========>>>>.",new_vid_files[v_f])
v1=os.path.basename(vid_files[v_f])
try:
vid_name = os.path.splitext(v1)[0]
vidcap = cv2.VideoCapture(new_vid_files[v_f])
except cv2.error as e:
print(e)
except:
print('error')
#condition
fsize=os.stat(new_vid_files[v_f])
print('=============size of video ===================:' , fsize.st_size)
try:
if (fsize.st_size > 1000):
fps = vidcap.get(cv2.CAP_PROP_FPS) # OpenCV2 version 2 used "CV_CAP_PROP_FPS"
frameCount = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
duration = frameCount/fps
minutes = int(duration/60)
print('fps = ' + str(fps))
print('number of frames = ' + str(frameCount))
print('duration (S) = ' + str(duration))
if (duration > 1):
success,image = vidcap.read()
count=0
success=True
while success:
img_name = vid_name + '_f' + str(count) + ".jpg"
success,image = vidcap.read()
if count % 10 == 0 or count ==0:
target_non_target(img_name, image)
count+=1
vidcap.release()
cv2.destroyAllWindows()
except:
print("error")
print('finished processing video ', new_vid_files[v_f])
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv', 'a') as csv_file:
fieldnames = ['Video_Name','Process']
file_is_empty = os.stat("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\"+'video_info.csv').st_size == 0
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
if file_is_empty:
writer.writeheader()
writer.writerow({'Video_Name':vid_name,'Process':'done'})
def get_completed_videos():
completed_videos = []
with open("C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv") as csv_file:
for row in csv.reader(csv_file):
for col in row:
try:
completed_videos.append(row[col])
except Exception as e:
print(str(e))
print(completed_videos[0])
If a csv file has videos A,B,C present then on adding video 4 to the folder and running the script first the video names present in the csv should be checked and then only video 4 should be processed instead of all the videos
Currently i am facing the below error while trying to loop over my csv data
list indices must be integers or slices, not str
list indices must be integers or slices, not str
list indices must be integers or slices, not str
list indices must be integers or slices, not str
list indices must be integers or slices, not str
list indices must be integers or slices, not str
list indices must be integers or slices, not str
list indices must be integers or slices, not str
Traceback (most recent call last):
File "C:\multi_cat_3\models\research\object_detection\code_modification_test (1).py", line 276, in <module>
extractFrames(x)
File "C:\multi_cat_3\models\research\object_detection\code_modification_test (1).py", line 29, in extractFrames
complete_videos = get_completed_videos()
File "C:\multi_cat_3\models\research\object_detection\code_modification_test (1).py", line 105, in get_completed_videos
print(completed_videos[0])
IndexError: list index out of range
Upvotes: 0
Views: 70
Reputation: 5397
When you read a CSV line, you are reading a string (usually separated by commas), so you need to convert it to a list by splitting, so in your function get completed videos, you need to change your for loop as:
for row in csv.reader(csv_file):
cols=row.split(",")
for col in cols:
try:
completed_videos.append(col)
except Exception as e:
print(str(e))
Anyway, i would do it this way better:
for row in csv.reader(csv_file):
cols=row.split(",")
completed_videos.extend(col for col in cols)
EDIT
If, as you say, you already have a list in your row, then you cannot reference a list by it's column name, you should do it by it's index:
for row in csv.reader(csv_file):
for col in range(0,len(row)):
try:
completed_videos.append(row[col])
except Exception as e:
print(str(e))
Upvotes: 1