西安哪有做网站的,汝南企业网站建设,郑州制作网站推荐,appstar官网关注置顶“算法猿的成长”每日8:30#xff0c;干货速递#xff01;2019 年第 73 篇文章#xff0c;总第 97 篇文章今天介绍一个简单、易用的开源计算机视觉库#xff0c;名字是 cvlib#xff0c;其 Github 地址#xff1a;https://github.com/arunponnusamy/cvlib官… 关注置顶“算法猿的成长”每日8:30干货速递2019 年第 73 篇文章总第 97 篇文章今天介绍一个简单、易用的开源计算机视觉库名字是 cvlib其 Github 地址https://github.com/arunponnusamy/cvlib官方文档地址http://cvlib.net/安装cvlib 这个库首先需要安装这两个库tensorflowopencv快速的安装方法是pip install opencv-python tensorflow
当然上述安装的 tensorflow 是 cpu 版本如果希望安装可以使用 gpu 的安装包名字是 tensorflow-gpu 安装 gpu 版本需要注意安装正确版本的英伟达驱动、CUDA、CUDNN。接着就是正式安装 cvlib简单的安装方法是通过 pippip install cvlib
如果是希望升级到最新版本命令为pip install --upgrade cvlib
第二种通过源码安装方法只需要按照下列命令依次执行即可git clone https://github.com/arunponnusamy/cvlib.git
cd cvlib
python setup.py sdist
pip install .
注意目前仅在 Python 3.x 版本测试通过而 2.x 版本并没有进行测试所以建议在 Python 3.x 环境使用该库。主要功能目前 cvlib 支持以下几种应用人脸检测性别检测目标检测人脸检测人脸检测的接口是 detect_face()返回的结果是所有检测到的人脸的一个坐标点和置信度。代码示例import cvlib as cv
import sys
import cv2
import os # read input image
image cv2.imread(sys.argv[1])# apply face detection
faces, confidences cv.detect_face(image)print(faces)
print(confidences)# loop through detected faces
for face,conf in zip(faces,confidences):(startX,startY) face[0],face[1](endX,endY) face[2],face[3]# draw rectangle over facecv2.rectangle(image, (startX,startY), (endX,endY), (0,255,0), 2)# display output
# press any key to close window
cv2.imshow(face_detection, image)
cv2.waitKey()# save output
cv2.imwrite(face_detection.jpg, image)# release resources
cv2.destroyAllWindows()
输出结果如下所示人脸检测的底层实现其实是通过 OpenCV 的 dnn 模块并加载一个预训练的 caffemodel。性别检测性别检测的接口是 detect_gender()返回的是标签男性or女性以及预测的概率。代码示例import cv2
import cvlib as cv
import sys
import numpy as np# read input image
img cv2.imread(sys.argv[1])# apply face detection
face, conf cv.detect_face(img)# loop through detected faces
for f in face:(startX,startY) f[0],f[1](endX,endY) f[2],f[3]# draw rectangle over facecv2.rectangle(img, (startX,startY), (endX,endY), (0,255,0), 2)face_crop np.copy(img[startY:endY, startX:endX])# apply gender detection(label, confidence) cv.detect_gender(face_crop)print(confidence)print(label)idx np.argmax(confidence)label label[idx]label {}: {:.2f}%.format(label, confidence[idx] * 100)Y startY - 10 if startY - 10 10 else startY 10cv2.putText(img, label, (startX, Y), cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 255, 0), 2)# display output
# press any key to close window
cv2.imshow(gender detection, img)
cv2.waitKey()# save output
cv2.imwrite(gender_detection.jpg, img)# release resources
cv2.destroyAllWindows()
输出结果如下所示从示例代码可以看到首先是需要检测人脸然后将人脸部分传入性别检测接口再得到最终的检测结果。其底层实现是采用 keras 的预训练模型不过其准确率并不是特别的高所以可以自己优化模型提升性别检测的准确率然后替换模型。目标检测目标检测的接口是 detect_common_objects()它用于检测常见的物体返回结果是图片中检测到的物体的坐标、类别标签以及置信度。代码示例如下import cvlib as cv
from cvlib.object_detection import draw_bbox
import sys
import cv2# read input image
image cv2.imread(sys.argv[1])# apply object detection
bbox, label, conf cv.detect_common_objects(image)print(bbox, label, conf)# draw bounding box over detected objects
out draw_bbox(image, bbox, label, conf)# display output
# press any key to close window
cv2.imshow(object_detection, out)
cv2.waitKey()# save output
cv2.imwrite(object_detection.jpg, out)# release resources
cv2.destroyAllWindows()
输出结果目标检测的底层实现是采用在 COCO 数据集上训练的 YOLOv3 模型。另外上述三个功能其实不仅是对图片进行检测还可以实时调用通过摄像头捕捉到的实时返回结果代码例子可以查看https://github.com/arunponnusamy/cvlib/tree/master/examples其他功能除了上述三个主要功能其实 cvlib 也还能实现以下两个功能获取视频的帧生成 gif 动图获取视频的帧是在 utils.py 中的函数 get_frames()输入是视频的路径使用方法如下所示import cvlib as cv
frames cv.get_frames(~/Downloads/demo.mp4)
也可以添加一个保存所有帧的文件夹路径返回的帧 frames 是用列表保存的 numpy 数组形式。frames cv.get_frames(~/Downloads/demo.mp4, ~/Downloads/demo_frames/)
生成 gif 动图则是函数 animate() 实现的它需要输入一批图片或者保存图片的文件夹路径然后返回一个 gif 并保存。cv.animate(frames, ~/Documents/frames.gif)
这两个功能具体可以查看https://github.com/arunponnusamy/cvlib/blob/master/cvlib/utils.py#L48https://github.com/arunponnusamy/cvlib/blob/master/cvlib/utils.py#L87小结今天介绍的计算机视觉库 cvlib 是一个非常容易上手的工具简单实现了人脸检测、性别检测和目标检测三个非常常用的应用。最后前两张图片其实是来自一部美剧《硅谷》这是一部讲述几个程序员创业的故事非常有趣又有常见的程序员梗目前出了第四季第五季也是最后一季估计是在年底播出还是非常推荐大家看看的。欢迎关注我的微信公众号--算法猿的成长或者扫描下方的二维码大家一起交流学习和进步如果觉得不错在看、转发就是对小编的一个支持推荐阅读- [资源分享] Github上八千Star的深度学习500问教程必读的AI和深度学习博客[Github项目推荐] 机器学习 Python 知识点速查表[Github项目推荐] 推荐三个助你更好利用Github的工具Github上的各大高校资料以及国外公开课视频