コード例 #1
0
def image_feed(S, image_dims):
    '''
  Given a list of file paths and a 2-tuple of (H, W), yields H x W x 3 images.
  '''
    for x in S:
        I = imageutils.read(x)
        if I.shape[:2] != image_dims:
            yield imageutils.resize(I, tuple(image_dims))
        else:
            yield I
コード例 #2
0
def image_feed_masked(S, image_dims):
    '''
  Given a list of file paths and a 2-tuple of (H, W), yields H x W x 3 images.
  '''
    for x in S:
        I = imageutils.read(x)
        if I.shape[:2] != image_dims:
            I = imageutils.resize(I, tuple(image_dims))
        center_mask_inplace(I)
        yield I
コード例 #3
0
  K=config.K
  delta_params=[float(x.strip()) for x in config.delta.split(',')]

  t0=time.time()
  result=[]
  original=[]
  # for each test image
  for i in range(len(X)):
    result.append([])
    xX=X[i].replace('lfw','lfw_aegan')
    o=imageutils.read(xX)
    image_dims=o.shape[:2]
    if min(image_dims)<minimum_resolution:
      s=float(minimum_resolution)/min(image_dims)
      image_dims=(int(round(image_dims[0]*s)),int(round(image_dims[1]*s)))
      o=imageutils.resize(o,image_dims)
    XF=model.mean_F([o])
    original.append(o)
    # for each transform
    for j,(a,b) in enumerate(pairs):
      _,P,Q=make_manifolds(b,[a],[],X=X[i:i+1],N=1)
      P=P[0]
      Q=Q[0]
      xP=[x.replace('lfw','lfw_aegan') for x in P]
      xQ=[x.replace('lfw','lfw_aegan') for x in Q]
      PF=model.mean_F(utils.image_feed(xP[:K],image_dims))
      QF=model.mean_F(utils.image_feed(xQ[:K],image_dims))
      if config.scaling=='beta':
        WF=(QF-PF)/((QF-PF)**2).mean()
      elif config.scaling=='none':
        WF=(QF-PF)
コード例 #4
0
  delta_params=[float(x.strip()) for x in config.delta.split(',')]

  X=config.input

  t0=time.time()
  opathlist=[]
  # for each test image
  for i in range(len(X)):  #X是程序的输入图片路径的list
    xX=X[i] #第i个图片的路径
    prefix_path=os.path.splitext(xX)[0]
    template,original=alignface.detect_landmarks(xX,face_d,face_p) #检测的关键点,和归一化的人脸图片
    image_dims=original.shape[:2]  #得到图片大小
    if min(image_dims)<minimum_resolution: #如果图片很小,就放大
      s=float(minimum_resolution)/min(image_dims)
      image_dims=(int(round(image_dims[0]*s)),int(round(image_dims[1]*s)))
      original=imageutils.resize(original,image_dims)
    XF=model.mean_F([original]) #求图片的列表的深度特征
    XA=classifier.score([xX])[0] #没看懂
    print(xX,', '.join(k for i,k in enumerate(fields) if XA[i]>=0))

    # select positive and negative sets based on gender and mouth
    #
    if config.method=='older':
      cP=[(gender,XA[gender]>=0),(smile,XA[smile]>=0),(fields.index('Young'),True)]  #[(索引数字,bool),。。。]
      cQ=[(gender,XA[gender]>=0),(smile,XA[smile]>=0),(fields.index('Young'),False)]
    elif config.method=='younger':
      cP=[(gender,XA[gender]>=0),(smile,XA[smile]>=0),(fields.index('Young'),False)]
      cQ=[(gender,XA[gender]>=0),(smile,XA[smile]>=0),(fields.index('Young'),True)]
    elif config.method=='facehair':
      cP=[(gender,XA[gender]>=0),(smile,XA[smile]>=0),(fields.index('No_Beard'),True),(fields.index('Mustache'),False)]
      cQ=[(gender,XA[gender]>=0),(smile,XA[smile]>=0),(fields.index('No_Beard'),False),(fields.index('Mustache'),True)]
コード例 #5
0
import argparse
import dlib
import cv2
from imageutils import overlay_transparent, resize

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
goggle = cv2.imread("images/goggle.png", cv2.IMREAD_UNCHANGED)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
offset = 8

image = resize(image, width=600)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect faces in the grayscale image
rects = detector(gray, 1)

# loop over the face detections
for (i, rect) in enumerate(rects):
    shape_68 = predictor(gray, rect)

    # calculate bounds to draw goggle
    left_x = shape_68.part(17).x - offset
    left_y = shape_68.part(19).y
    right_x = shape_68.part(26).x + offset
    resize_goggle = resize(goggle,
                           width=right_x - left_x,
コード例 #6
0
import argparse
import dlib
import cv2
from imageutils import resize

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
image = cv2.imread(args["image"])
image = resize(image, width=600)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect faces in the grayscale image
rects = detector(gray, 1)

# loop over the all face detected by dlib.get_frontal_face_detector
for (i, rect) in enumerate(rects):
    shape_68 = predictor(gray, rect)
    for part_index in range(shape_68.num_parts):
        cv2.circle(image,
                   (shape_68.part(part_index).x, shape_68.part(part_index).y),
                   2, (0, 255, 0), 1)

# show landmarks that were circled in the last step
cv2.imshow("Output", image)
cv2.waitKey(0)
コード例 #7
0
        import deepmodels_torch
        model = deepmodels_torch.vgg19g_torch(device_id=config.device_id)
    elif config.backend == 'caffe+scipy':
        model = deepmodels.vgg19g(device_id=config.device_id)
    else:
        raise ValueError('Unknown backend')

    # load interpolation data
    original = imageutils.read(ipath1)
    XF = model.mean_F([original])
    prefix_path = os.path.splitext(ipath1)[0]
    if not os.path.exists(prefix_path + postfix_comment):
        os.mkdir(prefix_path + postfix_comment)  # debug
    X_mask = prefix_path + '-mask.png'
    if 'mask' in postprocess and os.path.exists(X_mask):
        mask = imageutils.resize(imageutils.read(X_mask), image_dims)
    data = numpy.load(ipath2)
    if 'WF' in data:
        WF = data['WF']
    elif 'muQ' in data and 'muP' in data:
        WF = data['muQ'] - data['muP']

    # generate frames
    t0 = time.time()
    max_iter = config.iter
    init = original
    i = 0
    delta = delta0
    while delta <= delta1:
        print('Starting frame #{:06}, {}'.format(i + 1,
                                                 (time.time() - t0) / 60.0))
コード例 #8
0
def main(config):
  caffe.set_device(config['device_id'])
  caffe.set_mode_gpu()

  # step 1. configure cnn
  if config['arch']=='A4,G1':
    # an ensemble of alexnet and googlenet
    models=[
      caffe.Net('deploy-alexnet_full_conv.prototxt','minc-alexnet_full_conv.caffemodel',caffe.TEST),
      caffe.Net('deploy-googlenet_full_conv_no_pooling.prototxt','minc-googlenet_full_conv.caffemodel',caffe.TEST)
    ]

    # alexnet needs a padded input to get a full-frame prediction
    input_padding=[97,0]

    # nominal footprint is 46.4% for A4, 23.2% for G1
    scales=[256/550.0,256/1100.0]
    bgr_mean=numpy.array([104,117,124],dtype=numpy.float32)

    # inputs must be a multiple of the stride
    # otherwise, the full-frame prediction will be shifted
    effective_stride=[32,32]

    # TODO: A4 needs spatial oversampling (# shifts = 2)
  else:
    raise NotImplementedError

  # step 2. configure crf
  if config['crf']=='1':
    # these are the CRF parameters for MINC (see supplemental)
    # the parameters can have a big impact on the output
    # so they should be tuned for the target domain
    # (MINC note: this code is not exactly the same as the
    # original MINC code so these parameters will not
    # generate the exact same results)
    crf_params={
      "bilateral_pairwise_weight": 5.0, # w_p
      "bilateral_theta_xy": 0.1, # \theta_p
      "bilateral_theta_lab_l": 20.0, # \theta_L
      "bilateral_theta_lab_ab": 5.0, # \theta_ab
      "n_crf_iters": 10,
      "unary_prob_padding": 1e-05,
    }
  elif config['crf']=='matclass':
    # new CRF parameters
    crf_params={
      "bilateral_pairwise_weight": 8.0, # w_p
      "bilateral_theta_xy": 0.5, # \theta_p
      "bilateral_theta_lab_l": 0.5, # \theta_L
      "bilateral_theta_lab_ab": 3.0, # \theta_ab
      "n_crf_iters": 10,
      "unary_prob_padding": 1e-05,
    }
  else:
    raise NotImplementedError

  pad_value=bgr_mean[::-1]/255.0

  # step 3. extract class prediction maps
  for ipath in config['input']:
    # read image
    original=imageutils.read(ipath)
    z=config['min_dim']/float(min(original.shape[:2]))
    crf_shape=(23,int(round(original.shape[0]*z)),int(round(original.shape[1]*z)))

    # predict 6 maps: 3 scales for each model
    maps=[]
    for index,model in enumerate(models):
      p=input_padding[index]
      s=scales[index]
      for index2,multiscale in enumerate([0.7071067811865476,1.0,1.4142135623730951]):
        # resample the input so it is a multiple of the stride
        # and the receptive field matches the nominal footprint
        scale_factor=(256/s)/float(min(original.shape[:2]))
        scaled_size=[nearest_multiple(original.shape[i]*scale_factor*multiscale,effective_stride[index]) for i in range(2)]
        scaled=imageutils.resize(original,scaled_size)
        if p>0:
          # add input padding for alexnet
          pad=numpy.ones((scaled.shape[0]+2*p,scaled.shape[1]+2*p,scaled.shape[2]),dtype=scaled.dtype)*pad_value
          pad[p:-p,p:-p]=scaled
          scaled=pad

        # predict and resample the map to be the correct size
        data=preprocess_and_reshape(scaled,model,bgr_mean=bgr_mean)
        output=model.forward_all(data=data)['prob'][0]
        output=scipy.ndimage.interpolation.zoom(output,[1.0,crf_shape[1]/float(output.shape[1]),crf_shape[2]/float(output.shape[2])],order=1)
        maps.append(output)

    # step 4. average all maps
    crf_map=numpy.asarray(maps).mean(axis=0)
    if False:
      # output extra maps for debugging
      for i,x,j in [(i,x,j) for i,x in enumerate(maps) for j in range(23)]: imageutils.write('zzz_map_{}{}.jpg'.format(dataset.NETCAT_TO_NAME[j],i),x[j])
      for j in range(23): imageutils.write('zzz_mean_map_{}.jpg'.format(dataset.NETCAT_TO_NAME[j],i),crf_map[j])
      imageutils.write('zzz_naive_labels.png',labels_to_color(numpy.argsort(-crf_map.reshape(23,-1).T).T.reshape(*crf_map.shape)[0]))
    crf_color=imageutils.resize(original,(crf_shape[1],crf_shape[2]))
    assert crf_color.shape[0]==crf_map.shape[1] and crf_color.shape[1]==crf_map.shape[2]

    # step 5. dense crf
    #lcrf=densecrf_matclass.general_densecrf.LearnableDenseCRF(crf_color,crf_map,crf_params)
    lcrf=densecrf_matclass.densecrf.LearnableDenseCRF(crf_color,crf_map,crf_params)
    labels_crf=lcrf.map(crf_params)

    # step 6. visualize with color labels
    result=labels_to_color(labels_crf)
    if os.path.exists(config['output']) and os.path.isdir(config['output']):
      opath=os.path.join(config['output'],os.path.splitext(os.path.split(ipath)[1])[0])+'.png'
    else:
      opath=config['output']
    assert not os.path.exists(opath)
    imageutils.write(opath,result)
    print(opath)