Skip to content

Commit a74cdfa

Browse files
fixing NMSBoxesBatched function accepts Rect objects instead of two diagonal corner coordinates (#296)
* fix: fixing NMSBoxesBatched function accepts Rect objects instead of two diagonal corner coordinates * feat: move to hf (#297) * Revert "fix: fixing NMSBoxesBatched function accepts Rect objects instead of two diagonal corner coordinates" This reverts commit d708146. modified: models/object_detection_yolox/demo.cpp modified: models/object_detection_yolox/demo.py modified: models/object_detection_yolox/yolox.py * revert: modify to best conform to the original code style --------- Co-authored-by: cuixing158 <1229261804@qq.com> Co-authored-by: Yuantao Feng <yuantao.feng@opencv.org.cn>
1 parent 8ac7b08 commit a74cdfa

3 files changed

Lines changed: 22 additions & 22 deletions

File tree

models/object_detection_yolox/demo.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,38 +104,38 @@ class YoloX {
104104
boxes.rowRange(r, r + 1) = *ptr * boxes.rowRange(r, r + 1);
105105
}
106106
// get boxes
107-
Mat boxes_xyxy(boxes.rows, boxes.cols, CV_32FC1, Scalar(1));
107+
Mat boxes_xywh(boxes.rows, boxes.cols, CV_32FC1, Scalar(1));
108108
Mat scores = dets.colRange(5, dets.cols).clone();
109109
vector<float> maxScores(dets.rows);
110110
vector<int> maxScoreIdx(dets.rows);
111-
vector<Rect2d> boxesXYXY(dets.rows);
111+
vector<Rect2d> boxesXYWH(dets.rows);
112112

113-
for (int r = 0; r < boxes_xyxy.rows; r++, ptr++)
113+
for (int r = 0; r < boxes_xywh.rows; r++, ptr++)
114114
{
115-
boxes_xyxy.at<float>(r, 0) = boxes.at<float>(r, 0) - boxes.at<float>(r, 2) / 2.f;
116-
boxes_xyxy.at<float>(r, 1) = boxes.at<float>(r, 1) - boxes.at<float>(r, 3) / 2.f;
117-
boxes_xyxy.at<float>(r, 2) = boxes.at<float>(r, 0) + boxes.at<float>(r, 2) / 2.f;
118-
boxes_xyxy.at<float>(r, 3) = boxes.at<float>(r, 1) + boxes.at<float>(r, 3) / 2.f;
115+
boxes_xywh.at<float>(r, 0) = boxes.at<float>(r, 0) - boxes.at<float>(r, 2) / 2.f;
116+
boxes_xywh.at<float>(r, 1) = boxes.at<float>(r, 1) - boxes.at<float>(r, 3) / 2.f;
117+
boxes_xywh.at<float>(r, 2) = boxes.at<float>(r, 2);
118+
boxes_xywh.at<float>(r, 3) = boxes.at<float>(r, 3);
119119
// get scores and class indices
120120
scores.rowRange(r, r + 1) = scores.rowRange(r, r + 1) * dets.at<float>(r, 4);
121121
double minVal, maxVal;
122122
Point maxIdx;
123123
minMaxLoc(scores.rowRange(r, r+1), &minVal, &maxVal, nullptr, &maxIdx);
124124
maxScoreIdx[r] = maxIdx.x;
125125
maxScores[r] = float(maxVal);
126-
boxesXYXY[r].x = boxes_xyxy.at<float>(r, 0);
127-
boxesXYXY[r].y = boxes_xyxy.at<float>(r, 1);
128-
boxesXYXY[r].width = boxes_xyxy.at<float>(r, 2);
129-
boxesXYXY[r].height = boxes_xyxy.at<float>(r, 3);
126+
boxesXYWH[r].x = boxes_xywh.at<float>(r, 0);
127+
boxesXYWH[r].y = boxes_xywh.at<float>(r, 1);
128+
boxesXYWH[r].width = boxes_xywh.at<float>(r, 2);
129+
boxesXYWH[r].height = boxes_xywh.at<float>(r, 3);
130130
}
131131

132132
vector<int> keep;
133-
NMSBoxesBatched(boxesXYXY, maxScores, maxScoreIdx, this->confThreshold, this->nmsThreshold, keep);
133+
NMSBoxesBatched(boxesXYWH, maxScores, maxScoreIdx, this->confThreshold, this->nmsThreshold, keep);
134134
Mat candidates(int(keep.size()), 6, CV_32FC1);
135135
int row = 0;
136136
for (auto idx : keep)
137137
{
138-
boxes_xyxy.rowRange(idx, idx + 1).copyTo(candidates(Rect(0, row, 4, 1)));
138+
boxes_xywh.rowRange(idx, idx + 1).copyTo(candidates(Rect(0, row, 4, 1)));
139139
candidates.at<float>(row, 4) = maxScores[idx];
140140
candidates.at<float>(row, 5) = float(maxScoreIdx[idx]);
141141
row++;

models/object_detection_yolox/demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ def vis(dets, srcimg, letterbox_scale, fps=None):
5858
score = det[-2]
5959
cls_id = int(det[-1])
6060

61-
x0, y0, x1, y1 = box
61+
x0, y0, w, h = box
6262

6363
text = '{}:{:.1f}%'.format(classes[cls_id], score * 100)
6464
font = cv.FONT_HERSHEY_SIMPLEX
6565
txt_size = cv.getTextSize(text, font, 0.4, 1)[0]
66-
cv.rectangle(res_img, (x0, y0), (x1, y1), (0, 255, 0), 2)
66+
cv.rectangle(res_img, (x0, y0 , w, h), (0, 255, 0), 2)
6767
cv.rectangle(res_img, (x0, y0 + 1), (x0 + txt_size[0] + 1, y0 + int(1.5 * txt_size[1])), (255, 255, 255), -1)
6868
cv.putText(res_img, text, (x0, y0 + txt_size[1]), font, 0.4, (0, 0, 0), thickness=1)
6969

models/object_detection_yolox/yolox.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ def postprocess(self, outputs):
5050

5151
# get boxes
5252
boxes = dets[:, :4]
53-
boxes_xyxy = np.ones_like(boxes)
54-
boxes_xyxy[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.
55-
boxes_xyxy[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.
56-
boxes_xyxy[:, 2] = boxes[:, 0] + boxes[:, 2] / 2.
57-
boxes_xyxy[:, 3] = boxes[:, 1] + boxes[:, 3] / 2.
53+
boxes_xywh = np.ones_like(boxes)
54+
boxes_xywh[:, 0] = boxes[:, 0] - boxes[:, 2] / 2.
55+
boxes_xywh[:, 1] = boxes[:, 1] - boxes[:, 3] / 2.
56+
boxes_xywh[:, 2] = boxes[:, 2]
57+
boxes_xywh[:, 3] = boxes[:, 3]
5858

5959
# get scores and class indices
6060
scores = dets[:, 4:5] * dets[:, 5:]
6161
max_scores = np.amax(scores, axis=1)
6262
max_scores_idx = np.argmax(scores, axis=1)
6363

64-
keep = cv2.dnn.NMSBoxesBatched(boxes_xyxy.tolist(), max_scores.tolist(), max_scores_idx.tolist(), self.confThreshold, self.nmsThreshold)
64+
keep = cv2.dnn.NMSBoxesBatched(boxes_xywh.tolist(), max_scores.tolist(), max_scores_idx.tolist(), self.confThreshold, self.nmsThreshold)
6565

66-
candidates = np.concatenate([boxes_xyxy, max_scores[:, None], max_scores_idx[:, None]], axis=1)
66+
candidates = np.concatenate([boxes_xywh, max_scores[:, None], max_scores_idx[:, None]], axis=1)
6767
if len(keep) == 0:
6868
return np.array([])
6969
return candidates[keep]

0 commit comments

Comments
 (0)