def __call__(self, ori_img): # img to tensor assert isinstance(ori_img, np.ndarray), "input must be a numpy array!" # 转换类型 img = ori_img.astype(np.float) / 255. img = cv2.resize(img, self.size) # https://blog.csdn.net/zzw000000/article/details/80320040 # 将numpy的格式转化为tensor格式 # permute(dims) 将tensor的维度换位 # unsqueeze()这个函数主要是对数据维度进行扩充。 ??? img = torch.from_numpy(img).float().permute(2, 0, 1).unsqueeze(0) # forward with torch.no_grad(): img = img.to(self.device) out_boxes = self.net(img) boxes = get_all_boxes(out_boxes, self.conf_thresh, self.net.num_classes, use_cuda=self.use_cuda)[0] boxes = nms(boxes, self.nms_thresh) # print(boxes) # plot boxes if self.is_plot: return self.plot_bbox(ori_img, boxes) if len(boxes) == 0: return None, None, None height, width = ori_img.shape[:2] boxes = np.vstack(boxes) bbox = np.empty_like(boxes[:, :4]) if self.is_xywh: # bbox x y w h bbox[:, 0] = boxes[:, 0] * width bbox[:, 1] = boxes[:, 1] * height bbox[:, 2] = boxes[:, 2] * width bbox[:, 3] = boxes[:, 3] * height else: # bbox xmin ymin xmax ymax bbox[:, 0] = (boxes[:, 0] - boxes[:, 2] / 2.0) * width bbox[:, 1] = (boxes[:, 1] - boxes[:, 3] / 2.0) * height bbox[:, 2] = (boxes[:, 0] + boxes[:, 2] / 2.0) * width bbox[:, 3] = (boxes[:, 1] + boxes[:, 3] / 2.0) * height cls_conf = boxes[:, 5] cls_ids = boxes[:, 6] # print(cls_ids) return bbox, cls_conf, cls_ids
def __call__(self, ori_img): # img to tensor assert isinstance(ori_img, np.ndarray), "input must be a numpy array!" img = ori_img.astype(np.float) / 255. img = cv2.resize(img, self.size) img = torch.from_numpy(img).float().permute(2, 0, 1).unsqueeze(0) # forward with torch.no_grad(): img = img.to(self.device) out_boxes = self.net(img) boxes = get_all_boxes(out_boxes, self.conf_thresh, self.net.num_classes, self.use_cuda)[0] boxes = nms(boxes, self.nms_thresh) print(type(boxes)) re_boxes = boxes # print(boxes) # plot boxes if self.is_plot: return self.plot_bbox(ori_img, boxes) if len(boxes) == 0: return None, None, None height, width = ori_img.shape[:2] boxes = np.vstack(boxes) bbox = np.empty_like(boxes[:, :4]) if self.is_xywh: # bbox x y w h bbox[:, 0] = boxes[:, 0] * width bbox[:, 1] = boxes[:, 1] * height bbox[:, 2] = boxes[:, 2] * width bbox[:, 3] = boxes[:, 3] * height else: # bbox xmin ymin xmax ymax bbox[:, 0] = (boxes[:, 0] - boxes[:, 2] / 2.0) * width bbox[:, 1] = (boxes[:, 1] - boxes[:, 3] / 2.0) * height bbox[:, 2] = (boxes[:, 0] + boxes[:, 2] / 2.0) * width bbox[:, 3] = (boxes[:, 1] + boxes[:, 3] / 2.0) * height cls_conf = boxes[:, 5] cls_ids = boxes[:, 6] # print(bbox[:,0], bbox[:,1], bbox[:,2], bbox[:,3]) # return bbox, cls_conf, cls_ids return re_boxes, cls_conf, cls_ids
def __call__(self, ori_img): # img to tensor图像到张量 assert isinstance(ori_img, np.ndarray), "input must be a numpy array!" img = ori_img.astype(np.float) / 255. img = cv2.resize(img, self.size) img = torch.from_numpy(img).float().permute(2, 0, 1).unsqueeze(0) # forward(只进行前向传播且不计算梯度) with torch.no_grad(): img = img.to(self.device) out_boxes = self.net(img) boxes = get_all_boxes(out_boxes, self.conf_thresh, self.num_classes, use_cuda=self.use_cuda) # batch size is 1 # boxes = nms(boxes, self.nms_thresh) boxes = post_process(boxes, self.net.num_classes, self.conf_thresh, self.nms_thresh)[0].cpu() # bbox xmin ymin xmax ymax boxes = boxes[boxes[:, -2] > self.score_thresh, :] if len(boxes) == 0: bbox = torch.FloatTensor([]).reshape([0, 4]) cls_conf = torch.FloatTensor([]) cls_ids = torch.LongTensor([]) else: height, width = ori_img.shape[:2] bbox = boxes[:, :4] if self.is_xywh: # bbox x y w h bbox = xyxy_to_xywh(bbox) bbox = bbox * torch.FloatTensor([[width, height, width, height]]) cls_conf = boxes[:, 5] cls_ids = boxes[:, 6].long() return bbox.numpy(), cls_conf.numpy(), cls_ids.numpy()