def normalize_shape():
    delta_cy,delta_cx,delta_sx,delta_sy=0,0,0,0
    face_points=np.load("ferns/face_point.npy")
    shape=np.zeros((total_img,p*2),dtype=np.float32)
    bounding_box=np.zeros((total_img,8),dtype=np.int16)
    for i in range(0,total_img):
        for j in range(0,p):
            shape[i,j*2+1],shape[i,j*2]=face_points[i,j,0],face_points[i,j,1]
    np.save("ferns/shape(1520,40).npy",shape)
    for i in range(0,total_img):
        
        
        detect_ok=0
        filename="/home/wcs/opencv_assignments/lfw/images_reindex/"+str(i)+".jpg"
        img=cv2.imread(filename)
        
        if (img!=None):
            img0=img.copy()
            bounding_box0=detect_face(img,1)
        if (bounding_box0[0,0]!=0):
            for j in range(0,bounding_box0.shape[0]):
                  mean_x0,mean_y0,sx0,sy0=bounding_box0[j,0],bounding_box0[j,1],bounding_box0[j,2]/2,bounding_box0[j,3]/2
                  print (shape[i,40]-mean_y0-sy0*0.2)/sy0,(shape[i,41]-mean_x0)/sx0
                  if (abs(shape[i,40]-mean_y0-sy0*0.2)<sy0*0.2 and abs(shape[i,41]-mean_x0)<sx0*0.2):
                        mean_x,mean_y,sx,sy=bounding_box0[j,0],bounding_box0[j,1],bounding_box0[j,2]/2,bounding_box0[j,3]/2
                        detect_ok=1
                        
        if (detect_ok==0):
                  bounding_box[i,1]=min(shape[i,::2]) 
                  bounding_box[i,0]=max(shape[i,::2])
                  bounding_box[i,2]=(bounding_box[i,0]+bounding_box[i,1])/2*(1+(np.random.random_sample((1,5))[0,0]/10)-0.05)+delta_cy/i
                  bounding_box[i,3]=(bounding_box[i,0]-bounding_box[i,1])/2*(1+(np.random.random_sample((1,5))[0,0]/10)-0.05)+delta_sy/i
        
        
                  bounding_box[i,4]=max(shape[i,1::2])
                  bounding_box[i,5]=min(shape[i,1::2]) 
        
                  bounding_box[i,6]=(bounding_box[i,4]+bounding_box[i,5])/2*(1+(np.random.random_sample((1,5))[0,0]/10)-0.05)+delta_cx/i
                  bounding_box[i,7]=(bounding_box[i,4]-bounding_box[i,5])/2*(1+(np.random.random_sample((1,5))[0,0]/10)-0.05)+delta_sx/i
                  
        if (detect_ok==1):
            bounding_box[i,0]=mean_y+sy
            bounding_box[i,1]=mean_y-sy
            bounding_box[i,2]=mean_y
            bounding_box[i,3]=sy
            bounding_box[i,4]=mean_x+sx
            bounding_box[i,5]=mean_x-sx
            bounding_box[i,6]=mean_x
            bounding_box[i,7]=sx
        print bounding_box[i,3],bounding_box[i,7],i
        #if ( bounding_box[i,3]==np.NaN or bounding_box[i,7] ==np.NaN):
            #print i
        
        print bounding_box[i,2]
        delta_cy=bounding_box[i,2]-(max(shape[i,::2])+min(shape[i,::2]))/2+delta_cy
        delta_cx=bounding_box[i,6]-(max(shape[i,1::2])+min(shape[i,1::2]))/2+delta_cx
        delta_sy=bounding_box[i,3]-(max(shape[i,::2])-min(shape[i,::2]))/2 +delta_sy
        delta_sx=bounding_box[i,7]-(max(shape[i,1::2])-min(shape[i,1::2]))/2+delta_sx
        shape[i,::2]=(shape[i,::2]-bounding_box[i,2])/bounding_box[i,3]
        shape[i,1::2]=(shape[i,1::2]-bounding_box[i,6])/bounding_box[i,7]
        """
        if (detect_ok==0):
            cv2.rectangle(img, (bounding_box[i,6]- bounding_box[i,7],bounding_box[i,2]- bounding_box[i,3]), (bounding_box[i,6]+bounding_box[i,7],bounding_box[i,2]+bounding_box[i,3]), (0, 255, 0), 2)
            cv2.imshow("fews",img)
            cv2.waitKey(0)
        """
    mean_shape=np.mean(shape,axis=0)
    print mean_shape
    np.save("ferns/bound_box(1520,8).npy",bounding_box)
    
    return shape  
    #for lfpw
    delta_x=shape0[0,40]-mean_x
    delta_y=shape0[0,41]-mean_y
        
    #for bioid
    #delta_x=shape0[0,28]-mean_x-0.18*sizex
    #delta_y=shape0[0,29]-mean_y
    #print delta_x,delta_y,shape0[0,28],shape0[0,29],mean_x,mean_y
    new_shape[0,::2]=shape0[0,::2]-delta_x+0.2*sizex
    new_shape[0,1::2]=shape0[0,1::2]-delta_y
    #new_shape[0,28*2]=new_shape[0,28*2]+4
    #for i in range(18,22):
        #new_shape[0,i*2]=new_shape[0,i*2]-2
    return new_shape
if __name__ == '__main__':
    
    #shape=vectorize_shape()
    shape=normalize_shape()
    print shape
    np.save("ferns/align_shape(1520,40).npy",shape)
    #print shape[5,:]
    img=cv2.imread("face_detect/build/BioID-FaceDatabase-V1.2/BioID_0001.pgm")
    mean_y,mean_x,sx,sy=detect_face(img)
    shape=get_shape(np.mean(shape,axis=0),sx/2,sy/2,mean_y,mean_x)
    for i in range(0,p):
        y,x=shape[0,i*2+1],shape[0,i*2]
        img[x:x+5,y:y+5]=[255,0,0]
    cv2.namedWindow("imshow")
    cv2.imshow("imshow",img)
    cv2.waitKey(0)