-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
361 lines (322 loc) · 10 KB
/
main.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import os
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
import OpenGL.GL as gl
import OpenGL.GLUT as glut
import OpenGL.GLU as glu
from camera import Camera
from ground import Ground
from plane import Plane
from rectangle3D import Rectangle3D
from scene import Scene
import json
# Initialize Pygame and set up the window
pygame.init()
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLEBUFFERS, 1)
pygame.display.gl_set_attribute(pygame.GL_MULTISAMPLESAMPLES, 4)
display = (1600, 900)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
# Set up the perspective and the initial view
gluPerspective(45, (display[0] / display[1]), 0.1, 50.0)
# glTranslatef(0.0, -5.0, -150) # Lowered the initial view closer to the ground
# Initialize OpenGL
glEnable(GL_DEPTH_TEST)
glEnable(GL_MULTISAMPLE)
glMatrixMode(GL_PROJECTION)
glMatrixMode(GL_MODELVIEW)
select = 0
def init():
glClearColor(0.0, 0.0, 0.0, 0.0)
glEnable(GL_DEPTH_TEST) # Enable depth testing
# Main loop
init()
w = 33.95
h = 55.65
ground = Ground(w, h, 1)
#plane = Plane(0, -3., 0, w, h)
rec_list = [Rectangle3D(-w*20 / 2, -0.501, -h*20 / 2, w*20, 1, h*20)]
cow1 = Rectangle3D(15,0,24, 1,1.8,2.5, True)
rec_list.append(cow1)
cow2 = Rectangle3D(7,0,24, 2.5,1.8,1, True)
rec_list.append(cow2)
scene = Scene(display, ground, rec_list, select)
# Add multiple cameras at different positions
m_height = 4.5
scene.add_camera(
Camera(
position=[12.5, m_height, 24.4],
fov_width=26.0,
fov_height=15.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="Hikvision",
id=1
)
)
scene.add_camera(
Camera(
position=[12.5, m_height, 24.4 - 7.5],
fov_width=26.0,
fov_height=15.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="Hikvision",
id=2
)
)
scene.add_camera(
Camera(
position=[12.5, m_height, 24.4 - 7.5 - 5],
fov_width=26.0,
fov_height=15.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="Hikvision",
id=3
)
)
scene.add_camera(
Camera(
position=[12.5, m_height, 24.4 - 7.5 - 5 - 6.5],
fov_width=26.0,
fov_height=15.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="Hikvision",
id=4
)
)
scene.add_camera(
Camera(
position=[12.5, m_height, 24.4 - 7.5 - 5 - 6.5 -5],
fov_width=26.0,
fov_height=15.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="Hikvision",
id=5
)
)
# Horizontal coverage at m_height : 5.509 meters
# Vertical coverage at 3.5 : 4.116 meters
# scene.add_camera(
# Camera(
# position=[12.5-10, 3.5, 24.4],
# fov_width=35.0,
# fov_height=20.0,
# ground=ground,
# plane=Plane(0, -m_height+0.5, 0, w, h),
# label="QNV-C8012",
# id=6
# )
# )
#
# scene.add_camera(
# Camera(
# position=[12.5-10, 3.5, 24.4 -6],
# fov_width=35.0,
# fov_height=20.0,
# ground=ground,
# plane=Plane(0, -m_height+0.5, 0, w, h),
# label="QNV-C8012",
# id=6
# )
# )
#
# scene.add_camera(
# Camera(
# position=[12.5-10, 3.5, 24.4 -6*2],
# fov_width=35.0,
# fov_height=20.0,
# ground=ground,
# plane=Plane(0, -m_height+0.5, 0, w, h),
# label="QNV-C8012",
# id=7
# )
# )
#
# scene.add_camera(
# Camera(
# position=[12.5-10, 3.5, 24.4-6*3],
# fov_width=35.0,
# fov_height=20.0,
# ground=ground,
# plane=Plane(0, -m_height+0.5, 0, w, h),
# label="QNV-C8012",
# id=8
# )
# )
#
# scene.add_camera(
# Camera(
# position=[12.5-10, 3.5, 24.4-6*4],
# fov_width=35.0,
# fov_height=20.0,
# ground=ground,
# plane=Plane(0, -m_height+0.5, 0, w, h),
# label="QNV-C8012",
# id=9
# )
# )
# Horizontal coverage at 3.5 : 7.0 meters
# Vertical coverage at 3.5 : 4.592 meters
scene.add_camera(
Camera(
position=[12.5-10, m_height, 24.4],
fov_width=35.0,
fov_height=20.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="QNV-C8012",
id=6
)
)
scene.add_camera(
Camera(
position=[12.5-10, m_height, 24.4 -6],
fov_width=35.0,
fov_height=20.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="QNV-C8012",
id=7
)
)
scene.add_camera(
Camera(
position=[12.5-10, m_height, 24.4 -6*2],
fov_width=35.0,
fov_height=20.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="QNV-C8012",
id=8
)
)
scene.add_camera(
Camera(
position=[12.5-10, m_height, 24.4-6*3],
fov_width=35.0,
fov_height=20.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="QNV-C8012",
id=9
)
)
scene.add_camera(
Camera(
position=[12.5-10, m_height, 24.4-6*4],
fov_width=35.0,
fov_height=20.0,
ground=ground,
plane=Plane(0, -m_height+0.5, 0, w, h),
label="QNV-C8012",
id=10
)
)
# Camera Model Sensor Size Focal Length Height (m) Horizontal Coverage (m) Vertical Coverage (m)
# Hikvision DS-2CD5546G0-IZHS 1/1.8" 2.8 mm 4.5 11.709 8.748
# Hanwha Vision QNV-C8012 1/2.8" 2.4 mm 4.5 16.236 9.243
file_path = 'data.json'
# Check if the file exists
if os.path.exists(file_path):
# Read the dictionary from the file
with open(file_path, 'r') as f:
try:
DATA = json.load(f)
print("Data loaded from the file:", DATA)
print("load last config...")
data_ = list(DATA.values())
for cam, d in zip(scene.cameras, data_):
cam.position[0] = d["x"]
cam.position[1] = d["y"]
cam.position[2] = d["z"]
cam.rot = d["rotation"]
cam.pyramid_vertices = cam.rotate_pyramid(cam.pyramid_vertices, -d["rotation"], 2)
cam.plane.y = d["plane_y"]
except Exception as e:
print(e)
os.remove(file_path)
else:
print(f"The file {file_path} does not exist.")
DATA = {}
running = True
while running:
keys = pygame.key.get_pressed()
if keys[pygame.K_1]:
scene.select = 0
if keys[pygame.K_2]:
scene.select = 1
if keys[pygame.K_3]:
scene.select = 2
if keys[pygame.K_4]:
scene.select = 3
if keys[pygame.K_5]:
scene.select = 4
if keys[pygame.K_6]:
scene.select = 5
if keys[pygame.K_7]:
scene.select = 6
if keys[pygame.K_8]:
scene.select = 7
if keys[pygame.K_9]:
scene.select = 8
if keys[pygame.K_0]: # Check if key '1' is pressed
if not pressed_last_frame: # Check if the key was not pressed last frame
select += 1 # Increment select
if select >= len(scene.cameras): # Wrap around if select exceeds the number of cameras
select = 0
print(select)
scene.select = select
pressed_last_frame = True # Set the flag to indicate key was pressed this frame
else:
pressed_last_frame = False # Reset the flag if the key is not pressed
if keys[pygame.K_i]:
scene.cameras[scene.select].pyramid_vertices = scene.cameras[scene.select].rotate_pyramid(scene.cameras[scene.select].pyramid_vertices, 0.1, 1)
if keys[pygame.K_k]:
scene.cameras[scene.select].pyramid_vertices = scene.cameras[scene.select].rotate_pyramid(scene.cameras[scene.select].pyramid_vertices, -0.1, 1)
if keys[pygame.K_j]:
scene.cameras[scene.select].pyramid_vertices = scene.cameras[scene.select].rotate_pyramid(scene.cameras[scene.select].pyramid_vertices, 0.1, 0)
if keys[pygame.K_l]:
scene.cameras[scene.select].pyramid_vertices = scene.cameras[scene.select].rotate_pyramid(scene.cameras[scene.select].pyramid_vertices, -0.1, 0)
if keys[pygame.K_u]:
scene.cameras[scene.select].pyramid_vertices = scene.cameras[scene.select].rotate_pyramid(scene.cameras[scene.select].pyramid_vertices, 0.5, 2)
scene.cameras[scene.select].rot -= 0.5
if keys[pygame.K_o]:
scene.cameras[scene.select].pyramid_vertices = scene.cameras[scene.select].rotate_pyramid(scene.cameras[scene.select].pyramid_vertices, -0.5, 2)
scene.cameras[scene.select].rot += 0.5
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
if event.buttons[0]: # Left mouse button pressed
dx, dy = event.rel
scene.cameras[scene.select].position[2] += (
dx * 0.05
) # Adjust camera position based on mouse movement
scene.cameras[scene.select].position[0] -= dy * 0.05
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4: # Mouse wheel scrolled up
# Move camera up
scene.cameras[scene.select].position[1] += 0.1
scene.cameras[scene.select].plane.y -= 0.1
elif event.button == 5: # Mouse wheel scrolled down
# Move camera down
scene.cameras[scene.select].position[1] -= 0.1
scene.cameras[scene.select].plane.y += 0.1
#self.update()
scene.cameras[scene.select].plane.update()
scene.handle_camera_movement()
pos = scene.cameras[scene.select].position
DATA[scene.select] = {"id":scene.cameras[scene.select].id, "plane_y":scene.cameras[scene.select].plane.y, "x":pos[0], "y":pos[1], "z":pos[2], "height": pos[1], "rotation": scene.cameras[scene.select].rot}
with open('data.json', 'w') as f:
json.dump(DATA, f, indent=4)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
scene.draw()
pygame.display.flip()
pygame.time.wait(10)
pygame.quit()