def draw_result(out, im_scale, clss, bbox, rects, nms_thresh, conf, im_name): out = cv.resize(out, None, None, fx=im_scale, fy=im_scale, interpolation=cv.INTER_LINEAR) for cls_id in range(1, 21): _cls = clss[:, cls_id][:, np.newaxis] _bbx = bbox[:, cls_id * 4: (cls_id + 1) * 4] dets = np.hstack((_bbx, _cls)) keep = nms(dets, nms_thresh) dets = dets[keep, :] orig_rects = cuda.cupy.asnumpy(rects)[keep, 1:] inds = np.where(dets[:, -1] >= conf)[0] if CLASSES[cls_id] == 'person' and len(inds) == 0: print('not detected...') print >> box_write, '0,%s,%s'%(CLASSES[cls_id], os.path.basename(im_name)) for i in inds: _bbox = dets[i, :4] x1, y1, x2, y2 = orig_rects[i] width = x2 - x1 height = y2 - y1 center_x = x1 + 0.5 * width center_y = y1 + 0.5 * height dx, dy, dw, dh = map(int, _bbox) _center_x = dx * width + center_x _center_y = dy * height + center_y _width = np.exp(dw) * width _height = np.exp(dh) * height x1 = _center_x - 0.5 * _width y1 = _center_y - 0.5 * _height x2 = _center_x + 0.5 * _width y2 = _center_y + 0.5 * _height if CLASSES[cls_id] == 'person': print(('1,%s,%s,%s,%s,%s,%s,%s') %(CLASSES[cls_id], os.path.basename(im_name),x1,y1,x2,y2,dets[i,4])) print >> box_write, ('1,%s,%s,%s,%s,%s,%s,%s') %(CLASSES[cls_id], os.path.basename(im_name),x1,y1,x2,y2,dets[i,4]) else: print >> other_write, ('1,%s,%s,%s,%s,%s,%s,%s') %(CLASSES[cls_id], os.path.basename(im_name),x1,y1,x2,y2,dets[i,4]) """ cv.rectangle(out, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2) ret, baseline = cv.getTextSize(CLASSES[cls_id], cv.FONT_HERSHEY_SIMPLEX, 1.0, 1) cv.rectangle(out, (int(x1), int(y2) - ret[1] - baseline), (int(x1) + ret[0], int(y2)), (0, 0, 255), -1) cv.putText(out, CLASSES[cls_id], (int(x1), int(y2) - baseline), cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 1) """ print CLASSES[cls_id], dets[i, 4] return out
def draw_result(out, im_scale, clss, bbox, rects, nms_thresh, conf): out = cv.resize(out, None, None, fx=im_scale, fy=im_scale, interpolation=cv.INTER_LINEAR) for cls_id in range(1, 21): _cls = clss[:, cls_id][:, np.newaxis] _bbx = bbox[:, cls_id * 4:(cls_id + 1) * 4] dets = np.hstack((_bbx, _cls)) keep = nms(dets, nms_thresh) dets = dets[keep, :] orig_rects = cuda.cupy.asnumpy(rects)[keep, 1:] inds = np.where(dets[:, -1] >= conf)[0] for i in inds: _bbox = dets[i, :4] x1, y1, x2, y2 = orig_rects[i] width = x2 - x1 height = y2 - y1 center_x = x1 + 0.5 * width center_y = y1 + 0.5 * height dx, dy, dw, dh = map(int, _bbox) _center_x = dx * width + center_x _center_y = dy * height + center_y _width = np.exp(dw) * width _height = np.exp(dh) * height x1 = _center_x - 0.5 * _width y1 = _center_y - 0.5 * _height x2 = _center_x + 0.5 * _width y2 = _center_y + 0.5 * _height cv.rectangle(out, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2, cv.LINE_AA) ret, baseline = cv.getTextSize(CLASSES[cls_id], cv.FONT_HERSHEY_SIMPLEX, 1.0, 1) cv.rectangle(out, (int(x1), int(y2) - ret[1] - baseline), (int(x1) + ret[0], int(y2)), (0, 0, 255), -1) cv.putText(out, CLASSES[cls_id], (int(x1), int(y2) - baseline), cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 1, cv.LINE_AA) print CLASSES[cls_id], dets[i, 4] return out
def draw_result(out, im_scale, clss, bbox, rects, nms_thresh, conf): out = cv.resize(out, None, None, fx=im_scale, fy=im_scale, interpolation=cv.INTER_LINEAR) for cls_id in range(1, 21): _cls = clss[:, cls_id][:, np.newaxis] _bbx = bbox[:, cls_id * 4: (cls_id + 1) * 4] dets = np.hstack((_bbx, _cls)) keep = nms(dets, nms_thresh) dets = dets[keep, :] orig_rects = cuda.cupy.asnumpy(rects)[keep, 1:] inds = np.where(dets[:, -1] >= conf)[0] for i in inds: _bbox = dets[i, :4] x1, y1, x2, y2 = orig_rects[i] width = x2 - x1 height = y2 - y1 center_x = x1 + 0.5 * width center_y = y1 + 0.5 * height dx, dy, dw, dh = map(int, _bbox) _center_x = dx * width + center_x _center_y = dy * height + center_y _width = np.exp(dw) * width _height = np.exp(dh) * height x1 = _center_x - 0.5 * _width y1 = _center_y - 0.5 * _height x2 = _center_x + 0.5 * _width y2 = _center_y + 0.5 * _height cv.rectangle(out, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2, cv.LINE_AA) ret, baseline = cv.getTextSize(CLASSES[cls_id], cv.FONT_HERSHEY_SIMPLEX, 1.0, 1) cv.rectangle(out, (int(x1), int(y2) - ret[1] - baseline), (int(x1) + ret[0], int(y2)), (0, 0, 255), -1) cv.putText(out, CLASSES[cls_id], (int(x1), int(y2) - baseline), cv.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 1, cv.LINE_AA) print CLASSES[cls_id], dets[i, 4] return out
def nms(dets, thresh): """Apply classic DPM-style greedy NMS.""" if dets.shape[0] == 0: return [] return cython_nms.nms(dets, thresh)