def detect(img_path, model_path):
    file_id = utils.get_file_id(img_path)
    img, img_raw, scale = read_img(img_path)
    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]):
        ymin, xmin, ymax, xmax = pred_bboxes_[i, :]
        utils.draw_bounding_box_on_image_array(img_raw, ymin, xmin, ymax, xmax)
    plt.axis('off')
    plt.imshow(img_raw)
    if SAVE_FLAG == 1:
        plt.savefig(os.path.join(opt.test_output_path, file_id + '.png'),
                    bbox_inches='tight',
                    pad_inches=0)
    else:
        plt.show()
Exemple #2
0
def detect(img_path, model_path):
    file_id = utils.get_file_id(img_path)
    img, img_raw, scale = read_img(img_path)
    head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4])

    dict = torch.load(opt.caffe_pretrain_path)
    head_detector.load_state_dict(dict['model'])

    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]):
        ymin, xmin, ymax, xmax = pred_bboxes_[i, :]
        utils.draw_bounding_box_on_image_array(img_raw, ymin, xmin, ymax, xmax)
    cv2.imshow('asdf', img_raw)
    cv2.waitKey()
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()    
def detect2(img, img_raw, scale, model_path, file_file):
    head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4])
    trainer = Head_Detector_Trainer(head_detector).cuda()

    # load model
    trainer.load(model_path)
    img = at.totensor(img)
    img = img[None, :, :, :]
    img = img.cuda().float()

    # predict model
    pred_bboxes_, _ = head_detector.predict(img,
                                            scale,
                                            mode='evaluate',
                                            thresh=THRESH)

    for i in range(pred_bboxes_.shape[0]):
        ymin, xmin, ymax, xmax = pred_bboxes_[i, :]
        file_file.write(
            str(int(xmin / scale)) + ',' + str(int(ymin / scale)) + ',' +
            str(int(xmax / scale)) + ',' + str(int(ymax / scale)) + ',' + '0' +
            ' ')
Exemple #5
0
def train():
    # Get the dataset
    for phase in phases:
        if phase == 'train':
            if dataset_name == 'hollywood':
                train_data_list_path = os.path.join(
                    opt.hollywood_dataset_root_path, 'hollywood_train.idl')
                train_data_list = utils.get_phase_data_list(
                    train_data_list_path, dataset_name)
            if dataset_name == 'brainwash':
                train_data_list_path = os.path.join(
                    opt.brainwash_dataset_root_path, 'brainwash_train.idl')
                train_data_list = utils.get_phase_data_list(
                    train_data_list_path, dataset_name)
        elif phase == 'val':
            if dataset_name == 'hollywood':
                val_data_list_path = os.path.join(
                    opt.hollywood_dataset_root_path, 'hollywood_val.idl')
                val_data_list = utils.get_phase_data_list(
                    val_data_list_path, dataset_name)
            if dataset_name == 'brainwash':
                val_data_list_path = os.path.join(
                    opt.brainwash_dataset_root_path, 'brainwash_val.idl')
                val_data_list = utils.get_phase_data_list(
                    val_data_list_path, dataset_name)
        elif phase == 'test':
            if dataset_name == 'hollywood':
                test_data_list_path = os.path.join(
                    opt.hollywood_dataset_root_path, 'hollywood_test.idl')
                test_data_list = utils.get_phase_data_list(
                    test_data_list_path, dataset_name)
            if dataset_name == 'brainwash':
                test_data_list_path = os.path.join(
                    opt.brainwash_dataset_root_path, 'brainwash_test.idl')
                test_data_list = utils.get_phase_data_list(
                    test_data_list_path, dataset_name)

    print("Number of images for training: %s" % (len(train_data_list)))
    print("Number of images for val: %s" % (len(val_data_list)))
    print("Number of images for test: %s" % (len(test_data_list)))

    if data_check_flag:
        utils.check_loaded_data(train_data_list[random.randint(
            1, len(train_data_list))])
        utils.check_loaded_data(val_data_list[random.randint(
            1, len(val_data_list))])
        utils.check_loaded_data(test_data_list[random.randint(
            1, len(test_data_list))])

    # Load the train dataset
    train_dataset = Dataset(train_data_list)
    test_dataset = Dataset(val_data_list)
    print("Load data.")

    train_dataloader = data_.DataLoader(train_dataset,
                                        batch_size=1,
                                        shuffle=True,
                                        num_workers=1)
    test_dataloader = data_.DataLoader(test_dataset,
                                       batch_size=1,
                                       shuffle=True,
                                       num_workers=1)
    # Initialize the head detector.
    head_detector_vgg16 = Head_Detector_VGG16(ratios=[1],
                                              anchor_scales=[8, 16])
    print("model construct completed")
    trainer = Head_Detector_Trainer(head_detector_vgg16).cuda()
    lr_ = opt.lr
    for epoch in range(opt.epoch):
        trainer.reset_meters()
        for ii, (img, bbox_, scale) in enumerate(train_dataloader):
            scale = at.scalar(scale)
            img, bbox = img.cuda().float(), bbox_.cuda()
            img, bbox = Variable(img), Variable(bbox)
            _, _, _ = trainer.train_step(img, bbox, scale)
            print("Forward and backward pass done.")
            if (ii + 1) % opt.plot_every == 0:
                trainer.vis.plot_many(trainer.get_meter_data())
                ori_img_ = inverse_normalize(at.tonumpy(img[0]))
                gt_img = visdom_bbox(ori_img_, at.tonumpy(bbox_[0]))
                trainer.vis.img('gt_img', gt_img)
                rois, _ = trainer.head_detector.predict(img,
                                                        scale=scale,
                                                        mode='visualize')
                pred_img = visdom_bbox(ori_img_, at.tonumpy(rois))
                trainer.vis.img('pred_img', pred_img)
                trainer.vis.text(str(trainer.rpn_cm.value().tolist()),
                                 win='rpn_cm')

        avg_test_CorrLoc = eval(test_dataloader, head_detector_vgg16)

        print("Epoch {} of {}.".format(epoch + 1, opt.epoch))
        print("  test average corrLoc accuracy:\t\t{:.3f}".format(
            avg_test_CorrLoc))

        model_save_path = trainer.save(best_map=avg_test_CorrLoc)

        if epoch == 8:
            trainer.load(model_save_path)
            trainer.head_detector.scale_lr(opt.lr_decay)
            lr_ = lr_ * opt.lr_decay
Exemple #6
0
import cv2
from src.head_detector_vgg16 import Head_Detector_VGG16
from trainer import Head_Detector_Trainer
from PIL import Image
import numpy as np
from data.dataset import preprocess
import src.array_tool as at

THRESH = 0.0075
IM_RESIZE = True

model_path = "/home/zhang/FCHD-Fully-Convolutional-Head-Detector/checkpoints/head_detector_final"
head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2, 4])
trainer = Head_Detector_Trainer(head_detector).cuda()
trainer.load(model_path)


def read_img(path):
    f = Image.fromarray(path)
    if IM_RESIZE:
        f = f.resize((640, 480), Image.ANTIALIAS)

    f.convert('RGB')
    img_raw = np.asarray(f, dtype=np.uint8)
    img_raw_final = img_raw.copy()
    img = np.asarray(f, dtype=np.float32)
    _, H, W = img.shape
    img = img.transpose((2, 0, 1))
    img = preprocess(img)
    _, o_H, o_W = img.shape
 def __init__(self, model_path="/home/ubuntu/suraj/package/FCHD/checkpoints/head_detector_final"):
     self.head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2,4])
     trainer = Head_Detector_Trainer(self.head_detector).cuda()
     trainer.load(model_path)
import numpy as np
from data.dataset import preprocess
import matplotlib.pyplot as plt 
import src.array_tool as at
from src.vis_tool import visdom_bbox
import argparse
import src.utils as utils
from src.config import opt
#import simplejson as json


os.environ["CUDA_VISIBLE_DEVICES"]="0,1"

head_detector = Head_Detector_VGG16(ratios=[1], anchor_scales=[2,4])
trainer = Head_Detector_Trainer(head_detector).cuda()
trainer.load('./checkpoints/head_detector_final')



#data = {"taskId":"1", "roomId":"1", "liveUrl":"3.mp4", "recognitionDuration":30,"verifyCode":"xxxx"}
class IndexHandler(tornado.web.RequestHandler):
    executor = ThreadPoolExecutor(200)
    @tornado.web.asynchronous
    @tornado.gen.coroutine
    def post(self):
        try:
            #sends= json_decode(self.request.body)
            sends = self.request.body
            data = json.loads(self.request.body)
            liveUrl = data['liveUrl']
            recognitionDuration = data['recognitionDuration']