示例#1
0
def vis_detections(__appcfg, imgFileName, im, class_names, results):
  print("vis_detections")
  r = results[0]
  # file_name = "viz_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())

  # visualize.display_instances(im[..., ::-1], r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])

  # file_name = imgFileName+"-viz_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
  # file_name = imgFileName+"-"+__appcfg.ID+"-"+"-viz.png"
  fpath = osp.dirname(osp.abspath(imgFileName))

  file_name = osp.join(fpath, Util.getVizImageFileName(imgFileName, 'viz', __appcfg ))
  print("viz_detections: file_name:viz: {}".format(file_name))
  # file_name = osp.join(imgFileName,file_name)
  viz = display_instances(file_name, im, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'] )
 
  cv2.imwrite(file_name, viz)
  ## Color splash
  # file_name = imgFileName+"-splash_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
  # file_name = imgFileName+"-"+__appcfg.ID+"-"+"-splash.png"
  file_name = osp.join(fpath, Util.getVizImageFileName(imgFileName, 'splash', __appcfg ))
  # file_name = osp.join(imgFileName,file_name)

  splash = getColorSplash(im, r['masks'])
  # skimage.io.imshow(splash)
  # skimage.io.show()
  cv2.imwrite(file_name, splash)

  ## get mask
  # file_name = imgFileName+"-mask_{:%Y%m%dT%H%M%S}.png".format(datetime.datetime.now())
  # file_name = imgFileName+"-"+__appcfg.ID+"-"+"-mask.png"
  file_name = osp.join(fpath, Util.getVizImageFileName(imgFileName, 'mask', __appcfg ))
  # file_name = osp.join(imgFileName,file_name)

  cutmask = getSegmentMask(im[..., ::-1], r['masks'])
  ## Save output
  # skimage.io.imshow(cutmask)
  # skimage.io.show()
  skimage.io.imsave(file_name, cutmask)
示例#2
0
def predict_depricated(modelDtls, model, im_name, path, out_file, __appcfg):
  print("Inside {}: predict()".format(__file__))
  # Load the image
  im_file = osp.join(path, im_name)

  print('predict: im_name, im_file: {} {}'.format(im_name, im_file))
  # im = skimage.io.imread(im_file)
  im = cv2.imread(im_file)
  print("predict: {}".format(im))
  # Run detection
  
  results = model.detect([im], verbose=1)
  CLASSES = modelDtls["CLASSES"]
  r = results[0]

  imgFileName = Util.getOutFileName(out_file, im_name, "", __appcfg)
  ## Visualize results
  vis_detections(__appcfg, imgFileName, im, CLASSES, results)

  # jsonres = display_instances(
  #     fileName, im, r['rois'], r['masks'], r['class_ids'], CLASSES, r['scores']
  # )
  # # file_name = im_name+"_{:%Y%m%dT%H%M%S}.csv".format(datetime.datetime.now())

  # print("fileName: {}".format(fileName))

  # with open(fileName,'a') as f:
  #   for item in jsonres:
  #     row = ', '.join([str(i) for i in item.values()])
  #     print("row: {}".format(row))
  #     f.write(row+"\n")

  
  ## OpenCV read the images in BGR format R=0,G=1,B=2
  ## hence, when plotting with matplotlib specify the order
  im = im[:, :, (2, 1, 0)]
  dim = im.shape[:2]
  height, width = dim[0], dim[1]
  FILE_DELIMITER = __appcfg.FILE_DELIMITER

  all_rows_for_all_classes = {}
  boxes, masks, ids, scores = r['rois'], r['masks'], r['class_ids'], r['scores']
  
  # max_area will save the largest object for all the detection results
  max_area = 0
  # n_instances saves the amount of all objects
  n_instances = boxes.shape[0]
  if not n_instances:
    print('NO INSTANCES TO DISPLAY')
  else:
    assert boxes.shape[0] == masks.shape[-1] == ids.shape[0]

  for i in range(n_instances):
    if not np.any(boxes[i]):
      continue

    # compute the square of each object
    # y1, x1, y2, x2 = boxes[i]
    # bbox = boxes[i]
    score = scores[i]

    y1, x1, y2, x2 = boxes[i]
    square = (y2 - y1) * (x2 - x1)
    # use label to select person object from all the classes
    cls = CLASSES[ids[i]]
    max_area = square
    mask = masks[:, :, i]
    bbox = boxes[i]
  
    # apply mask for the image
    # all_rows = getDetections(width, height, cls, bbox, score, im_name, out_file, FILE_DELIMITER, __appcfg)
  
    print("getDetections")
    row = None;

    all_bbox = []
    fileName = Util.getOutFileName(out_file, im_name, ".csv", __appcfg)

    with open(fileName,'a') as f:
      # row = Util.getOutFileRow([bbox[1],bbox[0],bbox[3],bbox[2]], cls, score, width, height, FILE_DELIMITER)
      row = Util.getOutFileRow(bbox, cls, score, width, height, FILE_DELIMITER)      
      print("row:")
      print(row)
      ## TBD: type conversion mapping
      all_bbox.append(row.split(FILE_DELIMITER))
      print("Detection Row:"+row)
      f.write(row+'\n')

    all_rows = {
      "bbox":all_bbox
    }

    if all_bbox and len(all_bbox) > 0:
      all_rows_for_all_classes[cls] = all_rows
    else:
      all_rows_for_all_classes[cls] = None
  
  
  detections = [
    Util.getVizImageFileName(im_name, None, __appcfg )
    ,Util.getVizImageFileName(im_name, 'viz', __appcfg )
    ,Util.getVizImageFileName(im_name, 'splash', __appcfg )
    ,Util.getVizImageFileName(im_name, 'mask', __appcfg )
  ]
  # print("mask_rcnn::detections: {}".format(detections))
  res = Util.createResponseForVisionAPI(im_name, FILE_DELIMITER, __appcfg, all_rows_for_all_classes, detections, __appcfg.API_VISION_BASE_URL)
  return res
示例#3
0
def predict(modelDtls, net, im_name, path, out_file, __appcfg):
    print("Inside {}: predict()".format(__file__))
    # Load the image
    im_file = osp.join(path, im_name)

    print('im_name: ' + im_name)
    print('im_file: ' + im_file)
    im = cv2.imread(im_file)
    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    # print scores,boxes

    ## OpenCV read the images in BGR format R=0,G=1,B=2
    ## hence, when plotting with matplotlib specify the order
    im = im[:, :, (2, 1, 0)]

    modelCfg = modelDtls["config"]

    ## Ref: https://stackoverflow.com/questions/34768717/matplotlib-unable-to-save-image-in-same-resolution-as-original-image
    dim = im.shape[:2]
    height, width = dim[0], dim[1]
    FILE_DELIMITER = __appcfg.FILE_DELIMITER

    timer.toc()
    #print(np.amax(scores, axis=1))
    #print('Detection took {:.3f}s for {:d} object proposals').format(timer.total_time, boxes.shape[0])

    CONF_THRESH = modelCfg.CONF_THRESH
    NMS_THRESH = modelCfg.NMS_THRESH
    # Visualize detections for each class
    CLASSES = modelDtls["CLASSES"]

    # print("CLASSES, NMS_THRESH: "+CLASSES+","+NMS_THRESH)

    all_rows_for_all_classes = {}
    # all_labels = []
    labelNames = enumerate(CLASSES)
    # print("Label Names: {}").format(CLASSES)

    for cls_ind, cls in labelNames:
        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, :]
        all_rows = getDetections(width, height, cls, dets, im_name, out_file,
                                 CONF_THRESH, FILE_DELIMITER, __appcfg)

        # all_labels.append(cls)
        if all_rows and len(all_rows) > 0:
            if all_rows["bbox"] and len(all_rows["bbox"]) > 0:
                all_rows_for_all_classes[cls] = all_rows
            else:
                all_rows_for_all_classes[cls] = None

    detections = [Util.getVizImageFileName(im_name, None, __appcfg)]
    # print("faster_rcnn_end2end::detections: {}".format(detections))
    res = Util.createResponseForVisionAPI(im_name, FILE_DELIMITER, __appcfg,
                                          all_rows_for_all_classes, detections,
                                          __appcfg.API_VISION_BASE_URL)
    return res