MTCNN์ ์ฌ์ฉํ๋ฉด ์ด๋ฏธ์ง๋ ์์๋ฐ์ดํฐ์ ์ผ๊ตด ์ธ์์ ๊ฐ๋จํ ์ฝ๋๋ก ๊ตฌํํ ์ ์๋ค.
์ด์ ๊ฒ์๋ฌผ์์๋ ๋์ด/์ฑ๋ณ ์์ธก์ ์ํด ํ๋ จ๋ CAFFE ๋ชจ๋ธ๊ณผ Cascade ๋ฑ์ ์ดํด๋ณด์์ง๋ง,
๋จ์ํ ์ผ๊ตด์ธ์๋ง ์ํด์๋ MTCNN์ด ๋ณด๋ค ์ฌ์ฉํ๊ธฐ ๊ฐํธํ๊ณ , ์ผ๊ตด์ธ์ ์ฑ๋ฅ๋ ๋ฐ์ด๋๋ค.
์ด๋ฒ ์ฝ๋ ๊ตฌํ์์๋ ์์๋ฐ์ดํฐ์์ ์ฌ๋ฌ ์ผ๊ตด์ ์ธ์ํ๋ ๊ฒ ์ฃผ์๋ชฉ์ ์ด์๊ธฐ ๋๋ฌธ์ ๊ธฐ์กด ์ฝ๋์ ๋ฐ๋ณต๋ฌธ์ ์ถ๊ฐํด๋ณด์๋ค.
1. MTCNN ์ค์น ๋ฐ import
# ์ฝ๋ฉ์ด๋ฉด ์ฝ๋ฉ ์ฝ๋์, ์ฃผํผํฐ๋ฉด ์ฃผํผํฐ cmd์ฐฝ์ ์ค์น
pip install mtcnn
import mtcnn
# ์ด๋ฏธ์ง ํ์ธ ๋ฑ์ ์ํด matplotlib๋ ์ค์น๊ฐ ํ์ํ๋ค.
from matplotlib import pyplot
2. ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ก MTCNN ์ผ๊ตด์์ญ ๊ธฐ๋ฅ ํ์ธํ๊ธฐ
# load image from file
pixels = pyplot.imread('/content/sampley.jpeg')
# example of face detection with mtcnn
from matplotlib import pyplot
from PIL import Image
from numpy import asarray
from mtcnn.mtcnn import MTCNN
# extract a single face from a given photograph
def extract_face(filename, required_size=(224, 224)):
# load image from file
pixels = pyplot.imread(filename)
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
results = detector.detect_faces(pixels)
# extract the bounding box from the first face
x1, y1, width, height = results[0]['box']
x2, y2 = x1 + width, y1 + height
# extract the face
face = pixels[y1:y2, x1:x2]
# resize pixels to the model size
image = Image.fromarray(face)
image = image.resize(required_size)
face_array = asarray(image)
return face_array
# load the photo and extract the face
pixels = extract_face('/content/sampley.jpeg')
# plot the extracted face
pyplot.imshow(pixels)
# show the plot
pyplot.show()
์ฝ๋ ๊ฒฐ๊ณผํ๋ฉด
3. MTCNN์ผ๋ก ์ฃผ๋ณ ์์ญ ์์น๊ฐ ํ๋ณดํ๊ธฐ
pixels = pyplot.imread('/content/sampley.jpeg')
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
results = detector.detect_faces(pixels)
print(results)
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
[{'box': [104, 70, 143, 179], 'confidence': 0.9999216794967651, 'keypoints': {'left_eye': (148, 145), 'right_eye': (211, 136), 'nose': (195, 167), 'mouth_left': (172, 210), 'mouth_right': (219, 202)}}]
x1, y1, width, height = results[0]['box']
x2, y2 = x1 + width, y1 + height
# extract the face
face2 = pixels[y1-30:y2+30, x1-30:x2+30]
# resize pixels to the model size
image = Image.fromarray(face2)
image = image.resize((254,254))
face_array = asarray(image)
pyplot.imshow(image)
# show the plot
pyplot.show()
face2 = pixels[y1 -30 :y2+ 30 , x1 -30 :x2+ 30 ]
face2๋ ๋ค์๊ณผ ๊ฐ์ด ํฝ์
์์น๋ฅผ ์กฐ์ ํด์ ์ผ๊ตด์์ญ์ ๋ํ์ฃผ์๋ค.
์ฝ๋ ๊ฒฐ๊ณผํ๋ฉด
4. MTCNN์ผ๋ก ์ฌ๋ฌ ์ผ๊ตด ์ธ์ํ๊ธฐ
(MTCNN์ ๊ธฐ๋ณธ ๋ฐํ๊ฐ์ผ๋ก ์ ์ผ ๋จผ์ ์ธ์ํ ์ผ๊ตด์ญ์ญ ์ ๋ณด๋ฅผ ๋ฐํํ์ง๋ง, for๋ฌธ์ ์จ์ ์ฌ๋ฌ ์ผ๊ตด๋ ํ๋ณด๊ฐ ๊ฐ๋ฅํ๋ค.)
pixels = pyplot.imread('/content/PEOPLE.jpeg')
# create the detector, using default weights
detector = MTCNN()
# detect faces in the image
for results in detector.detect_faces(pixels):
print(results)
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ด ๋์จ๋ค.
{'box': [23, 62, 52, 71], 'confidence': 0.9998407363891602, 'keypoints': {'left_eye': (40, 87), 'right_eye': (64, 92), 'nose': (52, 103), 'mouth_left': (37, 113), 'mouth_right': (57, 117)}} {'box': [106, 118, 53, 69], 'confidence': 0.9998244643211365, 'keypoints': {'left_eye': (115, 145), 'right_eye': (138, 142), 'nose': (123, 158), 'mouth_left': (118, 168), 'mouth_right': (143, 165)}} {'box': [146, 15, 40, 50], 'confidence': 0.9995580315589905, 'keypoints': {'left_eye': (160, 32), 'right_eye': (177, 39), 'nose': (165, 47), 'mouth_left': (152, 48), 'mouth_right': (169, 55)}} {'box': [165, 107, 64, 78], 'confidence': 0.9987497329711914, 'keypoints': {'left_eye': (176, 135), 'right_eye': (203, 144), 'nose': (178, 158), 'mouth_left': (168, 161), 'mouth_right': (198, 170)}} {'box': [76, 73, 39, 53], 'confidence': 0.9984415173530579, 'keypoints': {'left_eye': (85, 93), 'right_eye': (104, 95), 'nose': (93, 107), 'mouth_left': (83, 109), 'mouth_right': (104, 111)}} {'box': [100, 21, 45, 58], 'confidence': 0.9919271469116211, 'keypoints': {'left_eye': (112, 43), 'right_eye': (132, 43), 'nose': (122, 57), 'mouth_left': (110, 62), 'mouth_right': (133, 62)}} {'box': [190, 28, 62, 73], 'confidence': 0.970379650592804, 'keypoints': {'left_eye': (205, 52), 'right_eye': (227, 63), 'nose': (203, 70), 'mouth_left': (191, 79), 'mouth_right': (211, 89)}}
๊ฒฐ๊ณผ๊ฐ์์ box๊ฐ๋ง ์ฌ์ฉํ๊ณ ์ถ์ผ๋ฉด results[ 'box' ]๋ก ์ฌ์ฉํ๋ฉด ๋๋ค.
#๊ฒฐ๊ณผ๋ฅผ ์ด๋ฏธ์ง๋ก ํ์ธํ๊ณ ์ถ์ ๋๋ ๋ค์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
for results in detector.detect_faces(pixels):
x, y, w, h = results['box']
print(results['box'])
cv2.rectangle(pixels, (x,y), (x+w, y+h), (255,255,255), thickness=2)
cv2_imshow(pixels)
๋ค์ค์ผ๊ตด ์ด๋ฏธ์ง๋ฅผ ํฌ์
ํ์ ๋์ ๊ฒฐ๊ณผ
5. ์์์์ ์ผ๊ตด์ธ์ ํ๊ธฐ (์ฝ๋ฉ ํ๊ฒฝ)
#์ฝ๋ฉ์์๋ ๊ฒฐ๊ณผํ๋ฉด์ผ๋ก ๋์์์ด ๋ง๋ค์ด์ง์ง ์์, ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๊ฐ ํ์ํ๋ค.
from google.colab.patches import cv2_imshow
def videoFaceDetector(cam, required_size=(224, 224)): #์์์์ ์ผ๊ตด์ ๊ฒ์ถํ๊ธฐ ์ํ ํจ์ ์ ์
while True:
ret, img = cam.read() #์์ ์บก์ณ
try:
img= cv2.resize(img,dsize=None, fx=1.0,fy=1.0) #์ด๋ฏธ์ง ํฌ๊ธฐ ์กฐ์
except:break
detector=MTCNN() #detector๋ก MTCNN ์ฌ์ฉ
for results in detector.detect_faces(img): #์ผ๊ตด ๋ค์ค ์ธ์์ ์ํ ๋ฐ๋ณต๋ฌธ
x, y, w, h = results['box'] #์ผ๊ตด ์์น๊ฐ
cv2.rectangle(img, (x,y), (x+w, y+h), (255,255,255), thickness=2) #์ด๋ฏธ์ง์ ์ผ๊ตด์์ญ ์์ํ์๋ฅผ ์ํ ์ฝ๋
cv2_imshow(img)#์ฝ๋ฉ์์ ์ด๋ฏธ์ง ํ์ผ์ ๋ณด๊ธฐ(์์์ ํ๋ ์ํ๋์ด ํ์)
if cv2.waitKey(1) > 0: break # while ๋ฃจํ์์ 1msec ๋์์ ์ถ๋ ฅ์ ๋ณด์ฌ์ค
cam = cv2.VideoCapture('/content/sample.mp4') #์์์
๋ก๋
videoFaceDetector(cam) #์คํ (์ฝ๋ฉ์ ํ ํ๋ ์์ฉ ๊ฒฐ๊ณผ๊ฐ ๋๊ธฐ๋ฉด์ ํ์๋๋ค.)