【python】MAC安装openCV人脸识别
一、OpenCV
1. 安裝openCV
pip install opencv-python
但因為openCV是外網(wǎng)的庫,安裝可能回很慢,建議從【清華鏡像園】進(jìn)行安裝
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
2. 找到文件分類器
MAC系統(tǒng)方法:在【訪達(dá)】中搜索cv2,即可找到
import cv2 import os import matplotlib.pyplot as pltos.chdir(‘d:\FaceRuyi’)#路徑選擇cv2/data的文件夾def detect(filename): face_cascade = cv2.CascadeClassifier(‘D:/FaceRuyi/data/haarcascade_frontalface_default.xml’)#找到default的文件復(fù)制路徑img = cv2.imread(filename) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)faces = face_cascade.detectMultiScale(gray, 1.3, 5)for (x, y, w, h) in faces:img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2) plt.imshow(img) plt.axis(‘off’) #去掉坐標(biāo)軸 plt.show()?但如果只是按以上這種寫法,就會報錯,因為在之前定義的detect函數(shù)沒有被調(diào)用
正確寫法如下??
import cv2 import os import matplotlib.pyplot as plt os.chdir('/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/data')def detect(filename):face_cascade=cv2.CascadeClassifier('/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/cv2/data/haarcascade_frontalface_default.xml')img= cv2.imread(filename)gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)faces=face_cascade.detectMultiScale(gray, 1.3, 5)for(x,y,w,h)in faces:img=cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)plt.imshow(img)plt.axis('off')plt.show() detect('/Users/liruiying/Desktop/上課/大三上/python/第二節(jié)/taylor.jpg')?def 下面的代碼記得tab
二、Face-recognition
1. 安裝
第一:pip install cmake?
第二:pip install dlib(人臉特征點(diǎn))?
第三:pip install face_recognition
以上安裝都可以通過【清華鏡像園】 安裝
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple xxxx(需要安裝的包)
2. 框線識別
import face_recognition import cv2 import matplotlib.pyplot as pltimage = face_recognition.load_image_file("/Users/liruiying/Desktop/上課/大三上/python/第二節(jié)/posterscope.JPG") face_locations=face_recognition.face_locations(image)face_num2=len(face_locations) print(face_num2) # The number of faces org = cv2.imread("/Users/liruiying/Desktop/上課/大三上/python/第二節(jié)/posterscope.JPG")for i in range(0,face_num2):top = face_locations[i][0]right = face_locations[i][1]bottom = face_locations[i][2]left = face_locations[i][3]#設(shè)置四個點(diǎn)的位置start = (left, top)end = (right, bottom)#進(jìn)行劃線color = (0,255,255)thickness = 2img=cv2.rectangle(org, start, end, color, thickness)plt.imshow(img)plt.axis('off') #去掉坐標(biāo)軸 plt.show()有可能的報錯:找不到face_recognition的包
遇到這種情況,可以嘗試在notebook里重新pip
3. 點(diǎn)狀識別
import cv2 import dlib import matplotlib.pyplot as pltpath = "/Users/liruiying/Desktop/上課/大三上/python/第二節(jié)/posterscope.JPG" img = cv2.imread(path) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#人臉分類器 detector = dlib.get_frontal_face_detector() # 獲取人臉檢測器 #predictor = dlib.shape_predictor(r’/opt/anaconda3/lib/python3.8/site-packages/face_recognition_models/models/shape_predictor_68_face_landmarks.dat‘) predictor = dlib.shape_predictor(r"/opt/anaconda3/lib/python3.8/site-packages/face_recognition_models/models/shape_predictor_68_face_landmarks.dat")dets = detector(gray, 3) for face in dets:shape = predictor(img, face) # 尋找人臉的68個標(biāo)定點(diǎn)# 遍歷所有點(diǎn),打印出其坐標(biāo),并圈出來for pt in shape.parts():pt_pos = (pt.x, pt.y)img=cv2.circle(img, pt_pos, 2, (0, 255, 0), 10)#如果點(diǎn)看不到,可以更改最后的數(shù)值,調(diào)節(jié)點(diǎn)的大小plt.imshow(img) plt.axis('off') #去掉坐標(biāo)軸 plt.show()1. 點(diǎn)位數(shù)量少?可以選擇68的文件
predictor = dlib.shape_predictor(r"/opt/anaconda3/lib/python3.8/site-packages/face_recognition_models/models/shape_predictor_68_face_landmarks.dat")
2. 如果點(diǎn)還是特別不明顯,可以進(jìn)行參數(shù)的調(diào)整
在倒數(shù)第四行img=cv2.circle(img, pt_pos, 2, (0, 255, 0), 10)
最后一個數(shù)值即點(diǎn)的大小~
總結(jié)
以上是生活随笔為你收集整理的【python】MAC安装openCV人脸识别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信支付退款 升级版 【码云gvp 】
- 下一篇: java教程——电商秒杀系统介绍