1.OpenCV人脸识别
首先安装opencv,达到定位多个人脸的目的。
在安装过程中遇到的问题是,电脑里已经装好了opencv,但在jupyter里引用会出现这样的报错。
ERROR: Could not find a version that satisfies the requirement opencv (from versions: none) ERROR: No matching distribution found for opencv
ModuleNotFoundError Traceback (most recent call last)in ----> 1 import cv2 ModuleNotFoundError: No module named 'cv2'
错误原因是:系统python环境与jupyter中环境不一样
解决办法是用这行代码查一下jupyter中python的环境版本
import sys print(sys.version) pip install ipykernel
我的得出版本为3.8
按照这一版本安装opencv的包,就可以在jupyter环境中成功引用cv2
安装好之后使用的代码为
import cv2
import os
import matplotlib.pyplot as plt
os.chdir("C:\Users\86159\Desktop\python")
def detect(filename):
face_cascade = cv2.CascadeClassifier("E:\Anaconda\Lib\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("one.jpg")
注意:文件的命名尽量用英文,数字和汉字容易报出奇怪的错误。
2.Dlib face-recognition人脸识别
先安装dlib
pip install cmake pip install boost
再按照刚才查的jupyter内python版本,下载相应版本dlib安装包,用这行代码在jupyter安装
pip install D:下载dlib-19.19.0-cp38-cp38-win_amd64.whl
再安装face recognition
pip install face_recognition
使用代码如下:
import face_recognition
import cv2
import matplotlib.pyplot as plt
image = face_recognition.load_image_file(r"C:Users86159Desktoppython1.jpg")
face_locations=face_recognition.face_locations(image)
face_num2=len(face_locations)
print(face_num2) # The number of faces
org = cv2.imread(r"C:Users86159Desktoppython1.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]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
img=cv2.rectangle(org, start, end, color, thickness)
plt.imshow(img)
plt.axis('off') #去掉坐标轴
plt.show()



