translation this code to c:def filter_box(org_box, conf_thres, iou_thres): org_box = np.squeeze(org_box) conf = org_box[..., 4] > conf_thres box = org_box[conf == True] print('box:') print(box.shape) cls_cinf = box[..., 5:] cls = [] for i in range(len(cls_cinf)): cls.append(int(np.argmax(cls_cinf[i]))) all_cls = list(set(cls)) output = [] for i in range(len(all_cls)): curr_cls = all_cls[i] curr_cls_box = [] curr_out_box = [] for j in range(len(cls)): if cls[j] == curr_cls: box[j][5] = curr_cls curr_cls_box.append(box[j][:6]) curr_cls_box = np.array(curr_cls_box) curr_cls_box = xywh2xyxy(curr_cls_box) curr_out_box = nms(curr_cls_box, iou_thres) for k in curr_out_box: output.append(curr_cls_box[k]) output = np.array(output) return output
时间: 2023-06-27 10:02:18 浏览: 100
极速检测:YOLO模型优化策略全解析
以下是该代码的 C 语言版本:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX_BOX_NUM 1000
typedef struct {
float x, y, w, h, score;
int cls;
} Box;
float max(float a, float b) {
return a > b ? a : b;
}
float min(float a, float b) {
return a < b ? a : b;
}
float iou(Box a, Box b) {
float inter_xmin = max(a.x - a.w / 2, b.x - b.w / 2);
float inter_ymin = max(a.y - a.h / 2, b.y - b.h / 2);
float inter_xmax = min(a.x + a.w / 2, b.x + b.w / 2);
float inter_ymax = min(a.y + a.h / 2, b.y + b.h / 2);
float inter_w = max(inter_xmax - inter_xmin, 0.f);
float inter_h = max(inter_ymax - inter_ymin, 0.f);
float inter_area = inter_w * inter_h;
float a_area = a.w * a.h;
float b_area = b.w * b.h;
float union_area = a_area + b_area - inter_area;
return inter_area / union_area;
}
void xywh2xyxy(float* box) {
float x = box[0], y = box[1], w = box[2], h = box[3];
box[0] = x - w / 2;
box[1] = y - h / 2;
box[2] = x + w / 2;
box[3] = y + h / 2;
}
void nms(Box* boxes, int box_num, float iou_thres, Box* out_boxes, int* out_box_num) {
int* mask = (int*)malloc(sizeof(int) * box_num);
int i, j, k;
for (i = 0; i < box_num; ++i) {
mask[i] = 1;
}
for (i = 0; i < box_num; ++i) {
if (!mask[i]) {
continue;
}
out_boxes[(*out_box_num)++] = boxes[i];
for (j = i + 1; j < box_num; ++j) {
if (!mask[j]) {
continue;
}
float iou_val = iou(boxes[i], boxes[j]);
if (iou_val > iou_thres) {
mask[j] = 0;
}
}
}
free(mask);
}
Box* filter_box(float* org_box, float conf_thres, float iou_thres, int* box_num) {
int i, j;
float* box = (float*)malloc(sizeof(float) * MAX_BOX_NUM * 6);
int conf_box_num = 0;
int cls[MAX_BOX_NUM];
int cls_num = 0;
for (i = 0; i < MAX_BOX_NUM; ++i) {
float* cur_box = org_box + i * 6;
if (cur_box[4] <= conf_thres) {
continue;
}
for (j = 0; j < 5; ++j) {
box[conf_box_num * 6 + j] = cur_box[j];
}
cls[conf_box_num] = (int)round(cur_box[5]);
++conf_box_num;
}
for (i = 0; i < conf_box_num; ++i) {
int cur_cls = cls[i];
int is_new_cls = 1;
for (j = 0; j < cls_num; ++j) {
if (cur_cls == cls[j]) {
is_new_cls = 0;
break;
}
}
if (is_new_cls) {
cls[cls_num++] = cur_cls;
}
}
Box* output = (Box*)malloc(sizeof(Box) * MAX_BOX_NUM);
int output_box_num = 0;
for (i = 0; i < cls_num; ++i) {
int cur_cls = cls[i];
float curr_cls_box[MAX_BOX_NUM][6];
int curr_cls_box_num = 0;
for (j = 0; j < conf_box_num; ++j) {
if (cls[j] == cur_cls) {
box[j * 6 + 5] = cur_cls;
int k;
for (k = 0; k < 6; ++k) {
curr_cls_box[curr_cls_box_num][k] = box[j * 6 + k];
}
++curr_cls_box_num;
}
}
for (j = 0; j < curr_cls_box_num; ++j) {
xywh2xyxy(curr_cls_box[j]);
}
Box curr_out_box[MAX_BOX_NUM];
int curr_out_box_num = 0;
nms((Box*)curr_cls_box, curr_cls_box_num, iou_thres, curr_out_box, &curr_out_box_num);
for (j = 0; j < curr_out_box_num; ++j) {
output[output_box_num++] = curr_out_box[j];
}
}
free(box);
*box_num = output_box_num;
return output;
}
```
阅读全文