Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import cv2
  2. import time
  3. import imutils
  4. import numpy as np
  5. import tqdm
  6. from imutils import face_utils
  7. import numpy as np
  8. import argparse
  9. import imutils
  10. import dlib
  11. import cv2
  12. from imutils import face_utils
  13. import glob
  14. import os
  15. import sys
  16. os.environ["CUDA_VISIBLE_DEVICES"]=sys.argv[1]
  17.  
  18. dlib.DLIB_USE_CUDA = True
  19. detector = dlib.get_frontal_face_detector()
  20. predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
  21.  
  22.  
  23. def overlay_transparent(background, overlay, x, y):
  24.  
  25. background_width = background.shape[1]
  26. background_height = background.shape[0]
  27.  
  28. if x >= background_width or y >= background_height:
  29. return background
  30.  
  31. h, w = overlay.shape[0], overlay.shape[1]
  32.  
  33. if x + w > background_width:
  34. w = background_width - x
  35. overlay = overlay[:, :w]
  36.  
  37. if y + h > background_height:
  38. h = background_height - y
  39. overlay = overlay[:h]
  40.  
  41. if overlay.shape[2] < 4:
  42. overlay = np.concatenate(
  43. [
  44. overlay,
  45. np.ones((overlay.shape[0], overlay.shape[1], 1), dtype = overlay.dtype) * 255
  46. ],
  47. axis = 2,
  48. )
  49.  
  50. overlay_image = overlay[..., :3]
  51. mask = overlay[..., 3:] / 255.0
  52.  
  53. background[y:y+h, x:x+w] = (1.0 - mask) * background[y:y+h, x:x+w] + mask * overlay_image
  54.  
  55. return background
  56.  
  57. def shapeconverter(shape):
  58. x0s = []
  59. y0s = []
  60. x1s = []
  61. y1s = []
  62.  
  63. x0s.append(shape[48][0])
  64. x0s.append(shape[49][0])
  65. x0s.append(shape[50][0])
  66. x0s.append(shape[51][0])
  67. x0s.append(shape[52][0])
  68. x0s.append(shape[53][0])
  69. x0s.append(shape[48][0])
  70. x0s.append(shape[60][0])
  71. x0s.append(shape[61][0])
  72. x0s.append(shape[62][0])
  73. x0s.append(shape[63][0])
  74. x0s.append(shape[64][0])
  75. x0s.append(shape[48][0])
  76. x0s.append(shape[67][0])
  77. x0s.append(shape[66][0])
  78. x0s.append(shape[65][0])
  79. x0s.append(shape[64][0])
  80. x0s.append(shape[48][0])
  81. x0s.append(shape[59][0])
  82. x0s.append(shape[58][0])
  83. x0s.append(shape[57][0])
  84. x0s.append(shape[56][0])
  85. x0s.append(shape[55][0])
  86.  
  87. y0s.append(shape[48][1])
  88. y0s.append(shape[49][1])
  89. y0s.append(shape[50][1])
  90. y0s.append(shape[51][1])
  91. y0s.append(shape[52][1])
  92. y0s.append(shape[53][1])
  93. y0s.append(shape[48][1])
  94. y0s.append(shape[60][1])
  95. y0s.append(shape[61][1])
  96. y0s.append(shape[62][1])
  97. y0s.append(shape[63][1])
  98. y0s.append(shape[64][1])
  99. y0s.append(shape[48][1])
  100. y0s.append(shape[67][1])
  101. y0s.append(shape[66][1])
  102. y0s.append(shape[65][1])
  103. y0s.append(shape[64][1])
  104. y0s.append(shape[48][1])
  105. y0s.append(shape[59][1])
  106. y0s.append(shape[58][1])
  107. y0s.append(shape[57][1])
  108. y0s.append(shape[56][1])
  109. y0s.append(shape[55][1])
  110.  
  111. x1s.append(shape[49][0])
  112. x1s.append(shape[50][0])
  113. x1s.append(shape[51][0])
  114. x1s.append(shape[52][0])
  115. x1s.append(shape[53][0])
  116. x1s.append(shape[54][0])
  117. x1s.append(shape[61][0])
  118. x1s.append(shape[61][0])
  119. x1s.append(shape[62][0])
  120. x1s.append(shape[63][0])
  121. x1s.append(shape[64][0])
  122. x1s.append(shape[54][0])
  123. x1s.append(shape[67][0])
  124. x1s.append(shape[66][0])
  125. x1s.append(shape[65][0])
  126. x1s.append(shape[64][0])
  127. x1s.append(shape[54][0])
  128. x1s.append(shape[59][0])
  129. x1s.append(shape[58][0])
  130. x1s.append(shape[57][0])
  131. x1s.append(shape[56][0])
  132. x1s.append(shape[55][0])
  133. x1s.append(shape[54][0])
  134.  
  135. y1s.append(shape[49][1])
  136. y1s.append(shape[50][1])
  137. y1s.append(shape[51][1])
  138. y1s.append(shape[52][1])
  139. y1s.append(shape[53][1])
  140. y1s.append(shape[54][1])
  141. y1s.append(shape[61][1])
  142. y1s.append(shape[61][1])
  143. y1s.append(shape[62][1])
  144. y1s.append(shape[63][1])
  145. y1s.append(shape[64][1])
  146. y1s.append(shape[54][1])
  147. y1s.append(shape[67][1])
  148. y1s.append(shape[66][1])
  149. y1s.append(shape[65][1])
  150. y1s.append(shape[64][1])
  151. y1s.append(shape[54][1])
  152. y1s.append(shape[59][1])
  153. y1s.append(shape[58][1])
  154. y1s.append(shape[57][1])
  155. y1s.append(shape[56][1])
  156. y1s.append(shape[55][1])
  157. y1s.append(shape[54][1])
  158. return x0s, y0s, x1s, y1s
  159.  
  160.  
  161. def facecontour(image):
  162. for i in range(0, 16):
  163. cv2.line(image, (shape[i][0], shape[i][1]), (shape[i+1][0], shape[i+1][1]), color, thickness)
  164. return image
  165.  
  166.  
  167.  
  168. def split_list(seq, num):
  169. avg = len(seq) / float(num)
  170. out = []
  171. last = 0.0
  172. while last < len(seq):
  173. out.append(seq[int(last):int(last + avg)])
  174. last += avg
  175. return out
  176.  
  177. color = (255,0,0)
  178. thickness = 1
  179. addition = 3
  180. images = glob.glob("fridman_images/*")
  181. images = split_list(images, 16)
  182.  
  183. for a,frame in tqdm.tqdm(enumerate(images[int(sys.argv[2])])):
  184. frame = cv2.imread(frame)
  185. image = frame.copy()
  186. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  187. rects = detector(gray, 1)
  188. for (i, rect) in enumerate(rects):
  189. shape = predictor(gray, rect)
  190. shape = face_utils.shape_to_np(shape)
  191. x0, y0, x1, y1 = (shape[48][0]-addition, shape[50][1]-addition, shape[54][0]+addition, shape[57][1]+addition)
  192. cv2.rectangle(image, (x0-20, y0-45), (x1+20, y1+40),(0,0,0), -1)
  193. x0s, y0s, x1s, y1s = shapeconverter(shape)
  194. for x in range(0,len(x0s)):
  195. cv2.line(image, (x0s[x], y0s[x]), (x1s[x], y1s[x]), color, thickness)
  196. cv2.imwrite(f"images/lips/{sys.argv[2]}_{a}.jpg", image[shape[30][1]-256:shape[30][1]+256, shape[30][0]-256:shape[30][0]+256])
  197. cv2.imwrite(f"images/originals/{sys.argv[2]}_{a}.jpg", frame[shape[30][1]-256:shape[30][1]+256, shape[30][0]-256:shape[30][0]+256])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement