video_to_image_sequence.ipynb
2.95 KB
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
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import random\n",
"import cv2\n",
"from PIL import Image"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"action = 'fight'\n",
"base_dir = '/mnt/c/Users/hyerilee/Downloads/{}'.format(action)\n",
"file_path = {}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"for (root ,dirs, files) in os.walk(base_dir):\n",
" if len(dirs) == 0 and len(files) != 0:\n",
" if root not in file_path:\n",
" file_path[root] = files"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"def getFrame(sec, saveFilePath, count):\n",
" vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)\n",
" hasFrames,img = vidcap.read()\n",
" if hasFrames:\n",
" img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
" im_pil = Image.fromarray(img) # wsl에서 cv2.imwirte 안돼서 하는 작업. cv2 to pillow\n",
" if os.path.exists(saveFilePath) and count==1: #동일한 시작지점을 또 capture하는 경우 skip\n",
" return False\n",
" if not os.path.exists(saveFilePath):\n",
" os.makedirs(saveFilePath)\n",
" im_pil.save(saveFilePath+'/'+'{:03d}'.format(count)+\".jpg\")\n",
"# cv2.imwrite(saveFilePath+'{:03d}'.format(count)+\".jpg\", image) # save frame as JPG file\n",
" return hasFrames\n",
"\n",
"starts = [i for i in range(180,291)] #가능시작지점\n",
"for (file_dir, file_list) in file_path.items():\n",
" for video_file in file_list:\n",
" video_path = file_dir + '/' + video_file\n",
" vidcap = cv2.VideoCapture(video_path)\n",
" for i in range(0,10):\n",
" sec = random.choice(starts)\n",
" start_sec = sec\n",
" frameRate = 0.5 # it will capture image in each 1 second\n",
" cnt = 1\n",
" success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)\n",
" while success:\n",
" cnt += 1\n",
" sec += frameRate\n",
" success = getFrame(sec, video_path[:-4]+'_start'+str(start_sec)+'s', cnt)\n",
" if cnt >= 20:\n",
" break"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}