This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextract_tiles.py
170 lines (128 loc) · 5.23 KB
/
extract_tiles.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
This script is used to extract frames from videos to store them as training data.
"""
from queue import PriorityQueue
from threading import Thread
from random import random
from typing import Tuple
from os import path
from glob import iglob
import cv2
import tensorflow as tf
import numpy as np
def extract_video_frames(queue: PriorityQueue,
source: int,
cap: cv2.VideoCapture,
crop: Tuple[int, int, int, int],
target_width: int,
target_height: int,
frame_step: int=1,
display_progress: bool=False):
window = 'video'
if display_progress:
cv2.namedWindow(window)
while True:
success, buffer = cap.read()
if not success:
break
# crop borders
buffer = buffer[crop[0]:-crop[2], crop[1]:-crop[3], :]
buffer = cv2.resize(buffer, (target_width, target_height), interpolation=cv2.INTER_AREA)
frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
random_priority = random()
queue.put((random_priority, (buffer, source)))
if display_progress:
cv2.imshow(window, buffer)
if (cv2.waitKey(33) & 0xff) == 27:
break
cap.set(cv2.CAP_PROP_POS_FRAMES, frame + frame_step)
if display_progress:
cv2.destroyWindow(window)
def load_images(queue: PriorityQueue,
source: int,
file_path: str,
target_width: int,
target_height: int,
display_progress: bool=False):
window = 'image'
if display_progress:
cv2.namedWindow(window)
for file in iglob(path.join(file_path, '**', '*.jpg'), recursive=True):
buffer = cv2.imread(file)
buffer = cv2.resize(buffer, (target_width, target_height), interpolation=cv2.INTER_AREA)
random_priority = random()
queue.put((random_priority, (buffer, source)))
if display_progress:
cv2.imshow(window, buffer)
if (cv2.waitKey(33) & 0xff) == 27:
break
if display_progress:
cv2.destroyWindow(window)
def main():
# contains approx. 500 paintings from http://leonidafremov.deviantart.com/gallery/
image_path = '~/Downloads/Leonid Afremov - Gallery'
# https://vimeo.com/22328077
video_file_0 = '~/Downloads/Wim - See You Hurry.mp4'
video_crop_0 = (64, 32, 64, 32)
# https://www.youtube.com/watch?v=b_KfnGBtVeA
video_file_1 = '~/Downloads/Disclosure - Magnets ft. Lorde.mp4'
video_crop_1 = (150, 0, 150, 1)
frame_queue = PriorityQueue()
cap_0 = cv2.VideoCapture(video_file_0)
cap_1 = cv2.VideoCapture(video_file_1)
try:
height, width = cap_0.get(cv2.CAP_PROP_FRAME_HEIGHT), \
cap_0.get(cv2.CAP_PROP_FRAME_WIDTH)
n_frames_0 = cap_0.get(cv2.CAP_PROP_FRAME_COUNT)
frame_step_0 = 10
n_frames_1 = cap_1.get(cv2.CAP_PROP_FRAME_COUNT)
frame_step_1 = 8
print('Extracting {0:d} and {1:d} frames.'.format(int(n_frames_0 // frame_step_0), int(n_frames_1 // frame_step_1)))
aspect = float(height) / float(width)
new_width = 320
new_height = 180 # int(new_width * aspect)
assert new_height == 180
threads = [Thread(target=extract_video_frames,
args=(frame_queue, 0, cap_0, video_crop_0, new_width, new_height, frame_step_0, False)),
Thread(target=extract_video_frames,
args=(frame_queue, 2, cap_1, video_crop_1, new_width, new_height, frame_step_1, False)),
Thread(target=load_images,
args=(frame_queue, 1, image_path, new_width, new_height, False))]
print('Loading frames ...')
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print('Done loading frames.')
finally:
cap_0.release()
cap_1.release()
print('Writing TFRecords ...')
display_progress = False
window = 'preview'
if display_progress:
cv2.namedWindow(window)
writer_options = tf.python_io.TFRecordOptions(tf.python_io.TFRecordCompressionType.GZIP)
writer = tf.python_io.TFRecordWriter(path.join('data', 'inputs.tfrecord.gz'), writer_options)
try:
while not frame_queue.empty():
priority, (buffer, source) = frame_queue.get()
img_raw = buffer.astype(np.uint8).tostring() # Note OpenCV channel order is BGR
feature = {
'type': tf.train.Feature(int64_list=tf.train.Int64List(value=[source])),
'raw': tf.train.Feature(bytes_list=tf.train.BytesList(value=[img_raw]))
}
example = tf.train.Example(features=tf.train.Features(feature=feature))
writer.write(example.SerializeToString())
if display_progress:
cv2.imshow(window, buffer)
if (cv2.waitKey(33) & 0xff) == 27:
break
# do something
frame_queue.task_done()
finally:
writer.close()
if display_progress:
cv2.destroyWindow(window)
if __name__ == '__main__':
main()