我具有从视频中提取帧的功能。我有一个csv文件,其中包含已经处理过的视频的名称。我想检查csv文件中是否存在新添加的视频文件的名称。如果存在,则退出代码,否则处理该函数以从新视频中提取帧
.box-layout {
background: url('/images/bg.jpg');
background-size: cover;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
width: 100vw;
}
.box-layout__box{
background: fade-out(white, .15);
border-radius: 3px;
padding: $l-size $m-size;
text-align: center;
width: 25rem;
}
.button {
background: $blue;
border: none;
color: white;
font-weight: 300;
font-size: $font-size-large;
padding: $s-size;
}
.button_anon {
background: green;
border: none;
color: white;
font-weight: 300;
font-size: 1.5rem;
padding: 0.8rem;
}
假设一个文件夹有2个视频V1和V2,已经从中提取了帧,并且在csv文件中添加了名称V1和V2。现在,当我添加视频V3时,代码应检查cv中是否已经存在V3。如果存在,则应跳过代码,否则应处理来自V3的帧,并在提取帧后将V3添加到csv文件中
答案 0 :(得分:1)
没有详细信息,您将拥有这样的代码
def extractFrames(m):
# do stuff
vid_files=glob(m)
for v_f in range(len(vid_files)):
#find vid_name
#do stuff
save_as_done(vid_name)
if __name == '__main__':
x="C:\\Python36\\videos\\*.mp4"
extractFrames(x)
如果您传递已完成的事情的列表,则类似
done = ['first.mp4', 'second.mp4']
您可以检查文件名是否已完成,例如:
>>> 'first.mp4' in done
True
因此,如果将已完成操作的文件名(完全路径化的文件名)保存到文件中,然后将其加载到列表中,就像这样
def load_done_list(): 使用open('video_info.csv')作为f:#或完整路径,也许传入文件名? 返回f.readlines()
您可以查看列表
def extractFrames(m, done):
# do stuff
vid_files=glob(m)
for v_f in range(len(vid_files)):
#find vid_name
if vid_name not in done: #,--- check if done already
#do stuff
save_as_done(vid_name)
if __name == '__main__':
x="C:\\Python36\\videos\\*.mp4"
done = load_done_list() #<--- You need to load this into a list
extractFrames(x, done) #<--- and pass it in to your function
这需要一些可以在完成时保存文件名的东西:
def save_as_done(vid_name):
with open('video_info.csv', 'a') as f: #maybe pass in the file name so you only define it once?
f.write(vid_name + '\n')
我没有填写所有详细信息,但是显示了可以在其中进行加载,保存和检查的地方。 写入的文件中只有文件名-在每一行的末尾“完成”似乎没有多大意义。 在处理文件时,这将继续打开和关闭文件。这可能会减慢速度,但可能并不重要:您可以传入文件句柄进行写入,以使其保持打开状态。您可以选择。
答案 1 :(得分:0)
我认为您可能需要一个函数来从csv文件中获取列表的完成/完成的视频。 诸如此类,可能需要对标题行进行一些调整。
def get_completed_videos():
completed_videos = []
with open(".../video_info.csv") as csv_file:
for row in csv.reader(csv_file):
completed_videos.append(row[0])
return completed_videos
然后将它们排除在提取函数中
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]
...
答案 2 :(得分:0)
def extractFrames(m,done):
global vid_name
vid_files=glob(m)
for v_f in range(len(vid_files)):
print("path of video========>>>>.",vid_files[v_f])
v1=os.path.basename(vid_files[v_f])
vid_name = os.path.splitext(v1)[0]
if vid_name not in done:
try:
vidcap = cv2.VideoCapture(vid_files[v_f])
except cv2.error as e:
print(e)
except:
print('error')
#condition
fsize=os.stat(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 ', 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'})
if __name__ == "__main__":
x="C:\\Python36\\videos\\*.mp4"
y="C:\\multi_cat_3\\models\\research\\object_detection\\my_imgs\\video_info.csv"
done=list(y)
extractFrames(x,done)