![5ccda190ca459a1bde03184649ca07f9.png](https://i-blog.csdnimg.cn/blog_migrate/44db1dd48140e7d57a1441e65652e738.jpeg)
当你还是个孩子坐车旅行的时候,你玩过数经过的汽车的数目的游戏吗?
在这篇文章中,我将教你如何使用10行Python代码构建自己的汽车计数程序。以下是环境及相应的版本库:
Python版本 3.6.9cvlib: 0.2.2opencv-python: 4.1.1.26tensorflow: 1.14.0matplotlib: 3.1.1Keras: 2.2.5
下面的代码用于导入所需的python库、从存储中读取图像、对图像执行目标检测、用边界框显示图像以及关于检测目标的标签、计算图像中的汽车数量并打印图像:
import cv2import matplotlib.pyplot as pltimport cvlib as cvfrom cvlib.object_detection import draw_bboxim = 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')))
![c22954c85d86a8dc195ac340ba29d45f.png](https://i-blog.csdnimg.cn/blog_migrate/a923c442e127573380175b1f4e6b2da0.jpeg)
此图像的输出:
Number of cars in the image is 29
![51b97fbabd779d30a97e1354e4d52c2b.png](https://i-blog.csdnimg.cn/blog_migrate/cc0de5250c648c3316e5b6a46a9a14b9.jpeg)
此图像的输出:
Number of cars in the image is 22
![395823f29ce6152ee784b98b49d4fcf0.png](https://i-blog.csdnimg.cn/blog_migrate/37f02b2201b181b6d47b60d3c2c3cbca.jpeg)
此图像的输出:
Number of cars in the image is 25
你的汽车计数器程序现在已经准备好了。你可以用它来做一些有趣的实验,比如计算每天经过你家门前车道的汽车数量。
要了解更多关于cvlib库的信息,可以访问下面的链接。
- cvlib[1]
[1]: https://www.cvlib.net