我要检查csv文件中存在的视频文件名。如果找到名称,则应跳过视频,否则应提取视频帧
我有用于帧提取的脚本,视频名称也正在csv中写入,但是我想每次运行脚本以检查视频名称是否存在时都遍历csv数据
导入cv2 导入球 从glob导入glob 导入操作系统 进口壁垒 将numpy导入为np 导入argparse 导入imutils 从keras.preprocessing.image导入img_to_array 从keras.models导入load_model 将tensorflow作为tf导入 从PIL导入图片 导入json 导入系统 导入csv 从utils导入label_map_util 从utils导入visualisation_utils作为vis_util 从os.path导入isfile,join 将熊猫作为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])
如果csv文件中存在视频A,B,C,则在将视频4添加到文件夹并运行脚本后,首先应检查csv中存在的视频名称,然后仅处理视频4,而不要处理所有视频视频
当前,我在尝试遍历csv数据时遇到以下错误
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
答案 0 :(得分:1)
当您读取CSV行时,您正在读取一个字符串(通常以逗号分隔),因此您需要通过拆分将其转换为列表,因此在获取完整视频的函数中,您需要将for循环更改为:
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))
无论如何,我会这样做得更好:
for row in csv.reader(csv_file):
cols=row.split(",")
completed_videos.extend(col for col in cols)
编辑
如果您说过,如果您的行中已经有一个列表,那么您就不能通过其列名来引用该列表,而应该通过其索引来进行引用:
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))