Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. #%%
  2. import os
  3. import numpy as np
  4. import dlib
  5. from skimage import io
  6. from scipy.spatial import distance
  7. from scipy.special import softmax
  8. import webbrowser
  9. from matplotlib import pyplot as plt
  10. from PIL import Image
  11. from multiprocessing import Pool
  12. #%%
  13.  
  14.  
  15. def make_description(name):
  16.     global sp, facerec, detector, direct
  17.     img = io.imread(direct + name)
  18.     dets_webcam = detector(img, 1)
  19.     for k, d in enumerate(dets_webcam):
  20.         shape = sp(img, d)
  21.     return facerec.compute_face_descriptor(img, shape)
  22.  
  23.  
  24. #%%
  25. print('Введите 1, если Вы будете искать мужчину;')
  26. print('Введите 2, если Вы будете искать женщину.')
  27. sex = int(input()) - 1
  28. #%%
  29. direct = './data/photos/women/' if sex else './data/photos/men/'
  30. fileNames = os.listdir(direct)
  31. amount = len(fileNames)
  32. #%%
  33. vect = np.zeros(amount)
  34. similarityMatrix = np.zeros((amount, amount))
  35. descripions = np.zeros((amount, 128))
  36. #%%
  37. sp = dlib.shape_predictor('models/shape_predictor_68_face_landmarks.dat')
  38. facerec = dlib.face_recognition_model_v1('models/dlib_face_recognition_resnet_model_v1.dat')
  39. detector = dlib.get_frontal_face_detector()
  40. #%%
  41. with Pool() as p:
  42.     descripions = np.array(p.map(make_description, fileNames))
  43. #%%
  44. for i in range(0, amount - 1):
  45.     for j in range(i + 1, amount):
  46.         similarityMatrix[i, j] = similarityMatrix[j, i] = distance.euclidean(descripions[i], descripions[j])
  47. #%%
  48. print(similarityMatrix)
  49. # %%
  50. answers = []
  51.  
  52.  
  53. def ask_image(i: int) -> bool:
  54.     img = Image.open(f'{direct}/{fileNames[i]}')
  55.     plt.imshow(img)
  56.     plt.show()
  57.     ans = ''
  58.     while ans not in ('+', '-'):
  59.         print("+/-/stop?")
  60.         ans = input()
  61.         if ans == 'stop':
  62.             raise ValueError("Stop")
  63.     answers.append(ans)
  64.     return ans == '+'
  65.  
  66.  
  67. # %%
  68. k = 1
  69.  
  70.  
  71. def run_iter(visited):
  72.     global vect
  73.  
  74.     vc = vect.copy() + 1e-4
  75.     vc[visited] = 1e9
  76.     p = softmax(1 / vc)
  77.     i = np.random.choice(amount, p=p)
  78.     while visited[i]:
  79.         i = np.random.choice(amount, p=p)
  80.     visited[i] = True
  81.     res = ask_image(i)
  82.     if res:
  83.         vect += k * (similarityMatrix[i] ** 2)
  84.     else:
  85.         vect += k / ((similarityMatrix[i] ** 2) + 1e-4)  # To avoid division by zero
  86.  
  87.  
  88. def run_cycle():
  89.     visited = np.zeros(amount, dtype=bool)
  90.     while True:
  91.         try:
  92.             run_iter(visited)
  93.         except ValueError:
  94.             break
  95.  
  96.  
  97. # %%
  98. ans_num = np.array(list(map(lambda x: x == '+', answers)), dtype=int)
  99. # %%
  100. plt.scatter(np.arange(amount), ans_num)
  101. plt.show()
  102. # %%
  103. ans_cumsum = np.cumsum(list(map(lambda x: 1 if x == 1 else -1, ans_num)))
  104. # %%
  105. plt.plot(np.arange(amount), ans_cumsum)
  106. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement