Advertisement
Guest User

Untitled

a guest
May 21st, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import glob
  4. import os, operator
  5.  
  6. def save_landmarks(coords, img_path):
  7.     base = os.path.splitext(img_path)[0]
  8.     print(base)
  9.     path = img_path.replace('.png', '.pts')
  10.     print(path)
  11.     with open(path, "w") as file:
  12.         for coord in coords:
  13.             file.write("%i," % coord[0])
  14.             file.write("%i\n" % coord[1])
  15.  
  16. def pos_of_landmarks(img):
  17.     red = img[:,:,2]
  18.     blue = img[:,:,0]
  19.     green = img[:,:,1]
  20.  
  21.     indices = np.argwhere((red==255)&(blue==0)&(green==0))
  22.  
  23.     coords = []
  24.     for index in range(len(indices)):
  25.         i, j = indices[index]
  26.         if index == 0:
  27.             coords.append([i, j])
  28.         for apex in range(len(coords)):
  29.             n, k = coords[apex]
  30.             if (abs(i - n) > 5 or abs(j - k) > 5) and index != 0:
  31.                 cond = True
  32.             else:
  33.                 cond = False
  34.                 break
  35.         if cond:
  36.             coords.append([i, j])
  37.  
  38.     return img, coords
  39.  
  40. def find_face(cascade_path, frame):
  41.     face_cascade = cv2.CascadeClassifier(cascade_path)
  42.     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  43.     faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  44.     for (x, y, w, h) in faces:
  45.         cv2.rectangle(frame, (x, y), (x+w, y+h+50), (255, 0, 0), 2)
  46.     return frame, faces
  47.  
  48. def resampling(frame, faces):
  49.     for (x, y, w, h) in faces:
  50.         ROI = frame[y:y+h+50, x:x+w]
  51.         resize = cv2.resize(ROI, (300,300), fx=2, fy=2, interpolation = cv2.INTER_CUBIC)
  52.     return resize
  53.  
  54. def average_of_landmarks(img, coords, faces):
  55.     totalCoords = [(0, 0)] * 51
  56.     totalBBw = 0
  57.     totalBBh = 0
  58.     bbX = 0
  59.     bbY = 0
  60.     for (x, y, w, h) in faces:
  61.         bbX = x
  62.         bbY = y
  63.  
  64.     newCoords = []
  65.     for index in range(len(coords)):
  66.         y, x = coords[index]
  67.         newCoords.append([x-bbX, y-bbY])
  68.         x1, y1 = totalCoords[index]
  69.         totalCoordX = x1 + abs(x-bbX)
  70.         totalCoordY = y1 + abs(y-bbY)
  71.         totalCoords[index] = (totalCoordX, totalCoordY)
  72.  
  73.     avgCoords = []
  74.     for index in range(len(totalCoords)):
  75.         x, y = totalCoords[index]
  76.         newX = int(round(x/5))
  77.         newY = int(round(y/5))
  78.         avgCoords.append((newX, newY))
  79.  
  80.     print(avgCoords)
  81.     for (i,j) in avgCoords:
  82.         cv2.circle(img,(j,i), 5, (255,0,0), -1)
  83.  
  84.     return img, avgCoords
  85.  
  86. def draw_landmarks(img, coords):
  87.     for (i,j) in coords:
  88.         cv2.circle(img,(j,i), 5, (255,225,255), 1)
  89.     return img
  90.  
  91. def average_face(avg_coords, coords):
  92.     if len(avg_coords) < len(coords):
  93.         return coords
  94.     for i in range(len(coords)):
  95.         x,y = coords[i]
  96.         j,k = avg_coords[i]
  97.         avg_coords[i] = (x+j, y+k)
  98.     return avg_coords
  99.  
  100. cascade_path = 'frontal_face_features.xml'
  101.  
  102. paths_annotated = glob.glob('Ibrahim emotions/sad_annotated/*.png')
  103. paths_original  = glob.glob('Ibrahim emotions/Sad/*.png')
  104.  
  105. avg_coords = []
  106. count = 0
  107. for path in paths_annotated:
  108.     count += 1
  109.     frame = cv2.imread(path)
  110.     img, faces = find_face(cascade_path, frame)
  111.     resized_img = resampling(img, faces)
  112.     img, coords = pos_of_landmarks(resized_img)
  113.     avg_coords = average_face(avg_coords, coords)
  114.  
  115. avg_coords[:] = [ (int(i/count), int(j/count)) for (i,j) in avg_coords]
  116. print(avg_coords)
  117. print(len(avg_coords))
  118.  
  119. for path in paths_original:
  120.     frame = cv2.imread(path)
  121.     img, faces = find_face(cascade_path, frame)
  122.     resized_img = resampling(img, faces)
  123.     img = draw_landmarks(resized_img, avg_coords)
  124.     #img, avg_coords = average_of_landmarks(img, coords, faces)
  125.  
  126.     #save_landmarks(coords, path)
  127.  
  128.     cv2.imshow('image',img)
  129.     cv2.waitKey(0)
  130.     cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement