コード例 #1
0
def detect(img_path, model_path):
    file_id = utils.get_file_id(img_path)
    img, img_raw, scale,scale_ = read_img(img_path)
    print("dim1",img_raw.ndim)
    head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2,4])
    trainer = Head_Detector_Trainer(head_detector).cuda()
    trainer.load(model_path)
    img = at.totensor(img)
    img = img[None, : ,: ,:]
    img = img.cuda().float()
    st = time.time()
    pred_bboxes_, _ = head_detector.predict(img, scale, mode='evaluate', thresh=THRESH)
    et = time.time()
    tt = et - st
    print ("[INFO] Head detection over. Time taken: {:.4f} s".format(tt))
    for i in range(pred_bboxes_.shape[0]):
        print(i)
        ymin, xmin, ymax, xmax = pred_bboxes_[i,:]
        print(ymin, xmin, ymax, xmax)
        image_raw=Image.fromarray(np.uint8(img_raw))
        utils.draw_bounding_box_on_image(image_raw,ymin*scale_, xmin*scale_, ymax*scale_, xmax*scale_)
        img_raw=np.array(image_raw)
    image_raw=Image.fromarray(np.uint8(img_raw))
    if SAVE_FLAG == 1:
       #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png')
        image_raw.save(write_path+'/'+os.path.basename(img_path))
        frame_end = cv2.imread(write_path+'/'+os.path.basename(img_path))
        cv2.imshow("frame_end",frame_end)
        key_end = cv2.waitKey(1) & 0xFF
    else:
        image_raw.show()    
コード例 #2
0
def detect(img_read, model_path, head_detector):
    img, img_raw, scale, scale_ = read_img(img_read)
    #print("dim1",img_raw.ndim)

    img = at.totensor(img)
    img = img[None, :, :, :]
    img = img.cuda().float()
    st = time.time()
    pred_bboxes_, _ = head_detector.predict(img,
                                            scale,
                                            mode='evaluate',
                                            thresh=THRESH)
    et = time.time()
    tt = et - st
    thispic_count = 0
    #print ("[INFO] Head detection over. Time taken: {:.4f} s".format(tt))
    for i in range(pred_bboxes_.shape[0]):
        thispic_count = i
        #print(i)
        ymin, xmin, ymax, xmax = pred_bboxes_[i, :]
        #print(ymin, xmin, ymax, xmax)
        image_raw = Image.fromarray(np.uint8(img_raw))
        utils.draw_bounding_box_on_image(image_raw, ymin * scale_,
                                         xmin * scale_, ymax * scale_,
                                         xmax * scale_)
        img_raw = np.array(image_raw)
    image_raw = Image.fromarray(np.uint8(img_raw))

    if SAVE_FLAG == 1:
        pass
        #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png')
        #image_raw.save(write_path+'/'+os.path.basename(img_path))
    else:
        image_raw.show()
    return thispic_count
コード例 #3
0
def detect(img_read, write_path, head_detector, img_path):
    global show
    img, img_raw, scale, scale_ = read_img(img_read)
    print("dim1", img_raw.ndim)

    img_raw_2 = img_raw.copy()
    img = at.totensor(img)
    img = img[None, :, :, :]
    img = img.cuda().float()
    st = time.time()
    pred_bboxes_, _ = head_detector.predict(img,
                                            scale,
                                            mode='evaluate',
                                            thresh=THRESH)
    et = time.time()
    tt = et - st
    print("[INFO] Head detection over. Time taken: {:.4f} s".format(tt))
    for i in range(pred_bboxes_.shape[0]):
        print(i)
        ymin, xmin, ymax, xmax = pred_bboxes_[i, :]
        print(ymin, xmin, ymax, xmax)
        image_raw = Image.fromarray(np.uint8(img_raw))
        image_raw_2 = Image.fromarray(np.uint8(img_raw_2))

        utils.draw_bounding_box_on_image(image_raw, ymin * scale_,
                                         xmin * scale_, ymax * scale_,
                                         xmax * scale_)
        #cv2.rectangle(image_raw_2, (int(xmin*scale_), int(ymin*scale_)), (int(xmax*scale_), int(ymax*scale_)), 255,cv2.FILLED)
        #draw_rect = ImageDraw.Draw(image_raw_2)
        #draw_rect.rectangle((xmin*scale_, ymin*scale_, xmax*scale_, ymax*scale_), 'red')

        img_raw = np.array(image_raw)
        img_raw_2 = np.array(image_raw_2)
    image_raw = Image.fromarray(np.uint8(img_raw))
    image_raw_2 = Image.fromarray(np.uint8(img_raw_2))

    image_raw = cv2.cvtColor(np.asarray(image_raw), cv2.COLOR_RGB2BGR)
    image_raw_2 = cv2.cvtColor(np.asarray(image_raw_2), cv2.COLOR_RGB2BGR)

    if SAVE_FLAG == 1:
        #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png')
        #image_raw.save(write_path+'/'+os.path.basename(img_path))
        #frame_end = cv2.imread(write_path+'/'+os.path.basename(img_path))
        #cv2.imshow("frame_end",frame_end)
        cv2.imwrite(write_path + '/' + os.path.basename(img_path), image_raw)
        cv2.imshow("image_raw", image_raw)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            show = 0
    else:
        image_raw.show()
コード例 #4
0
def detect(img_read, write_path, head_detector, img_path, face_detect):
    global show
    global people_num_detect
    global best_frame
    people_num_cache = 0
    img, img_raw, scale, scale_ = read_img(img_read)
    #print("dim1",img_raw.ndim)

    img_raw_2 = img_raw.copy()
    img = at.totensor(img)
    img = img[None, :, :, :]
    img = img.cuda().float()
    st = time.time()
    pred_bboxes_, _ = head_detector.predict(img,
                                            scale,
                                            mode='evaluate',
                                            thresh=THRESH)
    et = time.time()
    tt = et - st
    print("[INFO] Head detection over. Time taken: {:.4f} s".format(tt))
    for i in range(pred_bboxes_.shape[0]):
        #print(i)
        people_num_cache = i + 1
        ymin, xmin, ymax, xmax = pred_bboxes_[i, :]
        #print(ymin, xmin, ymax, xmax)
        image_raw = Image.fromarray(np.uint8(img_raw))
        image_raw_2 = Image.fromarray(np.uint8(img_raw_2))

        utils.draw_bounding_box_on_image(image_raw, ymin * scale_,
                                         xmin * scale_, ymax * scale_,
                                         xmax * scale_)
        #cv2.rectangle(image_raw_2, (int(xmin*scale_), int(ymin*scale_)), (int(xmax*scale_), int(ymax*scale_)), 255,cv2.FILLED)
        draw_rect = ImageDraw.Draw(image_raw_2)
        draw_rect.rectangle(
            (xmin * scale_, ymin * scale_, xmax * scale_, ymax * scale_),
            'red')

        img_raw = np.array(image_raw)
        img_raw_2 = np.array(image_raw_2)
    image_raw = Image.fromarray(np.uint8(img_raw))
    image_raw_2 = Image.fromarray(np.uint8(img_raw_2))

    image_raw = cv2.cvtColor(np.asarray(image_raw), cv2.COLOR_RGB2BGR)
    image_raw_2 = cv2.cvtColor(np.asarray(image_raw_2), cv2.COLOR_RGB2BGR)

    rects, landmarks = face_detect.detect_face(image_raw_2, 20)
    people_num_cache += len(rects)
    for (i, rect) in enumerate(rects):
        cv2.rectangle(image_raw, (rect[0], rect[1]),
                      (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 2)

    if SAVE_FLAG == 1:
        #image_raw.save('/home/hx/Project/FCHD-Fully-Convolutional-Head-Detector-master/'+file_id+'_1.png')
        #image_raw.save(write_path+'/'+os.path.basename(img_path))
        #frame_end = cv2.imread(write_path+'/'+os.path.basename(img_path))
        #cv2.imshow("frame_end",frame_end)
        cv2.imwrite(write_path + '/' + os.path.basename(img_path), image_raw)
        cv2.imshow("image_raw", image_raw)

        key = cv2.waitKey(1) & 0xFF
        if key == ord("q"):
            show = 0
            print("best frame is **********" + best_frame +
                  "   num is *************" + str(people_num_detect))
    else:
        image_raw.show()
    return people_num_cache