Last active
October 24, 2015 21:22
-
-
Save helxsz/0a035689534cfa5e10c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import numpy as np | |
import random | |
from PIL import Image | |
import numpy as np | |
import h5py | |
import matplotlib.pyplot as plt | |
import glob | |
import sys | |
sys.path.append('/usr/local/lib/python2.7/site-packages') | |
import cv2 | |
script_dir = os.path.dirname(os.path.abspath(__file__)) | |
def show_landmark(face, landmark): | |
face_copied = face.copy().astype(np.uint8) | |
for (x, y) in landmark: | |
xx = int(face.shape[0]*x) | |
yy = int(face.shape[1]*y) | |
cv2.circle(face_copied, (xx, yy), 4, (0,0,0), -1) | |
print 'landmark', len(landmark), landmark[0] | |
cv2.imshow("face_rot", face_copied) | |
# crop and rotate | |
def rotate(img, box, alpha): | |
left = box[0] | |
right = box[1] | |
top = box[2] | |
bottom = box[3] | |
center = ((left+right)/2, (top+bottom)/2) | |
rot_mat = cv2.getRotationMatrix2D(center, alpha, 1) | |
img_rotated_by_alpha = cv2.warpAffine(img, rot_mat, img.shape[:2]) | |
face_rotated = img_rotated_by_alpha[top:bottom+1,left:right+1] | |
#cv2.imshow('img'+str(alpha),face_rotated) | |
return face_rotated | |
def flip(face): | |
face_flipped_by_x = cv2.flip(face, 1) | |
return face_flipped_by_x | |
def processImage(imgs): | |
""" | |
process images before feeding to CNNs | |
imgs: N x 1 x W x H | |
""" | |
imgs = imgs.astype(np.float32) | |
for i, img in enumerate(imgs): | |
m = img.mean() | |
s = img.std() | |
imgs[i] = (img - m) / s | |
return imgs | |
#1.png,-6,32,19,16,59,66 | |
rotations = [0,-25,-20,-15-10,-5,5,10,15,20,25] | |
blurs = [3,7] | |
def normalize(img): | |
img = cv2.resize(img, (64, 64)) | |
reshaped = img.reshape(3, 64, 64) | |
return reshaped | |
def testHDF5(): | |
imgs = [] | |
img = cv2.imread(script_dir+"/images/1.png") | |
left = 19 | |
right = 19 + 59 | |
top = 16 | |
bottom = 16 + 66 | |
for degree in rotations: | |
rotated = rotate(img, (left,right,top,bottom),degree) | |
imgs.append( normalize(rotated) ) | |
for blur in blurs: | |
blured = cv2.GaussianBlur(rotated,(blur,blur),0) | |
imgs.append( normalize(blured) ) | |
imgs = np.asarray(imgs) | |
imgs = processImage(imgs) | |
print len(imgs), imgs[0].shape | |
''''' rotated example | |
rotated = rotate(img, (left,right,top,bottom),15) | |
cv2.imshow("aaa",img) | |
cv2.imshow("bbb",rotated) | |
rotated2 = rotate(img, (left,right,top,bottom),45) | |
cv2.imshow("ccc",rotated2) | |
#for degree in rotations: | |
# rotate(img, (left,right,top,bottom),degree) | |
''''' | |
''''' flipped example | |
flipped = flip(cropped ) | |
cv2.imshow('flipped',flipped) | |
''''' | |
''''' cropped | |
cv2.imshow("img",img) | |
cropped = img[top: bottom, left:right] | |
#cv2.imshow("img2",cropped) | |
''''' | |
''''' blured example | |
for blur in blurs: | |
blur = cv2.GaussianBlur(cropped,(blur,blur),0) | |
cv2.imshow("blur"+str(blur),blur) | |
cv2.imwrite(script_dir+"/1.png",blur) | |
''''' | |
cv2.waitKey(0) | |
testHDF5() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment