01-人臉識別-基於MTCNN,框選人臉區域-detect_face_main


(本系列隨筆持續更新)

搭建要求

詳細的搭建過程在 參考資料1 中已經有啦。

TensorFlow 1.6.0

OpenCV 2.4.8 僅僅是加載和讀取圖片的需要

Ubuntu 14.04 64bits

 

代碼:加載圖片,找出人臉。運行依賴detect_face.py

import cv2
import detect_face
import matplotlib.pyplot as plt
import math
from scipy import misc


img = misc.imread('face_airplane.jpg')

sess = tf.Session()
pnet, rnet, onet = detect_face.create_mtcnn(sess, None)

minsize = 20
threshold = [0.6, 0.7, 0.7]
factor = 0.709
df_result, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)

n_face = df_result.shape[0]
print('detected face number: {}'.format(n_face))
print(df_result)


fig = plt.figure('faces')
i = 0
plt_nrow = n_face/5
plt_nrow = plt_nrow + int(n_face != plt_nrow*5)

plt_ncol = 5
crop_face = []
for subfaceRec in df_result:
  i = i+1
  
  #ax = fig.subplot(plt_nrow, plt_ncol, i)
  subfaceRec = subfaceRec.astype(int)
  img_a_face = img[subfaceRec[1]:subfaceRec[3], subfaceRec[0]:subfaceRec[2]]
  crop_face.append(img_a_face)
  img_a_face = cv2.resize(img_a_face, (96, 96), interpolation = cv2.INTER_CUBIC)
  #plt.imshow(img_a_face)
  print('plt_nrow:{}, plt_ncol:{}, i:{}'.format(plt_nrow, plt_ncol, i))
  plt.subplot(plt_nrow, plt_ncol, i)
  plt.imshow(img_a_face)
  
  cv2.rectangle(img, (subfaceRec[0], subfaceRec[1]), (subfaceRec[2], subfaceRec[3]), (0,255,0), 2)
  
plt.figure('img')
plt.imshow(img)
plt.show()

sess.close()

  

顯示效果:

 

參考資料:

1 https://blog.csdn.net/mr_evanchen/article/details/77650883

2 https://github.com/ShyBigBoy/face-detection-mtcnn

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM