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 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 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, memory_storex, memory_storey, kitti_memory_0323, AN, sess2): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(image_name) im = cv2.resize(im, (1242, 375)) # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, bbox_pred, _, rois, fc = im_detect(sess, net, im, memory_storex, memory_storey) timer.toc() print('Detection took {:.3f}s for {:d} object proposals'.format( timer.total_time, bbox_pred.shape[0])) # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 im_shape = im.shape[:2] box_deltas = bbox_pred pred_boxes = bbox_transform_inv(rois, box_deltas) boxes = clip_boxes(pred_boxes, im_shape) # show.vis_detections(image_name, scores, boxes, dis_pre, fc, NMS_THRESH, CONF_THRESH) show.vis_detections(image_name, scores, boxes, fc, kitti_memory_0323, AN, sess2, NMS_THRESH, CONF_THRESH)
def demo(sess, net, image_name): # 根据路径,使用opencv读取图片数据 im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name) im = cv2.imread(im_file) # 进行目标检查 timer = Timer() timer.tic() # 进行预测返回300个box的得分和位置 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阈值 NMS_THRESH = 0.1 for cls_ind, cls in enumerate(CLASSES[1:]): cls_ind += 1 # +1需要跳过背景 # 获取到所有候选框对应这个分类的位置 cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] # 获取到所有候选框对应这个分类的得分 cls_scores = scores[:, cls_ind] # 合并所有的位置和得分,(x1,y1,x2,y2,score) dets = np.hstack( (cls_boxes, cls_scores[:, np.newaxis])).astype(np.float32) # 通过非极大值抑制保留0.1的候选框以及得分 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( '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_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, 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 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 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(sess, net): """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) CONF_THRESH = 0.6 NMS_THRESH = 0.1 # 非极大值抑制 # server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # server.bind(("localhost", 8888)) # server.listen(1) # tcp连接队列的大小,即连接数 im_names = ['39.jpg', '40.jpg', '41.jpg', '42.jpg', '43.jpg', '44.jpg'] im=cv2.imread("init.jpg") #目的是为了初始化相关变量,避免首次检测延时过大 scores, boxes = im_detect(sess, net, im) # connection, address = server.accept() #阻塞,等待连接 # print(connection, address) recv_str =im_names[0] print(recv_str) im_name =recv_str #im_names[int(recv_str)] #'G:/40.jpg' # # saveImgPath+=im_name print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') print('Demo for {}'.format(im_name)) timer = Timer() timer.tic() im = cv2.imread(im_name) try: im.shape except: print('fail to read '+im_name) return 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])) result=getResult(scores, boxes,CONF_THRESH,NMS_THRESH) #保存图像等操作 # timer2 = Timer() # timer2.tic() drawDefect(im,scores, boxes, CONF_THRESH, NMS_THRESH) cv2.imwrite(saveImgPath, im)
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 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 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, 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(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 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)
def demo(sess, net, image_name): dir_path = os.path.dirname(os.path.abspath(__file__)) im_file = dir_path + r'/images/position/' + image_name if not os.path.exists(dir_path + r'/images/position/'): os.makedirs(dir_path + r'/images/position/') 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, im): """Detect object classes in an image using pre-computed object proposals.""" # Detect all object classes and regress object bounds timer = Timer() timer.tic() scores, boxes = im_detect(sess, net, im) # boxes为走一遍网络后,读入图片,检测到所有可能的box timer.toc() sum_time = timer.total_time 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, :] img = vis_detections(im, cls, dets, thresh=CONF_THRESH) return img, sum_time
def evaluate(sess, net, dataset, img_file, voc_file, output_dir, demo_dir): """Detect object classes in an image using pre-computed object proposals.""" # Load the demo image im = cv2.imread(img_file) demo = im.copy() # 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])) #print(scores.shape, boxes.shape) #print(boxes) # Load the ground-truth bounding box # there is only one object of interest gt = parse_rec(voc_file)[0] #print(gt) # draw the gt bbox gt_bbox = gt["bbox"] gt_label = gt["name"] gt_color = (0, 0, 255) gt_thickness = 2 """ im = cv2.rectangle(im, (gt_bbox[0], gt_bbox[1]), (gt_bbox[2], gt_bbox[3]), gt_color, gt_thickness) im = cv2.putText(im, gt_label, (gt_bbox[0], gt_bbox[3] - 15), cv2.FONT_HERSHEY_PLAIN, 1.5, gt_color, gt_thickness, bottomLeftOrigin=False) """ # Visualize detections for each class CONF_THRESH = 0.1 NMS_THRESH = 0.1 est_label = "None" ret_iou = 0 whole_dets = None for cls_ind, cls in enumerate(CLASSES[DATASETS[dataset][0]][1:]): cls_ind += 1 # because we skipped background cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)] cls_scores = scores[:, cls_ind] cls_indices = np.array([cls_ind for i in range(len(cls_boxes))]).reshape(-1, 1) #print(cls_boxes, cls_scores) dets = np.hstack((cls_boxes, cls_scores[:, np.newaxis], cls_indices)).astype(np.float32) #print(dets) keep = nms(dets, NMS_THRESH) dets = dets[keep, :] # draw the original estimated bbox(es) inds = np.where(dets[:, 4] >= CONF_THRESH)[0] for i in inds: bbox = dets[i, :4].astype(int) score = dets[i, 4] est_label = CLASSES[DATASETS[dataset][0]][int(dets[i, -1])] demo = vis_detections(demo, bbox, score, est_label) if whole_dets is None: whole_dets = dets else: whole_dets = np.append(whole_dets, dets, axis=0) if whole_dets is None: return gt_label, est_label, ret_iou # get only one that is closest to the bounding box #print(whole_dets) #print(whole_dets.shape) dets, ret_iou = keep_closest(whole_dets, gt, thresh=CONF_THRESH) if dets is not None: # no appropriately estimated label # do evaluation bbox = dets[:4].astype(int) score = dets[4] est_label = CLASSES[DATASETS[dataset][0]][int(dets[-1])] # visualization im = vis_detections(im, bbox, score, est_label) # write the debugging file here output_file = osp.join(output_dir, osp.basename(img_file)) demo_file = osp.join(demo_dir, osp.basename(img_file)) #plt.savefig(output_file) cv2.imwrite(output_file, im) cv2.imwrite(demo_file, demo) print("Output written to {} and {}".format(output_file, demo_file)) return gt_label, est_label, ret_iou
def demo(sess, net): """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) CONF_THRESH = 0.6 NMS_THRESH = 0.2 # 非极大值抑制 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server.bind(("localhost", 8888)) server.listen(1) # tcp连接队列的大小,即连接数 im_names = [ 'K:/39.jpg', 'K:/40.jpg', 'K:/41.jpg', 'K:/42.jpg', 'K:/43.jpg', 'K:/44.jpg' ] im = cv2.imread("temp.jpg") #目的是为了初始化相关变量,避免首次检测延时过大 scores, boxes = im_detect(sess, net, im) while True: connection, address = server.accept() #阻塞,等待连接 print(connection, address) recv_str = connection.recv(1024) recv_str = recv_str.decode("ascii") if not recv_str: continue print(recv_str) im_name = recv_str #im_names[int(recv_str)] #'G:/40.jpg' # # saveImgPath+=im_name print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') print('Demo for {}'.format(im_name)) timer = Timer() timer.tic() im = cv2.imread(im_name) try: im.shape except: print('fail to read xxx.jpg') connection.send(bytes("000000", encoding="ascii")) connection.close() continue 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])) result = getResult(scores, boxes, CONF_THRESH, NMS_THRESH) #向cpp发送结果 strResult = map(str, result) #int列表转str列表 back_str = "".join(strResult) print(back_str) connection.send(bytes("%s" % back_str, encoding="ascii")) #保存图像等操作 # timer2 = Timer() # timer2.tic() drawDefect(im, scores, boxes, CONF_THRESH, NMS_THRESH) cv2.imwrite(saveImgPath, im) # timer2.toc() # print("保存图像耗时:"+str(timer2.total_time)) # connection.send(bytes("saved" , encoding="ascii")) #反馈图像保存成功 connection.close() time.sleep(2)
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 #score 阈值,最后画出候选框时需要,>thresh才会被画出 CONF_THRESH = 0.1 #非极大值抑制的阈值,剔除重复候选框 NMS_THRESH = 0.4 #利用enumerate函数,获得CLASSES中类别的下标cls_ind和类别名cls 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] #将bbox,score 一起存入dets,按列顺序把数组给堆叠 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) """Draw detected bounding boxes.""" #选取候选框score大于阈值的dets inds = np.where(dets[:, -1] >= CONF_THRESH)[0] if len(inds) == 0: # return continue for i in inds: # print("外层循环{}次,进入内层循环".format(cls_ind)) bbox = dets[i, :4]#坐标位置(Xmin,Ymin,Xmax,Ymax) score = dets[i, -1]#置信度得分 #根据起始点坐标以及w,h 画出矩形框 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()