[深度學習]人臉檢測-Tensorflow2.x keras程式碼實現

茫茫人海一粒沙發表於2020-10-22

1. 所需Python環境

pip install opencv-python
pip install mtcnn

import cv2
import mtcnn
print(mtcnn.__version__)
print(cv2.__version__)
0.1.0
4.4.0

2. 資料準備

下載下面圖片並且儲存為test1.jpg放在你的專案目錄裡
在這裡插入圖片描述

下載下面圖片並且儲存為test2.jpg放在你的專案目錄裡在這裡插入圖片描述

3. 程式碼實現

3.1 用OpenCV實現

OpenCV提供了許多預訓練的模型。 我們就用其中一個,在OpenCV GitHub專案上也可用

從OpenCV GitHub專案下載用於正面人臉檢測的預訓練模型,並將其放在檔名“ haarcascade_frontalface_default.xml”的當前工作目錄中。
下載haarcascade_frontalface_default.xml

執行下面程式碼

# plot photo with detected faces using opencv cascade classifier
from cv2 import imread
from cv2 import imshow
from cv2 import waitKey
from cv2 import destroyAllWindows
from cv2 import CascadeClassifier
from cv2 import rectangle
# load the photograph
pixels = imread('test1.jpg')
# load the pre-trained model
classifier = CascadeClassifier('haarcascade_frontalface_default.xml')
# perform face detection
bboxes = classifier.detectMultiScale(pixels)
# print bounding box for each detected face
for box in bboxes:
	# extract
	x, y, width, height = box
	x2, y2 = x + width, y + height
	# draw a rectangle over the pixels
	rectangle(pixels, (x, y), (x2, y2), (0,0,255), 1)
# show the image
imshow('face detection', pixels)
# keep the window open until we press a key
waitKey(0)
# close the window
destroyAllWindows()

在這裡插入圖片描述

修改為下面程式碼,載入test2.jpg,

pixels = imread('test2.jpg')

大部分都檢測到了,但是仔細看發現第二排第一個沒有檢測到,還有的是雙框
在這裡插入圖片描述
detectMultiScale()函式提供了一些引數來幫助調整分類器的用法。

bboxes = classifier.detectMultiScale(pixels, 1.05, 8)

經過微調,結果如下,其實還是不好,你們可以試一試。
在這裡插入圖片描述

3.2 用深度學習實現目標檢測

# face detection with mtcnn on a photograph
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from mtcnn.mtcnn import MTCNN

# draw an image with detected objects
def draw_image_with_boxes(filename, result_list):
	# load the image
	data = pyplot.imread(filename)
	# plot the image
	pyplot.imshow(data)
	# get the context for drawing boxes
	ax = pyplot.gca()
	# plot each box
	for result in result_list:
		# get coordinates
		x, y, width, height = result['box']
		# create the shape
		rect = Rectangle((x, y), width, height, fill=False, color='red')
		# draw the box
		ax.add_patch(rect)
	# show the plot
	pyplot.show()

filename = 'test1.jpg'
# load image from file
pixels = pyplot.imread(filename)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
faces = detector.detect_faces(pixels)
# display faces on the original image
draw_image_with_boxes(filename, faces)

在這裡插入圖片描述
換成test2.jpg, 執行結果如下: 可以看出效果不錯哦。

filename = 'test2.jpg'

在這裡插入圖片描述
我們可以通過Circle類為眼睛,鼻子和嘴巴畫一個圓。 例如

# draw the dots
for key, value in result['keypoints'].items():
	# create and draw dot
	dot = Circle(value, radius=2, color='red')
	ax.add_patch(dot)

執行下面程式碼

# face detection with mtcnn on a photograph
from matplotlib import pyplot
from matplotlib.patches import Rectangle
from matplotlib.patches import Circle
from mtcnn.mtcnn import MTCNN

# draw an image with detected objects
def draw_image_with_boxes(filename, result_list):
	# load the image
	data = pyplot.imread(filename)
	# plot the image
	pyplot.imshow(data)
	# get the context for drawing boxes
	ax = pyplot.gca()
	# plot each box
	for result in result_list:
		# get coordinates
		x, y, width, height = result['box']
		# create the shape
		rect = Rectangle((x, y), width, height, fill=False, color='red')
		# draw the box
		ax.add_patch(rect)
		# draw the dots
		for key, value in result['keypoints'].items():
			# create and draw dot
			dot = Circle(value, radius=2, color='red')
			ax.add_patch(dot)
	# show the plot
	pyplot.show()

filename = 'test1.jpg'
# load image from file
pixels = pyplot.imread(filename)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
faces = detector.detect_faces(pixels)
# display faces on the original image
draw_image_with_boxes(filename, faces)

在這裡插入圖片描述
換成test2.jpg, 執行結果如下: 可以看出效果不錯哦。

在這裡插入圖片描述

相關文章