-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_threads.py
408 lines (323 loc) · 13 KB
/
create_threads.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from multiprocessing import Process, shared_memory, Manager
import multiprocessing
from ultralytics import YOLO
import numpy as np
import argparse
import logging
import random
import signal
import torch
import time
import cv2
import sys
import os
from facial_recognition.commons.utils import (
run_facial_recognition_pipeline,
draw_information,
draw_box,
draw_person_keypoints,
)
from storage_service import store_video_data
from api import get_cameras, ping_cameras
from gui import process_status_gui
from config import Config
from sort import Sort
# Lambda function for generating shared memory stream names based on index
generate_shm_stream_name = lambda index: f"camera_{index}_stream"
# Set up the logger
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] [%(module)s:%(funcName)s:%(lineno)d] - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# Create a FileHandler and set its formatter
file_handler = logging.FileHandler("create_threads.log")
file_formatter = logging.Formatter(
"%(asctime)s [%(levelname)s] [%(module)s:%(funcName)s:%(lineno)d] - %(message)s"
)
file_handler.setFormatter(file_formatter)
# Add the FileHandler to the logger
logging.getLogger().addHandler(file_handler)
# Function for sending a frame to shared memory
def send_frame_to_shared_memory(frame, shm):
"""
Send a frame to the shared memory segment.
Args:
frame: The frame to be sent.
shm: The shared memory segment.
Returns:
None
Copies the frame data into the shared memory segment to be accessed by other processes.
"""
shared_array = np.ndarray(frame.shape, dtype=frame.dtype, buffer=shm.buf)
shared_array[:] = frame[:]
# Handle Ctr+C interruptions
def signal_handler(sig, frame):
"""
Function to handle the keyboard interrupt signal (Ctrl+C) and perform cleanup operations.
Args:
sig: The signal number.
frame: The current stack frame.
Returns:
None
This function is triggered when a keyboard interrupt (Ctrl+C) is detected. It terminates
all active processes, closes threads, and exits the program gracefully.
"""
logging.error("Interruption detected. Cleaning up processes and threads...")
for process in processes:
try:
process.terminate()
process.join(timeout=5) # Wait for the process to terminate gracefully
except Exception as e:
logging.error(f"Error while terminating process: {e}")
close_threads(urls)
sys.exit(0)
# Function for processing camera streams
def process_camera(index, url, name, camera_id, shared_dict):
"""
Process the camera streams.
Args:
index: The index of the camera stream.
url: The URL of the camera stream.
shared_dict: The shared dictionary for storing camera stream information.
Returns:
None
Captures frames from the camera stream, processes them, and updates the shared dictionary with relevant information.
"""
process_id = os.getpid()
model = YOLO(os.path.join("models", "yolov8n-pose.pt"))
tracker = Sort()
# Configure the GPU device for YOLO if CUDA is available on the system
if torch.cuda.is_available():
device_id = 0 # You may adjust this based on your specific use case
device_name = torch.cuda.get_device_name(device_id)
logging.debug(
f"[PID:{process_id}] - CUDA available: Using {device_name} (ID {device_id})."
)
torch.cuda.set_device(device_id)
else:
logging.debug(
f"[PID:{process_id}] - CUDA is not available. Using CPU for computations."
)
cap = cv2.VideoCapture(url)
frames = []
face_objects = []
box_objects = []
keypoint_objects = []
logging.debug(f"[PID:{process_id}] - Starting process for stream at index {index}")
try:
logging.debug(
f"[PID:{process_id}] - Creating SharedMemory for stream at index {index}"
)
shm = shared_memory.SharedMemory(
create=True,
size=Config.FRAME_SIZE_BYTES,
name=generate_shm_stream_name(index),
)
except FileExistsError:
logging.warning(
f"[PID:{process_id}] - Shared memory segment already exists for stream at index {index}"
)
shm = shared_memory.SharedMemory(name=generate_shm_stream_name(index))
try:
storage_start_time = time.time()
processing_start_time = time.time()
while True:
ret, frame = cap.read()
if ret:
function_start_time = time.time()
# Normalize frame to specified dimensions
frame = cv2.resize(frame, (Config.FRAME_WIDTH, Config.FRAME_HEIGHT))
frames.append(frame)
# Store frames as video locally
segmentation_interval = Config.VIDEO_SEGMENTATION_INTERVAL
elapsed_storage_time = function_start_time - storage_start_time
elapsed_processing_time = function_start_time - processing_start_time
# frame = cv2.flip(frame, -1)
if elapsed_storage_time >= Config.VIDEO_SEGMENTATION_INTERVAL:
estimated_fps = len(frames) / (elapsed_storage_time)
store_video_data(frames, camera_id, int(estimated_fps))
frames.clear()
storage_start_time = function_start_time
# Vision processing
if elapsed_processing_time >= Config.PROCESSING_SEGMENTATION_INTERVAL:
estimated_fps = len(frames) / (elapsed_processing_time)
logging.debug(
f"[PID:{process_id}] - Performing recognition for stream {camera_id}"
)
results = model(frame, verbose=False, stream=True)
for res in results:
filtered_indices = np.where(
res.boxes.conf.cpu().numpy()
> Config.BOX_CONFIDENCE_THRESHOLD
)[0]
boxes = (
res.boxes.xyxy.cpu().numpy()[filtered_indices].astype(int)
)
box_objects = boxes
keypoints = res.keypoints.xy.cpu().numpy()
keypoint_objects = keypoints
tracks = tracker.update(boxes)
tracks = tracks.astype(int)
for box, track_id, keypoint in zip(boxes, tracks, keypoints):
x1, y1, x2, y2 = box
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 1)
# draw_person_keypoints(frame, keypoint)
# Add track ID as text on the frame
cv2.putText(
frame,
f"Track ID: {track_id[-1]}",
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
1,
)
face_objects = run_facial_recognition_pipeline(frame)
processing_start_time = function_start_time
for face in face_objects:
frame = draw_information(
frame,
face["face_coordinates"],
face["user_name"],
face["euclidean_l2"],
)
for box in box_objects:
frame = draw_box(frame, box)
cv2.imshow(f"Process {process_id}", frame)
cv2.waitKey(1)
send_frame_to_shared_memory(frame, shm)
# Update the shared dictionary with relevant information
shared_dict[index] = {
"execution_time": time.time() - function_start_time,
"camera_name": name,
"stream_name": generate_shm_stream_name(index),
"faces_detected": 0,
}
except Exception as e:
logging.error(
f"[PID:{process_id}] - Error occurred while processing stream at index {index}: {e}"
)
finally:
cap.release()
terminate_shm(shm)
cv2.destroyAllWindows()
# Terminate all SharedMemory objects
def terminate_shm(shm):
"""
Close and unlink the shared memory segment.
Args:
shm: The shared memory segment.
Returns:
None
"""
try:
shm.close()
logging.debug("Shared memory segment closed successfully.")
except Exception as e:
logging.error(f"An error occurred while closing the shared memory segment: {e}")
try:
shm.unlink()
logging.debug("Shared memory segment unlinked successfully.")
except FileNotFoundError:
logging.warning("FileNotFoundError: Shared memory segment not found.")
except Exception as e:
logging.error(
f"An error occurred while unlinking the shared memory segment: {e}"
)
# Function to close threads and shared memory segments
def close_threads(urls):
"""
Close threads and shared memory segments.
Args:
urls: The list of URLs for the camera streams.
Returns:
None
Closes all active threads and shared memory segments associated with the camera streams.
"""
for i, url in enumerate(urls):
shm_name = generate_shm_stream_name(i)
try:
# Attempt to open the shared memory segment
with shared_memory.SharedMemory(name=shm_name) as shm:
# Close and unlink the shared memory segment
shm.close()
shm.unlink()
logging.debug(f"Shared memory segment {shm_name} closed successfully.")
except FileNotFoundError:
logging.debug(
f"Shared memory segment {shm_name} could not be closed because it was not found."
)
except Exception as e:
logging.error(
f"Error occurred while closing shared memory segment {shm_name}: {e}"
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-U",
"--update",
action="store_true",
default=False,
help="Skip camera updates when this flag is specified",
)
parser.add_argument(
"-M",
"--monitor",
action="store_true",
default=False,
help="Display monitoring information on a GUI during the process",
)
parser.add_argument(
"--loglevel",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="DEBUG",
help="Set the desired log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)",
)
parser.add_argument(
"--offline",
action="store_true",
default=False,
help="Run in offline mode; skip actions that require an online connection",
)
args = parser.parse_args()
# Set the log level based on the provided --loglevel argument
numeric_level = getattr(logging, args.loglevel, None)
if not isinstance(numeric_level, int):
raise ValueError("Invalid log level: %s" % args.loglevel)
logging.getLogger().setLevel(numeric_level)
processes = []
signal.signal(signal.SIGINT, signal_handler)
# Use a manager for shared dictionary
with Manager() as manager:
shared_dict = manager.dict()
cameras = get_cameras(update_cameras=args.update, offline=args.offline)
urls = [
camera["camera_url"]
for camera in cameras
if camera["camera_status"] != "offline"
]
names = [
camera["camera_name"]
for camera in cameras
if camera["camera_status"] != "offline"
]
ids = [
camera["camera_id"]
for camera in cameras
if camera["camera_status"] != "offline"
]
for i, (url, name, camera_id) in enumerate(zip(urls, names, ids)):
logging.debug(f"Appending process for stream index: {i}")
p = Process(
target=process_camera, args=(i, url, name, camera_id, shared_dict)
)
p.start()
processes.append(p)
monitor_p = Process(target=process_status_gui, args=(shared_dict, args.monitor))
monitor_p.start()
processes.append(monitor_p)
for p in processes:
p.join()
# Close all shared memory segments
close_threads(urls)