def predict():
    visualize = request.args.get('visualize', 'none')
    resize = request.args.get('resize', 'original')
    score_threshold = float(
        request.args.get('score_threshold', str(config.score_threshold)))
    nms_iou_threshold = float(
        request.args.get('nms_iou_threshold', str(config.nms_iou_threshold)))
    img = utils.extract_as_jpeg(request)

    x = utils.img2tensor(img, resize=True)

    x_original = utils.img2tensor(img, resize=False)
    scale = (x_original[0].size()[1] / x[0].size()[1],
             x_original[0].size()[2] / x[0].size()[2])

    y = model(x)

    ret = dict()
    if len(y[0]['boxes']) > 0:
        ret = utils.post_process(y[0]['boxes'], y[0]['scores'],
                                 score_threshold, nms_iou_threshold)
        if resize == 'original':
            ret['boxes'] = utils.rescale_box(ret['boxes'], scale)
    else:
        ret['boxes'] = [[]]
        ret['scores'] = []

    if visualize == 'none':
        return jsonify(ret)
    img_to_show = x_original[0] if resize == 'original' else x
    if visualize == 'bbox':
        fig = utils.show_detection(img_to_show,
                                   ret['boxes'],
                                   pred_score=ret['scores'])
        output = io.BytesIO()
        FigureCanvas(fig).print_png(output)
        return Response(output.getvalue(), mimetype='image/png')
    elif visualize == 'blur':
        return blur({'boxes': ret['boxes'], 'image': img})
Exemple #2
0
import os
import matplotlib.pyplot as plt
from datasets import DetectionTestDataset

print('start setup detector')
detector = DetectionAlgorithm('weights/Jan_net_epoch=25batch=322.pth',
                              ['open', 'close'],
                              conf_threshold=0.95,
                              iou_threshold=0.01)

print('end setup detector')
# detect, single image detect API test
if True:
    img = imageio.imread(r'E:\agent3\lab\switch\JPEGImages\1001.jpg')
    det = detector.detect(r'E:\agent3\lab\switch\JPEGImages\1001.jpg')
    show_detection(img, det['absolute_box'])

# test batch convert(cpu)
if False:
    for fname in glob.glob('E:/agent3/lab/switch2/JPEGImages/*.jpg'):
        img = imageio.imread(fname)
        det = detector.detect(img)

        name, _ = os.path.splitext(os.path.split(fname)[-1])
        target_path = os.path.join('images/eval', name + '.jpg')

        print('{} -> {}'.format(fname, target_path))

        show_detection(img,
                       det['absolute_box'],
                       text_list=det['class'],
Exemple #3
0
datasets = TestDatasets(config.datasets)

num_test = 10
for img_numpy in datasets:
    img_tensor = test_transform(img_numpy)
    img_wrap = img_tensor.unsqueeze(0)

    loc, conf = net(img_wrap)

    conf_prob = F.softmax(conf, dim=2)
    max_value, max_idx = conf_prob[0].max(0)
    print(max_value)
    print(max_idx)
    loc_decoded = decode(loc, net.priors_center_offset)
    h, w, c = img_numpy.shape
    loc_abs = loc_decoded * torch.tensor([w, h, w, h]).float()

    print(loc_abs[:, max_idx[0]])
    show_detection(img_numpy, loc_abs[:, max_idx[0]])  # Find max conf
    print(loc_abs[:, max_idx[1]])
    show_detection(img_numpy, loc_abs[:, max_idx[1]])  # Find max conf
    print(loc_abs[:, max_idx[2]])
    show_detection(img_numpy, loc_abs[:, max_idx[2]])  # Find max conf

    num_test -= 1
    if num_test <= 0:
        break

if False:
    from torchviz import make_dot, make_dot_from_trace
    make_dot(net(img_wrap))
Exemple #4
0

boxes_base = torch.tensor([[0.1, 0.1, 0.3, 0.3], [0.7, 0.7, 0.9, 0.9]])
boxes_noised = boxes_base.repeat(1, 10).view(-1, 4)
boxes_noised = boxes_noised + torch.randn_like(boxes_noised) * 0.02

datasets = TestDatasets(config.datasets)
for img in datasets:
    break

print(
    'The image is only selected for test, boxes are fake input instead of output of model.'
)
h, w, c = img.shape
print('before nms')
show_detection(img, boxes_noised * torch.tensor([w, h, w, h]).float())

print('after nms')
fake_conf = torch.zeros(boxes_noised.shape[0], 3)
fake_conf[:10, 1] = 10.
fake_conf[10:, 2] = 10.
boxes_idx = nms(boxes_noised, fake_conf)
boxes_nmsed = boxes_noised[boxes_idx, :]
show_detection(img, boxes_nmsed * torch.tensor([w, h, w, h]).float())

print('corner case test(one class miss)')
fake_conf = torch.zeros(boxes_noised.shape[0], 3)
fake_conf[:10, 1] = 10.
#fake_conf[10:,2] = 10.
boxes_idx = nms(boxes_noised, fake_conf)
boxes_nmsed = boxes_noised[boxes_idx, :]
Exemple #5
0
def main(model):

    #parser = argparse.ArgumentParser(prog='test.py', description='ref_emotion.')
    #parser.add_argument('--model', type=str, default='dgn')
    #args = parser.parse_args()

    lm_ph = tf.placeholder(tf.float32, [None, 51 * 2])
    img_ph = tf.placeholder(tf.float32, [None, 224, 224, 3])
    keep_prob = tf.placeholder(tf.float32)

    with tf.Session() as sess:

        dgn = vgg_face.DGN()
        # dgn.train = False
        dgn.build(lm_ph, keep_prob)
        pred_dgn = tf.argmax(dgn.prob, 1)

        dan = vgg_face.Vgg_face()
        # dan.train = False
        dan.build(img_ph, keep_prob)
        pred_dan = tf.argmax(dan.prob, 1)

        if model == 'weighted-sum':

            prob_sum = tf.nn.softmax(dan.fc8 + dgn.fc3)
            pred_sum = tf.argmax(prob_sum, 1)

        elif model == 'joint-fine-tune':

            saver = tf.train.Saver()
            saver.restore(sess, '../model/dgan.ckpt')
            prob_joint = tf.nn.softmax(dan.fc8 + dgn.fc3)
            pred_joint = tf.argmax(prob_joint, 1)

        sess.run(tf.global_variables_initializer())

        stream = urllib.urlopen('http://admin:@192.168.0.194:3333/MJPEG.CGI')
        #stream = cv2.VideoCapture('../../videos/20180226_154918.mp4')
        #stream = cv2.VideoCapture(clip)
        #if stream.isOpened() == False:
        #print('Error opening video stream of file')

        bytes = ''
        emo_record = (np.ones(frames, dtype=int) * 5).tolist()

        #TODO
        '''
        emo_buffer = collections.deque(maxlen=10)
        state_record = []
        final_states = []
        '''

        import time
        time.sleep(10.0)

        while True:
            #while stream.isOpened():

            bytes += stream.read(1024)
            a = bytes.find('\xff\xd8')
            b = bytes.find('\xff\xd9')
            #ret, frame = stream.read()
            if a != -1 and b != -1:
                #if ret == True:
                frame = cv2.imdecode(
                    np.fromstring(bytes[a:b + 2], dtype=np.uint8), 1)
                bytes = bytes[b + 2:]

                num, face, shape, shape_origin = dlib_detect(
                    frame, 2, face_det, lm_det, 224, 224)
                if num == 1:

                    shape_norm = shape[17:] - shape[30]
                    shape_norm = shape_norm.reshape([1, 51 * 2])
                    if model == 'dan':
                        pred = sess.run(pred_dan,
                                        feed_dict={
                                            img_ph:
                                            face.reshape([1, 224, 224, 3]),
                                            keep_prob: 1.0
                                        })
                    elif model == 'dgn':
                        pred = sess.run(pred_dgn,
                                        feed_dict={
                                            lm_ph: shape_norm,
                                            keep_prob: 1.0
                                        })
                    elif model == 'weighted-sum':
                        pred = sess.run(pred_dgn,
                                        feed_dict={
                                            img_ph:
                                            face.reshape([1, 224, 224, 3]),
                                            lm_ph: shape_norm,
                                            keep_prob: 1.0
                                        })
                    elif model == 'joint-fine-tune':
                        pred = sess.run(pred_joint,
                                        feed_dict={
                                            img_ph:
                                            face.reshape([1, 224, 224, 3]),
                                            lm_ph: shape_norm,
                                            keep_prob: 1.0
                                        })

                    emo_record.append(int(pred))
                    del emo_record[0]
                    ctr = collections.Counter(emo_record)

                    #TODO
                    '''
                    emo_buffer.append(ctr)
                    emo_his = collections.Counter()
                    emo_his_table = np.zeros(len(emo_list))
                    emo_now_table = np.zeros(len(emo_list))
                    for c in emo_buffer:
                        emo_his += c
                    emo_his_avg = [v/float(len(emo_buffer)) for v in emo_his.values()]
                    emo_his = emo_his.items()
                    emo_his = np.array([np.array([list(emo_his[i])[0], emo_his_avg[i]]) for i in range(len(emo_his))])
                    emo_his_table[emo_his[:,0].astype(int)] = emo_his[:,1]
                    emo_now = ctr.items()
                    emo_now = np.array([np.array([list(e)[0], list(e)[1]]) for e in emo_now])
                    emo_now_table[emo_now[:,0].astype(int)] = emo_now[:,1] 
                    state = np.array([(emo_now_table[i]>=3 and (emo_now_table[i]-emo_his_table[i])>=0.0) for i in range(len(emo_list))]).astype(int)

                    state_int = 0
                    for i, j in enumerate(state):
                        state_int += j<<i

                    state_record.append(state_int)
                    if len(state_record) == 10:
                        state_ctr = collections.Counter(state_record)
                        final_state = state_ctr.most_common()[0][0]
                        final_states.append(final_state)
                        del state_record[:]
                    '''

                    emotion = emo_list[ctr.most_common()[0][0]]
                    #print(emotion)
                    im_show = show_detection(frame, shape_origin, 1, emotion)

                    cv2.imshow('frame', im_show)
                else:
                    cv2.imshow('frame', frame)

                if cv2.waitKey(3) & 0xFF == ord('q'):
                    break
            #else:
            #break

        #stream.release()
        cv2.destroyAllWindows()
Exemple #6
0
    channel_swap, to_tensor, normalize, train_transform, test_transform, \
    enchancement_transform, base_transform,resize
from utils import rectangle, show_detection

if __name__ == '__main__':
    dataset = DetectionDataset(r'E:\agent3\lab\switch', ['open', 'close'])
    for c, d, e in dataset:
        break

    if True:
        print('test random_horizontal_flip')
        for i in range(5):
            ct = c.copy()
            dt = d.copy()
            ct, dt = random_horizontal_flip(ct, dt)
            show_detection(ct, dt)

    if True:
        print('test random_crop')
        for i in range(7):
            ct = c.copy()
            dt = d.copy()
            ce = e.copy()
            ct, dt, ce = random_crop(ct, dt, ce)
            show_detection(ct, dt)

    if True:
        print('test photometric_distort')
        for i in range(5):
            ct = c.copy()
            dt = d.copy()
Exemple #7
0
def main():

    parser = argparse.ArgumentParser(prog='test.py',
                                     description='ref_emotion.')
    parser.add_argument('--model', type=str, default='dgn')
    args = parser.parse_args()

    lm_ph = tf.placeholder(tf.float32, [None, 51 * 2])
    img_ph = tf.placeholder(tf.float32, [None, 224, 224, 3])
    keep_prob = tf.placeholder(tf.float32)

    with tf.Session() as sess:

        dgn = vgg_face.DGN()
        # dgn.train = False
        dgn.build(lm_ph, keep_prob)
        pred_dgn = tf.argmax(dgn.prob, 1)

        dan = vgg_face.Vgg_face()
        # dan.train = False
        dan.build(img_ph, keep_prob)
        pred_dan = tf.argmax(dan.prob, 1)

        if args.model == 'weighted-sum':

            prob_sum = tf.nn.softmax(dan.fc8 + dgn.fc3)
            pred_sum = tf.argmax(prob_sum, 1)

        elif args.model == 'joint-fine-tune':

            saver = tf.train.Saver()
            saver.restore(sess, '../model/dgan.ckpt')
            prob_joint = tf.nn.softmax(dan.fc8 + dgn.fc3)
            pred_joint = tf.argmax(prob_joint, 1)

        sess.run(tf.global_variables_initializer())

        cap = cv2.VideoCapture(0)
        emo_record = (np.ones(frames, dtype=int) * 5).tolist()

        while (cap.isOpened()):

            ret, frame = cap.read()
            if ret == True:

                num, face, shape, shape_origin = dlib_detect(
                    frame, 2, face_det, lm_det, 224, 224)
                if num == 1:

                    shape_norm = shape[17:] - shape[30]
                    shape_norm = shape_norm.reshape([1, 51 * 2])
                    if args.model == 'dan':
                        pred = sess.run(pred_dan,
                                        feed_dict={
                                            img_ph:
                                            face.reshape([1, 224, 224, 3]),
                                            keep_prob: 1.0
                                        })
                    elif args.model == 'dgn':
                        pred = sess.run(pred_dgn,
                                        feed_dict={
                                            lm_ph: shape_norm,
                                            keep_prob: 1.0
                                        })
                    elif args.model == 'weighted-sum':
                        pred = sess.run(pred_dgn,
                                        feed_dict={
                                            img_ph:
                                            face.reshape([1, 224, 224, 3]),
                                            lm_ph: shape_norm,
                                            keep_prob: 1.0
                                        })
                    elif args.model == 'joint-fine-tune':
                        pred = sess.run(pred_joint,
                                        feed_dict={
                                            img_ph:
                                            face.reshape([1, 224, 224, 3]),
                                            lm_ph: shape_norm,
                                            keep_prob: 1.0
                                        })

                    emo_record.append(int(pred))
                    del emo_record[0]
                    ctr = collections.Counter(emo_record)
                    emotion = emo_list[ctr.most_common()[0][0]]
                    im_show = show_detection(frame, shape_origin, 1, emotion)

                    cv2.imshow('frame', im_show)
                else:
                    cv2.imshow('frame', frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()