python cv2把视频转化为 图片

convert video to pic

def video_pic(path):
    cap = cv2.VideoCapture(path)
    sucess = cap.isOpened()
    frame_count = 0
    i = 0
    while sucess:
        frame_count += 1
        sucess, frame = cap.read()
        if (frame_count % 20 == 0):
            i += 1
            cv2.imwrite('/home/sy/s/s%d.jpg' % i, frame)
    cap.release()

   if __name__ == '__main__':
    path = "/home/sy/data/vison/C1-11F(开发者).mp4"
    video_pic(path)

产生pic相对应的,空的.txt 文件

def generate_empty_lable(src):
    files = os.listdir(src)
    for file in files:
        file = file.split('.')
        txt_file_path = src + file[0] + '.txt'
        txt_file = open(txt_file_path, 'w')
        txt_file.write(' ')

移动确定后缀的文件到指定文件夹中

def move_format_file(src, dst, format):
    files = os.listdir(src)
    for file in files:
        file = file.split('.')
        if (file[-1] == format):
            src_file = src + file[0] + '.' + file[1]
            dst_file = dst + file[0] + '.' + format
            shutil.move(src_file, dst_file)
        else:
            continue

产生训练pic的路径文件

def generate_train(pic_path, dst_file):
    f = open(dst_file, 'w')
    files = os.listdir(pic_path)
    for file in files:
        file = file.split('.')
        if file[-1] == 'jpg':
            line = pic_path + file[0] + '.jpg\n'
            f.writelines(line)
    f.close()

你可能感兴趣的:(code_python)