本文主要介绍如何基于openCV来实现一个简单的猫脸识别应用。
一、基础准备
首先需要安装openCV,因为python版的方便点,所以直接安装python版openCV,通过命令sudo apt-get install python-opencv 即可完成。
linaro@linaro-alip:~$ sudo apt-get install python-opencv
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
liba52-0.7.4 libdca0 libdrm-freedreno1 libdrm-tegra0
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
python-numpy python-pkg-resources
Suggested packages:
gfortran python-dev python-pytest python-numpy-dbg python-numpy-doc python-setuptools
The following NEW packages will be installed:
python-numpy python-opencv python-pkg-resources
0 upgraded, 3 newly installed, 0 to remove and 48 not upgraded.
Need to get 2,636 kB of archives.
After this operation, 14.7 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Get:1 http://mirrors.ustc.edu.cn/debian buster/main arm64 python-pkg-resources all 40.8.0-1 [182 kB]
Get:2 http://mirrors.ustc.edu.cn/debian buster/main arm64 python-numpy arm64 1:1.16.2-1 [1,975 kB]
Get:3 http://mirrors.ustc.edu.cn/debian buster/main arm64 python-opencv arm64 3.2.0+dfsg-6 [478 kB]
Fetched 2,636 kB in 0s (5,938 kB/s)
Selecting previously unselected package python-pkg-resources.
(Reading database ... 76876 files and directories currently installed.)
Preparing to unpack .../python-pkg-resources_40.8.0-1_all.deb ...
Unpacking python-pkg-resources (40.8.0-1) ...
Selecting previously unselected package python-numpy.
Preparing to unpack .../python-numpy_1%3a1.16.2-1_arm64.deb ...
Unpacking python-numpy (1:1.16.2-1) ...
Selecting previously unselected package python-opencv.
Preparing to unpack .../python-opencv_3.2.0+dfsg-6_arm64.deb ...
Unpacking python-opencv (3.2.0+dfsg-6) ...
Setting up python-pkg-resources (40.8.0-1) ...
Setting up python-numpy (1:1.16.2-1) ...
Setting up python-opencv (3.2.0+dfsg-6) ...
安装完成后检查下,import cv2 没问题就OK了。
linaro@linaro-alip:~$ python
Python 2.7.16 (default, Sep 20 2023, 07:59:17)
[GCC 8.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> exit()
二、设计和实现
在OpenCV中,目标检测使用的函数是cv2.CascadeClassifier.detectMultiScale(),它可以检测图像中所有的目标。其完整定义如下:
def detectMultiScale(self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None):
image:待检测的图像,通常为灰度图像
scaleFactor:表示在前后两次相继的扫描中,搜索窗口的缩放比例
minNeighbors:表示构成检测目标的相邻矩形的最小个数。默认值为3,表示有3个以上的检测标记存在时,才认为目标的存在。如果希望提高检测的准确率,可以将该值设置的更大,但同时可能会让一些目标无法被检测到
flags:不常用参数,一般省略。
minSize:目标的最小尺寸,小于这个尺寸的目标将被忽略
maxSize:目标的最大尺寸,大于这个尺寸的目标将被忽略
该函数的返回值是目标对象的矩形框向量组。
OpenCV已经自带了猫脸的Haar特征分类器,本文选择haarcascade_frontalcatface.xml识别分类器。
通过detectMultiScale函数返回的是猫脸的矩形框向量组,包括左上角坐标(x,y),长宽(w,h)。而绘制猫脸矩形框则通过rectangle函数实现。
具体实现python代码如下:
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalcatface.xml')
img = cv2.imread("./cats.jpg")
# convert to gray
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_rect = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=3)
print("number of cats: ", len(face_rect))
for (x,y,w,h) in face_rect:
cv2.rectangle(img, (x,y), (x+w, y+h), color=(0,255,0))
cv2.imwrite("cats-1.jpg", img)
cv2.release()
三、运行
测试的图片是网上随便下载的。
将图片、猫脸识别分类器还有python上传至幸狐 Core3566 模组,并运行python脚本。
只识别出2只猫咪,看来有一只的脸不合格,要想识别出来,需要在detectMultiScale中调试参数,进一步提示识别精准度,看了看时间,都快物业了,这里就不继续了,来个识别的结果。
看来这个黑不溜秋的不好识别。
|
评论