pip install opencv-python tensorflow当然上述安装的 tensorflow 是 cpu 版本,如果希望安装可以使用 gpu 的,安装包名字是 tensorflow-gpu
pip install cvlib如果是希望升级到最新版本,命令为:
pip install --upgrade cvlib第二种通过源码安装方法,只需要按照下列命令依次执行即可
git clone https://github.com/arunponnusamy/cvlib.git注意:目前仅在 Python 3.x 版本测试通过,而 2.x 版本并没有进行测试,所以建议在 Python 3.x 环境使用该库。
cd cvlib
python setup.py sdist
pip install .
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 face
cv2.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()
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 face
cv2.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 + 10
cv2.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()
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()
import cvlib as cv也可以添加一个保存所有帧的文件夹路径,返回的帧 frames是用列表保存的 numpy数组形式。
frames = cv.get_frames('~/Downloads/demo.mp4')
frames = cv.get_frames('~/Downloads/demo.mp4', '~/Downloads/demo_frames/')生成 gif 动图则是函数 =inheritanimate()实现的,它需要输入一批图片或者保存图片的文件夹路径,然后返回一个 gif 并保存。
cv.animate(frames, '~/Documents/frames.gif')这两个功能,具体可以查看:
作者:KBSC13
来源:算法猿的成长
本文为 @ 21CTO 创作并授权 21CTO 发布,未经许可,请勿转载。
内容授权事宜请您联系 webmaster@21cto.com或关注 21CTO 公众号。
该文观点仅代表作者本人,21CTO 平台仅提供信息存储空间服务。