程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Preprocessing of Jingdong pig face recognition competition data: extract and store each frame of video as a picture in Python

編輯:Python

Hello everyone , I meet you again , I'm your friend, Quan Jun .

Recently attended JD.COM Pig face recognition competition , The training set is 30 individual video , Each frame of the video needs to be extracted and stored as a picture , Save in the corresponding folder ( Category labels ).

This example directly calls cv2 Module VideoCapture. One run , Probably 10 minute , You can get the preprocessed classified pictures , The specific code is as follows .

Each frame of video is extracted and stored as picture code

#! encoding: UTF-8
import os
import cv2
import cv
videos_src_path='/sata_disk/E_office/zhouhongli/pig/train'
images_save_path='/sata_disk/E_office/zhouhongli/pig/frame'
videos = os.listdir(videos_src_path)
videos = filter(lambda x: x.endswith('mp4'), videos)
for each_video in videos:
print each_video
# get the name of each video, and make the directory to save frames
each_video_name,_=each_video.split('.')
os.mkdir(images_save_path +'/'+ each_video_name)
each_video_save_full_path=os.path.join(images_save_path, each_video_name) + '/'
# get the full path of each video, which will open the video tp extract frames
each_video_full_path=os.path.join(videos_src_path, each_video)
cap=cv2.VideoCapture(each_video_full_path)
frame_count = 1
success = True
while(success):
success, frame=cap.read()
print 'Read a new frame: ', success
params = []
params.append(cv.CV_IMWRITE_PXM_BINARY)
params.append(1)
cv2.imwrite(each_video_save_full_path + each_video_name + "_%d.jpg" % frame_count, frame, params)
frame_count = frame_count+1
cap.release()

The problem of recursively deleting files

But there's a problem , Every video conversion results in 30 In the subfolder , There are 2952 A picture , But the first 2952 Zhang Shikong , So only use the powerful Linux Recursively delete the eligible files , I delete drops like this .

[email protected]:~$ find . -name '*_2952.jpg' -size 0 -print0 |xargs -0 rm

Reference resources

python tools: Extract and save each frame of the video http://blog.csdn.net/u010167269/article/details/53268686 Linux find And rm Delete the eligible files in linkage https://maoxian.de/2015/12/1362.html

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/151885.html Link to the original text :https://javaforall.cn


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved