def inference_video(opt):
  #change to cpu/cuda
  DET = S3FD(device='cuda')

  flist = glob.glob(os.path.join(opt.frames_dir,opt.reference,'*.jpg'))
  flist.sort()

  dets = []
      
  for fidx, fname in enumerate(flist):

    start_time = time.time()
    
    image = cv2.imread(fname)

    image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    bboxes = DET.detect_faces(image_np, conf_th=0.9, scales=[opt.facedet_scale])

    dets.append([]);
    for bbox in bboxes:
      dets[-1].append({'frame':fidx, 'bbox':(bbox[:-1]).tolist(), 'conf':bbox[-1]})

    elapsed_time = time.time() - start_time

    print('%s-%05d; %d dets; %.2f Hz' % (os.path.join(opt.avi_dir,opt.reference,'video.avi'),fidx,len(dets[-1]),(1/elapsed_time))) 

  savepath = os.path.join(opt.work_dir,opt.reference, 'faces.pckl')

  with open(savepath, 'wb') as fil:
    pickle.dump(dets, fil)

  return dets
def inference_video(opt):

    DET = S3FD(device="cuda")

    flist = glob.glob(os.path.join(opt.frames_dir, opt.reference, "*.jpg"))
    flist.sort()

    dets = []

    for fidx, fname in enumerate(flist):

        start_time = time.time()

        image = cv2.imread(fname)

        image_np = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        bboxes = DET.detect_faces(image_np,
                                  conf_th=0.1,
                                  scales=[opt.facedet_scale])

        dets.append([])
        max_conf = 0
        temp = {"frame": fidx, "bbox": [400, 400, 650, 650], "conf": .1}
        for bbox in bboxes:
            # Only get the most confident one
            if bbox[-1] >= max_conf:
                max_conf = bbox[-1]
                temp = {
                    "frame": fidx,
                    "bbox": (bbox[:-1]).tolist(),
                    "conf": bbox[-1]
                }
        dets[-1].append(temp)

        elapsed_time = time.time() - start_time

        # print(
        #     "%s-%05d %d dets %.2f Hz"
        #     % (
        #         os.path.join(opt.avi_dir, opt.reference, "video.avi"),
        #         fidx,
        #         len(dets[-1]),
        #         (1 / elapsed_time),
        #     )
        # )

    savepath = os.path.join(opt.work_dir, opt.reference, "faces.pckl")

    with open(savepath, "wb") as fil:
        pickle.dump(dets, fil)

    return dets
Esempio n. 3
0
        axeslist.ravel()[ind].imshow(figures[title], cmap=plt.gray())
        axeslist.ravel()[ind].set_title(title)
    plt.tight_layout()
    plt.show()

# load image with cv in RGB.
IMAGE_PATH = 'selfie.jpg'
img = cv2.imread(IMAGE_PATH)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# load detectors.
DET1 = MTCNN(device='cuda')
DET2 = FaceBoxes(device='cuda')
DET3 = TinyFace(device='cuda')
DET4 = PyramidBox(device='cuda')
DET5 = S3FD(device='cuda')
DET6 = DSFD(device='cuda')

# MTCNN returns bboxes and landmarks.
t = time.time()
bboxes, _ = DET1.detect_faces(img, conf_th=0.9, scales=[1])
print('MTCNN : %d faces in %.4f seconds.' % (len(bboxes), time.time() - t))
img1 = draw_bboxes(img, bboxes)
sizes = []
for box in bboxes:
    sizes.append((box[2] - box[0]) * (box[3] - box[1]))
print(min(sizes))
print(max(sizes))

# FaceBoxes returns bboxes.
t = time.time()
        if os.path.exists(os.path.join(opt.avi_dir, reference)):
            rmtree(os.path.join(opt.avi_dir, reference))

        if os.path.exists(os.path.join(opt.frames_dir, reference)):
            rmtree(os.path.join(opt.frames_dir, reference))
    except:
        pass


# ========== ========== ========== ==========
# # MAIN
# ========== ========== ========== ==========

# ========== LOADING MANDATORY STUFF ==========
DET = S3FD(device='cuda')
s = SyncNetInstanceAnalyze()
s.loadParameters(opt.syncnet_model)
#print("Model %s loaded."%opt.syncnet_model);

# ========== FOR LOOP OVER ALL EXISTING MP4 FILES IN DATA_PATH ==========

videofiles = glob.glob(opt.data_path + '*/*.mp4')
videofiles = [
    v for i, v in enumerate(videofiles) if (i % opt.parts_num) == opt.part
]

#print('Started processing for {} GPUs'.format(opt.nthreads))

jobs = [(vfile, opt, i % opt.nthreads) for i, vfile in enumerate(videofiles)]
if opt.nthreads == 1:
Esempio n. 5
0
def main():
    args = get_args()
    method = args.method
    device = args.device
    conf_threshold = args.conf
    iou_threshold = args.iou

    WD = WIDERDataset()

    if method == 'mtcnn':
        det = MTCNN(device=device)
        scale_list = [0.0625, 0.125, 0.25, 0.5, 1, 2]
    elif method == 'tinyface':
        det = TinyFace(device=device)
        scale_list = [0.5, 1]
    elif method == 's3fd':
        det = S3FD(device=device)
        scale_list = [0.5, 1]
    elif method == 'dsfd':
        det = DSFD(device=device)
        scale_list = [0.5, 1]
    elif method == 'faceboxes':
        det = FaceBoxes(device=device)
        scale_list = [1, 2, 4]

    N = len(WD.data_dict.keys())
    total_iou = 0.0
    total_recall = 0.0
    total_precision = 0.0
    total_f1score = 0.0
    total_time = 0.0

    for image_index, image_name in enumerate(WD.data_dict.keys(), 1):

        print('%5d / %5d : %s' % (image_index, N, image_name))
        image = cv2.imread(os.path.join(IMAGE_DIR, image_name))
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        boxes = WD.data_dict[image_name]

        if method == 'mtcnn':
            img_time = time.time()
            pred_boxes, _ = det.detect_faces(image, conf_threshold, scale_list)
            img_time = time.time() - img_time
        else:
            img_time = time.time()
            pred_boxes = det.detect_faces(image, conf_threshold, scale_list)
            img_time = time.time() - img_time
        
        true_num = len(boxes)
        positive_num = len(pred_boxes)
        img_iou = 0.0
        img_recall = 0.0
        img_precision = 0.0
        img_f1score = 0.0

        pred_dict = dict()

        for box in boxes:
            max_iou = 0
            for i, pred_box in enumerate(pred_boxes):
                if i not in pred_dict.keys():
                    pred_dict[i] = 0
                iou = IoU(box, pred_box)
                if iou > max_iou:
                    max_iou = iou
                if iou > pred_dict[i]:
                    pred_dict[i] = iou
            img_iou += max_iou
        
        if true_num * positive_num > 0:
            true_positive = 0.0
            for i in pred_dict.keys():
                if pred_dict[i] > iou_threshold:
                    true_positive += 1.0
            img_recall = true_positive / true_num
            img_precision = true_positive / positive_num
            if img_recall * img_precision == 0:
                img_f1score = 0.0
            else:
                img_f1score = (2*img_recall*img_precision) / (img_recall+img_precision)
            img_iou = img_iou / true_num
        
            print('- | TP = %02d | TN =    |' % (true_positive))
            print('  | FP = %02d | FN = %02d |' % (positive_num - true_positive, true_num - true_positive))

        total_iou += img_iou
        total_recall += img_recall
        total_precision += img_precision
        total_f1score += img_f1score
        total_time += img_time

        print('- Avg.            IoU =', total_iou / image_index)
        print('- Avg.         Recall =', total_recall / image_index)
        print('- Avg.      Precision =', total_precision / image_index)
        print('- Avg.       F1-score =', total_f1score / image_index)
        print('- Avg. Inference Time =', total_time / image_index)
        print()