def load_test_img(gray, bbox): # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face = gray[bbox.top:bbox.bottom, bbox.left:bbox.right] face = cv2.resize(face, (39,39)).reshape(1,1,39,39) face = processImage(np.asarray(face)) return face
def load_test_img(gray, bbox): # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) face = gray[bbox.top:bbox.bottom, bbox.left:bbox.right] face = cv2.resize(face, (40, 40)).reshape(1, 1, 40, 40) face = processImage(np.asarray(face)) return face
def generate_hdf5(cephaTxt,output,fname,argument=False): data = getDataFromTxt(cephaTxt)#return [(img_path,landmark,bbox)] cepha_imgs = [] cepha_landmarks = [] for (imgPath,landmarkGt,bbox) in data: img = cv2.imread(imgPath,cv2.CV_LOAD_IMAGE_GRAYSCALE) assert(img is not None) logger("process %s" % imgPath) #downsampled by 3: 3x3 patch height,width = img.shape[:2] size = (int(width/3),int(height/3)) cephaImg = cv2.resize(img,size,interpolation=cv2.INTER_NEAREST) cepha_bbox = bbox#.subBBox(-0.05,1.05,-0.05,1.05) cepha_img = cephaImg[cepha_bbox.top:cepha_bbox.bottom+1,cepha_bbox.left:cepha_bbox.right+1] if argument and np.random.rand()>-1: ###rotation if np.random.rand() > 0.5: cepha_rotated_alpha,landmark_rotated = rotate(cepha_img,cepha_bbox,bbox.reprojectLandmark(landmarkGt),5) landmark_rotated = bbox.projectLandmark(landmark_rotated)#relative cepha_rotated_alpha = cv2.resize(cepha_rotated_alpha,(39,39)) cepha_imgs.append(cepha_rotated_alpha.reshape((1,39,39))) cepha_landmarks.append(landmark_rotated.reshape(38)) if np.random.rand() > 0.5: cepha_rotated_alpha,landmark_rotated = rotate(cepha_img,cepha_bbox,bbox.reprojectLandmark(landmarkGt),-5) landmark_rotated = bbox.projectLandmark(landmark_rotated) cepha_rotated_alpha = cv2.resize(cepha_rotated_alpha,(39,39)) cepha_imgs.append(cepha_rotated_alpha.reshape((1,39,39))) cepha_landmarks.append(landmark_rotated.reshape(38)) cepha_img = cv2.resize(cepha_img,(39,39)) cepha_img = cepha_img.reshape((1,39,39)) cepha_landmark = landmarkGt.reshape((38)) cepha_imgs.append(cepha_img) cepha_landmarks.append(cepha_landmark) cepha_imgs,cepha_landmarks = np.asarray(cepha_imgs),np.asarray(cepha_landmarks) cepha_imgs = processImage(cepha_imgs) shuffle_in_unison_scary(cepha_imgs,cepha_landmarks) #save file base = join(OUTPUT,'1_cepha')#train/1_cepha (or test) createDir(base) output = join(base,fname)#train/1_cepha/train.h5 (or test) output = output.replace('\\','/') logger("generate %s" % output) with h5py.File(output,'w') as h5: h5['data'] = cepha_imgs.astype(np.float32) h5['landmark'] = cepha_landmarks.astype(np.float32)
def generate_h5py(data, h5_path, txt_path, augment=False): ''' Get images and turn them into h5py files Input: - data: a tuple of [imgpath, bbox, landmark] - h5_path: h5py file name - txt_path: h5 txt name ''' F_imgs = [] F_landmarks = [] for (imgpath, bbox, landmark) in data: img = cv2.imread(imgpath, cv2.CV_LOAD_IMAGE_GRAYSCALE) print("processing %s") % imgpath if not check_bbox(img, bbox): print 'BBox out of range.' continue face = img[bbox.top:bbox.bottom, bbox.left:bbox.right] if augment: face_flip, landmark_flip = flip(face, landmark) face_flip = cv2.resize(face_flip, (39, 39)).reshape(1, 39, 39) landmark_flip = landmark_flip.reshape(10) F_imgs.append(face_flip) F_landmarks.append(landmark_flip) face = cv2.resize(face, (39, 39)).reshape(1, 39, 39) landmark = landmark.reshape(10) F_imgs.append(face) F_landmarks.append(landmark) F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks) F_imgs = processImage(F_imgs) F_imgs, F_landmarks = shuffle(F_imgs, F_landmarks, random_state=42) with h5py.File(h5_path, 'w') as f: f['data'] = F_imgs.astype(np.float32) f['landmarks'] = F_landmarks.astype(np.float32) with open(txt_path, 'w') as f: f.write(h5_path)
def generate_h5py(data, h5_path, txt_path, augment=False): ''' Get images and turn them into h5py files Input: - data: a tuple of [imgpath, bbox, landmark] - h5_path: h5py file name - txt_path: h5 txt name ''' F_imgs = [] F_landmarks = [] img_size = 40 num_landmark = 68 * 2 num_sample = 0 for (imgpath, bbox, landmarkGt) in data: img = cv2.imread(imgpath, cv2.CV_LOAD_IMAGE_GRAYSCALE) print("processing %s") % imgpath if not check_bbox(img, bbox): print 'BBox out of range.' continue face = img[bbox.top:bbox.bottom, bbox.left:bbox.right] if augment: # flip the face #face_flip, landmark_flip = flip(face, landmarkGt) #face_flip = cv2.resize(face_flip, (img_size,img_size)).reshape(1,img_size,img_size) #fit=0 #for i in range(0,num_landmark/2): # if landmark_flip[i,0]<0 or landmark_flip[i,0]>1 or landmark_flip[i,1]<0 or landmark_flip[i,1]>1: # fit=1 # break #if fit==0: #print landmark_flipped_alpha # F_imgs.append(face_flip) # F_landmarks.append(landmark_flip.reshape(num_landmark)) #print landmark_flip #angles=[5,10,15,20,25,30,-5,-10,-15,-20,-25,-30] #for alpha in angles: # rotate the face #face_rotated_by_alpha, landmark_rotated_alpha = rotate(img, bbox,bbox.reprojectLandmark(landmarkGt), alpha) #landmark_rotated_alpha = bbox.projectLandmark(landmark_rotated_alpha) #face_rotated_by_alpha = cv2.resize(face_rotated_by_alpha, (img_size,img_size)) #fit=0 #for i in range(0,num_landmark/2): # if landmark_rotated_alpha[i,0]<0 or landmark_rotated_alpha[i,0]>1 or landmark_rotated_alpha[i,1]<0 or landmark_rotated_alpha[i,1]>1: # fit=1 # break #if fit==0: #print landmark_rotated_alpha # F_imgs.append(face_rotated_by_alpha.reshape((1, img_size,img_size))) # F_landmarks.append(landmark_rotated_alpha.reshape(num_landmark)) #print landmark_rotated_5 # flip with the rotation #face_flipped_alpha, landmark_flipped_alpha = flip(face_rotated_by_alpha, landmark_rotated_alpha) #face_flipped_alpha = cv2.resize(face_flipped_alpha, (img_size,img_size)) #fit=0 #for i in range(0,num_landmark/2): # if landmark_flipped_alpha[i,0]<0 or landmark_flipped_alpha[i,0]>1 or landmark_flipped_alpha[i,1]<0 or landmark_flipped_alpha[i,1]>1: # fit=1 # break #if fit==0: #print landmark_flipped_alpha # F_imgs.append(face_flipped_alpha.reshape((1, img_size,img_size))) # F_landmarks.append(landmark_flipped_alpha.reshape(num_landmark)) # debug #center = ((bbox.left+bbox.right)/2, (bbox.top+bbox.bottom)/2) #rot_mat = cv2.getRotationMatrix2D(center, alpha, 1) #img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, img.shape) #landmark_rotated_alpha = bbox.reprojectLandmark(landmark_rotated_alpha) # will affect the flip "landmark_rotated_alpha" #img_rotated_by_alpha = drawLandmark(img_rotated_by_alpha, bbox, landmark_rotated_alpha) #fp = 'debug_results/'+ str(num_sample)+'.jpg' #cv2.imwrite(fp, img_rotated_by_alpha) #num_sample=num_sample+1 # debug #use bounding box perturbation y = bbox.top x = bbox.left h = bbox.bottom - bbox.top w = bbox.right - bbox.left # original landmark position landmarkGT_scale = bbox.reprojectLandmark(landmarkGt) for cur_scale in [0.83, 0.91, 1.0, 1.10, 1.21]: for cur_x in [-0.17, 0, 0.17]: for cur_y in [-0.17, 0, 0.17]: s_n = 1 / cur_scale x_n = -cur_x / cur_scale y_n = -cur_y / cur_scale x_temp = int(x - (x_n * w / s_n)) y_temp = int(y - (y_n * h / s_n)) w_temp = int(w / s_n) h_temp = int(h / s_n) # generate new bounding box bbox_left = x_temp bbox_right = x_temp + w_temp bbox_top = y_temp bbox_bottom = y_temp + h_temp new_bbox = map( int, [bbox_left, bbox_right, bbox_top, bbox_bottom]) new_bbox = BBox(new_bbox) if not check_bbox(img, new_bbox): print 'BBox out of range.' continue # project landmark onto the new bounding box new_landmarkGT = new_bbox.projectLandmark( landmarkGT_scale) new_landmarkGT_org = new_landmarkGT.copy() angles = [ 5, 10, 15, 20, 25, 30, -5, -10, -15, -20, -25, -30 ] for alpha in angles: # rotate the face face_rotated_by_alpha, landmark_rotated_alpha = rotate( img, new_bbox, new_bbox.reprojectLandmark(new_landmarkGT), alpha) landmark_rotated_alpha = new_bbox.projectLandmark( landmark_rotated_alpha) face_rotated_by_alpha = cv2.resize( face_rotated_by_alpha, (img_size, img_size)) fit = 0 for i in range(0, num_landmark / 2): if landmark_rotated_alpha[ i, 0] < 0 or landmark_rotated_alpha[ i, 0] > 1 or landmark_rotated_alpha[ i, 1] < 0 or landmark_rotated_alpha[ i, 1] > 1: fit = 1 break if fit == 0: #print landmark_rotated_alpha F_imgs.append( face_rotated_by_alpha.reshape( (1, img_size, img_size))) F_landmarks.append( landmark_rotated_alpha.reshape( num_landmark)) # debug #center = ((new_bbox.left+new_bbox.right)/2, (new_bbox.top+new_bbox.bottom)/2) #rot_mat = cv2.getRotationMatrix2D(center, alpha, 1) #img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, img.shape) #landmark_rotated_alpha = new_bbox.reprojectLandmark(landmark_rotated_alpha) # will affect the flip "landmark_rotated_alpha" #img_rotated_by_alpha = drawLandmark(img_rotated_by_alpha, new_bbox, landmark_rotated_alpha) #fp = 'debug_results/'+ str(num_sample)+'.jpg' #cv2.imwrite(fp, img_rotated_by_alpha) #num_sample=num_sample+1 # debug # project landmark onto the new bounding box landmarkGT_project = new_landmarkGT_org.copy( ) # error is fixed here #print landmarkGT_project fit = 0 for i in range(0, num_landmark / 2): if landmarkGT_project[ i, 0] < 0 or landmarkGT_project[ i, 0] > 1 or landmarkGT_project[ i, 1] < 0 or landmarkGT_project[ i, 1] > 1: fit = 1 break if fit == 0: #print landmarkGT_project #if landmarkGT_project[i,0]<0 or landmarkGT_project[i,0]>1 or landmarkGT_project[i,1]<0 or landmarkGT_project[i,1]>1: # pdb.set_trace() cropped_face = img[new_bbox.top:new_bbox.bottom, new_bbox.left:new_bbox.right] cropped_face = cv2.resize( cropped_face, (img_size, img_size)).reshape( 1, img_size, img_size) F_imgs.append(cropped_face) F_landmarks.append( landmarkGT_project.reshape(num_landmark)) # debug on the bounding box perturbation #landmark_org = new_bbox.reprojectLandmark(landmarkGT_project) # will affect the flip "landmark_rotated_alpha" #img_debug = drawLandmark(img, new_bbox, landmark_org) #fp = 'debug_results/'+ str(num_sample)+'_bbox.jpg' #cv2.imwrite(fp, img_debug) #num_sample=num_sample+1 face = cv2.resize(face, (img_size, img_size)).reshape(1, img_size, img_size) fit = 0 for i in range(0, num_landmark / 2): if landmarkGt[i, 0] < 0 or landmarkGt[i, 0] > 1 or landmarkGt[ i, 1] < 0 or landmarkGt[i, 1] > 1: fit = 1 break if fit == 0: #print landmark_flipped_alpha F_imgs.append(face) F_landmarks.append(landmarkGt.reshape(num_landmark)) F_imgs, F_landmarks = np.asarray(F_imgs), np.asarray(F_landmarks) F_imgs = processImage(F_imgs) F_imgs, F_landmarks = shuffle(F_imgs, F_landmarks, random_state=42) with h5py.File(h5_path, 'w') as f: f['data'] = F_imgs.astype(np.float32) f['landmarks'] = F_landmarks.astype(np.float32) with open(txt_path, 'w') as f: f.write(h5_path) f.write(str(len(F_landmarks)))