基于 Open_CV 模板匹配的简易微笑识别

要求:读取数张人脸图像,对面部进行识别,当人脸出现微笑表情时,指示框编程蓝色,如下图所示。

我做的就是应用一下刚学的  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 来存储图片,通过循环来遍历。判断笑脸位置。

对于想要判断任意图片,大家可用神经网络制作数据集,来训练网络。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_Mapp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值