Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. class FaceFinder:
  2. def __init__(self):
  3. self.img = None
  4. self.img_orig = None
  5. self.patches = []
  6. self.preds = None
  7.  
  8. def process_photo(self, img_name, patch_size=64):
  9. self.img = plt.imread(img_name)
  10. self.img = scipy.misc.imresize(self.img, (256, 256, 3))
  11. self.img_orig = self.img.copy()
  12.  
  13. self._split_pic_into_frames(size=patch_size)
  14. self._run_predictions()
  15. # self._rectangify_patches()
  16. return self._gray_out()
  17.  
  18. def _split_pic_into_frames(self, size, stride=1):
  19. sh_x = self.img.shape[0]
  20. sh_y = self.img.shape[1]
  21. len_x = size
  22. len_y = size
  23. stride_x = np.ceil(len_x*stride).astype(int)
  24. stride_y = np.ceil(len_y*stride).astype(int)
  25.  
  26. for y in range(0, sh_y, stride_y):
  27. for x in range(0, sh_x, stride_x):
  28. patch = self.img[y:y+len_y, x:x+len_x]
  29. if patch.shape[0]*patch.shape[1]!=0:
  30. self.patches.append([patch, (x, y), (x+len_x, y+len_y)])
  31.  
  32. def _rectangify_patches(self):
  33. columns = int(np.sqrt(len(self.preds)))
  34.  
  35. cont = True
  36. while cont:
  37. cont = False
  38. for r in range(columns-1):
  39. for c in range(columns-1):
  40. if self.preds[r*columns+c] == 0:
  41. LD = self.preds[(r+1)*columns+c-1]
  42. SD = self.preds[(r+1)*columns+c]
  43. RD = self.preds[(r+1)*columns+c+1]
  44. L = self.preds[r*columns+c-1]
  45. R = self.preds[r*columns+c+1]
  46. LG = self.preds[(r-1)*columns+c-1]
  47. SG = self.preds[(r-1)*columns+c]
  48. RG = self.preds[(r-1)*columns+c+1]
  49.  
  50. answ = L*LD*SD + SD*RD*R + R*RG*SG + SG*LG*L
  51.  
  52. if answ >= 1:
  53. cont = True
  54. self.preds[r*columns+c] = 1
  55.  
  56. def _gray_out(self):
  57. coord_start = np.array(self.patches)[:, 1]
  58. coord_end = np.array(self.patches)[:, 2]
  59.  
  60. self.img.setflags(write=1)
  61.  
  62. for start, end, pred in zip(coord_start, coord_end, self.preds):
  63. if pred < 0.5:
  64. self.img[max(0, start[0]-3):end[0]+3, max(0, start[1]-3):end[1]+3] = [255, 0, 0]
  65.  
  66. for start, end, pred in zip(coord_start, coord_end, self.preds):
  67. R = self.img_orig[start[0]:end[0], start[1]:end[1]][:, :, 0]
  68. G = self.img_orig[start[0]:end[0], start[1]:end[1]][:, :, 1]
  69. B = self.img_orig[start[0]:end[0], start[1]:end[1]][:, :, 2]
  70. if pred < 0.5:
  71. self.img[start[0]:end[0], start[1]:end[1]][:, :, 0] = R
  72. self.img[start[0]:end[0], start[1]:end[1]][:, :, 1] = G
  73. self.img[start[0]:end[0], start[1]:end[1]][:, :, 2] = B
  74.  
  75. return self.img, self.img_orig
  76.  
  77. def _run_predictions(self):
  78. a = scipy.misc.imresize(self.patches[0][0], (64, 64, 3)).reshape((1, 64, 64, 3))
  79. for i in self.patches[1:]:
  80. a = np.vstack((a, scipy.misc.imresize(i[0], (64, 64, 3)).reshape((1, 64, 64, 3))))
  81. self.preds = predict(a, sess)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement