add
wangzhibo
2022-03-24 db44060a8de04f55557badee55cd617392d65c5b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# -*- coding: utf-8 -*-
 
 
 
 
#将视频分给为一张一张的图片 
__author__ = '上海色宁'
 
import os
import sys
import cv2
import numpy as np
import uuid
import getopt
 
 
def start_check_argv_and_split(argv):
    """
        通过 getopt模块 来识别参数demo,  http://blog.csdn.net/ouyang_peng/
    """
    input_video = ""
    clip_rect = ""
    dest_dir = ""
    
    try:
        """
            options, args = getopt.getopt(args, shortopts, longopts=[])
 
            参数args:一般是sys.argv[1:]。过滤掉sys.argv[0],它是执行脚本的名字,不算做命令行参数。
            参数shortopts:短格式分析串。例如:"hp:i:",h后面没有冒号,表示后面不带参数;p和i后面带有冒号,表示后面带参数。
            参数longopts:长格式分析串列表。例如:["help", "ip=", "port="],help后面没有等号,表示后面不带参数;ip和port后面带冒号,表示后面带参数。
            返回值options是以元组为元素的列表,每个元组的形式为:(选项串, 附加参数),如:('-i', '192.168.0.1')
            返回值args是个列表,其中的元素是那些不含'-'或'--'的参数。
        """
        opts, args = getopt.getopt(argv, "hi:c:d:", ["help", "input_video=", "clip_rect=","dest_dir="])
    except getopt.GetoptError:
        print('Error: 0_split_video_into_image.py -i <input_video> -c <clip_rect> -d <dest_dir>')
        print('   or: 0_split_video_into_image.py --input_video=<video.mp4> --clip_rect=<[175:235,275:420]> --dest_dir=<split_image>')
        sys.exit(2)
 
    # 处理 返回值options是以元组为元素的列表。
    if len(opts) != 3 :
        print('Error: Parameters not enough!')
        print('Usage: 0_split_video_into_image.py -i <input_video> -c <clip_rect> -d <dest_dir>')
        print('   or: 0_split_video_into_image.py --input_video=<video.mp4> --clip_rect=<175:235,275:420> --dest_dir=<split_image>')
        sys.exit()
    for opt, arg in opts:
        if opt in ("-h", "--h","-help","--help"):
            print('Usage: 0_split_video_into_image.py -i <input_video> -c <clip_rect> -d <dest_dir>')
            print('   or: 0_split_video_into_image.py --input_video=<video.mp4> --clip_rect=<175:235,275:420> --dest_dir=<split_image>')
            sys.exit()
        elif opt in ("-i", "--input_video"):
            input_video = arg
        elif opt in ("-c", "--clip_rect"):
            clip_rect = arg
        elif opt in ("-d", "--dest_dir"):
            dest_dir = arg
    print('input_video:', input_video)
    print('clip_rect:', clip_rect )
    print(' . !!! we are assuming that this paramter is correct.')
    print('dest_dir:', dest_dir)
    
    if (not os.path.isfile(input_video)):
        print( input_video+ " is  not exist!!!")
        sys.exit()
    if not os.path.exists(dest_dir):
        print( dest_dir+ " is  not exist, we are going to creat it !!!")
        os.makedirs(dest_dir)
 
    # 打印 返回值args列表,即其中的元素是那些不含'-'或'--'的参数。
    #for i in range(0, len(args)):
    #   print('参数 %s 为:%s' % (i + 1, args[i]))
    
    split_into_image(input_video,clip_rect,dest_dir)
    
 
def split_into_image(input_video,clip_rect,dest_dir):
        
    cap = cv2.VideoCapture(input_video)
    rows = clip_rect.split(',')[0]  # 行
    cols = clip_rect.split(',')[1]  # 列
    rows_start = int(rows.split(':')[0])
    rows_end = int(rows.split(':')[1])
    cols_start = int(cols.split(':')[0])
    cols_end = int(cols.split(':')[1])
    
    totol_cols = (cols_end-cols_start)
    col_length_1 = int(totol_cols*0.212352113)
    col_length_2 = int(totol_cols*0.212352113)
    col_length_3_4 = int(totol_cols*0.212352113)
 
 
    col_start_2 = cols_start+int(totol_cols*0.265690141)
    col_start_3 = cols_start+int(totol_cols*0.485746479)
    col_start_4 = cols_start+int(totol_cols*0.75943662)
 
    i = 0 
    while True:
        if i % 10 == 0:
            ret, frame = cap.read()
            #cv2.imshow("capture", frame[rows_start:rows_end,cols_start:cols_end])
            cv2.imwrite(dest_dir+"/"+str(uuid.uuid4())+".jpg", frame[rows_start:rows_end,cols_start:cols_start+col_length_1])
            cv2.imwrite(dest_dir+"/"+str(uuid.uuid4())+".jpg", frame[rows_start:rows_end,col_start_2:col_start_2+col_length_2])
            cv2.imwrite(dest_dir+"/"+str(uuid.uuid4())+".jpg", frame[rows_start:rows_end,col_start_3:col_start_3+col_length_3_4])
            cv2.imwrite(dest_dir+"/"+str(uuid.uuid4())+".jpg", frame[rows_start:rows_end,col_start_4:col_start_4+col_length_3_4])
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        i += 1
    cap.release()
    cv2.destroyAllWindows()   
 
 
#程序入口
if __name__ == '__main__':
    start_check_argv_and_split(sys.argv[1:])