コード例 #1
0
ファイル: gen_megaface.py プロジェクト: LHQ0308/insightface
def get_feature(image_path, bbox, landmark, nets, image_shape, use_align, aligned, use_mean):
  img = face_preprocess.read_image(image_path, mode='rgb')
  #print(img.shape)
  if img is None:
    print('parse image',image_path,'error')
    return None
  if not aligned:
    _landmark = landmark
    if not use_align:
      _landmark = None
    #cv2.imwrite("./align/origin_%s"%image_path.split('/')[-1], img)
    img = face_preprocess.preprocess(img, bbox=bbox, landmark=_landmark, image_size='%d,%d'%(image_shape[1], image_shape[2]))
  else:
    assert img.shape==(image_shape[1],image_shape[2],image_shape[0])
    #print('already aligned', image_path, img.shape)
    #img = cv2.resize(img, (image_shape[2], image_shape[1]))
  #cv2.imwrite("./align/%s"%image_path.split('/')[-1], img)
  if use_mean>0:
    v_mean = np.array([127.5,127.5,127.5], dtype=np.float32).reshape( (1,1,3) )
    img = img.astype(np.float32) - v_mean
    img *= 0.0078125
  img = np.transpose( img, (2,0,1) )
  F = None
  for net in nets:
    embedding = None
    #ppatch = net.patch
    for flipid in [0,1]:
      _img = np.copy(img)
      if flipid==1:
        do_flip(_img)
      #nimg = np.zeros(_img.shape, dtype=np.float32)
      #nimg[:,ppatch[1]:ppatch[3],ppatch[0]:ppatch[2]] = _img[:, ppatch[1]:ppatch[3], ppatch[0]:ppatch[2]]
      #_img = nimg
      input_blob = np.expand_dims(_img, axis=0)
      data = mx.nd.array(input_blob)
      db = mx.io.DataBatch(data=(data,))
      net.model.forward(db, is_train=False)
      _embedding = net.model.get_outputs()[0].asnumpy().flatten()
      #print(_embedding.shape)
      if embedding is None:
        embedding = _embedding
      else:
        embedding += _embedding
    _norm=np.linalg.norm(embedding)
    embedding /= _norm
    if F is None:
      F = embedding
    else:
      #F += embedding
      F = np.concatenate((F,embedding), axis=0)
  _norm=np.linalg.norm(F)
  F /= _norm
  #print(F.shape)
  return F
コード例 #2
0
ファイル: face_model.py プロジェクト: LHQ0308/insightface
 def get_all_faces(self, img):
   str_image_size = "%d,%d"%(self.image_size[0], self.image_size[1])
   bounding_boxes, points = detect_face.detect_face(img, self.det_minsize, self.pnet, self.rnet, self.onet, self.det_threshold, self.det_factor)
   ret = []
   for i in xrange(bounding_boxes.shape[0]):
     bbox = bounding_boxes[i,0:4]
     landmark = points[:, i].reshape((2,5)).T
     aligned = face_preprocess.preprocess(img, bbox=bbox, landmark = landmark, image_size=str_image_size)
     aligned = np.transpose(aligned, (2,0,1))
     ret.append(aligned)
   return ret
コード例 #3
0
ファイル: face_model.py プロジェクト: niannanxue/insightface
 def get_input(self, face_img):
   ret = self.detector.detect_face(face_img, det_type = self.args.det)
   if ret is None:
     return None
   bbox, points = ret
   if bbox.shape[0]==0:
     return None
   bbox = bbox[0,0:4]
   points = points[0,:].reshape((2,5)).T
   #print(bbox)
   #print(points)
   nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112')
   nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
   aligned = np.transpose(nimg, (2,0,1))
   return aligned
コード例 #4
0
 def get_input(self, face_img):
   ret = self.detector.detect_face(face_img, det_type = self.args.det)
   if ret is None:
     return None
   bbox, points = ret
   if bbox.shape[0]==0:
     return None
   bbox = bbox[0,0:4]
   points = points[0,:].reshape((2,5)).T
   #print(bbox)
   #print(points)
   nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112')
   nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
   aligned = np.transpose(nimg, (2,0,1))
   input_blob = np.expand_dims(aligned, axis=0)
   data = mx.nd.array(input_blob)
   db = mx.io.DataBatch(data=(data,))
   return db
コード例 #5
0
def image_encode(args, i, item, q_out):
    if item.flag == 0:
        fullpath = item.image_path
        if item.aligned:
            with open(fullpath, 'rb') as fin:
                img = fin.read()
            q_out.put((i, img, fullpath))
        else:
            img = cv2.imread(fullpath)
            assert item.landmark is not None
            img = face_preprocess.preprocess(img,
                                             bbox=item.bbox,
                                             landmark=item.landmark,
                                             image_size='%d,%d' %
                                             (args.image_h, args.image_w))
            q_out.put((i, img, fullpath))
    else:
        pass
コード例 #6
0
    def detect_face_and_get_embedding(self, img):
        thresh = 0.8
        flip = False
        scales = self.get_scales(img)
        bboxes, rs = self.face_detector.detect(img, thresh, scales=scales, do_flip=flip)
        # print('bbox:', bboxes)
        if len(bboxes) <= 0:
            return None, None
        # print('landmark:', rs)
        if rs is not None:
            points = rs.astype(np.int32)
            # point = points[0, :].reshape((2, 5)).T
            nimg = preprocess(img, bboxes[0], points[0], image_size='112,112')
#             nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)      
            e = np.transpose(nimg, (2, 0, 1))
            embeddings = self.face_recognition.get_feature(e)
            return embeddings, nimg
        return None, None
コード例 #7
0
 def get_input_train(self, face_img):
     ret = self.detector.detect_face(face_img, det_type=self.loder[0])
     if ret is None:
         return None
     bbox, points = ret
     if bbox.shape[0] == 0:
         return None
     bbox = bbox[0, 0:4]
     points = points[0, :].reshape((2, 5)).T
     #print(bbox)
     #print(points)
     nimg = face_preprocess.preprocess(face_img,
                                       bbox,
                                       points,
                                       image_size='112,112')
     nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
     aligned = np.transpose(nimg, (2, 0, 1))
     return aligned
コード例 #8
0
 def get_img(self, face_img):
     #face_img is bgr image
     dets, points_all = self.get_face_dets_points(face_img)
     if not dets:
         return None, None
     # print dets
     #print points_all
     aligned_imgs = []
     for i, bbox in enumerate(dets):
         points = points_all[i]
         nimg = face_preprocess.preprocess(face_img,
                                           bbox,
                                           points,
                                           image_size='112,112')
         #nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
         #nimg = np.transpose(nimg, (2,0,1))
         aligned_imgs.append(nimg)
     return aligned_imgs, dets
コード例 #9
0
def image_encode(args, i, item, q_out):
    oitem = [item.id]
    #print('flag', item.flag)
    if item.flag == 0:
        fullpath = item.image_path
        header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
        #print('write', item.flag, item.id, item.label)
        if item.aligned:
            # with open(fullpath, 'rb') as fin:
            #     img = fin.read()
            # s = mx.recordio.pack(header, img)
            img = cv2.imread(fullpath, args.color)
            det = item.bbox
            margin = 44
            bb = np.zeros(4, dtype=np.int32)
            bb[0] = np.maximum(det[0] - margin / 2, 0)
            bb[1] = np.maximum(det[1] - margin / 2, 0)
            bb[2] = np.minimum(det[2] + margin / 2, img.shape[1])
            bb[3] = np.minimum(det[3] + margin / 2, img.shape[0])
            img = img[bb[1]:bb[3], bb[0]:bb[2], :]
            img = cv2.resize(img, (224, 224))
            s = mx.recordio.pack_img(header,
                                     img,
                                     quality=args.quality,
                                     img_fmt=args.encoding)
            q_out.put((i, s, oitem))
        else:
            img = cv2.imread(fullpath, args.color)
            assert item.landmark is not None
            img = face_preprocess.preprocess(img,
                                             bbox=item.bbox,
                                             landmark=item.landmark,
                                             image_size='%d,%d' %
                                             (args.image_h, args.image_w))
            s = mx.recordio.pack_img(header,
                                     img,
                                     quality=args.quality,
                                     img_fmt=args.encoding)
            q_out.put((i, s, oitem))
    else:
        header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
        #print('write', item.flag, item.id, item.label)
        s = mx.recordio.pack(header, b'')
        q_out.put((i, s, oitem))
コード例 #10
0
    def get_feature(self, face_img, tp, fname):
        #face_img is bgr image
        ret = self.detector.detect_face(face_img)
        # ret = self.detector.detect_face_limited(face_img, det_type = self.args.det)
        if ret is None:
            return None
        bbox, points = ret
        if bbox.shape[0] == 0:
            return None
        bbox = bbox[0, 0:4]
        points = points[0, :].reshape((2, 5)).T
        #print(bbox)
        #print(points)
        #face_img BGR
        nimg = face_preprocess.preprocess(face_img,
                                          bbox,
                                          points,
                                          image_size='112,112')

        # save image
        # fPath = os.path.join(IMG_OUTPUT, tp, fname + ".jpg")
        # cv2.imwrite(fPath, nimg)

        nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
        aligned = np.transpose(nimg, (2, 0, 1))
        #print(nimg.shape)
        embedding = None
        for flipid in [0, 1]:
            if flipid == 1:
                if self.args.flip == 0:
                    break
                do_flip(aligned)
            input_blob = np.expand_dims(aligned, axis=0)
            data = mx.nd.array(input_blob)
            db = mx.io.DataBatch(data=(data, ))
            self.model.forward(db, is_train=False)
            _embedding = self.model.get_outputs()[0].asnumpy()
            #print(_embedding.shape)
            if embedding is None:
                embedding = _embedding
            else:
                embedding += _embedding
        embedding = sklearn.preprocessing.normalize(embedding).flatten()
        return embedding
コード例 #11
0
    def get_input_multi(self, face_img):
        img = face_img

        im_shape = img.shape
        scales = [1024, 1980]
        target_size = scales[0]
        max_size = scales[1]
        im_size_min = np.min(im_shape[0:2])
        im_size_max = np.max(im_shape[0:2])
        #im_scale = 1.0
        #if im_size_min>target_size or im_size_max>max_size:
        im_scale = float(target_size) / float(im_size_min)
        # prevent bigger axis from being more than max_size:
        if np.round(im_scale * im_size_max) > max_size:
            im_scale = float(max_size) / float(im_size_max)

        print('im_scale', im_scale)

        scales = [im_scale]
        flip = False
        ret = self.detector.detect(img, thresh, scales=scales, do_flip=False)
        if ret is None:
            return None
        bbox, points = ret
        if bbox.shape[0] == 0:
            return None
        bbox_list = []
        aligned_list = []

        for i in range(bbox.shape[0]):
            bbox_ = bbox[i, 0:4]
            points_ = points[i, :, :]
            #print(bbox)
            #print(points)
            nimg = face_preprocess.preprocess(face_img,
                                              bbox_,
                                              points_,
                                              image_size='112,112')
            nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
            aligned = np.transpose(nimg, (2, 0, 1))
            bbox_list.append([int(h) for h in bbox_])
            aligned_list.append(aligned)
        return aligned_list, bbox_list
コード例 #12
0
def getGalleryFeature(detector,fr_model):
    gallery_paths = glob.glob('./gallery/*')
    names=[]
    imgs_feats=[]
    for p in gallery_paths:
        if os.path.isfile(p):
            names.append(p.split('/')[len(p.split('/'))-1][:-4])
            img=cv2.imread(p)
            results=detector.detect(img)
            landmark=results[0][1]
            img=preprocess(img,landmark)

            img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)

            img = transform(Image.fromarray(img)).unsqueeze(0).cuda()

            feat=fr_model(img)
            imgs_feats.append(feat)
    return names,imgs_feats
コード例 #13
0
    def get_input_loc(self, face_img):
        ret = self.detector.detect_face(face_img, det_type=self.det)
        if ret is None:
            return None, None, None
        bbox, points = ret
        if bbox.shape[0] == 0:
            return None, None, None
        bbox = bbox[0, 0:4]
        points = points[0, :].reshape((2, 5)).T
        nimg, bb = face_preprocess.preprocess(face_img,
                                              bbox,
                                              points,
                                              image_size='112,112')
        loc = face_img.copy()
        cv2.rectangle(loc, (bb[0], bb[1]), (bb[2], bb[3]), (0, 0, 255), 2)

        aligned = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
        aligned = np.transpose(aligned, (2, 0, 1))
        return aligned, nimg, loc
コード例 #14
0
    def despoof(self, img_bgr):
        img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
        bounding_boxes, points = detect_face.detect_face(
            img, self.MTCNN_params['minsize'], self.pnet, self.rnet, self.onet, self.MTCNN_params['threshold'], self.MTCNN_params['factor'])
        nrof_faces = bounding_boxes.shape[0]
        face_detected_flag = False
        if nrof_faces >= 1:
            for index, bbox in enumerate(bounding_boxes):
                score = bbox[-1]
                bbox = bbox[0: 4]
                landmark = points[:, index].reshape((2, 5)).T
            if score > 0.8:
                face_detected_flag = True
                # print(bbox[3] - bbox[1], bbox[2] - bbox[0])
                warped = face_preprocess.preprocess(
                    img, bbox=bbox, landmark=landmark, image_size=(112, 112))
                warped__ = preprocess_input(warped)
                to_test = np.expand_dims(warped__, 0)
                prediction = self.antispoofing_model.predict(to_test)
                # 0.001
                if prediction <= self.threshold:
                    # draw_box(img_bgr, [bbox[0], bbox[1], bbox[2],
                    #                    bbox[3]], color=[127, 255, 0])
                    # video_writer.write(img_bgr.astype(np.uint8))
                    self.pop_queue(1)
                else:
                    # draw_box(img_bgr, [bbox[0], bbox[1], bbox[2],
                    #                    bbox[3]], color=[0, 0, 255])
                    self.pop_queue(0)
            else:
                self.pop_queue(0)
        else:
            self.pop_queue(0)

        if face_detected_flag:
            if self.get_result():
                draw_box(img_bgr, [bbox[0], bbox[1], bbox[2],
                                   bbox[3]], color=[127, 255, 0])
            else:
                draw_box(img_bgr, [bbox[0], bbox[1], bbox[2],
                                   bbox[3]], color=[0, 0, 255])
        return img_bgr
コード例 #15
0
 def get_input(self, face_img):
   ret = self.detector.detect_face(face_img, det_type = self.args.det)
   if ret is None:
     return None,None
   bboxs, points = ret
   if bboxs.shape[0]==0:
     return None,None
   
   total = bboxs.shape[0]
   aligned_imgs = []
   for i in range(total):
       bbox = bboxs[i,0:4]
       point = points[i,:].reshape((2,5)).T
       #print(bbox)
       #print(points)
       nimg = face_preprocess.preprocess(face_img, bbox, point, image_size='112,112')
       nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
       aligned = np.transpose(nimg, (2,0,1))
       aligned_imgs.append(aligned)
   return aligned_imgs,bboxs
コード例 #16
0
 def get_input(self, face_img):
     ret = self.detector.detect_face(face_img, det_type=self.args.det)
     if ret is None:
         return np.transpose(
             cv2.cvtColor(cv2.resize(face_img, (112, 112)),
                          cv2.COLOR_BGR2RGB), (2, 0, 1))
     bbox, points = ret
     if bbox.shape[0] == 0:
         return np.transpose(
             cv2.cvtColor(cv2.resize(face_img, (112, 112)),
                          cv2.COLOR_BGR2RGB), (2, 0, 1))
     bbox = bbox[0, 0:4]
     points = points[0, :].reshape((2, 5)).T
     nimg = face_preprocess.preprocess(face_img,
                                       bbox,
                                       points,
                                       image_size='112,112')
     nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
     aligned = np.transpose(nimg, (2, 0, 1))
     return aligned
コード例 #17
0
    def get_input(self, face_img):
        ret = self.detector.detect_face(face_img)
        if ret is None:
            print('This image no faces..')
            return None
        bbox, points = ret
        #print(bbox)
        if bbox.shape[0] == 0:
            print('This image no faces..')
            return None
        bbox = bbox[0, 0:4]
        points = points[0, :].reshape((2, 5)).T  # matrix tranpose

        nimg = face_preprocess.preprocess(face_img,
                                          bbox,
                                          points,
                                          image_size='112,112')
        nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
        aligned = np.transpose(nimg, (2, 0, 1))
        return aligned
コード例 #18
0
ファイル: align_mt.py プロジェクト: ivychill/ensemble
 def get_input(self, face_img, output_filename_n):
     ret = self.detector.detect_face(face_img, det_type=self.args.det)
     if ret is not None:
         bbox, points = ret
         if bbox.shape[0] != 0:
             bbox = bbox[0, 0:4]
             points = points[0, :].reshape((2, 5)).T
             # print('bbox',bbox)
             # print('points',points)
             nimg = face_preprocess.preprocess(face_img,
                                               bbox,
                                               points,
                                               image_size=self.image_size)
             nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
             misc.imsave(output_filename_n, nimg)
             return 1
         else:
             return 0
     else:
         return 0
コード例 #19
0
    def get_all_input(self, face_img, save_img=False):
        face_num = self.max_face_number
        ret = self.detector.detect_face(face_img, det_type=self.args.det)
        if ret is None:
            return []

        bbox, points = ret

        sorted_index = bbox[:, 0].argsort()
        bbox = bbox[sorted_index]
        points = points[sorted_index]

        # print(bbox)
        # print(points)

        if bbox.shape[0] == 0:
            return None

        aligned = []
        for index in range(0, len(bbox[:face_num])):
            item_bbox = bbox[index, 0:4]
            item_points = points[index, :].reshape((2, 5)).T
            # print(bbox)
            # print(points)
            nimg = face_preprocess.preprocess(face_img,
                                              item_bbox,
                                              item_points,
                                              image_size='112,112')

            if save_img:
                cv2.imwrite(
                    './Temp/{}-{}.jpg'.format(time.time(), self.face_counter),
                    nimg)
                # print(self.face_counter)
                self.face_counter += 1

            nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
            aligned.append(np.transpose(nimg, (2, 0, 1)))

        # print(aligned)
        return zip(aligned, bbox)
コード例 #20
0
def image_encode(args, i, item, q_out):
    oitem = [item.id]
    #print('flag', item.flag)
    if item.flag == 0:
        fullpath = item.image_path
        header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
        #print('write', item.flag, item.id, item.label)
        if item.aligned:
            if args.size == 0:
                with open(fullpath, 'rb') as fin:
                    img = fin.read()
                s = mx.recordio.pack(header, img)
                q_out.put((i, s, oitem))
            else:
                img = cv2.imread(fullpath, args.color)
                h, w = img.shape[:2]
                h_off, w_off = (h - args.size) / 2, (w - args.size) / 2
                img = img[h_off:h_off + args.size, w_off:w_off + args.size, :]
                s = mx.recordio.pack_img(header,
                                         img,
                                         quality=args.quality,
                                         img_fmt=args.encoding)
                q_out.put((i, s, oitem))
        else:
            img = cv2.imread(fullpath, args.color)
            assert item.landmark is not None
            img = face_preprocess.preprocess(img,
                                             bbox=item.bbox,
                                             landmark=item.landmark,
                                             image_size='%d,%d' %
                                             (args.image_h, args.image_w))
            s = mx.recordio.pack_img(header,
                                     img,
                                     quality=args.quality,
                                     img_fmt=args.encoding)
            q_out.put((i, s, oitem))
    else:
        header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
        #print('write', item.flag, item.id, item.label)
        s = mx.recordio.pack(header, '')
        q_out.put((i, s, oitem))
コード例 #21
0
 def detect_face_and_get_embedding_test_2(self, img):
     thresh = 0.8
     flip = False
     scales = self.get_scales(img)
     bboxes, rs = self.face_detector.detect(img, thresh, scales=scales, do_flip=flip)
     if len(bboxes) <= 0:
         return None, None
     embeddings = []
     bbox_list = []
     if rs is not None:
         points = rs.astype(np.int32)
         for i, bbox in enumerate(bboxes):
             point = points[i, :].reshape((2, 5)).T
             nimg = preprocess(img, bbox, point, image_size='112,112')
             nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
             x = np.transpose(nimg, (2, 0, 1))
             embedding = self.face_recognition.get_feature(x)
             embeddings.append(embedding)
             bbox_list.append(bbox)
         return embeddings, bbox_list
     return None, None
コード例 #22
0
 def get_input(self, face_img):
   print('Input shape: {}'.format(face_img.shape))
   ret = self.detector.detect_face(face_img, det_type = self.args.det)
   if ret is None:
     return None
   # print('MTCNN return: {}'.format(ret))
   bbox, points = ret
   if bbox.shape[0]==0:
     return None
   bbox = bbox[0,0:4]
   points = points[0,:].reshape((2,5)).T
   # print(bbox)
   # print(points)
   
   nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112')
   # nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
   # cv2.imshow('window', nimg)
   # cv2.waitKey(0)
   aligned = np.transpose(nimg, (2,0,1))
   
   return aligned
コード例 #23
0
ファイル: face_model.py プロジェクト: samson-wang/insightface
 def get_input(self, face_img):
     ret = self.detector.detect_face(face_img, det_type=self.args.det)
     if ret is None:
         return None
     bbox, points = ret
     if bbox.shape[0] == 0:
         return None
     print("Detected box", bbox.shape, points.shape)
     aligned_faces = []
     for k, bb in enumerate(bbox):
         bb = bb[:4]
         pp = points[k].reshape((2, 5)).T
         nimg = face_preprocess.preprocess(face_img,
                                           bb,
                                           pp,
                                           image_size='112,112')
         nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
         aligned_faces.append(np.transpose(nimg, (2, 0, 1)))
     aligned_faces = np.array(aligned_faces)
     print("Aligned bbox", aligned_faces.shape)
     return aligned_faces, bbox, points
コード例 #24
0
 def mtcnn_align(img):
     ret = self.detector.detect_face_limited(face_img,
                                             det_type=self.args.det)
     if ret is None:
         # detected = False
         bbox, points = None, None
     else:
         bbox, points = ret
         if bbox.shape[0] == 0:
             # detected = False
             bbox, points = None, None
         else:
             bbox = bbox[0, 0:4]
             points = points[0, :].reshape((2, 5)).T
     # print(bbox)
     # print(points)
     nimg = face_preprocess.preprocess(face_img,
                                       bbox,
                                       points,
                                       image_size='112,112')
     return nimg
コード例 #25
0
 def get_input(self, face_img):
     ret = self.detector.detect_face(face_img, det_type=self.args.det)
     if ret is None:
         return None
     bbox, points = ret
     if bbox.shape[0] == 0:
         return None
     bbox = bbox[0, 0:4]
     print(bbox)
     # cv2.imshow("face", face_img[int(bbox[1]): int(bbox[3]), int(bbox[0]): int(bbox[2])])
     # cv2.waitKey(0)
     points = points[0, :].reshape((2, 5)).T
     # print(bbox)
     # print(points)
     nimg = face_preprocess.preprocess(face_img,
                                       bbox,
                                       points,
                                       image_size='112,112')
     nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
     aligned = np.transpose(nimg, (2, 0, 1))
     return aligned
コード例 #26
0
 def get_all_faces(self, img):
     str_image_size = "%d,%d" % (self.image_size[0], self.image_size[1])
     bounding_boxes, points = detect_face.detect_face(
         img, self.det_minsize, self.pnet, self.rnet, self.onet,
         self.det_threshold, self.det_factor)
     ret = []
     for i in xrange(bounding_boxes.shape[0]):
         bbox = bounding_boxes[i, 0:4]
         # print(bbox)
         cv2.rectangle(img, (int(bbox[0]), int(bbox[1])),
                       (int(bbox[2]), int(bbox[3])), (0, 0, 255), 2)
         landmark = points[:, i].reshape((2, 5)).T
         aligned = face_preprocess.preprocess(img,
                                              bbox=bbox,
                                              landmark=landmark,
                                              image_size=str_image_size)
         aligned = np.transpose(aligned, (2, 0, 1))
         ret.append(aligned)
     img = cv2.resize(img, (int(img.shape[1] / 4), int(img.shape[0] / 4)))
     cv2.imshow("faces", img)
     cv2.waitKey(0)
     return ret
コード例 #27
0
def image_encode(args, i, item, q_out):
    oitem = [item.id]
    if item.flag==0:
      fullpath = item.image_path
      header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
      #print('write', item.flag, item.id, item.label)
      if item.aligned:
        with open(fullpath, 'rb') as fin:
            img = fin.read()
        s = mx.recordio.pack(header, img)
        q_out.put((i, s, oitem))
      else:
        img = cv2.imread(fullpath, args.color)
        assert item.landmark is not None
        img = face_preprocess.preprocess(img, bbox = item.bbox, landmark=item.landmark, image_size='112,112')
        s = mx.recordio.pack_img(header, img, quality=args.quality, img_fmt=args.encoding)
        q_out.put((i, s, oitem))
    else: #flag==1 or 2
      header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
      #print('write', item.flag, item.id, item.label)
      s = mx.recordio.pack(header, '')
      q_out.put((i, s, oitem))
コード例 #28
0
ファイル: face2rec2.py プロジェクト: bupt-cv/insightface
def image_encode(args, i, item, q_out):
    oitem = [item.id]
    if item.flag==0:
      fullpath = item.image_path
      header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
      #print('write', item.flag, item.id, item.label)
      if item.aligned:
        with open(fullpath, 'rb') as fin:
            img = fin.read()
        s = mx.recordio.pack(header, img)
        q_out.put((i, s, oitem))
      else:
        img = cv2.imread(fullpath, args.color)
        assert item.landmark is not None
        img = face_preprocess.preprocess(img, bbox = item.bbox, landmark=item.landmark, image_size='%d,%d'%(args.image_h, args.image_w))
        s = mx.recordio.pack_img(header, img, quality=args.quality, img_fmt=args.encoding)
        q_out.put((i, s, oitem))
    else: #flag==1 or 2
      header = mx.recordio.IRHeader(item.flag, item.label, item.id, 0)
      #print('write', item.flag, item.id, item.label)
      s = mx.recordio.pack(header, '')
      q_out.put((i, s, oitem))
コード例 #29
0
ファイル: face_model.py プロジェクト: shiyu22/video_player
 def get_input(self, face_img):
     ret = self.detector.detect_face(face_img, det_type=self.args.det)
     if ret is None:
         return None
     bbox, points = ret
     print(len(bbox))
     if bbox.shape[0] == 0:
         return None
     bbox = bbox[0, 0:4]
     points = points[0, :].reshape((2, 5)).T
     #print(bbox)
     #print(points)
     nimg = face_preprocess.preprocess(face_img,
                                       bbox,
                                       points,
                                       image_size='112,112')
     nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
     cv2.imshow('image', nimg)
     cv2.waitKey(0)
     cv2.destroyAllWindows()
     aligned = np.transpose(nimg, (2, 0, 1))
     return aligned
コード例 #30
0
 def get_input1(self, face_img):
   start_time = time.time()
   ret = self.detector.detect_face(face_img, det_type = self.args.det)
   if ret is None:
     return None
   bbox, points = ret
   if bbox.shape[0]==0:
     return None
   bbox = bbox[0,0:4]
   points = points[0,:].reshape((2,5)).T
   print(face_img.shape)
   print(bbox)
   print(points)
   nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112')
   #nimg = face_img
   nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
   print(nimg.shape)
   aligned = np.transpose(nimg, (2,0,1))
   print(aligned.shape)
   end_time = time.time()
   print('cost of get input:' + str(end_time - start_time))
   return aligned
コード例 #31
0
    def find_faces(self, img, conf):
        faces = []
        boundb = []
        try:
            bounding_boxes, points = self.detector.detect_face(img, det_type=0)
        except:
            return boundb, faces
        for bb, point in zip(bounding_boxes, points):
            bb = bb[0:4]
            bb = [int(i / conf.resize_ratio) for i in bb]
            point = np.array([int(i / conf.resize_ratio) for i in point])

            point = point.reshape((2, 5)).T
            nimg = face_preprocess.preprocess(img,
                                              bb,
                                              point,
                                              image_size='112,112')
            nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
            aligned = np.transpose(nimg, (2, 0, 1))
            faces.append(aligned)
            boundb.append(bb)
        return boundb, faces
コード例 #32
0
 def get_aligned_face(self, img, force=False):
     bounding_boxes, points = detect_face.detect_face(
         img, self.det_minsize, self.pnet, self.rnet, self.onet,
         self.det_threshold, self.det_factor)
     if bounding_boxes.shape[0] == 0 and force:
         bounding_boxes, points = detect_face.detect_face_force(
             img, None, self.pnet, self.rnet, self.onet)
     if bounding_boxes.shape[0] == 0:
         return None
     bindex = 0
     nrof_faces = bounding_boxes.shape[0]
     det = bounding_boxes[:, 0:4]
     img_size = np.asarray(img.shape)[0:2]
     if nrof_faces > 1:
         bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] -
                                                        det[:, 1])
         img_center = img_size / 2
         offsets = np.vstack([(det[:, 0] + det[:, 2]) / 2 - img_center[1],
                              (det[:, 1] + det[:, 3]) / 2 - img_center[0]])
         offset_dist_squared = np.sum(np.power(offsets, 2.0), 0)
         bindex = np.argmax(bounding_box_size - offset_dist_squared *
                            2.0)  # some extra weight on the centering
     det = bounding_boxes[:, 0:4]
     det = det[bindex, :]
     points = points[:, bindex]
     landmark = points.reshape((2, 5)).T
     #points need to be transpose, points = points.reshape( (5,2) ).transpose()
     det = np.squeeze(det)
     bb = det
     points = list(points.flatten())
     assert (len(points) == 10)
     str_image_size = "%d,%d" % (self.image_size[0], self.image_size[1])
     warped = face_preprocess.preprocess(img,
                                         bbox=bb,
                                         landmark=landmark,
                                         image_size=str_image_size)
     warped = np.transpose(warped, (2, 0, 1))
     print(warped.shape)
     return warped
コード例 #33
0
def get_feature(buffer):
    global emb_size
    if use_flip:
        input_blob = np.zeros(
            (len(buffer) * 2, 3, image_shape[1], image_shape[2]))
    else:
        input_blob = np.zeros((len(buffer), 3, image_shape[1], image_shape[2]))
    idx = 0
    for item in buffer:
        img = face_preprocess.read_image(item[0], mode='rgb')
        img = face_preprocess.preprocess(img,
                                         bbox=None,
                                         landmark=item[1],
                                         image_size='%d,%d' %
                                         (image_shape[1], image_shape[2]))
        img = np.transpose(img, (2, 0, 1))
        attempts = [0, 1] if use_flip else [0]
        for flipid in attempts:
            _img = np.copy(img)
            if flipid == 1:
                do_flip(_img)
            input_blob[idx] = _img
            idx += 1
    data = mx.nd.array(input_blob)
    db = mx.io.DataBatch(data=(data, ))
    net.model.forward(db, is_train=False)
    _embedding = net.model.get_outputs()[0].asnumpy()
    if emb_size == 0:
        emb_size = _embedding.shape[1]
        print('set emb_size to ', emb_size)
    embedding = np.zeros((len(buffer), emb_size), dtype=np.float32)
    if use_flip:
        embedding1 = _embedding[0::2]
        embedding2 = _embedding[1::2]
        embedding = embedding1 + embedding2
    else:
        embedding = _embedding
    embedding = sklearn.preprocessing.normalize(embedding)
    return embedding
コード例 #34
0
  def get_feature(self, face_img):
    #face_img is bgr image
    ret = self.detector.detect_face_limited(face_img, det_type=self.args.det)
    if ret is None:
      return None
    bbox, points = ret
    if bbox.shape[0]==0:
      return None
    bbox = bbox[0,0:4]
    points = points[0,:].reshape((2,5)).T

    nimg = face_preprocess.preprocess(face_img, bbox, points, image_size='112,112')
    nimg = cv2.cvtColor(nimg, cv2.COLOR_BGR2RGB)
    aligned = np.transpose(nimg, (2,0,1))

    input_blob = np.expand_dims(aligned, axis=0)
    data = mx.nd.array(input_blob)
    db = mx.io.DataBatch(data=(data,))
    self.model.forward(db, is_train=False)
    embedding = self.model.get_outputs()[0].asnumpy()
    embedding = sklearn.preprocessing.normalize(embedding).flatten()
    return embedding
コード例 #35
0
ファイル: face_model.py プロジェクト: LHQ0308/insightface
 def get_aligned_face(self, img, force = False):
   #print('before det', img.shape)
   bounding_boxes, points = detect_face.detect_face(img, self.det_minsize, self.pnet, self.rnet, self.onet, self.det_threshold, self.det_factor)
   #if bounding_boxes.shape[0]==0:
   #  fimg = np.copy(img)
   #  do_flip(fimg)
   #  bounding_boxes, points = detect_face.detect_face(fimg, self.det_minsize, self.pnet, self.rnet, self.onet, self.det_threshold, self.det_factor)
   if bounding_boxes.shape[0]==0 and force:
     print('force det', img.shape)
     bounding_boxes, points = detect_face.detect_face(img, self.det_minsize, self.pnet, self.rnet, self.onet, [0.3, 0.3, 0.1], self.det_factor)
     #bounding_boxes, points = detect_face.detect_face_force(img, None, self.pnet, self.rnet, self.onet)
   #print('after det')
   if bounding_boxes.shape[0]==0:
     return None
   bindex = 0
   nrof_faces = bounding_boxes.shape[0]
   det = bounding_boxes[:,0:4]
   img_size = np.asarray(img.shape)[0:2]
   if nrof_faces>1:
     bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
     img_center = img_size / 2
     offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
     offset_dist_squared = np.sum(np.power(offsets,2.0),0)
     bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
   det = bounding_boxes[:,0:4]
   det = det[bindex,:]
   points = points[:, bindex]
   landmark = points.reshape((2,5)).T
   #points need to be transpose, points = points.reshape( (5,2) ).transpose()
   det = np.squeeze(det)
   bb = det
   points = list(points.flatten())
   assert(len(points)==10)
   str_image_size = "%d,%d"%(self.image_size[0], self.image_size[1])
   warped = face_preprocess.preprocess(img, bbox=bb, landmark = landmark, image_size=str_image_size)
   warped = np.transpose(warped, (2,0,1))
   print(warped.shape)
   return warped
コード例 #36
0
ファイル: align_lfw.py プロジェクト: bupt-cv/insightface
def main(args):
    #facenet.store_revision_info(src_path, output_dir, ' '.join(sys.argv))
    dataset = face_image.get_dataset('lfw', args.input_dir)
    print('dataset size', 'lfw', len(dataset))
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    
    minsize = 20
    threshold = [0.6,0.7,0.9]
    factor = 0.85

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    #output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)

    if not os.path.exists(args.output_dir):
      os.makedirs(args.output_dir)

    output_filename = os.path.join(args.output_dir, 'lst')
    
    
    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof = np.zeros( (5,), dtype=np.int32)
        for fimage in dataset:
            if nrof_images_total%100==0:
              print("Processing %d, (%s)" % (nrof_images_total, nrof))
            nrof_images_total += 1
            #if nrof_images_total<950000:
            #  continue
            image_path = fimage.image_path
            if not os.path.exists(image_path):
              print('image not found (%s)'%image_path)
              continue
            filename = os.path.splitext(os.path.split(image_path)[1])[0]
            #print(image_path)
            try:
                img = misc.imread(image_path)
            except (IOError, ValueError, IndexError) as e:
                errorMessage = '{}: {}'.format(image_path, e)
                print(errorMessage)
            else:
                if img.ndim<2:
                    print('Unable to align "%s", img dim error' % image_path)
                    #text_file.write('%s\n' % (output_filename))
                    continue
                if img.ndim == 2:
                    img = to_rgb(img)
                img = img[:,:,0:3]
                _paths = fimage.image_path.split('/')
                a,b = _paths[-2], _paths[-1]
                target_dir = os.path.join(args.output_dir, a)
                if not os.path.exists(target_dir):
                  os.makedirs(target_dir)
                target_file = os.path.join(target_dir, b)
                _minsize = minsize
                _bbox = None
                _landmark = None
                bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)
                nrof_faces = bounding_boxes.shape[0]
                if nrof_faces>0:
                  det = bounding_boxes[:,0:4]
                  img_size = np.asarray(img.shape)[0:2]
                  bindex = 0
                  if nrof_faces>1:
                      bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                      img_center = img_size / 2
                      offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                      offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                      bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
                  _bbox = bounding_boxes[bindex, 0:4]
                  _landmark = points[:, bindex].reshape( (2,5) ).T
                  nrof[0]+=1
                else:
                  nrof[1]+=1
                warped = face_preprocess.preprocess(img, bbox=_bbox, landmark = _landmark, image_size=args.image_size)
                bgr = warped[...,::-1]
                #print(bgr.shape)
                cv2.imwrite(target_file, bgr)
                oline = '%d\t%s\t%d\n' % (1,target_file, int(fimage.classname))
                text_file.write(oline)
コード例 #37
0
ファイル: align_celeb.py プロジェクト: bupt-cv/insightface
def main(args):
    output_dir = os.path.expanduser(args.output_dir)
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)
    datamap = {}
    pp = 0
    datasize = 0
    verr = 0
    for line in open(args.input_dir+"_clean_list.txt", 'r'):
      pp+=1
      if pp%10000==0:
        print('loading list', pp)
      line = line.strip()[2:]
      if not line.startswith('m.'):
        continue
      vec = line.split('/')
      assert len(vec)==2
      #print(line)
      person = vec[0]
      img = vec[1]
      try:
        img_id = int(img.split('.')[0])
      except ValueError:
        #print('value error', line)
        verr+=1
        continue
      if not person in datamap:
        labelid = len(datamap)
        datamap[person] = [labelid, {img_id : 1}]
      else:
        datamap[person][1][img_id] = 1
      datasize+=1

    print('dataset size', args.name, datasize)
    print('dataset err', verr)
    
    print('Creating networks and loading parameters')
    
    with tf.Graph().as_default():
        #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_memory_fraction)
        #sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False))
        sess = tf.Session()
        with sess.as_default():
            pnet, rnet, onet = detect_face.create_mtcnn(sess, None)
    
    minsize = 100 # minimum size of face
    threshold = [ 0.6, 0.7, 0.7 ]  # three steps's threshold
    factor = 0.709 # scale factor

    print(minsize)
    print(threshold)
    print(factor)

    # Add a random key to the filename to allow alignment using multiple processes
    #random_key = np.random.randint(0, high=99999)
    #bounding_boxes_filename = os.path.join(output_dir, 'bounding_boxes_%05d.txt' % random_key)
    output_filename = os.path.join(output_dir, 'faceinsight_align_%s.lst' % args.name)
    
    with open(output_filename, "w") as text_file:
        nrof_images_total = 0
        nrof_successfully_aligned = 0
        nrof_changed = 0
        nrof_iou3 = 0
        nrof_force = 0
        for line in open(args.input_dir, 'r'):
            vec = line.strip().split()
            person = vec[0]
            img_id = int(vec[1])
            v = datamap.get(person, None)
            if v is None:
              continue
            if not img_id in v[1]:
              continue
            labelid = v[0]
            img_str = base64.b64decode(vec[-1])
            nparr = np.fromstring(img_str, np.uint8)
            img = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR)
            img = img[...,::-1] #to rgb
            if nrof_images_total%100==0:
              print("Processing %d, (%d)" % (nrof_images_total, nrof_successfully_aligned))
            nrof_images_total += 1
            target_dir = os.path.join(output_dir, person)
            if not os.path.exists(target_dir):
              os.makedirs(target_dir)
            target_path = os.path.join(target_dir, "%d.jpg"%img_id)
            _minsize = minsize
            fimage = edict()
            fimage.bbox = None
            fimage.image_path = target_path
            fimage.classname = str(labelid)
            if fimage.bbox is not None:
              _bb = fimage.bbox
              _minsize = min( [_bb[2]-_bb[0], _bb[3]-_bb[1], img.shape[0]//2, img.shape[1]//2] )

            bounding_boxes, points = detect_face.detect_face(img, _minsize, pnet, rnet, onet, threshold, factor)
            bindex = -1
            nrof_faces = bounding_boxes.shape[0]
            if fimage.bbox is None and nrof_faces>0:
              det = bounding_boxes[:,0:4]
              img_size = np.asarray(img.shape)[0:2]
              bindex = 0
              if nrof_faces>1:
                bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
                img_center = img_size / 2
                offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
                offset_dist_squared = np.sum(np.power(offsets,2.0),0)
                bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
            if fimage.bbox is not None:
              if nrof_faces>0:
                assert(bounding_boxes.shape[0]==points.shape[1])
                det = bounding_boxes[:,0:4]
                img_size = np.asarray(img.shape)[0:2]
                index2 = [0.0, 0]
                for i in xrange(det.shape[0]):
                  _det = det[i]
                  iou = IOU(fimage.bbox, _det)
                  if iou>index2[0]:
                    index2[0] = iou
                    index2[1] = i
                if index2[0]>-0.3:
                  bindex = index2[1]
                  nrof_iou3+=1
              if bindex<0:
                bounding_boxes, points = detect_face.detect_face_force(img, fimage.bbox, pnet, rnet, onet)
                bindex = 0
                nrof_force+=1
                    
            if bindex>=0:

                det = bounding_boxes[:,0:4]
                det = det[bindex,:]
                points = points[:, bindex]
                landmark = points.reshape((2,5)).T
                #points need to be transpose, points = points.reshape( (5,2) ).transpose()
                det = np.squeeze(det)
                bb = det
                points = list(points.flatten())
                assert(len(points)==10)
                warped = face_preprocess.preprocess(img, bbox=bb, landmark = landmark, image_size=args.image_size)
                misc.imsave(target_path, warped)
                nrof_successfully_aligned += 1
                oline = '%d\t%s\t%d' % (1,fimage.image_path, int(fimage.classname))
                #oline = '%d\t%s\t%d\t%d\t%d\t%d\t%d\t' % (0,fimage.image_path, int(fimage.classname), bb[0], bb[1], bb[2], bb[3])
                #oline += '\t'.join([str(x) for x in points])
                text_file.write("%s\n"%oline)
                            
    print('Total number of images: %d' % nrof_images_total)
    print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
    print('Number of changed: %d' % nrof_changed)
    print('Number of iou3: %d' % nrof_iou3)
    print('Number of force: %d' % nrof_force)
コード例 #38
0
ファイル: cfp2pack.py プロジェクト: LHQ0308/insightface
        bounding_boxes, points = detect_face.detect_face_force(img, minsize, pnet, rnet, onet)
        nrof_faces = bounding_boxes.shape[0]
        if nrof_faces>0:
          nrof[1]+=1
        else:
          nrof[2]+=1
      if nrof_faces>0:
        det = bounding_boxes[:,0:4]
        img_size = np.asarray(img.shape)[0:2]
        bindex = 0
        if nrof_faces>1:
            bounding_box_size = (det[:,2]-det[:,0])*(det[:,3]-det[:,1])
            img_center = img_size / 2
            offsets = np.vstack([ (det[:,0]+det[:,2])/2-img_center[1], (det[:,1]+det[:,3])/2-img_center[0] ])
            offset_dist_squared = np.sum(np.power(offsets,2.0),0)
            bindex = np.argmax(bounding_box_size-offset_dist_squared*2.0) # some extra weight on the centering
        _bbox = bounding_boxes[bindex, 0:4]
        _landmark = points[:, bindex].reshape( (2,5) ).T
      warped = face_preprocess.preprocess(img, bbox=_bbox, landmark = _landmark, image_size=args.image_size)
      warped = warped[...,::-1] #to bgr
      _, s = cv2.imencode('.jpg', warped)
      bins.append(s)
  print(nrof)
  outname = os.path.join(args.output, part[1]+'.bin')
  with open(outname, 'wb') as f:
    pickle.dump((bins, issame_list), f, protocol=pickle.HIGHEST_PROTOCOL)