mtcnn.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # MIT License
  2. #
  3. # Copyright (c) 2016 David Sandberg
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in all
  13. # copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import absolute_import
  23. from __future__ import division
  24. from __future__ import print_function
  25. import tensorflow as tf
  26. import align.detect_face
  27. from scipy import misc
  28. with tf.Graph().as_default():
  29. sess = tf.Session()
  30. with sess.as_default():
  31. with tf.variable_scope('pnet'):
  32. data = tf.placeholder(tf.float32, (None,None,None,3), 'input')
  33. pnet = align.detect_face.PNet({'data':data})
  34. pnet.load('../../data/det1.npy', sess)
  35. with tf.variable_scope('rnet'):
  36. data = tf.placeholder(tf.float32, (None,24,24,3), 'input')
  37. rnet = align.detect_face.RNet({'data':data})
  38. rnet.load('../../data/det2.npy', sess)
  39. with tf.variable_scope('onet'):
  40. data = tf.placeholder(tf.float32, (None,48,48,3), 'input')
  41. onet = align.detect_face.ONet({'data':data})
  42. onet.load('../../data/det3.npy', sess)
  43. pnet_fun = lambda img : sess.run(('pnet/conv4-2/BiasAdd:0', 'pnet/prob1:0'), feed_dict={'pnet/input:0':img})
  44. rnet_fun = lambda img : sess.run(('rnet/conv5-2/conv5-2:0', 'rnet/prob1:0'), feed_dict={'rnet/input:0':img})
  45. onet_fun = lambda img : sess.run(('onet/conv6-2/conv6-2:0', 'onet/conv6-3/conv6-3:0', 'onet/prob1:0'), feed_dict={'onet/input:0':img})
  46. minsize = 20 # minimum size of face
  47. threshold = [ 0.6, 0.7, 0.7 ] # three steps's threshold
  48. factor = 0.709 # scale factor
  49. source_path = '/home/david/datasets/casia/CASIA-maxpy-clean/0000045/002.jpg'
  50. img = misc.imread(source_path)
  51. bounding_boxes, points = align.detect_face.detect_face(img, minsize, pnet_fun, rnet_fun, onet_fun, threshold, factor)
  52. print('Bounding box: %s' % bounding_boxes)