Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.30 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. import sys
  5.  
  6.  
  7. def get_eyes_nose_open_face(csv_path):
  8.  
  9.     return nose, (left_eye_x, left_eye_y), (right_eyes_x, right_eyes_y)
  10.  
  11.  
  12. def rotate_point(origin, point, angle):
  13.     ox, oy = origin
  14.     px, py = point
  15.  
  16.     qx = ox + np.cos(angle) * (px - ox) - np.sin(angle) * (py - oy)
  17.     qy = oy + np.sin(angle) * (px - ox) + np.cos(angle) * (py - oy)
  18.     return qx, qy
  19.  
  20.  
  21. def is_between(point1, point2, point3, extra_point):
  22.     c1 = (point2[0] - point1[0]) * (extra_point[1] - point1[1]) - (point2[1] - point1[1]) * (extra_point[0] - point1[0])
  23.     c2 = (point3[0] - point2[0]) * (extra_point[1] - point2[1]) - (point3[1] - point2[1]) * (extra_point[0] - point2[0])
  24.     c3 = (point1[0] - point3[0]) * (extra_point[1] - point3[1]) - (point1[1] - point3[1]) * (extra_point[0] - point3[0])
  25.     if (c1 < 0 and c2 < 0 and c3 < 0) or (c1 > 0 and c2 > 0 and c3 > 0):
  26.         return True
  27.     else:
  28.         return False
  29.  
  30.  
  31. def distance(a, b):
  32.     return np.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
  33.  
  34.  
  35. def cosine_formula(length_line1, length_line2, length_line3):
  36.     cos_a = -(length_line3 ** 2 - length_line2 ** 2 - length_line1 ** 2) / (2 * length_line2 * length_line1)
  37.     return cos_a
  38.  
  39. def shape_to_normal(shape):
  40.     shape_normal = []
  41.     for i in range(0, 5):
  42.         shape_normal.append((i, (shape.part(i).x, shape.part(i).y)))
  43.     return shape_normal
  44.  
  45.  
  46. def rotate_opencv(img, nose_center, angle):
  47.     M = cv2.getRotationMatrix2D(nose_center, angle, 1)
  48.     rotated = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]), flags=cv2.INTER_CUBIC)
  49.     return rotated
  50.  
  51.  
  52. def align(img, landmarks):
  53.  
  54.     nose, left_eye, right_eye = landmarks
  55.  
  56.     center_of_forehead = ((left_eye[0] + right_eye[0]) // 2, (left_eye[1] + right_eye[1]) // 2)
  57.     center_pred = (int((x + w) / 2), int((y + y) / 2))
  58.     length_line1 = distance(center_of_forehead, nose)
  59.     length_line2 = distance(center_pred, nose)
  60.     length_line3 = distance(center_pred, center_of_forehead)
  61.     cos_a = cosine_formula(length_line1, length_line2, length_line3)
  62.     angle = np.arccos(cos_a)
  63.     rotated_point = rotate_point(nose, center_of_forehead, angle)
  64.     rotated_point = (int(rotated_point[0]), int(rotated_point[1]))
  65.     if is_between(nose, center_of_forehead, center_pred, rotated_point):
  66.         angle = np.degrees(-angle)
  67.     else:
  68.         angle = np.degrees(angle)
  69.  
  70.     img = rotate_opencv(img, nose, angle)
  71.  
  72.     return img
  73.  
  74. def align_sequences(frames_path, landmarks):
  75.     file_list = glob.glob('{}/*.png'.format(frames_path))
  76.     file_list.sort()
  77.     data = []
  78.  
  79.     dir_aligned = os.path.join(frames_path,'aligned')
  80.  
  81.     if not os.path.isdir(dir_aligned):
  82.         os.makedirs(dir_aligned)
  83.  
  84.     print("Reading frames")
  85.     current_num_samples = 0
  86.     for f in range(0, file_list.__len__()):
  87.         try:
  88.             data.append(cv2.imread(file_list[f]))
  89.    
  90.     for i in range(len(landmarks)):
  91.         try:
  92.             aligned_img = align(data[i],landmarks[i])
  93.             output_aligned_path = 'frame_%6d'.format(i)
  94.             cv2.imwrite(output_aligned_path,aligned_img)
  95.         except:
  96.             print("Error Aligning frame")
  97.  
  98.  
  99. if __name__ == '__main__':
  100.     landmarks = get_eyes_nose_open_face("csvpath")
  101.     align_sequences('folder containing frames from ffmpeg',landmarks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement