-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_training_data.py
61 lines (50 loc) · 1.51 KB
/
collect_training_data.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
import CONST
import numpy as np
import cv2
import os
import time
# helper functions
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
def record_frames(video_name):
# capture a video
cap = cv2.VideoCapture(0)
time.sleep(2)
frame_count = 1
createFolder('./dataset/'+video_name+'/')
print('Keep r pressed to record')
print('Keep q pressed to exit')
# record frames
while(frame_count <= CONST.FRAMES_PER_VIDEO):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if ret==True:
if cv2.waitKey(1) & 0xFF == ord('r'):
print ('.', end="")
#save the frame as an image
cv2.imwrite('dataset/%s/%d.jpg' % (video_name, frame_count), frame) # save frame as JPEG file
frame_count += 1
elif cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release opencv resources
cap.release()
cv2.destroyAllWindows()
# GETTING THE DATASET
# extract frames from live video
label = input("In a word, what am I about to see?")
record_frames(label)
print ('\nGot it')
label = input("And now?")
record_frames(label)
print ('\nAlright')
label = input("what about the last?")
record_frames(label)
print ('\nDone')