-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatageneration_GeoLoc.py
340 lines (269 loc) · 14.6 KB
/
datageneration_GeoLoc.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from utils.Constants import IMG_WIDTH, IMG_HEIGHT
from deepgtav.messages import *
from deepgtav.client import Client
from utils.BoundingBoxes import add_bboxes, parseBBox2d_LikePreSIL, parseBBoxesVisDroneStyle, parseBBox_YoloFormatStringToImage
from utils.utils import save_image_and_bbox, save_image, save_meta_data, getRunCount, generateNewTargetLocation
from utils.colors import pickRandomColor
import argparse
import time
import cv2
import matplotlib.pyplot as plt
from PIL import Image
from random import uniform
import random
from math import sqrt
import math
import numpy as np
import os
import sys
def gaussin_random_truncted(lower_bound, upper_bound, mean, std_dev):
number = random.gauss(mean, std_dev)
number = max(number, lower_bound)
number = min(number, upper_bound)
return number
def euler_to_rotation_matrix(pitch, roll, yaw):
# Convert angles from degrees to radians
pitch = np.radians(pitch)
roll = np.radians(roll)
yaw = np.radians(yaw)
# Rotation matrix around x-axis (pitch)
Rx = np.array([
[1, 0, 0],
[0, np.cos(pitch), -np.sin(pitch)],
[0, np.sin(pitch), np.cos(pitch)]
])
# Rotation matrix around y-axis (roll)
Ry = np.array([
[np.cos(roll), 0, np.sin(roll)],
[0, 1, 0],
[-np.sin(roll), 0, np.cos(roll)]
])
# Rotation matrix around z-axis (yaw)
Rz = np.array([
[np.cos(yaw), -np.sin(yaw), 0],
[np.sin(yaw), np.cos(yaw), 0],
[0, 0, 1]
])
# Combined rotation matrix
R = Rz @ Ry @ Rx
return R
def calculate_projection_points(height, rot_x, rot_y, rot_z, temp_x, temp_y, hfov=60, vfov=38.9):
# rot_x, rot_y, rot_z represents pitch, roll, and yaw in eular system
# Convert angles from degrees to radians
hfov_rad = math.radians(hfov)
vfov_rad = math.radians(vfov)
rot_x = abs(rot_x + 90)
tilt_angle_rad = math.radians(rot_x)
# print(hfov_rad, vfov_rad, tilt_angle_rad)
# Calculate the width and length of the projection on the ground
W = 2 * height * math.tan(hfov_rad / 2)
L = 2 * height * math.tan(vfov_rad / 2)
# Calculate the shift in the projection center due to the tilt angle
D = height * math.tan(tilt_angle_rad)
# Calculate the four corner points
P1 = (-W / 2, -L / 2 + D)
P2 = (W / 2, -L / 2 + D)
P3 = (-W / 2, L / 2 + D)
P4 = (W / 2, L / 2 + D)
relative_points = [
[-W / 2, -L / 2 + D, 0],
[W / 2, -L / 2 + D, 0],
[-W / 2, L / 2 + D, 0],
[W / 2, L / 2 + D, 0]
]
# print(relative_points)
R = euler_to_rotation_matrix(pitch=rot_x, roll=rot_y, yaw=rot_z)
actual_points = []
for point in relative_points:
rotated_point = R @ np.array(point)
actual_x = temp_x + rotated_point[0]
actual_y = temp_y + rotated_point[1]
actual_points.append(actual_x)
actual_points.append(actual_y)
return actual_points
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=None)
parser.add_argument('-l', '--host', default='localhost', help='The IP where DeepGTAV is running')
parser.add_argument('-p', '--port', default=8000, help='The port where DeepGTAV is running')
parser.add_argument('-s', '--save_dir', default='D:\\data\\GTA-UAV\\Captured\\randcam2_std0_stable_5area', help='The directory the generated data is saved to')
args = parser.parse_args()
client = Client(ip=args.host, port=args.port)
# voltic
# scenario = Scenario(drivingMode=786603, vehicle="buzzard", location=[245.23306274414062, -998.244140625, 29.205352783203125], spawnedEntitiesDespawnSeconds=200)
scenario = Scenario(drivingMode=[786603,0], vehicle="voltic", location=[245.23306274414062, -998.244140625, 29.205352783203125], spawnedEntitiesDespawnSeconds=200)
dataset = Dataset(location=True, time=True, exportBBox2D=True)
client.sendMessage(Start(scenario=scenario, dataset=dataset))
message = client.recvMessage()
# f = open('log.txt', 'w')
# sys.stdout = f
CAMERA_OFFSET_Z = -10
CAMERA_OFFSET_ROT_Z = 20
TRAVEL_HEIGHT = 200
# TRAVEL_HEIGHT_LIST = [100, 200, 300, 400, 500, 600]
TRAVEL_HEIGHT_LIST = [600]
TRAVEL_HEIGHT_ATEMPT = 1000
CAMERA_ROT_X = -90 # [-70, 110]
CAMERA_ROT_X_L = -110
CAMERA_ROT_X_R = -70
CAMERA_ROT_Y = 0 # [-10, 10]
CAMERA_ROT_Y_L = -10
CAMERA_ROT_Y_R = 10
CAMERA_ROT_Z = 0 # [-180, 180]
CAMERA_ROT_Z_L = -180
CAMERA_ROT_Z_R = 180
STD_DEV = 5
ERROR_EPS = 10
rot_x = CAMERA_ROT_X
rot_y = CAMERA_ROT_Y
rot_z = CAMERA_ROT_Z + CAMERA_OFFSET_ROT_Z
step = 100
# STEP_LIST = [50, 100, 150, 200, 250, 300]
STEP_LIST = [300]
# Adjustments for recording
# from UAV perspective
# client.sendMessage(SetCameraPositionAndRotation(z = CAMERA_OFFSET_Z, rot_x = uniform(CAMERA_ROT_X_LOW, CAMERA_ROT_X_HIGH)))
client.sendMessage(SetCameraPositionAndRotation(z = CAMERA_OFFSET_Z, rot_x=rot_x, rot_z=rot_z, rot_y=rot_y))
message = client.recvMessage()
print('start camera', message['CameraAngle'])
xAr_min, xAr_max, yAr_min, yAr_max = -3418, 3945, -3370, 7251
x_step = step
y_step = step
x_y_list = [
# [-2480, 1764, -3349, 1304],
# [-361, 3171, 2283, 3889],
# [1572, 3064, 4168, 5311],
# [-1007, 798, 5761, 6745],
# [-3316, -2135, -28, 1589]
# [-3418, 3945, -3370, 3740]
[-1431, x, -877, y]
]
# x_start, x_end = -1700, 1599
# y_start, y_end = -2586, 710
# x_start, x_end = 245, 1000
# y_start, y_end = -998, 100
z_loc = 0
# x_target, y_target = generateNewTargetLocation(xAr_min, xAr_max, yAr_min, yAr_max)
# run_count = getRunCount(save_dir)
run_count = 1
# weather = random.choice(["CLEAR", "EXTRASUNNY", "CLOUDS", "OVERCAST"])
count = 28339
for TRAVEL_HEIGHT, step in zip(TRAVEL_HEIGHT_LIST, STEP_LIST):
save_dir = f'{args.save_dir}'
save_dir = os.path.normpath(save_dir)
if not os.path.exists(os.path.join(save_dir, 'images')):
os.makedirs(os.path.join(save_dir, 'images'))
if not os.path.exists(os.path.join(save_dir, 'labels')):
os.makedirs(os.path.join(save_dir, 'labels'))
if not os.path.exists(os.path.join(save_dir, 'meta_data')):
os.makedirs(os.path.join(save_dir, 'meta_data'))
for i in range(len(x_y_list)):
x_start, x_end, y_start, y_end = x_y_list[i]
for x_temp in range(x_start, x_end, x_step):
for y_temp in range(y_start, y_end, y_step):
for f in range(20):
if f == 1:
weather = "CLEAR"
client.sendMessage(SetWeather(weather))
message = client.recvMessage()
elif f == 2:
client.sendMessage(SetClockTime(12))
message = client.recvMessage()
elif f == 3:
client.sendMessage(TeleportToLocation(x_temp, y_temp, TRAVEL_HEIGHT_ATEMPT))
message = client.recvMessage()
heightAboveGround = message['HeightAboveGround']
z_loc = message['location'][2]
z_ground = z_loc - heightAboveGround
z_loc = z_ground + TRAVEL_HEIGHT - CAMERA_OFFSET_Z
z_temp = z_ground + TRAVEL_HEIGHT
elif f == 4:
client.sendMessage(TeleportToLocation(x_temp, y_temp, z_loc))
message = client.recvMessage()
# elif f == 4:
# rot_x = gaussin_random_truncted(CAMERA_ROT_X_L, CAMERA_ROT_X_R, CAMERA_ROT_X, STD_DEV)
# rot_y = gaussin_random_truncted(CAMERA_ROT_Y_L, CAMERA_ROT_Y_R, CAMERA_ROT_Y, STD_DEV)
# rot_z = random.randint(CAMERA_ROT_Z_L, CAMERA_ROT_Z_R)
# client.sendMessage(SetCameraPositionAndRotation(x=x_temp, y=y_temp, z=TRAVEL_HEIGHT, rot_x=rot_x, rot_y=rot_y, rot_z=rot_z))
# elif f == 11:
# client.sendMessage(GoToLocation(x_temp, y_temp, z_loc))
# message = client.recvMessage()
elif f == 7:
rot_x = gaussin_random_truncted(CAMERA_ROT_X_L, CAMERA_ROT_X_R, CAMERA_ROT_X, STD_DEV)
rot_y = gaussin_random_truncted(CAMERA_ROT_Y_L, CAMERA_ROT_Y_R, CAMERA_ROT_Y, STD_DEV)
rot_z = random.randint(CAMERA_ROT_Z_L, CAMERA_ROT_Z_R)
# print(z_loc, heightAboveGround, TRAVEL_HEIGHT, z_temp)
client.sendMessage(SetCameraPositionAndRotation(z = CAMERA_OFFSET_Z, rot_x=rot_x, rot_y=rot_y, rot_z=rot_z))
message = client.recvMessage()
rot_x_m, rot_y_m, rot_z_m = message['CameraAngle']
pos_x_m, pos_y_m, pos_z_m = message['location']
print('!!!!! rot', rot_x, rot_x_m, rot_y, rot_y_m, rot_z, rot_z_m)
print('!!!!! pos', pos_x_m, pos_y_m, pos_z_m)
elif f == 9:
client.sendMessage(StartRecording())
message = client.recvMessage()
heightAboveGround_1 = message['HeightAboveGround']
elif f == 13:
client.sendMessage(StopRecording())
message = client.recvMessage()
filename = f'{TRAVEL_HEIGHT}' + '_' + f'{run_count:04}' + '_' + f'{count:010}'
x_temp, y_temp, z_temp = message['CameraPosition']
heightAboveGround_2 = message['HeightAboveGround']
diff1 = abs(heightAboveGround_2 - heightAboveGround_1)
if diff1 > ERROR_EPS:
print(f'Warning!! heightAboveGround value unstable! h1={heightAboveGround_1:.2f}, h2={heightAboveGround_2:.2f}, diff={diff1:.2f}')
continue
rot_x, rot_y, rot_z = message['CameraAngle']
if rot_x > CAMERA_ROT_X_R or rot_x < CAMERA_ROT_X_L \
or rot_y > CAMERA_ROT_Y_R or rot_y < CAMERA_ROT_Y_L \
or rot_z > CAMERA_ROT_Z_R or rot_z < CAMERA_ROT_Z_L:
print(f'Warning!! camera rot error! rot_x={rot_x}, rot_y={rot_y}, rot_z={rot_z}')
continue
proj_points = calculate_projection_points(heightAboveGround_1 + CAMERA_OFFSET_Z, rot_x, rot_y, rot_z, x_temp, y_temp)
save_image(save_dir, filename, frame2numpy(message['frame']))
save_meta_data(save_dir, filename, message["location"], message["HeightAboveGround"], proj_points, message["CameraPosition"], message["CameraAngle"], message["time"])
count += 1
elif f == 15:
rot_x = gaussin_random_truncted(CAMERA_ROT_X_L, CAMERA_ROT_X_R, CAMERA_ROT_X, STD_DEV)
rot_y = gaussin_random_truncted(CAMERA_ROT_Y_L, CAMERA_ROT_Y_R, CAMERA_ROT_Y, STD_DEV)
rot_z = random.randint(CAMERA_ROT_Z_L, CAMERA_ROT_Z_R)
client.sendMessage(SetCameraPositionAndRotation(z=CAMERA_OFFSET_Z, rot_x=rot_x, rot_y=rot_y, rot_z=rot_z))
message = client.recvMessage()
rot_x_m, rot_y_m, rot_z_m = message['CameraAngle']
pos_x_m, pos_y_m, pos_z_m = message['location']
print('!!!!! rot', rot_x, rot_x_m, rot_y, rot_y_m, rot_z, rot_z_m)
print('!!!!! pos', pos_x_m, pos_y_m, pos_z_m)
print('!!!!! heightAboveGround', message["HeightAboveGround"])
elif f == 16:
client.sendMessage(StartRecording())
message = client.recvMessage()
heightAboveGround_3 = message['HeightAboveGround']
elif f == 19:
client.sendMessage(StopRecording())
message = client.recvMessage()
filename = f'{TRAVEL_HEIGHT}' + '_' + f'{run_count:04}' + '_' + f'{count:010}'
x_temp, y_temp, z_temp = message['CameraPosition']
heightAboveGround_4 = message['HeightAboveGround']
diff2 = abs(heightAboveGround_4 - heightAboveGround_3)
diff_12 = abs(heightAboveGround_4 - heightAboveGround_2)
if diff2 > ERROR_EPS or diff_12 > ERROR_EPS:
print(f'Warning!! heightAboveGround value unstable! h3={heightAboveGround_3:.2f}, h4={heightAboveGround_4:.2f}, diff2={diff2:.2f} \
diff_12:{diff_12:.2f}')
continue
rot_x, rot_y, rot_z = message['CameraAngle']
if rot_x > CAMERA_ROT_X_R or rot_x < CAMERA_ROT_X_L \
or rot_y > CAMERA_ROT_Y_R or rot_y < CAMERA_ROT_Y_L \
or rot_z > CAMERA_ROT_Z_R or rot_z < CAMERA_ROT_Z_L:
print(f'Warning!! camera rot error! rot_x={rot_x}, rot_y={rot_y}, rot_z={rot_z}')
continue
# print(rot_x, rot_y, rot_z)
proj_points = calculate_projection_points(heightAboveGround_2 + CAMERA_OFFSET_Z, rot_x, rot_y, rot_z, x_temp, y_temp)
save_image(save_dir, filename, frame2numpy(message['frame']))
save_meta_data(save_dir, filename, message["location"], message["HeightAboveGround"], proj_points, message["CameraPosition"], message["CameraAngle"], message["time"])
count += 1
else:
message = client.recvMessage()
# We tell DeepGTAV to stop
client.sendMessage(Stop())
client.close()