Ejemplo n.º 1
0
def init_net(net1, net2):
    # Warmup on a dummy image
    im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
    if net1 is not None:
        for i in xrange(2):
            im_detect(net1, im)
        print "\n\n***********************************\n\n"

    if net2 is not None:
        time.sleep(3)
        for i in xrange(2):
            im_detect(net2, im)

    print "\n\nInit Net Done!\n\n"
    time.sleep(3)
Ejemplo n.º 2
0
def _image2bbox_top1(net, im, classes, t_cls, NMS_THRESH=0.3, CONF_THRESH=0.8):
    """
  Detect object classes in an image using pre-computed object proposals.
    only highest-score one 
    (very suitable for model trained on cropped images by person detector)
    (also for model trained on whole image but maybe not good for multi-people in the image)
  """
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)

    # ignore bg
    bboxes = []
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        cls_ind += 1
        cls_scores = scores[:, cls_ind]
        order = cls_scores.argsort()[::-1]
        obj_ind = order[0]
        score = cls_scores[obj_ind]
        bbox = boxes[obj_ind, 4 * cls_ind:4 * (cls_ind + 1)]
        bbox = [int(b) for b in bbox]
        bboxes.append((bbox, score, cls))

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])

    return bboxes
def _demo4image(net,
                im_path,
                classes,
                t_cls,
                out_dire=None,
                NMS_THRESH=0.3,
                CONF_THRESH=0.8):
    """Detect object classes in an image using pre-computed object proposals."""
    timer = Timer()
    timer.tic()
    print 'Demo for {}'.format(im_path)
    im = cv2.imread(im_path)
    scores, boxes = im_detect(net, im)

    # ignore bg
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        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, :]
        vis_detections(im, cls, dets, im_path, out_dire, thresh=CONF_THRESH)

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])
def _demo4image_2(net,
                  im_path,
                  classes,
                  t_cls,
                  out_dire=None,
                  NMS_THRESH=0.3,
                  CONF_THRESH=0.6,
                  im_ext=".jpg"):
    """Detect object classes in an image using pre-computed object proposals."""
    timer = Timer()
    timer.tic()
    print 'Demo for {}'.format(im_path)
    im = cv2.imread(im_path)
    scores, boxes = im_detect(net, im)

    # ignore bg
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        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, :]
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        if len(inds) == 0:
            continue
        for i in inds:
            bbox = dets[i, :4]
            bbox = [int(b) for b in bbox]
            score = dets[i, -1]
            p1 = (bbox[0], bbox[1])
            p2 = (bbox[2], bbox[3])
            cv2.rectangle(im, p1, p2, (38, 231, 16), 2)
            p3 = (bbox[0], bbox[1] - 5)
            cv2.putText(im, '{:s} {:s}'.format(cls, str(score),), p3, \
                cv2.FONT_HERSHEY_SIMPLEX, .56, (123, 19, 208), 1)

    im_name = im_path.rsplit("/", 1)[1]
    im_name = im_name.rsplit(".", 1)[0]
    if out_dire and len(out_dire) > 0:
        im_path2 = out_dire + im_name + im_ext
        cv2.imwrite(im_path2, im)
    else:
        cv2.imshow(im_name, im)
        cv2.waitKey(1)
        cv2.destroyAllWindows()

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])
def _demo4image2file(net,
                     im_path,
                     fhd,
                     classes,
                     t_cls,
                     NMS_THRESH=0.3,
                     CONF_THRESH=0.8):
    """
  Detect object classes in an image using pre-computed object proposals.
  And write the results of bboxes into file by file handler `fhd` object.

  Format:
    im_path [[score bbox cls] ...]
  """
    timer = Timer()
    timer.tic()

    print 'Demo for {}'.format(im_path)
    im = cv2.imread(im_path)
    scores, boxes = im_detect(net, im)

    # ignore bg
    h, w, _ = im.shape
    info = im_path.strip()
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        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, :]
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        if len(inds) == 0:
            return
        for i in inds:
            score = dets[i, -1]
            score = str(score)
            bbox = dets[i, :4]
            bbox = [int(b) for b in bbox]
            bbox = [x1, y1, x2, y2]
            bbox = [str(b) for b in bbox]
            bbox = " ".join(bbox).strip()
            info = info + " " + score + " " + bbox + " " + cls
        fhd.write(info.strip() + "\n")

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])
Ejemplo n.º 6
0
def _image2bbox2(net, im, classes, target_cls, cls_type=True):
    """
  Detect object classes in an image using pre-computed object proposals.
    >= threshold (maybe empty)
  """
    if cls_type:
        nms_thres = cfg.TEST.P_NMS_THRES  # person
        conf_thres = cfg.TEST.P_CONF_THRES
    else:
        nms_thres = cfg.TEST.T_NMS_THRES  # torso
        conf_thres = cfg.TEST.T_CONF_THRES

    timer = Timer()
    timer.tic()

    # forward
    scores, boxes, = im_detect(net, im)
    # nms and threshold
    bboxes = []
    has_target = len(target_cls) > 0
    for ind, cls in enumerate(classes[1:]):
        if has_target and cls not in target_cls:
            continue

        ind += 1  # ignore bg
        boxes2 = boxes[:, 4 * ind:4 * (ind + 1)]
        scores2 = scores[:, ind]
        dets = np.hstack((boxes2, scores2[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, nms_thres)
        dets = dets[keep, :]

        inds = np.where(dets[:, -1] >= conf_thres)[0]
        if len(inds) == 0:
            break

        for i in inds:
            bbox = dets[i, :4]
            bbox = [int(b) for b in bbox]
            score = dets[i, -1]
            bboxes.append((bbox, score, cls))

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])

    if len(bboxes) <= 0:
        return None
    return bboxes
def _demo4image_top1(net,
                     im_path,
                     classes,
                     t_cls,
                     out_dire=None,
                     im_ext=".jpg"):
    """Detect object classes in an image using pre-computed object proposals."""
    timer = Timer()
    timer.tic()
    print 'Demo for {}'.format(im_path)
    im = cv2.imread(im_path)
    scores, boxes = im_detect(net, im)

    # ignore bg
    h, w, _ = im.shape
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        cls_ind += 1
        cls_scores = scores[:, cls_ind]
        order = cls_scores.argsort()[::-1]
        obj_ind = order[0]
        score = cls_scores[obj_ind]
        bbox = boxes[obj_ind, 4 * cls_ind:4 * (cls_ind + 1)]
        bbox = [int(b) for b in bbox]
        bbox = [x1, y1, x2, y2]
        cls = classes[cls_ind]
        p1 = (bbox[0], bbox[1])
        p2 = (bbox[2], bbox[3])
        cv2.rectangle(im, p1, p2, (38, 231, 16), 2)
        p3 = (bbox[0], bbox[1] - 5)
        cv2.putText(im, '{:s} {:s}'.format(cls, str(score),), p3, \
            cv2.FONT_HERSHEY_SIMPLEX, .56, (123, 19, 208), 1)

    im_name = im_path.rsplit("/", 1)[1]
    im_name = im_name.rsplit(".", 1)[0]
    if out_dire and len(out_dire) > 0:
        im_path2 = out_dire + im_name + im_ext
        cv2.imwrite(im_path2, im)
    else:
        cv2.imshow(im_name, im)
        cv2.waitKey(1)
        cv2.destroyAllWindows()

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" \
          % (total_time, boxes.shape[0])
def _demo4video(net, im, classes, t_cls, NMS_THRESH=0.3, CONF_THRESH=0.8):
    """Detect object classes in an image using pre-computed object proposals."""
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)

    # ignore bg
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        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, :]

        # draw bboxes
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        if len(inds) == 0:
            cv2.imshow("frame", im)
            return

        for i in inds:
            bbox = dets[i, :4]
            bbox = [int(b) for b in bbox]
            score = dets[i, -1]
            # bbox
            x1 = bbox[0]
            y1 = bbox[1]
            x2 = bbox[2]
            y2 = bbox[3]
            # draw
            p1 = (x1, y1)
            p2 = (x2, y2)
            cv2.rectangle(im, p1, p2, (38, 231, 16), 2)
            p3 = (x1, y1 - 3)
            cv2.putText(im, '{:s} {:.3f}'.format(cls, score), p3, \
                cv2.FONT_HERSHEY_SIMPLEX, .36, (23, 119, 188))
        # show for all categories
        cv2.imshow("frame", im)

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])
Ejemplo n.º 9
0
def _image2bbox(net, im, classes, t_cls, NMS_THRESH=0.3, CONF_THRESH=0.8):
    """
  Detect object classes in an image using pre-computed object proposals.
    >= threshold (maybe empty)
  """
    timer = Timer()
    timer.tic()
    scores, boxes, = im_detect(net, im)

    # ignore bg
    bboxes = []
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        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, :]

        # draw bboxes
        inds = np.where(dets[:, -1] >= CONF_THRESH)[0]
        if len(inds) == 0:
            break

        for i in inds:
            bbox = dets[i, :4]
            bbox = [int(b) for b in bbox]
            score = dets[i, -1]
            bboxes.append((bbox, score, cls))

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])

    return bboxes
def _demo4image2file_top1(net, im_path, fhd, classes, t_cls):
    """
  Detect object classes in an image using pre-computed object proposals.
  And write the results of bboxes into file by file handler `fhd`

  Format:
    im_path [[score bbox cls] ...]
  """
    timer = Timer()
    timer.tic()
    print 'Demo for {}'.format(im_path)
    im = cv2.imread(im_path)
    scores, boxes = im_detect(net, im)

    # ignore bg
    h, w, _ = im.shape
    info = im_path.strip()
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        cls_ind += 1
        cls_scores = scores[:, cls_ind]
        order = cls_scores.argsort()[::-1]
        obj_ind = order[0]
        score = cls_scores[obj_ind]
        score = str(score)
        bbox = boxes[obj_ind, 4 * cls_ind:4 * (cls_ind + 1)]
        bbox = [int(b) for b in bbox]
        bbox = [str(b) for b in bbox]
        bbox = " ".join(bbox).strip()
        info = info + " " + score + " " + bbox + " " + cls
    # for all categories in one line
    fhd.write(info.strip() + "\n")

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" \
          % (total_time, boxes.shape[0])
Ejemplo n.º 11
0
def _image2bbox_top1_cond(net,
                          im,
                          classes,
                          t_cls,
                          cond_bbox,
                          NMS_THRESH=0.3,
                          CONF_THRESH=0.8):
    """
  Detect object classes in an image using pre-computed object proposals.

  """
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)

    # ignore bg
    bboxes = []
    is_target = len(t_cls) > 0
    for cls_ind, cls in enumerate(classes[1:]):
        if is_target and cls not in t_cls:
            continue
        cls_ind += 1
        cls_scores = scores[:, cls_ind]
        order = cls_scores.argsort()[::-1]
        for obj_ind in order:
            score = cls_scores[obj_ind]
            bbox = boxes[obj_ind, 4 * cls_ind:4 * (cls_ind + 1)]
            bbox = [int(b) for b in bbox]
            if _within_bbox(cond_bbox, bbox):
                bboxes.append((bbox, score, cls))
                break

    total_time = timer.toc(average=False)
    print "Detection took %ss for %s object proposals" % (total_time,
                                                          boxes.shape[0])

    return bboxes
  print "t_cls:", t_cls
  print "classes:", classes
  print "im_path:", im_path
  print "out_dire:", out_dire
  time.sleep(2)

  return net, args, classes, im_path, out_dire, out_file, t_cls, im_ext

if __name__ == '__main__':
  """Demo for human or torso detection"""
  net, args, classes, im_path, out_dire, out_file, t_cls, im_ext = _init_parse()
  
  # Warmup on a dummy image
  im = 128 * np.ones((300, 500, 3), dtype=np.uint8)
  for i in xrange(2):
    im_detect(net, im)

  # #####################################################
  # begin
  # #####################################################
  if args.is_video != 0:
    print "\n\nDection of Person | Torso in Video"
    time.sleep(2)
    pose4video(net, classes, t_cls)
  else:
    print "\n\nDection of Person | Torso in images"
    time.sleep(2)
    pose4images_top1(net, classes, im_path, t_cls, out_dire, out_file)
  
  print "\n\nDetection has been done.\n\n"