Exemple #1
0
    def BaseFeat(self, leader):
        feature = list()
        bases = glob.glob('raw2/' + leader + '-base/' +leader+'_*.jpg')
        # bases = glob.glob('raw2/' + leader + '-base/*.jpg')
        for n, path in enumerate(bases):
            label = os.path.basename(path)[:-4] + ';0;'
            
            if label[7:9] =='61':
                img = cv2.imread(path)
                img = img[:, img.shape[1]/2:img.shape[1]]
                boxes,points = model.detect(img)
                f = model.get_feature(img, boxes[0], points[0])
                img = img[int(boxes[0][1]):int(boxes[0][3]), int(boxes[0][0]):int(boxes[0][2])]
                cv2.imwrite('DXP_00061_2.jpg', img)
                if f is None: print('DXP_00061.jpg is None')
                feature.append(f)
                continue

            img = cv2.imread(path)
            bbs, bbs_other = utils.get_bbox('txts/' + leader + '/', label, default_leader=leader)
            for i in range(0, int(max(float(bbs[0][2]), float(bbs[0][3])))):
                bbs_temp = (max(0, int(float(bbs[0][0])) - i), 
                            max(0, int(float(bbs[0][1])) - i),
                            min(img.shape[1], int(float(bbs[0][0]) + float(bbs[0][2]) + i)),
                            min(img.shape[0], int(float(bbs[0][1]) + float(bbs[0][3]) + i)))
                img_temp = img[bbs_temp[1]:bbs_temp[3], bbs_temp[0]:bbs_temp[2]]
                # cv2.imshow("crop", img_temp)
                # cv2.waitKey(0)
                f = model.get_feature_limited(img_temp)
                if f is not None:
                    # cv2.imwrite('raw2/' + leader + '-base/' +str(n)+'.jpg', img_temp)
                    break
            if f is None: print(path + ' is None')
            feature.append(f)
        return feature
Exemple #2
0
 def BaseFeat(self, leader):
     feature = list()
     bases = glob.glob('raw/' + leader + '-base/*.jpg')
     for path in bases:
         print(path)
         img = cv2.imread(path)
         label = os.path.basename(path)[:-4] + ';0;'
         bbs, bbs_other = utils.get_bbox('txts/' + leader + '/',
                                         label,
                                         default_leader=leader)
         for i in range(2, 10):
             pad = [float(bbs[0][2]) / i, float(bbs[0][3]) / i]
             bbs_temp = (
                 max(0, int(float(bbs[0][0]) - pad[0])),
                 max(0, int(float(bbs[0][1]) - pad[1])),
                 min(img.shape[1],
                     int(float(bbs[0][0]) + float(bbs[0][2]) + pad[0])),
                 min(img.shape[0],
                     int(float(bbs[0][1]) + float(bbs[0][3]) + pad[1])))
             img_temp = img[bbs_temp[1]:bbs_temp[3],
                            bbs_temp[0]:bbs_temp[2]]
             # cv2.imshow("crop", img_temp)
             # cv2.waitKey(0)
             f = model.get_feature(img_temp)
             if f is not None:
                 break
         if f is None: print(path)
         feature.append(f)
     return feature
    def BaseFeat(self, leader):
        feature_fro = list()
        feature_pro = list()
        bases = glob.glob('raw2/' + leader + '-base/' + leader + '_*.jpg')
        # bases = glob.glob('raw2/' + leader + '-base/*.jpg')
        for n, path in enumerate(bases):
            label = os.path.basename(path)[:-4] + ';0;'
            print(label)

            # if label[7:9] =='61':
            #     img = cv2.imread(path)
            #     img = img[:, int(img.shape[1]/2):img.shape[1]]
            #     boxes,points = model.detect(img)
            #     f = model.get_feature(img, boxes[0], points[0])
            #     img = img[int(boxes[0][1]):int(boxes[0][3]), int(boxes[0][0]):int(boxes[0][2])]
            #     cv2.imwrite('DXP_00061_2.jpg', img)
            #     if f is None: print('DXP_00061.jpg is None')
            #     feature.append(f)
            #     continue

            img = cv2.imread(path)
            # print(img.shape)
            bbs, bbs_other = utils.get_bbox('txts/' + leader + '/',
                                            label,
                                            default_leader=leader)
            bound1 = (float(bbs[0][0]),
                      float(bbs[0][1]), float(bbs[0][2]) + float(bbs[0][0]),
                      float(bbs[0][3]) + float(bbs[0][1]))
            preds = fa.get_landmarks(path, bound1)[-1]
            ## face embedding 512d
            f = model.get_feature_by_landmark(img, bound1, preds)

            # for i in range(0, int(max(float(bbs[0][2]), float(bbs[0][3])))):
            #     bbs_temp = (max(0, int(float(bbs[0][0])) - i),
            #                 max(0, int(float(bbs[0][1])) - i),
            #                 min(img.shape[1], int(float(bbs[0][0]) + float(bbs[0][2]) + i)),
            #                 min(img.shape[0], int(float(bbs[0][1]) + float(bbs[0][3]) + i)))
            #     img_temp = img[bbs_temp[1]:bbs_temp[3], bbs_temp[0]:bbs_temp[2]]
            #     # cv2.imshow("crop", img_temp)
            #     # cv2.waitKey(0)
            #     f = model.get_feature_limited(img_temp)
            #     if f is not None:
            #         # cv2.imwrite('raw2/' + leader + '-base/' +str(n)+'.jpg', img_temp)
            #         break
            # if f is None: print(path + ' is None')
            if bbs[0][6] == 1:
                feature_pro.append(f)
            elif bbs[0][6] == 0:
                feature_fro.append(f)
            else:
                print('no profile / frontal file\n')
        return feature_fro, feature_pro
Exemple #4
0
 def FP(self, leader):
     FP_flags = list()
     bases = glob.glob('raw2/' + leader + '-base/' +leader+'_*.jpg')
     # bases = glob.glob('raw2/' + leader + '-base/*.jpg')
     for n, path in enumerate(bases):
         label = os.path.basename(path)[:-4] + ';0;'
         print(label)
         img = cv2.imread(path)
         # print(img.shape)
         bbs, bbs_other = utils.get_bbox('txts/' + leader + '/', label, default_leader=leader)
         bound1 = (float(bbs[0][0]), float(bbs[0][1]), float(bbs[0][2])+float(bbs[0][0]), float(bbs[0][3])+float(bbs[0][1]))
         preds = fa.get_landmarks(path , bound1)[-1]
                     ## face embedding 512d
         f1 = model.get_feature_by_landmark(img, bound1, preds)
         f2 = model.get_fp_by_landmark(img, bound1, preds)
         c = (f1,f2)
         FP_flags.append(c)
     return FP_flags
    def BaseFeat(self, leader):
        feature_fro = list()
        feature_pro = list()
        bases = glob.glob('raw3/' + leader + '-base/' +leader+'_*.jpg')
        # bases = glob.glob('raw2/' + leader + '-base/*.jpg')
        for n, path in enumerate(bases):
            label = os.path.basename(path)[:-4] + ';0;'
            file = open('txts_new/' + leader + '/'+ label.split(';')[0] + '.txt')
            for line in file:
                if line.split(' ')[-2] == leader:
                    print(line)
                    break
            continue
            img = cv2.imread(path)
            # print(img.shape)
            bbs, bbs_other = utils.get_bbox('txts/' + leader + '/', label, default_leader=leader)
            bound1 = (float(bbs[0][0]), float(bbs[0][1]), float(bbs[0][2])+float(bbs[0][0]), float(bbs[0][3])+float(bbs[0][1]))
            preds = fa.get_landmarks(path , bound1)[-1]
                        ## face embedding 512d
            f, _  = model.get_feature_by_landmark(img, bound1, preds)

            # for i in range(0, int(max(float(bbs[0][2]), float(bbs[0][3])))):
            #     bbs_temp = (max(0, int(float(bbs[0][0])) - i), 
            #                 max(0, int(float(bbs[0][1])) - i),
            #                 min(img.shape[1], int(float(bbs[0][0]) + float(bbs[0][2]) + i)),
            #                 min(img.shape[0], int(float(bbs[0][1]) + float(bbs[0][3]) + i)))
            #     img_temp = img[bbs_temp[1]:bbs_temp[3], bbs_temp[0]:bbs_temp[2]]
            #     # cv2.imshow("crop", img_temp)
            #     # cv2.waitKey(0)
            #     f = model.get_feature_limited(img_temp)
            #     if f is not None:
            #         # cv2.imwrite('raw2/' + leader + '-base/' +str(n)+'.jpg', img_temp)
            #         break
            # if f is None: print(path + ' is None')
            if bbs[0][6] == 1:
                feature_pro.append(f)
            elif bbs[0][6] == 0:
                feature_fro.append(f)
            else:
                print('no profile / frontal file\n')
        return feature_fro, feature_pro
    def BaseFeat(self, leader):
        feature = list()
        bases = glob.glob('raw2/' + leader + '-base/' + leader + '_*.jpg')
        # bases = glob.glob('raw2/' + leader + '-base/*.jpg')

        for n, path in enumerate(bases):
            label = os.path.basename(path)[:-4] + ';0;'
            print(label)
            img = cv2.imread(path)
            bbs, bbs_other = utils.get_bbox('txts/' + leader + '/',
                                            label,
                                            default_leader=leader)
            bound1 = (float(bbs[0][0]),
                      float(bbs[0][1]), float(bbs[0][2]) + float(bbs[0][0]),
                      float(bbs[0][3]) + float(bbs[0][1]))
            preds = fa.get_landmarks(path, bound1)[-1]
            ## face embedding 512d
            f, nimg = model.get_feature_by_landmark(img, bound1, preds)
            cv2.imwrite('raw2/{}-base/{}.jpg'.format(leader, n), nimg)

            feature.append(f)
        return feature
 def BaseFeat(self, leader):
     feature = list()
     bases = glob.glob('raw/' + leader + '-base/*.jpg')
     for n, path in enumerate(bases):
         print(path)
         # img_name = os.path.basename(path)[:-4]
         # if len(img_name) > 3:
         #     continue
         # img = cv2.imread(path)
         # f = model.get_feature(img)
         # if f is None:
         #     print(path +' is None')
         label = os.path.basename(path)[:-4] + ';0;'
         if len(label) < 6:
             continue
         img = cv2.imread(path)
         bbs, bbs_other = utils.get_bbox('txts/' + leader + '/',
                                         label,
                                         default_leader=leader)
         for i in range(2, 10):
             pad = [float(bbs[0][2]) / i, float(bbs[0][3]) / i]
             bbs_temp = (
                 max(0, int(float(bbs[0][0]) - pad[0])),
                 max(0, int(float(bbs[0][1]) - pad[1])),
                 min(img.shape[1],
                     int(float(bbs[0][0]) + float(bbs[0][2]) + pad[0])),
                 min(img.shape[0],
                     int(float(bbs[0][1]) + float(bbs[0][3]) + pad[1])))
             img_temp = img[bbs_temp[1]:bbs_temp[3],
                            bbs_temp[0]:bbs_temp[2]]
             # cv2.imshow("crop", img_temp)
             # cv2.waitKey(0)
             f = model.get_feature(img_temp)
             if f is not None:
                 break
         if f is None: print(path)
         cv2.imwrite('raw/{}-base/{}.jpg'.format(leader, n), img_temp)
         feature.append(f)
     return feature
 def BaseFeat(self, leader):
     feature = list()
     bases = glob.glob('raw/' + leader + '-base/' + leader + '*.jpg')
     num = 0
     for path in bases:
         print(path)
         img = cv2.imread(path)
         label = os.path.basename(path)[:-4] + ';0;'
         bbs, bbs_other = utils.get_bbox('txts/' + leader + '/',
                                         label,
                                         default_leader=leader)
         for i in [0] + list(range(20, 102, 2)):
             i = i * 0.1
             if i == 0:
                 pad = [0, 0]
             else:
                 pad = [float(bbs[0][2]) / i, float(bbs[0][3]) / i]
             bbs_temp = (
                 max(0, int(float(bbs[0][0]) - pad[0])),
                 max(0, int(float(bbs[0][1]) - pad[1])),
                 min(img.shape[1],
                     int(float(bbs[0][0]) + float(bbs[0][2]) + pad[0])),
                 min(img.shape[0],
                     int(float(bbs[0][1]) + float(bbs[0][3]) + pad[1])))
             img_temp = img[bbs_temp[1]:bbs_temp[3],
                            bbs_temp[0]:bbs_temp[2]]
             # cv2.imshow("crop", img_temp)
             # cv2.waitKey(0)
             f = model.get_feature(img_temp)
             if f is not None:
                 # cv2.imwrite('raw/' + leader + '-base/'+str(num)+'.jpg', img_temp)
                 num += 1
                 break
         if f is None: print(path + ' is None')
         feature.append(f)
     print(leader + ' ' + str(num))
     return feature
Exemple #9
0
    def BaseFeat(self, leader):
        feature = list()
        bases = glob.glob('raw2/' + leader + '-base/' + leader + '_*.jpg')
        # bases = glob.glob('raw2/' + leader + '-base/*.jpg')
        for n, path in enumerate(bases):
            label = os.path.basename(path)[:-4] + ';0;'
            print(label)
            # if len(label) > 6: continue
            # print(path)
            # img = cv2.imread(path)
            # f = model.get_feature(img)

            img = cv2.imread(path)
            bbs, bbs_other = utils.get_bbox('txts/' + leader + '/',
                                            label,
                                            default_leader=leader)
            for i in range(0, int(max(float(bbs[0][2]), float(bbs[0][3])))):
                bbs_temp = (max(0,
                                int(float(bbs[0][0])) - i),
                            max(0,
                                int(float(bbs[0][1])) - i),
                            min(img.shape[1],
                                int(float(bbs[0][0]) + float(bbs[0][2]) + i)),
                            min(img.shape[0],
                                int(float(bbs[0][1]) + float(bbs[0][3]) + i)))
                img_temp = img[bbs_temp[1]:bbs_temp[3],
                               bbs_temp[0]:bbs_temp[2]]
                # cv2.imshow("crop", img_temp)
                # cv2.waitKey(0)
                f = model.get_feature_limited(img_temp)
                if f is not None:
                    # cv2.imwrite('raw2/' + leader + '-base/' +str(n)+'.jpg', img_temp)
                    break
            if f is None: print(path + ' is None')
            feature.append(f)
        return feature
                    ### there need change!! PLY_01700 have no number but have others groundturth
                    if len(label) == 10:
                        label = label[:-1]
                    print(label)
                    img_name = label.split(';')[0]+'.jpg'
                    # load image
                    img_cv = cv2.imread(img_path + img_name)
                    if img_cv is None:
                        img_name = img_name[:-4]+'.JPG'
                        img_cv = cv2.imread(img_path + img_name)
                    # img_tf = open(img_path + img_name, 'rb').read()
                    # img_tf = tf.image.decode_jpeg(img_tf, channels=3)

                    # Get groundtruth of one image 
                    # bbs: '[x, y, w, h, num, leader]'
                    gt = utils.get_bbox(bbs_path, label, default_leader = leader)
                    if gt is None:
                        continue
                    bbs = gt[0]
                    bbs_other = gt[1]
                    countData[0] += len(bbs) # 
                    countData[1] += len(bbs_other)
                    # new_bbs_txt = open(bbs_path + img_name[:-4] + '_new.txt', 'w')


                    ###############################
                    # Detect all faces in a image #
                    ###############################

                    scores, boxes = im_detect(net, img_cv)
                    height, width, c = img_cv.shape
Exemple #11
0
def demo(net, image_path, label):
    """Detect object classes in an image using pre-computed object proposals."""
    #print image_path
    # Load the demo image
    #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(image_path)
    if im is None:
        image_path = image_path[:-4] + '.JPG'
        im = cv2.imread(image_path)
    leader = image_path.split('/')[1][:3]
    img_name = image_path.split('/')[2][:-4]
    bbs_path = 'txts/' + leader + '/'
    if im is None:
        print(image_path + ' is None')
        return
    # Detect all object classes and regress object bounds
    # if os.path.ifexists(save_root + image_name + '*.jpg'):
    # 	return
    bbs, bbs_other = utils.get_bbox(bbs_path, label, default_leader=leader)
    if (len(bbs) + len(bbs_other)) == 0:
        print(image_path + ' have no gt')
        print(label)
        return
    scores, boxes = im_detect(net, im)
    CONF_THRESH = 0.9
    NMS_THRESH = 0.5
    bbox = []
    for cls_ind, cls in enumerate(['face']):
        cls_ind += 1  # because we skipped background
        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]
        for i in inds:
            bbox.append(dets[i, :4])
            # cv2.rectangle(im, (dets[i,0], dets[i, 1]), (dets[i, 2], dets[i, 3]), (255, 0, 0))
            # cv2.putText(im, str(dets[i, -1]), (int((dets[i,0]+dets[i,2])/2), int((dets[i, 1]+ dets[i, 3])/2)), 0, 1, (255,0,0))

    # !!! Visualize GroundTurth, Compare and Compute IOU!!!
    # global iou,Sum
    # if os.path.exists('txts/' + leader + '/' + img_name[:-4] +'.txt'):
    #     bbs, bbs_other = utils.get_bbox('txts/' + leader + '/', label, default_leader = leader)
    #     for bb in bbs:
    #         bound2 = (float(bb[0]), float(bb[1]),
    #                   float(bb[0]) + float(bb[2]), float(bb[1])+float(bb[3]))
    #         iou_temp = 0
    #         for d in bbox:
    #             iou_temp = calculateIoU(d, bound2)
    #             if iou_temp >= 0.5:
    #                 break
    #         print(iou_temp)
    #         if iou_temp < 0.5:
    #             cv2.rectangle(im, (int(bound2[0]), int(bound2[1])), (int(bound2[2]), int(bound2[3])), (0, 255, 0))
    #             cv2.imwrite('RFCN_result/NoDetect/'+ leader + '/' + img_name, im)
    #             continue
    #         iou += iou_temp
    #         Sum += 1
    # print(iou / Sum)

    for i, d in enumerate(bbox):
        # judge whether leader by label
        iou = 0
        positive = 'OTHERS'
        for bb in bbs:
            bound2 = (float(bb[0]), float(bb[1]), float(bb[0]) + float(bb[2]),
                      float(bb[1]) + float(bb[3]))
            iou = calculateIoU(d, bound2)
            if iou >= 0.5:
                positive = bb[5]
                break

        preds = fa.get_landmarks(image_path, d)[-1]

        lefteye_x = int(
            less_average([
                preds[36, 0], preds[37, 0], preds[38, 0], preds[39, 0],
                preds[40, 0], preds[41, 0]
            ]))
        lefteye_y = int(
            less_average([
                preds[36, 1], preds[37, 1], preds[38, 1], preds[39, 1],
                preds[40, 1], preds[41, 1]
            ]))

        righteye_x = int(
            less_average([
                preds[42, 0], preds[43, 0], preds[44, 0], preds[45, 0],
                preds[46, 0], preds[47, 0]
            ]))
        righteye_y = int(
            less_average([
                preds[42, 1], preds[43, 1], preds[44, 1], preds[45, 1],
                preds[46, 1], preds[47, 1]
            ]))

        nose_x = int(
            less_average([
                preds[27, 0], preds[28, 0], preds[29, 0], preds[30, 0],
                preds[32, 0], preds[33, 0], preds[34, 0]
            ]))
        nose_y = int(
            less_average([
                preds[27, 1], preds[28, 1], preds[29, 1], preds[30, 1],
                preds[32, 1], preds[33, 1], preds[34, 1]
            ]))

        leftmouth_x = int(
            less_average(
                [preds[48, 0], preds[49, 0], preds[60, 0], preds[59, 0]]))
        leftmouth_y = int(
            less_average(
                [preds[48, 1], preds[49, 1], preds[60, 1], preds[59, 1]]))

        rightmouth_x = int(
            less_average(
                [preds[54, 0], preds[53, 0], preds[64, 0], preds[55, 0]]))
        rightmouth_y = int(
            less_average(
                [preds[54, 1], preds[53, 1], preds[64, 1], preds[55, 1]]))

        five = []
        five.append(lefteye_x)
        five.append(righteye_x)
        five.append(nose_x)
        five.append(leftmouth_x)
        five.append(rightmouth_x)
        five.append(lefteye_y)
        five.append(righteye_y)
        five.append(nose_y)
        five.append(leftmouth_y)
        five.append(rightmouth_y)
        # for j in range(0,5):
        #     cv2.circle(im, (five[j], five[j+5]), 1, (0, 0, 255), 2)
        # cv2.imwrite('result.jpg',im)
        # exit(0)
        five = np.array([five])
        #points = points[1,:].reshape((2,5)).T
        five = five.reshape((2, 5)).T
        # width = d[2]-d[0]
        # height = d[3]- d[1]
        # pad = [width/5, height/5]
        # d = (max(0, d[0]- pad[0]), max(0, d[1]-pad[1]), min(im.shape[1], d[2]+pad[0]), min(im.shape[1], d[3]+ pad[1]))
        nimg = face_preprocess.preprocess(im, d, five, image_size='112,112')

        img_temp_cls = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)
        # img_temp_cls = cv2.resize(img_temp_cls, (299, 299), cv2.INTER_LINEAR)
        img_temp_cls = np.array(img_temp_cls, dtype=np.uint8)
        img_temp_cls = img_temp_cls.astype('float32')
        img_temp_cls = np.multiply(img_temp_cls, 1.0 / 255.0)
        img_temp_cls = np.subtract(img_temp_cls, 0.5)
        img_temp_cls = np.multiply(img_temp_cls, 2.0)
        img_temp_cls = np.expand_dims(img_temp_cls, 0)
        predictions = sess.run(logits, feed_dict={
            tensor_input: img_temp_cls
        }).indices
        pose = predictions[0]

        if positive == 'OTHERS':
            leader = 'OTHERS'
        if positive != leader:
            leader = positive
        if pose == 0:
            save_path = save_root + leader + "/fro/" + img_name + '_{}.jpg'.format(
                i)
        elif pose == 1:
            save_path = save_root + leader + "/pro/" + img_name + '_{}.jpg'.format(
                i)
        last_index = save_path.rfind('/')
        if not os.path.exists(save_path[:last_index]):
            print('save path \"' + save_path + '\" error')
            exit(0)
        cv2.imwrite(save_path, nimg)
Exemple #12
0
def main(_):
    # tf.logging.set_verbosity(tf.logging.INFO)
    with tf.Graph().as_default():
        # tf_global_step = slim.get_or_create_global_step()

        ####################
        # Select the model #
        ####################
        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=(FLAGS.num_classes),
            is_training=False)

        #####################################
        # Select the preprocessing function #
        #####################################
        preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
        image_preprocessing_fn = preprocessing_factory.get_preprocessing(
            preprocessing_name,
            is_training=False)
        test_image_size = FLAGS.test_image_size or network_fn.default_image_size

        if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
            checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
        else:
            checkpoint_path = FLAGS.checkpoint_path

        tensor_input = tf.placeholder(tf.float32, [1, test_image_size, test_image_size, 3])
        logits, _ = network_fn(tensor_input)
        logits = tf.nn.top_k(logits, 1)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        # test_ids = [line.strip() for line in open(FLAGS.test_list)]
        # tot = len(test_ids)
        # results = list()

        ###############################
        # Deploy file path and params #
        ###############################
        cfg = Config()
        feat_box = dict()
        countDet = 0
        countData = [0, 0]
        no_label = 0
        right = [0, 0]
        FN = 0
        FP = 0
        precision = 0
        recall = 0
        num = 0

        with tf.Session(config=config) as sess:
            sess.run(tf.global_variables_initializer())
            saver = tf.train.Saver()
            saver.restore(sess, checkpoint_path)
            ###############################
            # Detect image Loop by leader #
            ###############################
            for leader in cfg.leaders_sml[:-1]:
            	# leader = 'DXP'
                # output.write(leader)
                # result2/DXP/DXP.txt
                label_file_path = '{0}/{1}/{1}.txt'.format(cfg.label_path_root, leader)
                # txts/DXP/
                bbs_path = '{}/{}/'.format(cfg.bbs_path_root, leader)
                # raw/DXP-craw/
                img_path = '{}/{}-craw/'.format(cfg.img_path_root, leader)
                # detect_result/DXP/
                save_root = '{}/{}/'.format(cfg.save_root, leader)

                right = 0
                wrong = 0
                for label in open(label_file_path):
                    if len(label) <= 10:
                        continue
                    # if int(label[5:9]) > 1583 and int(label[5:9]) <= 1760: continue
                    img_name = label.split(';')[0]+'.jpg'
                    # print('raw2/' + leader + '-base/' + img_name)
                    # if os.path.exists('raw2/' + leader + '-base/' + img_name) is False: continue
                    if os.path.exists(bbs_path + img_name[:-4] + '_new.txt'): continue
                    print(img_name)
                    # load image
                    img_cv = cv2.imread(img_path + img_name)
                    if img_cv is None:
                        img_name = img_name[:-4]+'.JPG'
                        img_cv = cv2.imread(img_path + img_name)
                    # img_tf = open(img_path + img_name, 'rb').read()
                    # img_tf = tf.image.decode_jpeg(img_tf, channels=3)
                    ###############################
                    # Detect all faces in a image #
                    ###############################
                    height, width, c = img_cv.shape

                    # print(num)
                    # Get groundtruth of one image 
                    # bbs: '[x, y, w, h, num, leader]'
                    bbs, bbs_other = utils.get_bbox(bbs_path, label, default_leader=leader)
                    countData[0] += len(bbs) # 
                    countData[1] += len(bbs_other)
                    new_bbs_txt = open(bbs_path + img_name[:-4] + '_new.txt', 'w')

                    for bb in (bbs + bbs_other):

                        d = (int(float(bb[0])), int(float(bb[1])),
                                      int(float(bb[0]) + float(bb[2])), int(float(bb[1])+float(bb[3])))

                        bound1 = (max(0, d[0]), max(0, d[1]),
                                  min(d[2], width), min(d[3], height))
                        img_temp = img_cv[bound1[1]:bound1[3], bound1[0]:bound1[2]]
                        
                        img_temp_cls = cv2.cvtColor(img_temp, cv2.COLOR_RGB2BGR)
                        start = time.time()
                        # print(bound1)
                        # run classification
                        img_temp_cls = cv2.resize(img_temp_cls, (299, 299), cv2.INTER_LINEAR)
                        img_temp_cls = np.array(img_temp_cls, dtype = np.uint8)
                        img_temp_cls = img_temp_cls.astype('float32')
                        img_temp_cls = np.multiply(img_temp_cls, 1.0/255.0)
                        img_temp_cls = np.subtract(img_temp_cls, 0.5)
                        img_temp_cls = np.multiply(img_temp_cls, 2.0)
                        img_temp_cls = np.expand_dims(img_temp_cls, 0)
                            # images = list()
                            # # crop from img_tf, create a new image img_tf2, then process , convert to array ,and run 
                            # img_tf2 = tf.image.crop_to_bounding_box(img_tf, bound1[1], bound1[0], bound1[3]-bound1[1], bound1[2]-bound1[0])
                            # # plt.imshow(img_tf2.eval())
                            # # plt.show()
                            # processed_image = image_preprocessing_fn(img_tf2, test_image_size, test_image_size)
                            # processed_image = sess.run(processed_image)
                            # images.append(processed_image)
                            # images = np.array(images)
                        predictions = sess.run(logits, feed_dict = {tensor_input : img_temp_cls}).indices
                        new_bbs_txt.write('{} {} {} {} {} {}\n'.format(bb[4], bb[0], bb[1], bb[2], bb[3], predictions[0]))

                        if predictions[0] == 0:
                            cv2.imwrite(save_root + 'fro/' + img_name[:-4] + '_' + bb[4] + '.jpg', img_temp)
                        else:
                            cv2.imwrite(save_root + 'pro/' + img_name[:-4] + '_' + bb[4] + '.jpg', img_temp)

                        end = time.time()
                        print( '{:.2} seconds per bnx\n'.format(end - start))
                        # 1 : profile 0: frontal
                    new_bbs_txt.close()
                    gc.collect()
def demo(net, leader, label):
    """Detect object classes in an image using pre-computed object proposals."""
    #print image_path
    # Load the demo image
    #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    img_name = label.split(';')[0]
    image_path = 'raw/{}-craw/{}.jpg'.format(leader, img_name)
    ### txt path

    gt = utils.get_bbox('txts/' + leader + '/', label, default_leader=leader)
    if gt is None:
        return
    # x,y,w,h, id, person, pose
    bbs = gt[0]
    for bb in bbs:
        GT[bb[5]] += 1
    bbs_other = gt[1]
    GT['OTHERS'] += len(bbs_other)
    print(label)
    new_txt_file = open(save_path + leader + '/' + img_name + '.txt', 'w')
    im = cv2.imread(image_path)
    if im is None:
        image_path = image_path[:-4] + '.JPG'
        im = cv2.imread(image_path)

    if im is None:
        print(image_path + ' is None')
        return

    scores, boxes = im_detect(net, im)

    # Visualize detections for each class
    CONF_THRESH = 0.9
    NMS_THRESH = 0.5
    bbox = []
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        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]
        for i in inds:
            bbox.append(dets[i, :4])
            # cv2.rectangle(im, (dets[i,0], dets[i, 1]), (dets[i, 2], dets[i, 3]), (255, 0, 0))
            # cv2.putText(im, str(dets[i, -1]), (int((dets[i,0]+dets[i,2])/2), int((dets[i, 1]+ dets[i, 3])/2)), 0, 1, (255,0,0))

    for i, d in enumerate(bbox):
        pose = -1
        # bbs: x,y,w,h, id, person, pose
        for j in range(len(bbs)):
            bb = bbs[j]
            bound2 = (float(bb[0]), float(bb[1]), float(bb[0]) + float(bb[2]),
                      float(bb[1]) + float(bb[3]))
            if calculateIoU(d, bound2) >= 0.5:
                RT[bb[5]] += 1
                f_img_path = fp_path + bb[
                    5] + '/fro/' + img_name + '_{}.jpg'.format(i)
                if os.path.exists(f_img_path):
                    pose = 0
                p_img_path = fp_path + bb[
                    5] + '/pro/' + img_name + '_{}.jpg'.format(i)
                if os.path.exists(p_img_path):
                    pose = 1
                q_img_path = fp_path + bb[
                    5] + '/quarter/' + img_name + '_{}.jpg'.format(i)
                if os.path.exists(q_img_path):
                    pose = 2
                bbs[j][6] = pose
                break

        for j in range(len(bbs_other)):
            bb = bbs_other[j]
            bound2 = (float(bb[0]), float(bb[1]), float(bb[0]) + float(bb[2]),
                      float(bb[1]) + float(bb[3]))
            if calculateIoU(d, bound2) >= 0.5:
                RT['OTHERS'] += 1
                f_img_path = fp_path + 'OTHERS/fro/' + img_name + '_{}.jpg'.format(
                    i)
                if os.path.exists(f_img_path):
                    pose = 0
                p_img_path = fp_path + 'OTHERS/pro/' + img_name + '_{}.jpg'.format(
                    i)
                if os.path.exists(p_img_path):
                    pose = 1
                q_img_path = fp_path + bb[
                    5] + '/quarter/' + img_name + '_{}.jpg'.format(i)
                if os.path.exists(q_img_path):
                    pose = 2
                bbs_other[j][6] = pose
                break

    for gt in (bbs + bbs_other):
        line = '{} {} {} {} {} {} {}\n'.format(gt[4], gt[0], gt[1], gt[2],
                                               gt[3], gt[5], gt[6])
        new_txt_file.write(line)
Exemple #14
0
def main(_):
    # tf.logging.set_verbosity(tf.logging.INFO)
    with tf.Graph().as_default():
        # tf_global_step = slim.get_or_create_global_step()

        ####################
        # Select the model #
        ####################
        network_fn = nets_factory.get_network_fn(
            FLAGS.model_name,
            num_classes=(FLAGS.num_classes),
            is_training=False)

        #####################################
        # Select the preprocessing function #
        #####################################
        preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name
        image_preprocessing_fn = preprocessing_factory.get_preprocessing(
            preprocessing_name,
            is_training=False)
        test_image_size = FLAGS.test_image_size or network_fn.default_image_size

        if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
            checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path)
        else:
            checkpoint_path = FLAGS.checkpoint_path

        tensor_input = tf.placeholder(tf.float32, [1, test_image_size, test_image_size, 3])
        logits, _ = network_fn(tensor_input)
        logits = tf.nn.top_k(logits, 1)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        # test_ids = [line.strip() for line in open(FLAGS.test_list)]
        # tot = len(test_ids)
        # results = list()

        #################################
        # Load face detector model RFCN #
        #################################
        prototxt = '/home/disk3/py/py-R-FCN/models/pascal_voc/ResNet-101/rfcn_end2end/test_agonistic_face.prototxt'
        #caffemodel = os.path.join( args.demo_net )
        caffemodel = '/home/disk3/py/py-R-FCN/data/rfcn_models/resnet101_rfcn_ohem_iter_40000.caffemodel'
        detect_cfg.TEST.HAS_RPN = True
        caffe.set_mode_gpu()
        caffe.set_device(0)
        detect_cfg.GPU_ID = 0
        net = caffe.Net(prototxt, caffemodel, caffe.TEST)
        print ('\n\nLoaded network {:s}'.format(caffemodel))

        ##################################
        # Load landmark detect and align #
        ##################################
        fa = face_align.FaceAlignment(face_align.LandmarksType._2D, enable_cuda=True)

        ####################
        # Load InsightFace #
        ####################
        parser = argparse.ArgumentParser(description='face model test')
        parser.add_argument('--image-size', default='112,112', help='')
        parser.add_argument('--model', default='./model-r50-am-lfw/model,0', help='path to load model.')
        parser.add_argument('--gpu', default=0, type=int, help='gpu id')
        parser.add_argument('--det', default=2, type=int, help='mtcnn option, 2 means using R+O, else using O')
        parser.add_argument('--flip', default=0, type=int, help='whether do lr flip aug')
        args = parser.parse_args()
        model = face_embedding.FaceModel(args)

        ###############################
        # Deploy file path and params #
        ###############################
        cfg = Config()
        # feat_box = dict()
        # output = open('result_txt/result_align_mul_2.txt', 'w')
        # output.write('total leaders: {}'.format(len(cfg.leaders_sml[:-1])))
        # countDet = 0
        # countData = [0, 0]
        # no_label = 0
        # right = dict()
        # FN = dict()
        # FP = dict() 
        # precision = dict()
        # recall = dict()
        # score = dict()
        # result = dict()
        # num = 0

        # true_profile = dict() 
        # true_frontal = dict()
        # false_profile = dict()
        # false_frontal = dict()

        for leader in cfg.leaders_sml[:-1]:
            save_root = '{}/{}/'.format(cfg.save_root, leader)
            f1 = cfg.BaseFeat(leader)
            feat_box[leader] = f1

        for threshold in range(60, 130, 5):
            threshold = threshold * 0.01
            right[threshold] = [0, 0]
            FN[threshold] = 0
            FP[threshold] = 0
            score[threshold] = 0
            precision[threshold] = 0
            recall[threshold] = 0
            true_frontal[threshold] = 0
            false_frontal[threshold] = 0
            true_profile[threshold] = 0
            false_profile[threshold] = 0
            result[threshold] = 'OTHERS'

        with tf.Session(config=config) as sess:
            sess.run(tf.global_variables_initializer())
            saver = tf.train.Saver()
            saver.restore(sess, checkpoint_path)

            ###############################
            # Detect image Loop by leader #
            ###############################
            for leader in ['DXP']:
                output.write(leader)
                # result2/DXP/DXP.txt
                label_file_path = '{0}/{1}/{1}.txt'.format(cfg.label_path_root, leader)
                # txts/DXP/
                bbs_path = '{}/{}/'.format(cfg.bbs_path_root, leader)
                # raw/DXP-craw/
                img_path = '{}/{}-craw/'.format(cfg.img_path_root, leader)
                # detect_result/DXP/
                save_root = '{}/{}/'.format(cfg.save_root, leader)
                for label in open(label_file_path):
                    if len(label) <= 10:
                        continue
                    print(label)
                    img_name = label.split(';')[0]+'.jpg'
                    # load image
                    img_cv = cv2.imread(img_path + img_name)
                    if img_cv is None:
                        img_name = img_name[:-4]+'.JPG'
                        img_cv = cv2.imread(img_path + img_name)
                    img_tf = open(img_path + img_name, 'rb').read()
                    img_tf = tf.image.decode_jpeg(img_tf, channels=3)
                    ###############################
                    # Detect all faces in a image #
                    ###############################
                    scores, boxes = im_detect(net, img_cv)
                    height, width, c = img_cv.shape
                    thresh = 0.8
                    bbox = []
                    for cls_ind, cls in enumerate(['face']):
                        cls_ind += 1 # because we skipped background
                        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, 0.3)
                        dets = dets[keep, :]
                        inds = np.where(dets[:, -1] >= thresh)[0]
                        for i in inds:
                            bbox.append(dets[i, :4])
                    num += len(bbox)

                    # Get groundtruth of one image 
                    # bbs: '[x, y, w, h, num, leader]'
                    bbs, bbs_other = utils.get_bbox(bbs_path, label, default_leader = leader)
                    countData[0] += len(bbs) # 
                    countData[1] += len(bbs_other)
                    # new_bbs_txt = open(bbs_path + img_name[:-4] + '_new.txt', 'w')

                    for d in bbox:
                        bound1 = (float(d[0]), float(d[1]), float(d[0])+float(d[2]), float(d[1])+ float(d[3]))
                        ## face landmark detection
                        preds = fa.get_landmarks(img_path + img_name, bound1)[-1]
                        ## face embedding 512d
                        f = model.get_feature_by_landmark(img_cv, bound1, preds)

                        # d = [int(round(d[0])), int(round(d[1])), int(round(d[2])), int(round(d[3]))]
                        bound1 = (max(0, bound1[0]), max(0, bound1[1]),
                                  min(bound1[2], width), min(bound1[3], height))

                        countDet += 1
                        for s in score:
                            score[s] = 999
                            result[s] = 'OTHERS'
                        for feat_leader in feat_box:
                            for f2 in feat_box[feat_leader]:
                                dist = np.sum(np.square(f - f2))
                                for threshold in range(60, 130, 5):
                                    threshold = threshold * 0.01
                                    if dist < threshold and dist < score[threshold]:
                                        score[threshold] = dist
                                        result[threshold] = feat_leader
                        iou = 0
                        positive = 'OTHERS'
                        pose = -1
                        for bb in (bbs + bbs_other):
                            bound2 = (float(bb[0]), float(bb[1]),
                                      float(bb[0]) + float(bb[2]), float(bb[1])+float(bb[3]))
                            iou = calculateIoU(bound1, bound2)
                            if iou >= 0.5:
                                positive = bb[5]
                                pose = bb[6]
                                break
                        if iou < 0.5:
                            print("the face no label")
                            # cv2.putText(img_cv, str(i) + 'no label', (bound1[0], bound1[1]), 0, 1, (255, 0, 0))
                            # cv2.imwrite(save_root +'no_label/' +img_name + '.jpg', img_cv)
                            no_label += 1
                            continue
                        for threshold in range(60, 130, 5):
                        threshold = threshold * 0.01
                        if result[threshold] == positive: 
                            if result[threshold] == 'OTHERS':
                                right[threshold][1] += 1
                                if pose == 0:
                                    true_frontal[threshold] += 1
                                else pose == 1:
                                    true_profile[threshold] += 1
                            else:
                                right[threshold][0] += 1
                                if pose == 0:
                                    true_frontal[threshold] += 1
                                else pose == 1:
                                    true_profile[threshold] += 1

                                
                        elif positive != 'OTHERS': 
                            # cv2.putText(img_cv, str(i) + result[threshold] + ' ' + positive, (bound1[0], bound1[1]), 0, 0.5, (0, 255, 0))
                            # cv2.imwrite(save_root + 'falsePositive/' + img_name + '.jpg', img_cv)
                            FP[threshold] += 1
                            if pose == 0:
                                false_frontal[threshold] += 1
                            else pose == 1:
                                false_profile[threshold] += 1
                        else: # result != 'OTHERS': 
                            FN[threshold] += 1
                            if pose == 0:
                                false_frontal[threshold] += 1
                            else pose == 1:
                                false_profile[threshold] += 1
Exemple #15
0
def demo(net, leader, label):
    """Detect object classes in an image using pre-computed object proposals."""
    #print image_path
    # Load the demo image
    #im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    img_name = label.split(';')[0]
    image_path = 'raw/{}-craw/{}.jpg'.format(leader, img_name)
    ### txt path

    gt = utils.get_bbox('txts/' + leader + '/', label, default_leader = leader)
    if gt is None:
    	return
    print( label )
    new_txt_file = open(save_path + leader + '/' + img_name + '.txt', 'w') 
    im = cv2.imread(image_path)
    if im is None:
        image_path = image_path[:-4] + '.JPG'
        im = cv2.imread(image_path)

    if im is None:
    	print(image_path + ' is None')
    	return

    scores, boxes = im_detect(net, im)

    # Visualize detections for each class
    CONF_THRESH = 0.9
    NMS_THRESH = 0.5
    bbox = []
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        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]
        for i in inds:
            bbox.append(dets[i,:4])
            # cv2.rectangle(im, (dets[i,0], dets[i, 1]), (dets[i, 2], dets[i, 3]), (255, 0, 0))
            # cv2.putText(im, str(dets[i, -1]), (int((dets[i,0]+dets[i,2])/2), int((dets[i, 1]+ dets[i, 3])/2)), 0, 1, (255,0,0))

    # for i, d in enumerate(bbox):
    #     iou_temp = 0
    #     for bb in (bbs + bbs_other):
    #         bound2 = (float(bb[0]), float(bb[1]),
    #                   float(bb[0]) + float(bb[2]), float(bb[1])+float(bb[3]))
    #         iou_temp = calculateIoU(d, bound2)
    #         if iou_temp >= 0.5:
    #             break
    #     if iou_temp < 0.5:
    #         continue
    #     iou += iou_temp
    #     Sum += 1

    # Visualize GroundTurth
    # create a fake label
    if os.path.exists('txts/' + leader + '/' + img_name[:-4] +'.txt'):
        bbs, bbs_other = utils.get_bbox('txts/' + leader + '/', label, default_leader = leader)
        for bb in bbs:
            bound2 = (float(bb[0]), float(bb[1]),
                      float(bb[0]) + float(bb[2]), float(bb[1])+float(bb[3]))
            iou_temp = 0
            for d in bbox:
                iou_temp = calculateIoU(d, bound2)
                if iou_temp >= 0.5:
                    break
            print(iou_temp)
            if iou_temp < 0.5:
                cv2.rectangle(im, (int(bound2[0]), int(bound2[1])), (int(bound2[2]), int(bound2[3])), (0, 255, 0))
                cv2.imwrite('RFCN_result/NoDetect/'+ leader + '/' + img_name, im)
                continue
            iou += iou_temp
            Sum += 1
    print(iou / Sum)