You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vision.py 5.1 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from matplotlib.patches import Circle
  2. def vis_two(im_array, dets1, dets2, thresh=0.9):
  3. """Visualize detection results before and after calibration
  4. Parameters:
  5. ----------
  6. im_array: numpy.ndarray, shape(1, c, h, w)
  7. test image in rgb
  8. dets1: numpy.ndarray([[x1 y1 x2 y2 score]])
  9. detection results before calibration
  10. dets2: numpy.ndarray([[x1 y1 x2 y2 score]])
  11. detection results after calibration
  12. thresh: float
  13. boxes with scores > thresh will be drawn in red otherwise yellow
  14. Returns:
  15. -------
  16. """
  17. import matplotlib.pyplot as plt
  18. import random
  19. figure = plt.figure()
  20. plt.subplot(121)
  21. plt.imshow(im_array)
  22. color = 'yellow'
  23. for i in range(dets1.shape[0]):
  24. bbox = dets1[i, :4]
  25. landmarks = dets1[i, 5:]
  26. score = dets1[i, 4]
  27. if score > thresh:
  28. rect = plt.Rectangle((bbox[0], bbox[1]),
  29. bbox[2] - bbox[0],
  30. bbox[3] - bbox[1], fill=False,
  31. edgecolor='red', linewidth=0.7)
  32. plt.gca().add_patch(rect)
  33. landmarks = landmarks.reshape((5,2))
  34. for j in range(5):
  35. plt.scatter(landmarks[j,0],landmarks[j,1],c='yellow',linewidths=0.1, marker='x', s=5)
  36. # plt.gca().text(bbox[0], bbox[1] - 2,
  37. # '{:.3f}'.format(score),
  38. # bbox=dict(facecolor='blue', alpha=0.5), fontsize=12, color='white')
  39. # else:
  40. # rect = plt.Rectangle((bbox[0], bbox[1]),
  41. # bbox[2] - bbox[0],
  42. # bbox[3] - bbox[1], fill=False,
  43. # edgecolor=color, linewidth=0.5)
  44. # plt.gca().add_patch(rect)
  45. plt.subplot(122)
  46. plt.imshow(im_array)
  47. color = 'yellow'
  48. for i in range(dets2.shape[0]):
  49. bbox = dets2[i, :4]
  50. landmarks = dets1[i, 5:]
  51. score = dets2[i, 4]
  52. if score > thresh:
  53. rect = plt.Rectangle((bbox[0], bbox[1]),
  54. bbox[2] - bbox[0],
  55. bbox[3] - bbox[1], fill=False,
  56. edgecolor='red', linewidth=0.7)
  57. plt.gca().add_patch(rect)
  58. landmarks = landmarks.reshape((5, 2))
  59. for j in range(5):
  60. plt.scatter(landmarks[j, 0], landmarks[j, 1], c='yellow',linewidths=0.1, marker='x', s=5)
  61. # plt.gca().text(bbox[0], bbox[1] - 2,
  62. # '{:.3f}'.format(score),
  63. # bbox=dict(facecolor='blue', alpha=0.5), fontsize=12, color='white')
  64. # else:
  65. # rect = plt.Rectangle((bbox[0], bbox[1]),
  66. # bbox[2] - bbox[0],
  67. # bbox[3] - bbox[1], fill=False,
  68. # edgecolor=color, linewidth=0.5)
  69. # plt.gca().add_patch(rect)
  70. plt.show()
  71. def vis_face(im_array, dets, landmarks=None):
  72. """Visualize detection results before and after calibration
  73. Parameters:
  74. ----------
  75. im_array: numpy.ndarray, shape(1, c, h, w)
  76. test image in rgb
  77. dets1: numpy.ndarray([[x1 y1 x2 y2 score]])
  78. detection results before calibration
  79. dets2: numpy.ndarray([[x1 y1 x2 y2 score]])
  80. detection results after calibration
  81. thresh: float
  82. boxes with scores > thresh will be drawn in red otherwise yellow
  83. Returns:
  84. -------
  85. """
  86. import matplotlib.pyplot as plt
  87. import random
  88. import pylab
  89. figure = pylab.figure()
  90. # plt.subplot(121)
  91. pylab.imshow(im_array)
  92. figure.suptitle('DFace Detector', fontsize=20)
  93. for i in range(dets.shape[0]):
  94. bbox = dets[i, :4]
  95. rect = pylab.Rectangle((bbox[0], bbox[1]),
  96. bbox[2] - bbox[0],
  97. bbox[3] - bbox[1], fill=False,
  98. edgecolor='yellow', linewidth=0.9)
  99. pylab.gca().add_patch(rect)
  100. if landmarks is not None:
  101. for i in range(landmarks.shape[0]):
  102. landmarks_one = landmarks[i, :]
  103. landmarks_one = landmarks_one.reshape((5, 2))
  104. for j in range(5):
  105. # pylab.scatter(landmarks_one[j, 0], landmarks_one[j, 1], c='yellow', linewidths=0.1, marker='x', s=5)
  106. cir1 = Circle(xy=(landmarks_one[j, 0], landmarks_one[j, 1]), radius=2, alpha=0.4, color="red")
  107. pylab.gca().add_patch(cir1)
  108. # plt.gca().text(bbox[0], bbox[1] - 2,
  109. # '{:.3f}'.format(score),
  110. # bbox=dict(facecolor='blue', alpha=0.5), fontsize=12, color='white')
  111. # else:
  112. # rect = plt.Rectangle((bbox[0], bbox[1]),
  113. # bbox[2] - bbox[0],
  114. # bbox[3] - bbox[1], fill=False,
  115. # edgecolor=color, linewidth=0.5)
  116. # plt.gca().add_patch(rect)
  117. pylab.show()

开源的深度学习人脸检测和人脸识别系统。所有功能都采用 pytorch 框架开发。pytorch是一个由facebook开发的深度学习框架,它包含了一些比较有趣的高级特性,例如自动求导,动态构图等。DFace天然的继承了这些优点,使得它的训练过程可以更加简单方便,并且实现的代码可以更加清晰易懂

Contributors (1)