[PYTHON] I tried face detection with MTCNN

In machine learning for face detection, I used only OpenCV, including model creation, so I decided to use others as well. So I tried using MTCNN.

MTCNN seems to use tensorflow behind the scenes. It feels good to be in deep running with one foot.

Preparation

pip3 install cv2
pip3 install matplotlib
pip3 install mtcnn
pip3 install tensorflow

Image used in the test

It's just right for verification, isn't it? Various exposed faces. There are many things other than the face

test.png

Face detection with MTCNN

verification code

# 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
import cv2

# 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 = 'test.png'
# load image from file
pixels = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
# 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)

result

Everything is detected correctly. It seems that this image has not been falsely detected.

result_1.png

By the way, here is the one using OpenCV (haarcascade_frontalface_default). I can detect all the faces, but they are erroneously detected. Ice cream is really a face w

result_cv.png

Here's what I made before because I wasn't satisfied with the default model. False detections are no longer detected, but one person cannot be detected.

Reference) A story about a gopher touching OpenCV https://speakerdeck.com/usk81/introduction-about-opencv-for-gophers

mine.png

MTCNN ... Completely defeated ... It took about 40 hours _| ̄|○ il||li

I also tried cutting out

I tried to save only one that can be cut out the largest.

code

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

# draw each face separately
def save_biggest_face_image(filename, result_list):
	# load the image
	# data = pyplot.imread(filename)
	data = cv2.imread(filename)
	# plot each face as a subplot
	maxlen = -1
	for i in range(len(result_list)):
		# get coordinates
		x1, y1, width, height = result_list[i]['box']
		if width > height:
			length = width 
		else:
			length = height
		x2, y2 = x1 + length, y1 + length
		# # define subplot
		# pyplot.subplot(1, len(result_list), i+1)
		# pyplot.axis('off')
		# # plot face
		# pyplot.imshow(data[y1:y2, x1:x2])
		d = data[y1:y2, x1:x2]
		if length > 100 and length > maxlen:
			maxlen = length
			md = d
		# cv2.imwrite('/Users/komatsu/Desktop/'+str(i)+'.png', d)

	# show the plot
	# pyplot.show()
	if maxlen > 0:
		cv2.imwrite('result.png', md)
 
filename = 'test.png'
# load image from file
pixels = cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2RGB)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
faces = detector.detect_faces(pixels)
# save face image
save_biggest_face_image(filename, faces)

result

result_2.png

Remarks

The mystery is that the image file could not be read by pyplot.imread. I can use it in a function ...

Recommended Posts

I tried face detection with MTCNN
I tried face recognition with OpenCV
I tried fp-growth with python
I tried scraping with Python
I tried Learning-to-Rank with Elasticsearch!
I tried face recognition using Face ++
I tried clustering with PyCaret
Face detection with Python + OpenCV
I tried gRPC with Python
Face detection with Haar Cascades
I tried scraping with python
Anime face detection with OpenCV
Face detection with YOLO Face (Windows10, Python3.6)
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
Face detection with Lambda (Python) + Rekognition
I tried machine learning with liblinear
I tried web scraping with python.
I tried moving food with SinGAN
I tried implementing DeepPose with PyTorch
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
Hello World and face detection with opencv-python 4.2
I tried sending an SMS with Twilio
I tried linebot with flask (anaconda) + heroku
I tried to visualize AutoEncoder with TensorFlow
I tried to get started with Hy
I tried factor analysis with Titanic data!
I tried learning with Kaggle's Titanic (kaggle②)
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried batch normalization with PyTorch (+ note)
I tried implementing DeepPose with PyTorch PartⅡ
I tried to implement CVAE with PyTorch
I tried playing with the image with Pillow
I tried to solve TSP with QAOA
I tried simple image recognition with Jupyter
I tried CNN fine tuning with Resnet
I tried natural language processing with transformers.
I tried 3D detection of a car
#I tried something like Vlookup with Python # 2
I tried object detection with YOLO v3 (TensorFlow 2.0) on a windows CPU!
I tried scraping
I tried PyQ
I tried AutoKeras
I tried papermill
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried handwriting recognition of runes with scikit-learn
I tried to predict next year with AI
Hello World and face detection with OpenCV 4.3 + Python
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
I tried to use lightGBM, xgboost with Boruta
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried to learn logical operations with TF Learn