def detect(img_path, model_path): file_id = utils.get_file_id(img_path) img, img_raw, scale = read_img(img_path) head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4]) dict = torch.load(opt.caffe_pretrain_path) head_detector.load_state_dict(dict['model']) trainer = Head_Detector_Trainer(head_detector).cuda() trainer.load(model_path) img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st print("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) for i in range(pred_bboxes_.shape[0]): ymin, xmin, ymax, xmax = pred_bboxes_[i, :] utils.draw_bounding_box_on_image_array(img_raw, ymin, xmin, ymax, xmax) cv2.imshow('asdf', img_raw) cv2.waitKey()
def detect(img_path, model_path): file_id = utils.get_file_id(img_path) img, img_raw, scale = read_img(img_path) head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4]) trainer = Head_Detector_Trainer(head_detector).cuda() trainer.load(model_path) img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st print("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) for i in range(pred_bboxes_.shape[0]): ymin, xmin, ymax, xmax = pred_bboxes_[i, :] utils.draw_bounding_box_on_image_array(img_raw, ymin, xmin, ymax, xmax) plt.axis('off') plt.imshow(img_raw) if SAVE_FLAG == 1: plt.savefig(os.path.join(opt.test_output_path, file_id + '.png'), bbox_inches='tight', pad_inches=0) else: plt.show()
def detect(img_path, model_path): file_id = utils.get_file_id(img_path) img, img_raw, scale,scale_ = read_img(img_path) print("dim1",img_raw.ndim) head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2,4]) trainer = Head_Detector_Trainer(head_detector).cuda() trainer.load(model_path) img = at.totensor(img) img = img[None, : ,: ,:] img = img.cuda().float() st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st print ("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) for i in range(pred_bboxes_.shape[0]): print(i) ymin, xmin, ymax, xmax = pred_bboxes_[i,:] print(ymin, xmin, ymax, xmax) image_raw=Image.fromarray(np.uint8(img_raw)) utils.draw_bounding_box_on_image(image_raw,ymin*scale_, xmin*scale_, ymax*scale_, xmax*scale_) img_raw=np.array(image_raw) image_raw=Image.fromarray(np.uint8(img_raw)) if SAVE_FLAG == 1: #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png') image_raw.save(write_path+'/'+os.path.basename(img_path)) frame_end = cv2.imread(write_path+'/'+os.path.basename(img_path)) cv2.imshow("frame_end",frame_end) key_end = cv2.waitKey(1) & 0xFF else: image_raw.show()
def find_head(self, cv2_im): """ This function will filter background of person's body. """ f = Image.fromarray(cv2_im) f.convert('RGB') img_raw = np.asarray(f, dtype=np.uint8) img_raw_final = img_raw.copy() img = np.asarray(f, dtype=np.float32) img = img.transpose((2,0,1)) _, H, W = img.shape img = preprocess(img) _, o_H, o_W = img.shape scale = o_H / H img = at.totensor(img) img = img[None, : ,: ,:] img = img.cuda().float() st = time.time() pred_bboxes_, _ = self.head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st print ("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) box_list = [] type_list = [] prob_list = [] for i in range(pred_bboxes_.shape[0]): (y1, x1, y2, x2) = pred_bboxes_[i,:]/scale box_list.append((x1, y1, x2-x1, y2-y1)) type_list.append("person") prob_list.append(0.5) return box_list, type_list, prob_list
def forward(self, imgs, bboxs, scale): n,_,_ = bboxs.size() if n != 1: raise ValueError('Currently only batch size 1 is supported.') _, _, H, W = imgs.size() img_size = (H, W) features = self.head_detector.extractor(imgs) rpn_locs, rpn_scores, rois, rois_scores, anchor = self.head_detector.rpn(features, img_size, scale) bbox = bboxs[0] rpn_score = rpn_scores[0] rpn_loc = rpn_locs[0] # ------------------ RPN losses -------------------# gt_rpn_loc, gt_rpn_label = self.anchor_target_creator(at.tonumpy(bbox),anchor,img_size) gt_rpn_label = at.tovariable(gt_rpn_label).long() gt_rpn_loc = at.tovariable(gt_rpn_loc) rpn_loc_loss = head_detector_loss( rpn_loc, gt_rpn_loc, gt_rpn_label.data, self.rpn_sigma) rpn_cls_loss = F.cross_entropy(rpn_score, gt_rpn_label.cuda(), ignore_index=-1) _gt_rpn_label = gt_rpn_label[gt_rpn_label > -1] _rpn_score = at.tonumpy(rpn_score)[at.tonumpy(gt_rpn_label) > -1] self.rpn_cm.add(at.totensor(_rpn_score, False), _gt_rpn_label.data.long()) losses = [rpn_loc_loss, rpn_cls_loss] losses = losses + [sum(losses)] return LossTuple(*losses), rois, rois_scores
def detect(img_read, model_path, head_detector): img, img_raw, scale, scale_ = read_img(img_read) #print("dim1",img_raw.ndim) img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st thispic_count = 0 #print ("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) for i in range(pred_bboxes_.shape[0]): thispic_count = i #print(i) ymin, xmin, ymax, xmax = pred_bboxes_[i, :] #print(ymin, xmin, ymax, xmax) image_raw = Image.fromarray(np.uint8(img_raw)) utils.draw_bounding_box_on_image(image_raw, ymin * scale_, xmin * scale_, ymax * scale_, xmax * scale_) img_raw = np.array(image_raw) image_raw = Image.fromarray(np.uint8(img_raw)) if SAVE_FLAG == 1: pass #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png') #image_raw.save(write_path+'/'+os.path.basename(img_path)) else: image_raw.show() return thispic_count
def detect(img): img, img_raw, scale = read_img(img) img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) # for i in range(pred_bboxes_.shape[0]): # ymin, xmin, ymax, xmax = pred_bboxes_[i,:] # utils.draw_bounding_box_on_image_array(img_raw,ymin, xmin, ymax, xmax) print(str(pred_bboxes_.shape[0])) return pred_bboxes_.shape[0]
def detect(img_read, write_path, head_detector, img_path): global show img, img_raw, scale, scale_ = read_img(img_read) print("dim1", img_raw.ndim) img_raw_2 = img_raw.copy() img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st print("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) for i in range(pred_bboxes_.shape[0]): print(i) ymin, xmin, ymax, xmax = pred_bboxes_[i, :] print(ymin, xmin, ymax, xmax) image_raw = Image.fromarray(np.uint8(img_raw)) image_raw_2 = Image.fromarray(np.uint8(img_raw_2)) utils.draw_bounding_box_on_image(image_raw, ymin * scale_, xmin * scale_, ymax * scale_, xmax * scale_) #cv2.rectangle(image_raw_2, (int(xmin*scale_), int(ymin*scale_)), (int(xmax*scale_), int(ymax*scale_)), 255,cv2.FILLED) #draw_rect = ImageDraw.Draw(image_raw_2) #draw_rect.rectangle((xmin*scale_, ymin*scale_, xmax*scale_, ymax*scale_), 'red') img_raw = np.array(image_raw) img_raw_2 = np.array(image_raw_2) image_raw = Image.fromarray(np.uint8(img_raw)) image_raw_2 = Image.fromarray(np.uint8(img_raw_2)) image_raw = cv2.cvtColor(np.asarray(image_raw), cv2.COLOR_RGB2BGR) image_raw_2 = cv2.cvtColor(np.asarray(image_raw_2), cv2.COLOR_RGB2BGR) if SAVE_FLAG == 1: #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png') #image_raw.save(write_path+'/'+os.path.basename(img_path)) #frame_end = cv2.imread(write_path+'/'+os.path.basename(img_path)) #cv2.imshow("frame_end",frame_end) cv2.imwrite(write_path + '/' + os.path.basename(img_path), image_raw) cv2.imshow("image_raw", image_raw) key = cv2.waitKey(1) & 0xFF if key == ord("q"): show = 0 else: image_raw.show()
def detect(head_detector, trainer, frame): #file_id = utils.get_file_id(img_path) img, img_raw, scale = read_img(frame) img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() # st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) # et = time.time() # tt = et - st # print ("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) matrixs = [] #存放一张图里面的所有人头矩形 need_delete = set() #需要被删除的矩形 height = np.shape(img_raw)[0] width = np.shape(img_raw)[1] #绘制感兴趣区域 # cv2.line(img_raw, (leftstart, top), (rightend, top), (0,0,255),2) # cv2.line(img_raw, (leftstart, bottom), (rightend, bottom), (0,0,255),2) # cv2.line(img_raw, (leftstart, top), (leftstart, bottom), (0, 0, 255),2) # cv2.line(img_raw, (rightend, top), (rightend, bottom), (0, 0, 255),2) #添加所有的矩形区域到list for i in range(pred_bboxes_.shape[0]): ymin, xmin, ymax, xmax = pred_bboxes_[i, :] matrixs.append([xmin, ymin, xmax, ymax]) #计算重叠的矩形,并标记需要删除的矩形 for i in np.arange(len(matrixs)): if (calcRectanctArea(matrixs[i]) <= LIMIT_MIN_VALUE_AREA): need_delete.add(i) for j in np.arange(len(matrixs)): if (i != j): if (mat_inter(matrixs[i], matrixs[j])): if (calcRectanctArea(matrixs[i]) > calcRectanctArea( matrixs[j])): need_delete.add(j) else: need_delete.add(i) deleted_matrix = [] for i in np.arange(len(matrixs)): if (i not in need_delete): deleted_matrix.append(matrixs[i]) del (matrixs) #释放空间 # 划分区域 return img_raw, deleted_matrix
def predict(self, x, scale=1., mode='evaluate', thresh=0.01): if mode == 'evaluate': nms_thresh = 0.3 #0.3 score_thresh = thresh elif mode == 'visualize': nms_thresh = 0.3 #0.3 score_thresh = thresh _, _, rois, rois_scores, _ = self.forward(x, scale=scale) roi = at.totensor(rois) probabilities = at.tonumpy(F.softmax(at.tovariable(rois_scores))) _, _, H, W = x.size() size = (H,W) roi[:, 0::2] = (roi[:, 0::2]).clamp(min=0, max=size[0]) roi[:, 1::2] = (roi[:, 1::2]).clamp(min=0, max=size[1]) roi_raw = at.tonumpy(roi) probabilities = np.squeeze(probabilities) bbox, score = self._suppress(roi_raw, probabilities, nms_thresh, score_thresh) return bbox, score
def detect2(img, img_raw, scale, model_path, file_file): head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4]) trainer = Head_Detector_Trainer(head_detector).cuda() # load model trainer.load(model_path) img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() # predict model pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) for i in range(pred_bboxes_.shape[0]): ymin, xmin, ymax, xmax = pred_bboxes_[i, :] file_file.write( str(int(xmin / scale)) + ',' + str(int(ymin / scale)) + ',' + str(int(xmax / scale)) + ',' + str(int(ymax / scale)) + ',' + '0' + ' ')
def detect(img_read, write_path, head_detector, img_path, face_detect): global show global people_num_detect global best_frame people_num_cache = 0 img, img_raw, scale, scale_ = read_img(img_read) #print("dim1",img_raw.ndim) img_raw_2 = img_raw.copy() img = at.totensor(img) img = img[None, :, :, :] img = img.cuda().float() st = time.time() pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH) et = time.time() tt = et - st print("[INFO] Head detection over. Time taken: {:.4f} s".format(tt)) for i in range(pred_bboxes_.shape[0]): #print(i) people_num_cache = i + 1 ymin, xmin, ymax, xmax = pred_bboxes_[i, :] #print(ymin, xmin, ymax, xmax) image_raw = Image.fromarray(np.uint8(img_raw)) image_raw_2 = Image.fromarray(np.uint8(img_raw_2)) utils.draw_bounding_box_on_image(image_raw, ymin * scale_, xmin * scale_, ymax * scale_, xmax * scale_) #cv2.rectangle(image_raw_2, (int(xmin*scale_), int(ymin*scale_)), (int(xmax*scale_), int(ymax*scale_)), 255,cv2.FILLED) draw_rect = ImageDraw.Draw(image_raw_2) draw_rect.rectangle( (xmin * scale_, ymin * scale_, xmax * scale_, ymax * scale_), 'red') img_raw = np.array(image_raw) img_raw_2 = np.array(image_raw_2) image_raw = Image.fromarray(np.uint8(img_raw)) image_raw_2 = Image.fromarray(np.uint8(img_raw_2)) image_raw = cv2.cvtColor(np.asarray(image_raw), cv2.COLOR_RGB2BGR) image_raw_2 = cv2.cvtColor(np.asarray(image_raw_2), cv2.COLOR_RGB2BGR) rects, landmarks = face_detect.detect_face(image_raw_2, 20) people_num_cache += len(rects) for (i, rect) in enumerate(rects): cv2.rectangle(image_raw, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 2) if SAVE_FLAG == 1: #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png') #image_raw.save(write_path+'/'+os.path.basename(img_path)) #frame_end = cv2.imread(write_path+'/'+os.path.basename(img_path)) #cv2.imshow("frame_end",frame_end) cv2.imwrite(write_path + '/' + os.path.basename(img_path), image_raw) cv2.imshow("image_raw", image_raw) key = cv2.waitKey(1) & 0xFF if key == ord("q"): show = 0 print("best frame is **********" + best_frame + " num is *************" + str(people_num_detect)) else: image_raw.show() return people_num_cache