cv2+faced人脸检测

1. 安装cv2

pip install opencv-python

2. 安装faced

使用pip默认安装的faced版本比较低,建议采用github提供的方法安装,参考 iitzco/faced: 🚀 😏 Near Real Time CPU Face detection using deep learning

pip install git+https://github.com/iitzco/faced.git

PS: 这种方式需要先安装git,如果速度太慢,可以用gitee的镜像

3. 代码

import cv2
from faced import FaceDetector

face_detector = FaceDetector()

def get_face_count(path):
    '''
    检测相片人脸数量
    '''
    img = cv2.imread(path)
    rgb_img = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2RGB)

    # Receives RGB numpy image (HxWxC) and
    # returns (x_center, y_center, width, height, prob) tuples. 
    faces = face_detector.predict(rgb_img, 0.85)
    return len(faces)


if __name__ == '__main__':
    print('face count: %d' % get_face_count('test.jpg'))

4. 错误处理

4.1 vc runtime错误
ImportError: Could not find the DLL(s) 'msvcp140.dll or msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environment variable. You may install these DLLs by downloading "Microsoft C++ Redistributable for Visual Studio 2015, 2017 and 2019" for your platform from this URL: https://support.microsoft.com/help/2977003/the-latest-supported-visual-c-downloads

这个提示的比较明白,缺少vc runtime,按提示安装就可解决。

4.2 tensorflow错误
AttributeError: module 'tensorflow' has no attribute 'Session'. Did you mean: 'version'?

这个问题稍微麻烦,faced引用了tensorflow,装的是tensorflow 2.0版本,但语法是1.0的,导致这个报错。需要修改faced的detector.py文件,路径是python安装目录/Lib/site-packages/faced/detector.py

# 注视掉这一行
# import tensorflow as tf

# 新增这一行
import tensorflow.compat.v1 as tf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值