Skip to content

Commit 3ddf7bd

Browse files
Rough in memx support
1 parent 1daf5a4 commit 3ddf7bd

9 files changed

Lines changed: 770 additions & 98 deletions

CMakeLists.txt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ endif()
3232
#set(CMAKE_INSTALL_ALWAYS ON)
3333

3434
# Default CLFAGS and CXXFLAGS:
35-
set(CMAKE_C_FLAGS_RELEASE "-O2")
36-
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
37-
set(CMAKE_C_FLAGS_DEBUG "-g")
38-
set(CMAKE_CXX_FLAGS_DEBUG "-g")
39-
set(CMAKE_C_FLAGS_OPTIMISED "-O3")
40-
set(CMAKE_CXX_FLAGS_OPTIMISED "-O3")
35+
set(CMAKE_C_FLAGS_RELEASE "-O2 -fopenmp")
36+
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -fopenmp")
37+
set(CMAKE_C_FLAGS_DEBUG "-g -fopenmp")
38+
set(CMAKE_CXX_FLAGS_DEBUG "-g -fopenmp")
39+
set(CMAKE_C_FLAGS_OPTIMISED "-O3 -fopenmp")
40+
set(CMAKE_CXX_FLAGS_OPTIMISED "-O3 -fopenmp")
4141
set(CMAKE_INCLUDE_CURRENT_DIR ON)
4242
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
4343
set(CMAKE_CXX_STANDARD 17)
@@ -357,10 +357,21 @@ endif()
357357

358358
find_file(HAVE_MEMX_H memx.h /usr/include/memx)
359359
if(HAVE_MEMX_H)
360-
find_library(MEMX_LIBRARIES mx_accl)
360+
find_library(MEMX_LIBRARIES memx)
361361
if(MEMX_LIBRARIES)
362-
set(optlibsfound "${optlibsfound} mx_accl")
362+
set(optlibsfound "${optlibsfound} memx")
363363
list(APPEND ZM_BIN_LIBS "${MEMX_LIBRARIES}")
364+
else()
365+
set(optlibsnotfound "${optlibsnotfound} memx")
366+
endif()
367+
endif()
368+
369+
find_file(HAVE_MX_ACCL_H MxAccl.h /usr/include/memx/accl)
370+
if(HAVE_MX_ACCL_H)
371+
find_library(MX_ACCL_LIBRARIES mx_accl)
372+
if(MX_ACCL_LIBRARIES)
373+
set(optlibsfound "${optlibsfound} mx_accl")
374+
list(APPEND ZM_BIN_LIBS "${MX_ACCL_LIBRARIES}")
364375
else()
365376
set(optlibsnotfound "${optlibsnotfound} mx_accl")
366377
endif()

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ set(ZM_BIN_SRC_FILES
3737
zm_libvnc_camera.cpp
3838
zm_local_camera.cpp
3939
zm_memx.cpp
40+
zm_mx_accl.cpp
41+
zm_memx_yolo_core.cpp
42+
zm_memx_yolov8.cpp
4043
zm_monitor.cpp
4144
zm_monitor_monitorlink.cpp
4245
zm_monitor_rtsp2web.cpp

src/zm_ai_server.cpp

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,17 @@ void Usage() {
8787
Quadra quadra;
8888
#endif
8989

90+
#undef HAVE_MEMX_H
9091
#if HAVE_MEMX_H
9192
#include "zm_memx.h"
9293
MemX *memx;
9394
#endif
9495

96+
#if HAVE_MX_ACCL_H
97+
#include "zm_mx_accl.h"
98+
MxAccl *mx_accl;
99+
#endif
100+
95101
int main(int argc, char *argv[]) {
96102
self = argv[0];
97103

@@ -172,17 +178,30 @@ int main(int argc, char *argv[]) {
172178
return 0;
173179
}
174180
#endif
181+
175182
#if HAVE_MEMX_H
176183
Debug(1, "Including MemX");
177184
memx = new MemX();
178-
if (!memx->setup("yolov5", "/var/cache/zoneminder/models/YOLO_v5_nano_leaky_416_416_3_tensorflow.dfp")) {
185+
if (!memx->setup("yolov8", "/var/cache/zoneminder/models/YOLO_v8_nano_640_640_3_onnx.dfp")) {
186+
//if (!memx->setup("yolov5", "/var/cache/zoneminder/models/YOLO_v5_nano_leaky_416_416_3_tensorflow.dfp")) {
179187
delete memx;
180188
memx = nullptr;
181189
return 0;
182190
}
183191
#else
184192
Debug(1, "Not Including MemX");
185193
#endif
194+
#if HAVE_MX_ACCL_H
195+
Debug(1, "Including MemX_Accl");
196+
mx_accl = new MxAccl();
197+
if (!mx_accl->setup("yolov8", "/var/cache/zoneminder/models/YOLO_v8_nano_640_640_3_onnx.dfp")) {
198+
delete mx_accl;
199+
mx_accl = nullptr;
200+
return 0;
201+
}
202+
#else
203+
Debug(1, "Not Including MXACCL");
204+
#endif
186205

187206
std::unordered_map<unsigned int, std::shared_ptr<Monitor>> monitors;
188207
std::unordered_map<unsigned int, AIThread *> threads;
@@ -206,6 +225,9 @@ int main(int argc, char *argv[]) {
206225
#endif
207226
#if HAVE_MEMX_H
208227
, memx
228+
#endif
229+
#if HAVE_MX_ACCL_H
230+
, mx_accl
209231
#endif
210232
);
211233
}
@@ -278,6 +300,13 @@ void AIThread::Inference() {
278300
sleep(1);
279301
}
280302
#endif
303+
#ifdef HAVE_MX_ACCL_H
304+
Debug(1, "Starting for mx_accl");
305+
while (!terminate_ and !( mx_accl_job = mx_accl->get_job() )) {
306+
Warning("Waiting for job");
307+
sleep(1);
308+
}
309+
#endif
281310

282311
#if HAVE_QUADRA
283312
#if !SOFT_DRAWBOX
@@ -300,40 +329,35 @@ void AIThread::Inference() {
300329

301330
while (!(terminate_ or zm_terminate)) {
302331
std::shared_ptr<ZMPacket> packet = nullptr;
303-
Debug(1, "Getting lock");
332+
Debug(4, "Getting shm lock");
304333
// Need to hold the lock because it guards shared_mem as well.
305334
std::unique_lock<std::mutex> lck(mutex_);
306-
Debug(1, "Having lock");
335+
Debug(4, "Having lock");
307336
while (!(send_queue.size() or terminate_)) {
308-
Debug(1, "Waiting");
309337
condition_.wait(lck);
310-
Debug(1, "Send queue size for monitor %d is %zu", monitor_->Id(), send_queue.size());
338+
Debug(4, "Send queue size for monitor %d is %zu", monitor_->Id(), send_queue.size());
311339
}
312340
if (terminate_) break;
313341
packet = send_queue.front();
314342

315343
if (packet) {
316344
Monitor::SharedData *shared_data = monitor_->getSharedData();
317345
Debug(4, "Sending image %d for monitor %d", packet->image_index, monitor_->Id());
318-
#ifdef HAVE_UNTETHER_H
319-
speedai->send_image(job, packet->image);
320346

321347
Image *ai_image = monitor_->GetAnalysisImage(packet->image_index);
322-
nlohmann::json detections = speedai->receive_detections(job, monitor_->ObjectDetection_Object_Threshold());
323-
Debug(1, "detections %s", detections.dump().c_str());
324348

325-
if (detections.size()) {
326-
draw_boxes(packet->image, ai_image, detections, monitor_->LabelSize(), monitor_->LabelSize());
327-
//draw_boxes(drawbox_filter, drawbox_filter_ctx, packet->image, ai_image, detections, monitor_->LabelSize(), monitor_->LabelSize());
328-
} else {
329-
ai_image->Assign(*packet->image);
330-
}
349+
#ifdef HAVE_UNTETHER_H
350+
speedai->send_image(job, packet->image);
351+
nlohmann::json detections = speedai->receive_detections(job, monitor_->ObjectDetection_Object_Threshold());
331352
#endif
332353
#ifdef HAVE_MEMX_H
333354
memx->send_image(memx_job, packet->image);
334-
335-
Image *ai_image = monitor_->GetAnalysisImage(packet->image_index);
336355
nlohmann::json detections = memx->receive_detections(memx_job, monitor_->ObjectDetection_Object_Threshold());
356+
#endif
357+
#ifdef HAVE_MX_ACCL_H
358+
mx_accl->send_image(mx_accl_job, packet->image);
359+
nlohmann::json detections = mx_accl->receive_detections(mx_accl_job, monitor_->ObjectDetection_Object_Threshold());
360+
#endif
337361
Debug(1, "detections %s", detections.dump().c_str());
338362

339363
if (detections.size()) {
@@ -342,7 +366,6 @@ void AIThread::Inference() {
342366
} else {
343367
ai_image->Assign(*packet->image);
344368
}
345-
#endif
346369

347370
shared_data->last_analysis_index = packet->image_index;
348371

@@ -436,7 +459,7 @@ void AIThread::Run() {
436459
(send_queue.size() <= static_cast<unsigned int>(image_buffer_count))
437460
) {
438461
image_index = shared_data->last_decoder_index % image_buffer_count;
439-
Debug(3, "Doing SpeedAI on monitor %d. Decoder index is %d=%d Our index is %d=%d, queue %zu",
462+
Debug(3, "Doing ai on monitor %d. Decoder index is %d=%d Our index is %d=%d, queue %zu",
440463
monitor_->Id(),
441464
decoder_image_count, shared_data->last_decoder_index,
442465
analysis_image_count, image_index, send_queue.size());
@@ -469,7 +492,7 @@ void AIThread::Run() {
469492
}
470493

471494
} else {
472-
Debug(4, "Not Doing SpeedAI on monitor %d. Decoder count is %d index %d Our count is %d, last_index is %d, index %d",
495+
Debug(4, "Not Doing ai on monitor %d. Decoder count is %d index %d Our count is %d, last_index is %d, index %d",
473496
monitor_->Id(), decoder_image_count, shared_data->last_decoder_index,
474497
shared_data->analysis_image_count, shared_data->last_analysis_index, image_index);
475498

@@ -651,6 +674,9 @@ AIThread::AIThread(const std::shared_ptr<Monitor> monitor
651674
#endif
652675
#if HAVE_MEMX_H
653676
, MemX *p_memx
677+
#endif
678+
#if HAVE_MX_ACCL_H
679+
, MxAccl *p_mx_accl
654680
#endif
655681
) :
656682
monitor_(monitor), terminate_(false)
@@ -660,6 +686,9 @@ AIThread::AIThread(const std::shared_ptr<Monitor> monitor
660686
#if HAVE_MEMX_H
661687
, memx(p_memx)
662688
#endif
689+
#if HAVE_MX_ACCL_H
690+
, mx_accl(p_mx_accl)
691+
#endif
663692
#if HAVE_QUADRA
664693
,drawbox_filter(nullptr)
665694
#endif

src/zm_ai_server.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,23 @@ extern "C" {
2424
#if HAVE_MEMX_H
2525
#include "zm_memx.h"
2626
#endif
27+
#if HAVE_MX_ACCL_H
28+
#include "zm_mx_accl.h"
29+
#endif
2730

2831
class Monitor;
2932

3033
class AIThread {
3134
public:
32-
explicit AIThread(const std::shared_ptr<Monitor> monitor
3335
#if HAVE_UNTETHER_H
34-
, SpeedAI *speedai
36+
explicit AIThread(const std::shared_ptr<Monitor> monitor, SpeedAI *speedai);
3537
#endif
3638
#if HAVE_MEMX_H
37-
, MemX *memx
39+
explicit AIThread(const std::shared_ptr<Monitor> monitor, MemX *memx);
40+
#endif
41+
#if HAVE_MX_ACCL_H
42+
explicit AIThread(const std::shared_ptr<Monitor> monitor, MxAccl *memx);
3843
#endif
39-
);
4044
~AIThread();
4145
AIThread(AIThread &rhs) = delete;
4246
AIThread(AIThread &&rhs) = delete;
@@ -62,6 +66,10 @@ class AIThread {
6266
MemX *memx;
6367
MemX::Job *memx_job;
6468
#endif
69+
#if HAVE_MX_ACCL_H
70+
MxAccl *mx_accl;
71+
MxAccl::Job *mx_accl_job;
72+
#endif
6573
#if HAVE_QUADRA
6674
Quadra::filter_worker *drawbox_filter;
6775
#endif

0 commit comments

Comments
 (0)