コード例 #1
0
def getDetections(width, height, class_name, dets, im_name, out_file,
                  CONF_THRESH, FILE_DELIMITER, __appcfg):
    print("getDetections")
    row = None

    all_bbox = []

    inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
    print(len(inds))

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

    with open(fileName, 'a') as f:
        if len(inds) == 0:
            if __appcfg.SAVE_NULL_RESULTS:
                row = Util.getOutFileRow([], class_name, "null", width, height,
                                         FILE_DELIMITER)
                f.write(row + '\n')
        else:
            for i in inds:
                bbox = dets[i, :4]
                score = dets[i, -1]

                # mask_rcnn: getOutFileRow: bbox:: image coordinates
                # [ 306 23 1080 1920] => [y1,x1,y2,x2] => [top, left, bottom, right] mapping in Util.getOutFileRow

                # faster_rcnn_end2end: getOutFileRow::bbox:
                # [643.95715  105.885155 717.3395   177.24414 ] => [left, top, right, bottom] => [x1,y1,x2,y2]

                # row = Util.getOutFileRow(bbox, class_name, score, width, height, FILE_DELIMITER)
                row = Util.getOutFileRow([bbox[1], bbox[0], bbox[3], bbox[2]],
                                         class_name, 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}
    return all_rows
コード例 #2
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)
コード例 #3
0
def load(dnn, modelDtls, args):
    model = None
    loadModel = Util.get(dnn, "loadModel")
    if loadModel:
        model = loadModel(modelDtls, args)
        # loadWeights = Util.get(dnn, "loadWeights")
        # loadWeights(modelDtls, model)

    # mod = import_module(dnn)
    # if mod:
    #   loadModel = getattr(mod, "loadModel")
    #   model = loadModel(modelDtls, args)

    # print("load: {}".format(mod))
    return model
コード例 #4
0
def predict(modelDtls, model, im_name, path, out_file, __appcfg):
  print("Inside {}: predict()".format(__file__))
  # Load the image
  im_name = im_name.split(osp.sep)[-1]
  im_file = osp.join(path, im_name)
  size_image = osp.getsize(im_file)

  print('predict: path, im_name, im_file: {} {}'.format(path, im_name, im_file))
  # im = skimage.io.imread(im_file)
  im = cv2.imread(im_file)
  # print("predict: {}".format(im))

  # Run detection
  t1 = time.time()

  results = model.detect([im], verbose=1)
  CLASSES = modelDtls["CLASSES"]
  r = results[0]
  t2 = time.time()
  time_taken = (t2 - t1)  
  print ('Total time taken in get_detections: %f seconds' %(time_taken))

  
  ## 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

  boxes, masks, ids, scores = r['rois'], r['masks'], r['class_ids'], r['scores']
  
  t1 = time.time()
  class_names = CLASSES
  jsonres = get_detections(im, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
  
  t2 = time.time()
  time_taken = (t2 - t1)
  print ('Total time taken in get_detections: %f seconds' %(time_taken))

  jsonres["filename"] = im_name
  jsonres["size"] = size_image
  via_jsonres = {}
  via_jsonres[im_name+str(size_image)] = jsonres
  detections = []
  res = Util.createResponseForVisionAPI(im_name, FILE_DELIMITER, __appcfg, via_jsonres, detections, __appcfg.API_VISION_BASE_URL)
  return res
コード例 #5
0
def exec_prediction(dnn, modelDtls, net, im_names, path, out_file, __appcfg):
    print("Inside: Model::exec_prediction")
    total = len(im_names)
    count = 0
    all_rows_for_all_classes = None
    print("exec_prediction:: outfile:" + out_file)
    print("exec_prediction:: Total images to be predicted:".format(str(total)))

    print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
    ## DNN specific calls dunamic binding
    # mod = import_module(dnn)
    # fn = getattr(mod, "predict")

    print("dnn: {}".format(dnn))
    fn = Util.get(dnn, "predict")
    if not fn:
        print("fn: no function in the module: TBD exit")

    FILE_DELIMITER = __appcfg.FILE_DELIMITER
    for im_name in im_names:
        ## TBD: out_file with timestamp

        # fileName = Util.getOutFileName(out_file, im_name, ".csv", __appcfg)
        # print("exec_prediction:: fileName: {}".format(fileName))
        # # fileName = getCsvFileName(out_file, im_name, __appcfg)

        # with open(fileName,'w') as f:
        #   header = Util.getOutFileHeader(FILE_DELIMITER)
        #   f.write(header+'\n')

        ## vis detection output
        all_rows_for_all_classes = fn(modelDtls, net, im_name, path, out_file,
                                      __appcfg)
        print("exec_prediction::all_rows_for_all_classes: {}".format(
            all_rows_for_all_classes))
        ++count

        # if __appcfg.VIS_DETECTIONS:
        #   Util.vis_detections_from_csvfile(im_name, FILE_DELIMITER, path, out_file, __appcfg)
        # else:
        #   Util.delete_no_detection_csvfile(im_name, FILE_DELIMITER, path, out_file, __appcfg)

        print("-----------------------")

    return all_rows_for_all_classes
コード例 #6
0
def warmup(dnn, net):
    fn = Util.get(dnn, "warmup")
    if fn:
        fn(net)
コード例 #7
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
コード例 #8
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