-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataCollectionModule.py
62 lines (54 loc) · 1.8 KB
/
DataCollectionModule.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
"""
- This module saves images and a log file.
- Images are saved in a folder.
- Folder should be created manually with the name "DataCollected"
- The name of the image and the steering angle is logged
in the log file.
- Call the saveData function to start.
- Call the saveLog function to end.
- If runs independent, will save ten images as a demo.
"""
import pandas as pd
import os
import cv2
from datetime import datetime
global imgList, steeringList
countFolder = 0
count = 0
imgList = []
steeringList = []
#GET CURRENT DIRECTORY PATH
myDirectory = os.path.join(os.getcwd(), 'DataCollected')
# print(myDirectory)
# CREATE A NEW FOLDER BASED ON THE PREVIOUS FOLDER COUNT
while os.path.exists(os.path.join(myDirectory,f'IMG{str(countFolder)}')):
countFolder += 1
newPath = myDirectory +"/IMG"+str(countFolder)
os.makedirs(newPath)
# SAVE IMAGES IN THE FOLDER
def saveData(img,steering):
global imgList, steeringList
now = datetime.now()
timestamp = str(datetime.timestamp(now)).replace('.', '')
#print("timestamp =", timestamp)
fileName = os.path.join(newPath,f'Image_{timestamp}.jpg')
cv2.imwrite(fileName, img)
imgList.append(fileName)
steeringList.append(steering)
# SAVE LOG FILE WHEN THE SESSION ENDS
def saveLog():
global imgList, steeringList
rawData = {'Image': imgList,
'Steering': steeringList}
df = pd.DataFrame(rawData)
df.to_csv(os.path.join(myDirectory,f'log_{str(countFolder)}.csv'), index=False, header=False)
print('Log Saved')
print('Total Images: ',len(imgList))
if __name__ == '__main__':
cap = cv2.VideoCapture(1)
for x in range(12):
_, img = cap.read()
saveData(img, 0.5)
cv2.waitKey(1)
cv2.imshow("Image", img)
saveLog()