新网创想网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

怎么在Python中使用opencv实现人脸检测功能-创新互联

怎么在Python中使用opencv 实现人脸检测功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

创新互联公司2013年至今,先为彭州等服务建站,彭州等地企业,进行企业商务咨询服务。为彭州企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

导入库,并做命令行参数处理。你在命令行可以输入如下:

python detect_faces.py --image image/family.jpg  --detector haarcascade_frontalface_default.xml

我在程序中都有缺省参数处理,你如果集成测试或命令行不输参数的话,就要修改好你的缺省值。

这样命令行就是python detect_faces.py ,同时也可以输入命令行输入参数。

# USAGE 使用方法是:
# python detect_faces.py --image images/family.jpg \
# --detector haarcascade_frontalface_default.xml
# import the necessary packages 输入包
# import imutils 
import argparse
import cv2
# construct the argument parser and parse the arguments //构造命令行参数分析
# 为了集成测试,或者命令行输入的简单,这里都有缺省参数
#image 是 images/family.jpg
#detector 是 haarcascade_frontalface_default.xml
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='images/family.jpg',
 help="path to the input image")
ap.add_argument("-d", "--detector", default='haarcascade_frontalface_default.xml',
 help="path to Haar cacscade face detector")
args = vars(ap.parse_args())
 导入图形文件,并灰度处理
# load our image and convert it to grayscale 导入图形文件,并灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,width=800)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
导入检测文件,检测图中人脸,显示检测到的人脸数
# load the face detector and detect faces in the image
# 导入脸部检测文件
detector = cv2.CascadeClassifier(args["detector"])
#检测图形中的脸部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#显示检测到的人脸数目
print("[INFO] detected {} faces".format(len(rects)))
 循环,绘图每个检测到的人脸框,并图形显示
# load the face detector and detect faces in the image
# 导入脸部检测
detector = cv2.CascadeClassifier(args["detector"])
#检测图形中的脸部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#显示检测到的人脸数目
print("[INFO] detected {} faces".format(len(rects)))

最后串接所有代码如下:

# USAGE 使用方法是:
# python detect_faces.py --image images/family.jpg \
# --detector haarcascade_frontalface_default.xml
# import the necessary packages 输入包
# import imutils 如果需要成比例缩放图形才需要,这里不需要
import argparse
import cv2
# construct the argument parser and parse the arguments //构造命令行参数分析
# 为了集成测试,或者命令行输入的简单,这里都有缺省参数
#image 是 images/family.jpg
#detector 是 haarcascade_frontalface_default.xml
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", default='images/family.jpg',
 help="path to the input image")
ap.add_argument("-d", "--detector", default='haarcascade_frontalface_default.xml',
 help="path to Haar cacscade face detector")
args = vars(ap.parse_args())
# load our image and convert it to grayscale 导入图形文件,并灰度化
image = cv2.imread(args["image"])
#image =imutils.resize(image,width=800)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# load the face detector and detect faces in the image
# 导入脸部检测文件
detector = cv2.CascadeClassifier(args["detector"])
#检测图形中的脸部
rects = detector.detectMultiScale(gray, scaleFactor=1.05, minNeighbors=9,
 minSize=(40, 40), flags=cv2.CASCADE_SCALE_IMAGE)
#显示检测到的人脸数目
print("[INFO] detected {} faces".format(len(rects)))
# loop over the bounding boxes and draw a rectangle around each face
# 循环rects,绘图每个检测到的人脸框
for (x, y, w, h) in rects:
 cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# show the detected faces
cv2.imshow("Faces", image)
cv2.waitKey(0)

看完上述内容,你们掌握怎么在Python中使用opencv 实现人脸检测功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联成都网站设计公司行业资讯频道,感谢各位的阅读!

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


标题名称:怎么在Python中使用opencv实现人脸检测功能-创新互联
标题网址:http://www.wjwzjz.com/article/hiipp.html
在线咨询
服务热线
服务热线:028-86922220
TOP