def analyzePhoto(): """ Analyzes the photo and stores it on the user facial_analysis """ photo = request.files['webcam'] np_arr = np.fromstring(photo.read(), np.uint8) gray_img = cv2.imdecode(np_arr, cv2.IMREAD_GRAYSCALE) landmarks = stasm.search_single(gray_img) if len(landmarks) == 0: return jsonify(success=False, message="Face not found. Please try again.") face = FaceInfo() face.generateInfoFromStasm(landmarks) landmarks = stasm.force_points_into_image(landmarks, gray_img) for point in landmarks: gray_img[round(point[1])][round(point[0])] = 255 comparison_photo = cv2.imencode('.jpeg', gray_img)[1] b64_comparison_photo = base64.encodestring(comparison_photo) return jsonify(data=face.getInfo(), img='data:image/jpeg;base64,' + b64_comparison_photo, success=True)
def Predict(X_test, Y_train): #Compute prediction using stasm library #Compute mean model mean_model = np.mean(Y_train, axis=0) mean_model = mean_model.reshape(number_of_keypoints, 2) print "mean_model", mean_model.shape Y_pred = np.zeros((X_test.shape[0], 2 * number_of_keypoints), np.float32) for i in range(0, X_test.shape[0]): print i img = X_test[i, :, :] #print X_test[i,:,:].shape landmarks = stasm.search_single(img) landmarks = stasm.force_points_into_image(landmarks, img) #(77,2) if len(landmarks) == 0: #Detection failed, so just use mean_model landmarks = mean_model else: landmarks = stasm_77_to_kaggle_15(landmarks) # #don't work # #landmarks= stasm.search_pinned(landmarks.astype(np.uint8)[:2,:], img) #show prediction #img= X_test[i,:,:] #for point in landmarks: # img[round(point[1])][round(point[0])] = 255 #cv2.imshow("stasm keypoints", img) #cv2.waitKey(0) Y_pred[i, :] = landmarks.reshape(1, 2 * number_of_keypoints) return Y_pred
def calculateGoldenRatio(img): landmarks = stasm.search_single(img) if len(landmarks) == 0: return -1 else: landmarks = stasm.force_points_into_image(landmarks, img) lips = [(landmarks[67][0] + landmarks[70][0]) / 2, (landmarks[67][1] + landmarks[70][1]) / 2] #a = calculateEuclideanDistance(landmarks[change], landmarks[6]) #b = calculateEuclideanDistance(landmarks[change], landmarks[38]) c = calculateEuclideanDistance(landmarks[38], landmarks[56]) d = calculateEuclideanDistance(landmarks[14], lips) e = calculateEuclideanDistance(landmarks[56], landmarks[54]) f = calculateEuclideanDistance(landmarks[34], landmarks[44]) g = calculateEuclideanDistance(landmarks[0], landmarks[12]) h = calculateEuclideanDistance(landmarks[14], landmarks[38]) i = calculateEuclideanDistance(landmarks[56], landmarks[6]) j = calculateEuclideanDistance(lips, landmarks[6]) k = calculateEuclideanDistance(landmarks[62], landmarks[74]) l = calculateEuclideanDistance(landmarks[56], lips) #res1 = a/g #res2 = b/d res3 = i / j res4 = i / c res5 = e / l res6 = f / h res7 = k / e #finalRes = (res1 + res2 + res3 + res4 + res5 + res6 + res7)/7 finalRes = (res3 + res4 + res5 + res6 + res7) / 5 return finalRes
def update(self): self.ret, self.frame = self.cap.read() if self.ret == True and found: self.frame = cv2.flip(self.frame, 1) if (self.firstRun): segment = 16 height = np.size(self.frame, 0) width = np.size(self.frame, 1) # print (height, "x", width) self.deadZoneLeft = int((width / 2) - (width / segment)) self.deadZoneRight = int((width / 2) + (width / segment)) self.deadZoneUp = int((height / 2) - (height / segment)) self.deadZoneDown = int((height / 2) + (height / segment)) self.firstRun = False #imgCol = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) self.img = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY) # do processing here self.landmarks = stasm.search_single(self.img) # print (len(landmarks)) if len(self.landmarks) == 0: self.face_found = False else: self.landmarks = stasm.force_points_into_image( self.landmarks, self.img) self.face_found = True # print("Number of landmark points: ", len(landmarks)) pts = [] #pts = np.array(, np.int32) for point in self.landmarks: pts.append([int(round(point[0])), int(round(point[1]))]) #print(pts) ptsNp = np.array(pts, np.int32) #print (pts) ptsNp = ptsNp.reshape((-1, 1, 2)) #cv2.polylines(frame, [ptsNp], False, (0,255,0)) self.frame = self.paintDaFaceBro(self.frame, pts) if (self.paintDebug): self.frame = self.paintDebugLines(self.frame, pts) self.recogniseActions(self.frame, pts) self.frame = self.displayActions(self.frame) for i in range(0, len(pts)): # print("i: ",i, " - ", pts[i][0], " , ", pts[i][1]) # cv2.putText(frame, str(i), (pts[i][0], pts[i][1]), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,255,0), 1, cv2.LINE_AA) # frame[int(round(point[1]))][int(round(point[0]))] = (0,255,0) continue
def __init__(self, img): self.landmarks = {} self.segments = {} # STASM landmarks = asm.search_single(img) assert len(landmarks) > 0, "cannot detect face" landmarks = asm.force_points_into_image(landmarks, img) landmarks = np.uint8(landmarks) self.segment_face(img, landmarks)
def update(self): self.ret, self.frame = self.cap.read() self.og_frame = self.frame if self.ret == True and found: self.frame = cv2.flip(self.frame, 1) if (self.firstRun): segment = 16 height = np.size(self.frame, 0) width = np.size(self.frame, 1) # print (height, "x", width) self.deadZoneLeft = int((width/2) - (width/segment)) self.deadZoneRight = int((width/2) + (width/segment)) self.deadZoneUp = int((height/2) - (height/segment)) self.deadZoneDown = int((height/2) + (height/segment)) self.firstRun = False #imgCol = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) self.img = cv2.cvtColor(self. frame, cv2.COLOR_BGR2GRAY) if (found): # do processing here self.landmarks = stasm.search_single(self.img) # print (len(landmarks)) if len(self.landmarks) == 0: self.face_found = False else: self.landmarks = stasm.force_points_into_image(self.landmarks, self.img) self.face_found = True # print("Number of landmark points: ", len(landmarks)) pts = [] #pts = np.array(, np.int32) for point in self.landmarks: pts.append([int(round(point[0])), int(round(point[1]))]) #print(pts) ptsNp = np.array(pts, np.int32) #print (pts) ptsNp = ptsNp.reshape((-1,1,2)) #cv2.polylines(frame, [ptsNp], False, (0,255,0)) self.frame = self.paintDaFaceBro(self.frame, pts) if (self.paintDebug): self.frame = self.paintDebugLines(self.frame, pts)
def get_face_landmarks(self): self.__resize_image() stasm.init() stasm.open_image(self.img, multiface=True) self.landmarks = stasm.search_auto() if len(self.landmarks) == 0: print "Error in get_face_landmarks(): Cannot find face", self.path self.landmarks = None else: self.landmarks = stasm.force_points_into_image(self.landmarks, self.img) self.landmarks = stasm.convert_shape(self.landmarks, stasm.MUCT76) stasm.init() self.__coordinate_correction()
def get_face_landmarks(self): self.__resize_image() stasm.init() stasm.open_image(self.img, multiface=True) self.landmarks = stasm.search_auto() if len(self.landmarks) == 0: print "Error in get_face_landmarks(): Cannot find face", self.path self.landmarks = None else: self.landmarks = stasm.force_points_into_image( self.landmarks, self.img) self.landmarks = stasm.convert_shape(self.landmarks, stasm.MUCT76) stasm.init() self.__coordinate_correction()
def get_face_points(image_name): path = os.path.join(stasm.DATADIR, image_name) img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) if img is None: print("Cannot load", path) raise SystemExit landmarks = stasm.search_single(img) if len(landmarks) == 0: print("No face found in", path) else: landmarks = stasm.force_points_into_image(landmarks, img) for point in landmarks: img[round(point[1])][round(point[0])] = 255 cv2.imshow("stasm minimal", img) cv2.waitKey(0)
def VisualizeDetectedKeypoints(X, Y): #Compute mean model m = np.mean(Y, axis=0) print "m", m.shape m = m.reshape(number_of_keypoints, 2) for img in X: #print "img",img.shape landmarks = stasm.search_single(img) landmarks = stasm.force_points_into_image(landmarks, img) #(77,2) if len(landmarks) == 0: # print 'Detection failed!' landmarks = m else: landmarks = stasm_77_to_kaggle_15(landmarks) #don't work #landmarks= stasm.search_pinned(landmarks.astype(np.uint8)[:2,:], img) for point in landmarks: img[round(point[1])][round(point[0])] = 255 cv2.imshow("stasm keypoints", img) cv2.waitKey(0)
import os import cv2 import stasm import csv rootDir = '/home/shivani/Pictures/cohn-kanade-images-extracted/' csvFile = open('featuredata.csv', 'wb') writer = csv.writer(csvFile) for subdir, dirs, files in os.walk(rootDir): for imageFile in files: path = os.path.join(subdir, imageFile) img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) if img is None: print("Cannot load", path) raise SystemExit landmarks = stasm.search_single(img) landmarks = stasm.force_points_into_image(landmarks, img) list1 = [imageFile] for point in landmarks: list1.append(point[0]) list1.append(point[1]) writer.writerows([list1]) #print len(list1) csvFile.close()
def main(): print "Reading subject image" img1 = cv2.imread(sys.argv[1]) img_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) img_LAB = cv2.cvtColor(img1, cv2.COLOR_BGR2LAB) size = img1.shape print "Getting feature points of subject image" landmarks = stasm.search_single(img_gray) points1 = stasm.force_points_into_image(landmarks, img_gray) points1 = list(map(tuple, points1)) print "Reading example image" img2 = cv2.imread(sys.argv[2]) img_gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) print "Getting feature points of example image" landmarks2 = stasm.search_single(img_gray2) points2 = stasm.force_points_into_image(landmarks2, img_gray2) points2 = list(map(tuple, points2)) #printing the feature points img_points = img1.copy() img_points2 = img2.copy() for point in points1: img_points[int(round(point[1]))][int(round(point[0]))] = 255 for point in points2: img_points2[int(round(point[1]))][int(round(point[0]))] = 255 #display(img_points) #display(img_points2) print "Face alignment through warping" if( len(points1) == len(points2) ): points = [] alpha = 0 # Compute weighted average point coordinates for i in range(0, len(points1)): x = ( 1 - alpha ) * points1[i][0] + alpha * points2[i][0] y = ( 1 - alpha ) * points1[i][1] + alpha * points2[i][1] points.append((x,y)) imgMorph = np.zeros(img1.shape, dtype = img1.dtype) ##denaulay triangulation rect = (0, 0, size[1], size[0]) subdiv = cv2.Subdiv2D(rect) for p in points1: subdiv.insert(p) triangle_list = subdiv.getTriangleList() points_map = {} for i in range(len(points1)): points_map[ str(points1[i]) ] = i #morphing triangle one by one for p in triangle_list: p1, p2, p3 = (p[0],p[1]), (p[2],p[3]), (p[4],p[5]) if rect_contains(rect, p1) and rect_contains(rect, p2) and rect_contains(rect, p3) : index1 = points_map[str(p1)] index2 = points_map[str(p2)] index3 = points_map[str(p3)] t1 = [p1, p2, p3] t2 = [points2[index1], points2[index2], points2[index3]] t = [points[index1], points[index2], points[index3]] face_morphing.morphTriangle(img1, img2, imgMorph, t1, t2, t, 1) distance = 8 sigmacolor = 85 #display(imgMorph) print "Partitioning the image into C1, C2, C3" cmat = cpartition(points1,size[0],size[1]) print "Layer decomposition" lightness_layer = img_LAB[..., 0] Ic_A = img_LAB[..., 1] Ic_B = img_LAB[..., 2] img_LAB2 = cv2.cvtColor(imgMorph, cv2.COLOR_BGR2LAB) lightness_layer2 = img_LAB2[..., 0] Ec_A = img_LAB2[..., 1] Ec_B = img_LAB2[..., 2] print "Applying wls filter on base image" #face_structure_layer = cv2.bilateralFilter(lightness_layer,distance,sigmacolor,0) face_structure_layer, skin_detail_layer = wls_filter.wlsfilter_layer(lightness_layer,cmat) #display(face_structure_layer,mode='gray') #skin_detail_layer = lightness_layer - face_structure_layer Is, Id = face_structure_layer, skin_detail_layer print "Applying wls filter on example image" #face_structure_layer2 = cv2.bilateralFilter(lightness_layer2,distance,sigmacolor,0) face_structure_layer2, skin_detail_layer2 = wls_filter.wlsfilter_layer(lightness_layer2,cmat) #skin_detail_layer2 = lightness_layer2 - face_structure_layer2 #display(skin_detail_layer2,mode='gray') Es, Ed = face_structure_layer2, skin_detail_layer2 #skin detail transfer print "Skin detail transfer" del_I = 0 #original skin details weight del_E = 1 #example skin details weight Rd = del_I * Id + del_E * Ed #color transfer print "Color transfer" #printing the C3 points #img_gray = np.zeros((size[0],size[1])) #lrev = points1[30:33] #left_eye = points1[33:38] + lrev #for point in left_eye: # img_gray[int(round(point[1]))][int(round(point[0]))] = 255 #display(img_gray,mode='gray') #temp = 71 #img_gray[int(round(points1[temp][1]))][int(round(points1[temp][0]))] = 0 gamma = 0.8 Rc_A = (1-gamma)*Ic_A + (gamma)*Ec_A Rc_B = (1-gamma)*Ic_B + (gamma)*Ec_B for y in xrange(size[0]): for x in xrange(size[1]): if cmat[y][x]==0 or cmat[y][x]==3: #Rc_A[y][x] = 255 #Rc_B[y][x] = 255 Rc_A[y][x] = Ic_A[y][x] Rc_B[y][x] = Ic_B[y][x] Rd[y][x] = Id[y][x] #Highlight and shading transfer print("Highlight and shading transfer") Rs = Is.copy() # shading_transfer(Is, Es, cmat, Rs, size) result_LAB = img_LAB.copy() result_LAB[..., 0] = Rs + Rd result_LAB[..., 1] = Rc_A result_LAB[..., 2] = Rc_B result = cv2.cvtColor(result_LAB, cv2.COLOR_LAB2BGR) #Lip makeup print("Lip makeup") M = result.copy() """ lip_makeup(M, lightness_layer, imgMorph, lightness_layer2, cmat, size) M_LAB = cv2.cvtColor(M, cv2.COLOR_BGR2LAB) M_L = M_LAB[..., 0] M_A = M_LAB[..., 1] M_B = M_LAB[..., 2] for y in range(size[0]): for x in range(size[1]): if(cmat[y][x]==2): result_LAB[y][x][0] = M_L[y][x] result_LAB[y][x][1] = M_A[y][x] result_LAB[y][x][2] = M_B[y][x] result = cv2.cvtColor(result_LAB, cv2.COLOR_LAB2BGR) """ cv2.imwrite('morphed.jpg', imgMorph) cv2.imwrite('result.jpg', result) cv2.imwrite('base_points.jpg', img_points) cv2.imwrite('example_points.jpg', img_points2) display(result)
if n in emotions.keys() and emotions[n] == i: ind = np.where(res_dict["name"]==n)[0][0] X.append(pca_features[ind]) Y.append(i) classifier = svm.SVC(kernel="rbf", gamma=0.08) classifier.fit(X, Y) img_neutral = cv2.imread("test/neutral.png", cv2.IMREAD_GRAYSCALE) img_emotion = cv2.imread("test/emotion.png", cv2.IMREAD_GRAYSCALE) landmarks = stasm.search_single(img_neutral) landmarks = stasm.force_points_into_image(landmarks, img_neutral) neutral_list = [] for point in landmarks: neutral_list.append([point[0], point[1]]) neutral_dists = {} for ai,bi in combinations(range(0,len(neutral_list)), 2): x = neutral_list[ai] y = neutral_list[bi] if ai > bi: ai,bi = bi,ai header = str(ai) + ":" + str(bi) neutral_dists[header] = distance.euclidean(np.array(x), np.array(y)) landmarks = stasm.search_single(img_emotion) landmarks = stasm.force_points_into_image(landmarks, img_emotion)