ITPub博客

首页 > 应用开发 > > 10行Python代码计算汽车数量

10行Python代码计算汽车数量

作者:coyan 时间:2019-10-21 10:53:26 0 删除 编辑

人工智能遇见磐创 



当你还是个孩子坐车旅行的时候,你玩过数经过的汽车的数目的游戏吗?

在这篇文章中,我将教你如何使用10行Python代码构建自己的汽车计数程序。以下是环境及相应的版本库:

Python版本 3.6.9

cvlib: 0.2.2

opencv-python: 4.1.1.26

tensorflow: 1.14.0

matplotlib: 3.1.1

Keras: 2.2.5

下面的代码用于导入所需的python库、从存储中读取图像、对图像执行目标检测、用边界框显示图像以及关于检测目标的标签、计算图像中的汽车数量并打印图像:

import cv2

import matplotlib.pyplot as plt

import cvlib as cv

from cvlib.object_detection import draw_bbox

im = cv2.imread('cars_4.jpeg')

bbox, label, conf = cv.detect_common_objects(im)

output_image = draw_bbox(im, bbox, label, conf)

plt.imshow(output_image)

plt.show()

print('Number of cars in the image is '+ str(label.count('car')))

此图像的输出:

Number of cars in the image is 29

此图像的输出:

Number of cars in the image is 22

此图像的输出:

Number of cars in the image is 25

你的汽车计数器程序现在已经准备好了。你可以用它来做一些有趣的实验,比如计算每天经过你家门前车道的汽车数量。

要了解更多关于cvlib库的信息,可以访问下面的链接。

cvlib[1]

[1]: https://www.cvlib.net

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2236/viewspace-3148253/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-07-11

  • 博文量
    271
  • 访问量
    16170