def align_all(filelist, savePath): ''' --advised: custom reference image-- @breif:对其所有的人脸图像,需要选择参考的图像,默认为第一张 --advised-- NOTICE: HARD CODE!! custom reference image: CASIA-WebFace/5587543/52.jpg ''' ref_word = ['/workspace/datasets/CASIA-WebFace/5587543/052.jpg', \ 93.8033091545, \ 107.536554384, \ 154.941061592, \ 105.698314667, \ 127.14125061, \ 146.21746006, \ 98.2846255302, \ 164.093176651, \ 148.728798866, \ 163.938378143] print 'reference image:' + ref_word[0] #随机选择一个参考图像和参考点 #todo:人工选择一张比较合适的图像作为参考图像,默认情况下,第一张作为参考图像 ref_points = np.empty((10,1), dtype = np.int) points = np.empty((10,1), dtype = np.int) ref_filePath = ref_word[0] for i in range(10): ref_points[i] = float(ref_word[i+1]) refimage = skimage.io.imread(ref_filePath) if refimage.ndim == 3: rows, cols, ch = refimage.shape else: rows, cols = refimage.shape #rows,cols,ch = refimage.shape #为保留数据的完整性,重新扫描 fid = open(filelist,'r') lines = fid.readlines() fid.close() logger = Logger(os.path.join(FACE_ALIGNMENT_ROOT, 'faceAlignemnt.list')) for line in lines: word = line.split() filePath = word[0] for j in range(10): points[j] = float(word[j+1]) warpimage, warpimage_cv2 = alignment(filePath, points, ref_points) ''' filePath format: origin: /workspace/datasets/CASIA-WebFace/5587543/001.jpg splited: ['', 'workspace', 'datasets', 'CASIA-WebFace', '5587543', '001.jpg'] ''' splitedPath = filePath.split('/') className = splitedPath[-2] # NOTICE: HARD CODE!! filename = splitedPath[-1] # NOTICE: HARD CODE!! savename = join(savePath, className, filename) #处理多层文件,不能写入的问题,新建文件 dirname, basename = os.path.split(savename) if not os.path.exists(dirname): os.makedirs(dirname) if warpimage_cv2.shape == refimage.shape: skimage.io.imsave(savename, warpimage_cv2) else: if warpimage_cv2.ndim == 3: rows,cols,ch = warpimage_cv2.shape skimage.io.imsave(savename, warpimage_cv2[0:rows, 0:cols, :]) else: rows,cols = warpimage_cv2.shape skimage.io.imsave(savename, warpimage_cv2[0:rows, 0:cols]) print filePath logger.writeMsg("%s\n" % filePath) ''' free memory: force the Garbage Collector to release ''' gc.collect()