def get_feature(buffer):
  embedding = np.zeros( (len(buffer), emb_size), dtype=np.float32 )
  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 use_flip:
    embedding1 = _embedding[0::2]
    embedding2 = _embedding[1::2]
    embedding = embedding1+embedding2
  else:
    embedding = _embedding
  embedding = sklearn.preprocessing.normalize(embedding)
  return embedding
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
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)
      net.arg_params["data"] = mx.nd.array(input_blob, net.ctx)
      net.arg_params["softmax_label"] = mx.nd.empty((1,), net.ctx)
      exe = net.sym.bind(net.ctx, net.arg_params ,args_grad=None, grad_req="null", aux_states=net.aux_params)
      exe.forward(is_train=False)
      _embedding = exe.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
Exemple #4
0
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