Skip to content

Commit 129cf13

Browse files
dynamically allocate model_ctx, clear model if setup fails, fixes crash of model file not found
1 parent 8ce4695 commit 129cf13

2 files changed

Lines changed: 9 additions & 7 deletions

File tree

src/zm_netint_yolo.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Quadra_Yolo::Quadra_Yolo(Monitor *p_monitor, bool p_use_hwframe) :
3131
nms_thresh(0.45),
3232
network_ctx(nullptr),
3333
model(nullptr),
34-
//model_ctx(nullptr),
34+
model_ctx(nullptr),
3535
net_frame(),
3636
sw_scale_ctx(nullptr),
3737

@@ -67,8 +67,8 @@ Quadra_Yolo::~Quadra_Yolo() {
6767
ni_packet_buffer_free(&net_frame.api_packet.data.packet);
6868
if (network_ctx)
6969
ni_cleanup_network_context(network_ctx, use_hwframe);
70-
if (model) {
71-
model->destroy_model(&model_ctx);
70+
if (model && model_ctx) {
71+
model->destroy_model(model_ctx);
7272
}
7373

7474
free(last_roi);
@@ -128,10 +128,12 @@ bool Quadra_Yolo::setup(
128128
devid /*dev_id*/, 30 /* keep alive */, model_format, model_width, model_height, nbg_file.c_str());
129129
if (ret != 0) {
130130
Error("failed to allocate network context on card %d", devid);
131+
model = nullptr;
131132
return false;
132133
}
133134

134-
ret = model->create_model(&model_ctx, &network_ctx->network_data, obj_thresh, nms_thresh, model_width, model_height);
135+
model_ctx = new YoloModelCtx();
136+
ret = model->create_model(model_ctx, &network_ctx->network_data, obj_thresh, nms_thresh, model_width, model_height);
135137
if (ret != 0) {
136138
Error("failed to initialize yolo model");
137139
return false;
@@ -226,7 +228,7 @@ int Quadra_Yolo::detect(std::shared_ptr<ZMPacket> in_packet, std::shared_ptr<ZMP
226228
int Quadra_Yolo::receive_detection(std::shared_ptr<ZMPacket> packet) {
227229
SystemTimePoint starttime = std::chrono::system_clock::now();
228230
/* pull filtered frames from the filtergraph */
229-
int ret = ni_get_network_output(network_ctx, use_hwframe, &net_frame, true /* blockable */, true /*convert*/, model_ctx.out_tensor);
231+
int ret = ni_get_network_output(network_ctx, use_hwframe, &net_frame, true /* blockable */, true /*convert*/, model_ctx->out_tensor);
230232
SystemTimePoint endtime = std::chrono::system_clock::now();
231233
Debug(1, "*** AI inference took %.2f seconds ***",FPSeconds(endtime-starttime).count());
232234
if (ret != 0 && ret != NIERROR(EAGAIN)) {
@@ -694,7 +696,7 @@ int Quadra_Yolo::ni_read_roi(AVFrame *out, int frame_count) {
694696
struct roi_box *roi_box = nullptr;
695697
int roi_num = 0;
696698

697-
int ret = model->ni_get_boxes(&model_ctx, out->width, out->height, &roi_box, &roi_num);
699+
int ret = model->ni_get_boxes(model_ctx, out->width, out->height, &roi_box, &roi_num);
698700
if (ret < 0) {
699701
Error( "failed to get roi.");
700702
if (roi_box) free(roi_box);

src/zm_netint_yolo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Quadra_Yolo {
3232
float nms_thresh = 0.45;
3333
NiNetworkContext *network_ctx;
3434
YoloModel *model;
35-
YoloModelCtx model_ctx;
35+
YoloModelCtx *model_ctx;
3636
NiNetworkFrame net_frame;
3737
//ni_session_data_io_t *ai_frame;
3838

0 commit comments

Comments
 (0)