本文主要介紹如何基于openCV來實(shí)現(xiàn)一個簡單的貓臉識別應(yīng)用。
一、基礎(chǔ)準(zhǔn)備
首先需要安裝openCV,因為python版的方便點(diǎn),所以直接安裝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()
二、設(shè)計和實(shí)現(xiàn)
在OpenCV中,目標(biāo)檢測使用的函數(shù)是cv2.CascadeClassifier.detectMultiScale(),它可以檢測圖像中所有的目標(biāo)。其完整定義如下:
def detectMultiScale(self, image, scaleFactor=None, minNeighbors=None, flags=None, minSize=None, maxSize=None):
image:待檢測的圖像,通常為灰度圖像
scaleFactor:表示在前后兩次相繼的掃描中,搜索窗口的縮放比例
minNeighbors:表示構(gòu)成檢測目標(biāo)的相鄰矩形的最小個數(shù)。默認(rèn)值為3,表示有3個以上的檢測標(biāo)記存在時,才認(rèn)為目標(biāo)的存在。如果希望提高檢測的準(zhǔn)確率,可以將該值設(shè)置的更大,但同時可能會讓一些目標(biāo)無法被檢測到
flags:不常用參數(shù),一般省略。
minSize:目標(biāo)的最小尺寸,小于這個尺寸的目標(biāo)將被忽略
maxSize:目標(biāo)的最大尺寸,大于這個尺寸的目標(biāo)將被忽略
該函數(shù)的返回值是目標(biāo)對象的矩形框向量組。
OpenCV已經(jīng)自帶了貓臉的Haar特征分類器,本文選擇haarcascade_frontalcatface.xml識別分類器。
通過detectMultiScale函數(shù)返回的是貓臉的矩形框向量組,包括左上角坐標(biāo)(x,y),長寬(w,h)。而繪制貓臉矩形框則通過rectangle函數(shù)實(shí)現(xiàn)。
具體實(shí)現(xiàn)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()
三、運(yùn)行
測試的圖片是網(wǎng)上隨便下載的。
將圖片、貓臉識別分類器還有python上傳至幸狐 Core3566 模組,并運(yùn)行python腳本。
只識別出2只貓咪,看來有一只的臉不合格,要想識別出來,需要在detectMultiScale中調(diào)試參數(shù),進(jìn)一步提示識別精準(zhǔn)度,看了看時間,都快物業(yè)了,這里就不繼續(xù)了,來個識別的結(jié)果。
看來這個黑不溜秋的不好識別。
|
評論