def _detect(self, imgmsg, rects_msg): bridge = cv_bridge.CvBridge() im_orig = bridge.imgmsg_to_cv2(imgmsg, desired_encoding='bgr8') im, im_scale = img_preprocessing(im_orig, self.pixel_means) rects_orig = jsk_recognition_utils.rects_msg_to_ndarray(rects_msg) if len(rects_orig) == 0: return rects = rects_orig * im_scale scores, bbox_pred = self._im_detect(im, rects) rects = RectArray(header=imgmsg.header) labels = [] label_proba = [] for cls_id in range(1, len(self.target_names)): _cls = scores[:, cls_id][:, np.newaxis] _bbx = bbox_pred[:, cls_id * 4: (cls_id + 1) * 4] dets = np.hstack((_bbx, _cls)) keep = nms(dets, self.nms_thresh) dets = dets[keep, :] orig_rects = cuda.cupy.asnumpy(rects_orig)[keep, :] inds = np.where(dets[:, -1] >= self.conf_thresh)[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 = _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 rect = Rect(x=x1, y=y1, width=x2-x1, height=y2-y1) rects.rects.append(rect) labels.append(cls_id) label_proba.append(dets[:, -1][i]) # publish classification result clss = ClassificationResult( header=imgmsg.header, classifier=self.classifier_name, target_names=self.target_names, labels=labels, label_names=[self.target_names[l] for l in labels], label_proba=label_proba, ) self._pub_rects.publish(rects) self._pub_class.publish(clss)
def _detect(self, imgmsg, rects_msg): bridge = cv_bridge.CvBridge() im_orig = bridge.imgmsg_to_cv2(imgmsg, desired_encoding='bgr8') im, im_scale = img_preprocessing(im_orig, self.pixel_means) rects_orig = jsk_recognition_utils.rects_msg_to_ndarray(rects_msg) if len(rects_orig) == 0: return rects = rects_orig * im_scale scores, bbox_pred = self._im_detect(im, rects) rects = RectArray(header=imgmsg.header) labels = [] label_proba = [] for cls_id in range(1, len(self.target_names)): _cls = scores[:, cls_id][:, np.newaxis] _bbx = bbox_pred[:, cls_id * 4:(cls_id + 1) * 4] dets = np.hstack((_bbx, _cls)) keep = nms(dets, self.nms_thresh) dets = dets[keep, :] orig_rects = cuda.cupy.asnumpy(rects_orig)[keep, :] inds = np.where(dets[:, -1] >= self.conf_thresh)[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 = _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 rect = Rect(x=x1, y=y1, width=x2 - x1, height=y2 - y1) rects.rects.append(rect) labels.append(cls_id) label_proba.append(dets[:, -1][i]) # publish classification result clss = ClassificationResult( header=imgmsg.header, classifier=self.classifier_name, target_names=self.target_names, labels=labels, label_names=[self.target_names[l] for l in labels], label_proba=label_proba, ) self._pub_rects.publish(rects) self._pub_class.publish(clss)
def _draw_result(self, im, clss, bbox, rects, nms_thresh, conf): for cls_id in range(1, len(self.target_names)): _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, :] 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 = _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 cv2.rectangle(im, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2, cv2.CV_AA) ret, baseline = cv2.getTextSize(self.target_names[cls_id], cv2.FONT_HERSHEY_SIMPLEX, 1.0, 1) cv2.rectangle(im, (int(x1), int(y2) - ret[1] - baseline), (int(x1) + ret[0], int(y2)), (0, 0, 255), -1) cv2.putText( im, self.target_names[cls_id], (int(x1), int(y2) - baseline), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 1, cv2.CV_AA, ) return im
def _draw_result(self, im, clss, bbox, rects, nms_thresh, conf): for cls_id in range(1, len(self.target_names)): _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, :] 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 = _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 cv2.rectangle(im, (int(x1), int(y1)), (int(x2), int(y2)), (0, 0, 255), 2, cv2.CV_AA) ret, baseline = cv2.getTextSize( self.target_names[cls_id], cv2.FONT_HERSHEY_SIMPLEX, 1.0, 1) cv2.rectangle(im, (int(x1), int(y2) - ret[1] - baseline), (int(x1) + ret[0], int(y2)), (0, 0, 255), -1) cv2.putText(im, self.target_names[cls_id], (int(x1), int(y2) - baseline), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), 1, cv2.CV_AA) return im