要求:读取数张人脸图像,对面部进行识别,当人脸出现微笑表情时,指示框编程蓝色,如下图所示。
我做的就是应用一下刚学的 Open_CV 中模板匹配的方法,用模板匹配的方法来识别已经处理的图片中,是否微笑。而没有用神经网络来对没有处理的图片识别。
首先截取了现有图片的中的一部分,图片处理如下:
程序如下:
# -*- coding: utf-8 -*-
import cv2
import sys
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img1 = cv2.imread('n_smile.jpg')
img2 = cv2.imread('smile.jpg')
img_normal = cv2.imread('n_smile.jpg', 0)
template_normal = cv2.imread('n_smile_template.png', 0)
h_normal, w_normal = template_normal.shape[:2]
img_smile = cv2.imread('smile.jpg', 0)
template_smile = cv2.imread('smile_template.png', 0)
h_smile, w_smile = template_smile.shape[:2]
pd = True
while pd:
img = input("请输入图片:")
if img == 'q':
pd = False
sys.exit()
img = cv2.imread(img)
if (img == img1).all():
res = cv2.matchTemplate(img_normal, template_normal, cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_right = min_loc
bottom_right = (top_right[0] + w_normal, top_right[1] + h_normal)
cv2.rectangle(img1, top_right, bottom_right, (0, 0, 255), 8)
cv_show('img', img1)
elif (img == img2).all():
res = cv2.matchTemplate(img_smile, template_smile, cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_right = min_loc
bottom_right = (top_right[0] + w_smile, top_right[1] + h_smile)
cv2.rectangle(img2, top_right, bottom_right, (255, 0, 0), 8)
cv_show('img', img2)
微笑时的图片传入,用蓝色的框将表情框住,结果展示:
正常的表情传入时,用红色的框将表情框住,结果展示:
这只是我在学习 Open_CV 中的一个小想法,如果想要预测大量的图片,大家可以写一个list 来存储图片,通过循环来遍历。判断笑脸位置。
对于想要判断任意图片,大家可用神经网络制作数据集,来训练网络。