def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ht = np.array([]) print(ht.shape) ax.imshow(im, aspect='equal') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] cls_int = [] for i in range(len(dets)): cls_int.append(cls_ind) cls_int = np.array(cls_int) dets = np.hstack((dets, cls_int[:, np.newaxis])).astype(np.float32) if ht.shape[0] > 0: ht = np.vstack((ht, dets)).astype(np.float32) else: ht = dets print(ht.shape) keep = nms(ht, 0.8) ht = ht[keep, :] vis_detections(im, CLASSES, ax, ht, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.8 NMS_THRESH = 0.3 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # 将ax做为参数传入vis_detections vis_detections(im, cls, dets, ax, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image #im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) im_file = os.path.join(path1, image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.1 thresh = CONF_THRESH im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] #vis_detections(im, cls, dets, thresh=CONF_THRESH) inds = np.where(dets[:, -1] >= thresh)[0] if len(inds) == 0: continue for i in inds: bbox = dets[i, :4] score = dets[i, -1] ax.add_patch( plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor='red', linewidth=3.5)) ax.text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(cls, score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') plt.axis('off') plt.tight_layout() plt.draw() os.chdir(path2) plt.savefig(im_name)
def boxdetect(sess, net, im_file, output_path): """Detect object classes in an image using pre-computed object proposals.""" # Load the image im_file = im_file.replace('\\', '/') im = cv2.imread(im_file) image_name = im_file.split(r'/')[-1] # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 geetcode_bbox = [] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] bbox = vis_detections(im, cls, dets, image_name, output_path, thresh=CONF_THRESH) geetcode_bbox.append(bbox) return geetcode_bbox
def demo_video(sess, net, frame, camera_url): """Detect object classes in an image using pre-computed object proposals.""" im = frame timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() # Visualize detections for each class CONF_THRESH = 0.6 # threshold NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if cls == 'crow' or cls == 'magpie' or cls == 'pigeon' or cls == 'swallow' \ or cls == 'sparrow' and len(inds) != 0: if time.time() - timer_trigger.start_time > residence_time: images = vis_detections_video(im, cls, dets, timer.start_time, timer.total_time, inds, CONF_THRESH) socket_client_target_detection(cls, len(inds), images, time.ctime(), camera_url, True) timer_trigger.tic() # 修改起始时间 else: images = vis_detections_video(im, cls, dets, timer.start_time, timer.total_time, inds, CONF_THRESH) socket_client_target_detection(cls, len(inds), images, time.ctime(), camera_url, False) elif cls == 'airplane' and len(inds) != 0: pass elif cls == 'person' and len(inds) != 0: pass else: pass
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join( 'G:\DeepLearning\Project\LJProject\Faster-RCNN\Faster-RCNN-TensorFlow-Python3-master-NEU\data\demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # 此处的boxes是经过bbox_pre修正过的Bbox的位置坐标,并且对于预测的每一个类别,都有一个预测的Bbox坐标 scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 #对每个类别进行一次画图 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) #利用非极大值抑制,从300个proposal中剔除掉与更大得分的proposal的IOU大于0.1的proposal keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def SignalImage_Test(sess, net,image_path): im=cv2.imread(image_path) timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.4 NMS_THRESH = 0.35 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] # print('\nboxes:',boxes) # print('\ncls_boxes:',cls_boxes) # print('\n ',boxes.shape) # print(len(cls_boxes),len(boxes))] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, frame, targetsize, keypoint, descrip): """ Detect object classes in an image using pre-computed object proposals. frame为传入的图像帧 """ # Detect all object classes and regress object bounds scores, boxes = im_detect(sess, net, frame) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] if cls == 'car': max_score, max_pos = vis_detections(frame, cls, dets, targetsize, keypoint, descrip, thresh=CONF_THRESH) print('similarity:', max_score) print('position:', max_pos) return max_pos
def drawDefect(im, scores, boxes, conf_thresh=0.6, nms_thresh=0.2): """ #缺陷画框 :param scores: :param boxes: :param conf_thresh: :param nms_thresh:非极大值抑制 :return: """ # CONF_THRESH = 0.6 # NMS_THRESH = 0.2 # 非极大值抑制 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # 水平方向堆叠 keep = nms(dets, nms_thresh) dets = dets[keep, :] # print(dets.shape) inds = np.where(dets[:, -1] >= conf_thresh)[0] # 大于阈值的缺陷索引index if len(inds) > 0: # 缺陷 vis_detections(im, cls, dets, inds) # 画框
def demo(sess, net, image_name): # 加载目标图片 im_file = os.path.join('test_images', image_name) im = cv2.imread(im_file) # 检测所有对象类并回归对象边界 timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # 对每个检查的检测进行可视化 CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # 因为跳过了背景background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_dections(im, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') if cfg_key == "TRAIN": pre_nms_topN = cfg.FLAGS.rpn_train_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_train_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_train_nms_thresh else: pre_nms_topN = cfg.FLAGS.rpn_test_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_test_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_test_nms_thresh # 从config文件读取配置 # post_nms_topN(执行NMS算法后proposal的数量) # nms_thresh(NMS阈值) # 学习参数:rpn_bbox_pred # 原始anchor给出的proposal通过学习参数转换为与gt接近的边框,裁剪掉超出图片的部分 im_info = im_info[0] # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4)) scores = scores.reshape((-1, 1)) proposals = bbox_transform_inv(anchors, rpn_bbox_pred) # bbox_transform_inv: # 每个anchor的边框学习之前得到的偏移量(这里的偏移量就是需要学习的rpn_bbox_pred)做位移和缩放,获取最终的预测边框 # 也就是原始proposal A,通过学习rpn_bbox_pred中的参数,得到一个与ground truth G相近的预测边框G' proposals = clip_boxes(proposals, im_info[:2]) # clip_boxes: # 裁剪掉超出原始图片边框的部分 # Pick the top region proposals order = scores.ravel().argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] proposals = proposals[order, :] scores = scores[order] # Non-maximal suppression keep = nms(np.hstack((proposals, scores)), nms_thresh) # Pick th top region proposals after NMS # 执行NMS算法,获取最终的proposals if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] scores = scores[keep] # Only support single image as input batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False))) return blob, scores
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') if cfg_key == "TRAIN": # 预处理,非极大值抑制前,保留12000个bbox pre_nms_topN = cfg.FLAGS.rpn_train_pre_nms_top_n # 非极大值抑制后,选取2000个bbox post_nms_topN = cfg.FLAGS.rpn_train_post_nms_top_n # 非极大值抑制阈值0.7,与最大概率的bbox IoU超过0.7的bbox会被丢弃 nms_thresh = cfg.FLAGS.rpn_train_nms_thresh else: # 预处理,非极大值抑制前,6000个bbox pre_nms_topN = cfg.FLAGS.rpn_test_pre_nms_top_n # 非极大值抑制后,选取300个bbox post_nms_topN = cfg.FLAGS.rpn_test_post_nms_top_n # nms阈值0.7 nms_thresh = cfg.FLAGS.rpn_test_nms_thresh im_info = im_info[0] # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4)) scores = scores.reshape((-1, 1)) proposals = bbox_transform_inv(anchors, rpn_bbox_pred) proposals = clip_boxes(proposals, im_info[:2]) # Pick the top region proposals order = scores.ravel().argsort()[::-1] # 按照分类得分,取前pre_nms_topN个bbox(12000/6000) if pre_nms_topN > 0: order = order[:pre_nms_topN] proposals = proposals[order, :] scores = scores[order] # 做nms # Non-maximal suppression keep = nms(np.hstack((proposals, scores)), nms_thresh) # nms后保留post_nms_topN个bbox(2000/300) # Pick th top region proposals after NMS if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] scores = scores[keep] # Only support single image as input batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False))) # 返回bbox以及分类得分 return blob, scores
def demo(sess, net, image_name, det_txt): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo_bengbian', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # detect the picture to find score and boxes scores, boxes = im_detect(sess, net, im, image_name) # 检测主体部分,在这里加上save_feature_picture # 这里的net内容是vgg timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.1 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(10, 10)) ax.imshow(im, aspect='equal') image_id = image_name.split('.')[0] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= 0.5)[0] print("!!!") print(inds) # 是否检测出来东西,如果有的话为0如果没有为空 if len(inds) == 0: a = 1 else: a = 0 vis_detections(det_txt, image_id, ax, im, cls, dets, thresh=CONF_THRESH) # vis_detections(det_txt, image_id, ax, im, cls, dets, thresh=CONF_THRESH) plt.draw() return a
def demo(sess, net, image_name, im_file): """Detect object classes in an image using pre-computed object proposals. :param im_file 图片路径 G:\PyCharm\Doc """ # Load the demo image # im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) # opencv读取图片 im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # 调用了lib/model/test.py里的im_detect()方法,返回的是分数和检测框。 scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): '''cls 标签的类型 roses''' cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) # [[ 0. 7.872568 63. 55.951324 0.7933805]] # 最后一个值是相似度 dets = dets[keep, :] vis_detections(im, cls, dets, image_name=image_name, thresh=CONF_THRESH) s_uuid = str(uuid.uuid4()) l_uuid = s_uuid.split('-') s_uuid = ''.join(l_uuid) image_name = s_uuid + image_name resdict["imageName"] = image_name # 保存标记的图片 if not os.path.exists(SAVA_DIR): os.makedirs(SAVA_DIR) plt.savefig(os.path.join(SAVA_DIR, image_name)) # 标记的图片保存的路径 saveFilePath = SAVA_DIR + "/" + image_name # print('{"saveFilePath":"%s"}' % saveFilePath) resdict["saveFilePath"] = saveFilePath
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') # train时,根据得分顺序,取前12000个anchor,再NMS,最后取前面2000个 # test时,变为6000和300 if cfg_key == "TRAIN": pre_nms_topN = cfg.FLAGS.rpn_train_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_train_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_train_nms_thresh else: pre_nms_topN = cfg.FLAGS.rpn_test_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_test_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_test_nms_thresh im_info = im_info[0] # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.reshape( (-1, 4)) #每一行对应于一个anchor,每9行对应于一个点产生的9个anchor scores = scores.reshape((-1, 1)) #每一行对应于一个anchor,每9行对应于一个点产生的9个anchor # 使用经过rpn网络层后生成的rpn_box_prob把anchor位置进行第一次修正 proposals = bbox_transform_inv(anchors, rpn_bbox_pred) # Clip boxes to image boundaries. proposals = clip_boxes(proposals, im_info[:2]) # Pick the top region proposals # argsort()函数时将array中的元素从小到大排列,提取其对应的index,然后输出到y order = scores.ravel().argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] proposals = proposals[order, :] scores = scores[order] # Non-maximal suppression keep = nms(np.hstack((proposals, scores)), nms_thresh) # Pick th top region proposals after NMS if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] scores = scores[keep] # Only support single image as input batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False))) # 返回的blob中多加了一列 return blob, scores
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') if cfg_key == "TRAIN": pre_nms_topN = cfg.FLAGS.rpn_train_pre_nms_top_n #('rpn_train_pre_nms_top_n', 12000, post_nms_topN = cfg.FLAGS.rpn_train_post_nms_top_n #rpn_train_post_nms_top_n', 2000 nms_thresh = cfg.FLAGS.rpn_train_nms_thresh #'rpn_train_nms_thresh', 0.7 else: pre_nms_topN = cfg.FLAGS.rpn_test_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_test_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_test_nms_thresh im_info = im_info[0] # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] #[,H,W,2*num_anchors]---#18个channel 按照(bg,fg)这里只取了fg的9个channel((1,9,h,w)) # reshape to (1 * H * W * A, 4) where rows are ordered by (h, w, a) # in slowest to fastest order rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4)) #(, height, width, A * 4) scores = scores.reshape((-1, 1)) ## #计算经过偏移后的预测坐标 proposals = bbox_transform_inv(anchors, rpn_bbox_pred) # 2. clip predicted boxes to image 将预测框剪切到图像范围内 proposals = clip_boxes(proposals, im_info[:2]) # Pick the top region proposals # ravel()平铺扁平化 argsort()函数是将x中的元素从小到大排列,提取其对应的index(索引),然后输出 [::-1]反向取索引 order = scores.ravel().argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] # 取出值scores最大的 pre_nms_topN个 proposals = proposals[order, :] scores = scores[order] # Non-maximal suppression keep = nms(np.hstack((proposals, scores)), nms_thresh) # Pick th top region proposals after NMS if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] scores = scores[keep] # Only support single image as input batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False))) return blob, scores
def demo(sess, net, image_name, conter, result=True): im_read = cv2.imread(image_name, cv2.IMREAD_ANYCOLOR) b, g, r = cv2.split(im_read) im = cv2.merge([r, g, b]) timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.3 fig, ax = plt.subplots(figsize=(12, 12)) for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if len(inds) == 0: conter += 1 continue ax.imshow(im, aspect='equal') for i in inds: bbox = dets[i, :4] score = dets[i, -1] ax.add_patch( plt.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1], fill=False, edgecolor=COLORSET[cls], linewidth=3.5)) ax.text(bbox[0], bbox[1] - 2, '{:s} {:.3f}'.format(cls, score), bbox=dict(facecolor='blue', alpha=0.5), fontsize=14, color='white') if conter == len(CLASSES) - 1: result = False print(result) plt.axis('off') plt.tight_layout() plt.draw() return result
def proposal_layer(rpn_cls_prob, rpn_bbox_pred, im_info, cfg_key, _feat_stride, anchors, anchors_dis, num_anchors): """A simplified version compared to fast/er RCNN For details please see the technical report """ if type(cfg_key) == bytes: cfg_key = cfg_key.decode('utf-8') if cfg_key == "TRAIN": pre_nms_topN = cfg.FLAGS.rpn_train_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_train_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_train_nms_thresh else: pre_nms_topN = cfg.FLAGS.rpn_test_pre_nms_top_n post_nms_topN = cfg.FLAGS.rpn_test_post_nms_top_n nms_thresh = cfg.FLAGS.rpn_test_nms_thresh im_info = im_info[0] # Get the scores and bounding boxes scores = rpn_cls_prob[:, :, :, num_anchors:] rpn_bbox_pred = rpn_bbox_pred.reshape((-1, 4)) scores = scores.reshape((-1, 1)) # print("anchors_dis", anchors_dis.shape) # anc (16848, 4) (16848, 4) proposals = bbox_transform_inv(anchors, rpn_bbox_pred) proposals = clip_boxes(proposals, im_info[:2]) # Pick the top region proposals order = scores.ravel().argsort()[::-1] if pre_nms_topN > 0: order = order[:pre_nms_topN] proposals = proposals[order, :] proposals_dis = anchors_dis[order, :] scores = scores[order] # Non-maximal suppression keep = nms(np.hstack((proposals, scores)), nms_thresh) # Pick th top region proposals after NMS if post_nms_topN > 0: keep = keep[:post_nms_topN] proposals = proposals[keep, :] proposals_dis = proposals_dis[keep, :] scores = scores[keep] # Only support single image as input proposals.shape[0]:2000 blob.shape:[2000, 8] batch_inds = np.zeros((proposals.shape[0], 1), dtype=np.float32) blob = np.hstack((batch_inds, proposals.astype(np.float32, copy=False), proposals_dis.astype(np.float32, copy=False))) return blob, scores
def demo(sess, net, im, image_name, out_path): #im, im_ref,im_path """Detect object classes in an image using pre-computed object proposals.""" #path_to_imgs = "/Users/dwaithe/Documents/collaborators/WaitheD/micro_vision/acquisitions/zstacks/test3/pos1_resize/" # Load the demo image #im_file = os.path.join(cfg.FLAGS2["data_dir"], path_to_imgs, image_name) #print(im_file) #im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.7 NMS_THRESH = 0.7 out_name = os.path.join(cfg.FLAGS2["data_dir"], out_path, str(image_name) + str('.txt')) f = open(out_name, 'w') for cls_ind, cls in enumerate(cfg.FLAGS2["CLASSES"][1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if len(inds) > 0: for i in inds: bbox = dets[i, :4] score = dets[i, -1] out_str = cls + "\t" + str(score) + "\t" + str( bbox[0]) + "\t" + str(bbox[1]) + "\t" + str( bbox[2]) + "\t" + str(bbox[3]) + "\n" f.write(out_str) #vis_detections(im, cls, dets, thresh=CONF_THRESH) f.close()
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) im = cv2.imread(im_file) # im = cv2.imread("G:/Python Projects/py3/Faster-RCNN-TensorFlow-Python3.5-master/data/demo/000456.jpg") # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class # score 阈值,最后画出候选框时需要,>thresh才会被画出 CONF_THRESH = 0.4 # 其实是输出了很多得分高的框,只不过后续通过nms的方式将这些框进行了合并,从而达到很好的检测效果。 # NMS_THRESH表示非极大值抑制,这个值越小表示要求的红框重叠度越小,0.0表示不允许重叠。 NMS_THRESH = 0.1 # python-opencv 中读取图片默认保存为[w,h,channel](w,h顺序不确定) # 其中 channel:BGR 存储,而画图时,需要按RGB格式,因此此处作转换。 im = im[:, :, (2, 1, 0)] fig, ax = plt.subplots(figsize=(12, 12)) ax.imshow(im, aspect='equal') for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # 进行非极大值抑制,得到抑制后的 dets keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # vis_detections(im, cls, dets, thresh=CONF_THRESH) # 画框 vis_detections(im, cls, dets, ax, thresh=CONF_THRESH) plt.axis('off') plt.tight_layout() plt.draw()
def demo(sess, net, image_name): im_file = os.path.join(cfg.FLAGS2["data_dir"], 'test_result/position/', image_name) im = cv_imread(im_file) # Detect all object classes and regress object bounds scores, boxes = im_detect(sess, net, im) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo/temp_jpg', image_name) im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer2 = Timer() timer2.tic() timer.tic() scores, boxes = im_detect(sess, net, im) # boxes为走一遍网络后,读入图片,检测到所有可能的box timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 # for cls_ind, cls in enumerate(CLASSES[1:]): # cls_ind += 1 # because we skipped background # cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] # cls_scores = scores[:, cls_ind] # dets = np.hstack((cls_boxes, # cls_scores[:, np.newaxis])).astype(np.float32) # keep = nms(dets, NMS_THRE SH) # dets = dets[keep, :] # vis_detections(im, cls, dets, thresh=CONF_THRESH) cls_ind = 15 cls = 'person' cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] # cls_boxes为某个类型的所有box的集合 cls_scores = scores[:, cls_ind] # 四个坐标点 dets = np.hstack(( cls_boxes, # 保存了所有的框的坐标and得分值 cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) # keep为nms非极大值抑制过滤后剩下的框 dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH, image_name=image_name) timer2.toc() print('timer2 time cost is {}'.format(timer2.average_time))
def demo(sess, net, image_name, output_path): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image image_path = image_path_from_index(image_name) im = cv2.imread(image_path) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # 此处的boxes是经过bbox_pre修正过的Bbox的位置坐标,并且对于预测的每一个类别,都有一个预测的Bbox坐标 scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 #对每个类别进行一次画图 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) #利用非极大值抑制,从300个proposal中剔除掉与更大得分的proposal的IOU大于0.1的proposal keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] dets = dets[inds, :] output_dir = os.path.join(output_path, "comp3_det_test_{:s}.txt".format(cls)) with open(output_dir, 'a') as f: for i in range(len(dets)): bbox = dets[i, :4] score = dets[i, -1] bbox_result = "%s\t%f\t%f\t%f\t%f\t%f\n" % ( image_name, score, bbox[0], bbox[1], bbox[2], bbox[3]) f.write(bbox_result)
def demo(image_name, out_file, sess): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(image_name) blobs, im_scales = _get_blobs(im) assert len(im_scales) == 1, "Only single-image batch implemented" im_blob = blobs['data'] blobs['im_info'] = np.array( [im_blob.shape[1], im_blob.shape[2], im_scales[0]], dtype=np.float32) print(blobs["im_info"]) #Detect all object classes and regress object bounds timer = Timer() timer.tic() print("====freeze_graph_test()-> blobs====\n", blobs) scores, boxes = freeze_graph_test(sess, blobs) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.5 NMS_THRESH = 0.3 # im = im[:, :, (2, 1, 0)] for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, out_file, thresh=CONF_THRESH) cv2.imencode('.jpg', im)[1].tofile(out_file)
def demo(sess, net, image_id, image_name): im_file = os.path.join(cfg.FLAGS2["data_dir"], 'image', image_name) im = cv2.imread(im_file) timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() # print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, image_id, image_name, thresh=CONF_THRESH)
def getResult(scores, boxes, conf_thresh=0.6, nms_thresh=0.2): result = [0, 0, 0, 0, 0, 0, 0] # Detect all object classes and regress object bounds # Visualize detections for each class for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # 水平方向堆叠 keep = nms(dets, nms_thresh) dets = dets[keep, :] # print(dets.shape) inds = np.where(dets[:, -1] >= conf_thresh)[0] # 大于阈值的缺陷索引index # if len(inds) > 0: # 缺陷 # vis_detections(im, cls, dets,inds) #画框 result[cls_ind] = len(inds) return result
def demo_video(sess, net, frame, camera_url, max_residence_frame): """Detect object classes in an image using pre-computed object proposals.""" im = frame timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) timer.toc() # print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = detect_threshold # threshold NMS_THRESH = nms_threshold for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] inds = np.where(dets[:, -1] >= CONF_THRESH)[0] vis_detections_video(im, cls, dets, timer.start_time, timer.total_time, inds, CONF_THRESH)
def demo(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image # G:\PyCharm\PyCharmSpaceWork\Faster-RCNN-TensorFlow2-Python3\data\demo\000001.jpg im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) # print("==================im_file===========", im_file) # opencv读取图片 im = cv2.imread(im_file) # Detect all object classes and regress object bounds timer = Timer() timer.tic() # 调用了lib/model/test.py里的im_detect()方法,返回的是分数和检测框。 scores, boxes = im_detect(sess, net, im) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): '''cls 标签的类型 roses''' cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) # [[ 0. 7.872568 63. 55.951324 0.7933805]] # 最后一个值是相似度 dets = dets[keep, :] vis_detections(im, cls, dets, image_name=image_name, thresh=CONF_THRESH ) # 保存标记的图片 if not os.path.exists(SAVA_DIR): os.makedirs(SAVA_DIR) plt.savefig(os.path.join(SAVA_DIR, image_name))
def demo_posiotion(sess, net, image_name): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im_file = os.path.join(cfg.FLAGS2["data_dir"], 'image_android', image_name).encode('utf8') im_file = im_file # im = cv2.imread(im_file) im = cv_imread(im_file) scores, boxes = im_detect(sess, net, im) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] # print(cls_scores)#一个300个数的数组 # np.newaxis增加维度 np.hstack将数组拼接在一起 dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] vis_detections(im, cls, dets, thresh=CONF_THRESH)