从头开始构建牌照跟踪器

In this tutorial, we’ll cover how to create your own license plate tracker using the license plate detection model that was created using alwaysAI’s model training tool. If you want to read more about how the license plate detection model was built, read this blog. To read more about model training in general, you can read this model training overview article.

在本教程中,我们将介绍如何使用通过alwaysAI的模型训练工具创建的车牌检测模型创建自己的车牌跟踪器。 如果您想了解有关车牌检测模型如何构建的更多信息,请阅读此博客 。 要大致了解有关模型训练的更多信息,请阅读此模型训练概述文章

The model training tool will become available soon, however if you’re interested in training your own object detection model now, you can fill out this survey to participate in our model training beta program now. You can use a freely available dataset to train your own license plate detection model, found here, which is 185MB, and consists of 584 image/annotation pairs. You can then test out your new model using the example app we’ll build in this tutorial! The finished code from this tutorial, as well as test videos and dataset links, is available on GitHub.

模型训练工具将很快可用,但是,如果您现在有兴趣训练自己的对象检测模型,则可以填写此调查以立即加入我们的模型训练beta程序。 您可以使用免费提供的数据集来训练自己的车牌检测模型,该模型 185MB处,由584个图像/注释对组成。 然后,您可以使用我们将在本教程中构建的示例应用程序来测试您的新模型! 本教程中的最终代码以及测试视频和数据集链接可在GitHub找到

To complete the tutorial, you must have:

要完成本教程,您必须具备:

  1. An alwaysAI account (it’s free!)

    alwaysAI帐户 (免费!)

  2. alwaysAI set up on your machine (also free)

    在计算机上设置 alwaysAI(也是免费的)

  3. A text editor such as sublime or an IDE such as PyCharm, both of which offer free versions, or whatever else you prefer to code in

    文本编辑器(例如sublime)或IDE(例如PyCharm) ,两者均提供免费版本,或者您希望在其中进行编码的其他任何版本

This app includes a few useful features you can incorporate in your other projects, namely:

该应用程序包括一些可以合并到其他项目中的有用功能,即:

  • Using a correlation tracker to reduce overhead

    使用相关跟踪器减少开销
  • Using multiple files as input to your app

    使用多个文件作为您的应用程序的输入
  • Labeling individual detected objects for readability

    标记单个检测到的对象以提高可读性

Please see the alwaysAI blog for more background on computer vision, developing models, how to change models, and more.

请访问alwaysAI 博客,以获得有关计算机视觉开发模型 ,如何更改模型等的更多背景信息。

Let’s get started!

让我们开始吧!

We’ll break down this tutorial in two parts:

我们将本教程分为两部分:

  1. Set up

    建立
  2. App.py

    应用程式

建立 (Set Up)

In this tutorial, we’ll build the app from scratch. After you’ve signed up and logged in, go to https://alwaysai.co and navigate to your Dashboard. You can follow the steps outlined here to create a new project. For this app, you’ll want to choose ‘create a project from scratch’. Once your project is created on the Dashboard, scroll down to the ‘Models’ section and click the ‘+’ sign to add a model. Browse the model category to find “alwaysai/vehicle_license_mobilenet_ssd”, which will be in the ‘object detection’ models. Add this model to your project by clicking on it, and selecting ‘add to project’ and choosing your project from the drop down menu. Navigate back to your project Dashboard to get the configuration hash code and then finish configuring your project locally as described in the documentation page.

在本教程中,我们将从头开始构建应用程序。 注册并登录后,请访问https://alwaysai.co并导航至仪表板。 您可以按照此处概述的步骤创建一个新项目。 对于此应用,您需要选择“从头开始创建项目”。 在仪表板上创建项目后,向下滚动到“模型”部分,然后单击“ +”号以添加模型。 浏览模型类别,找到“ alwaysai / vehicle_license_mobilenet_ssd”,它将位于“对象检测”模型中。 单击此模型并将其添加到项目中,然后选择“添加到项目”,然后从下拉菜单中选择项目。 浏览回到项目仪表板以获取配置哈希码,然后按照文档页面中的说明完成本地配置项目。

Finally, create a folder inside the directory that app.py is in named ‘video’. This is where we’ll store the input videos. You can find some sample videos in the GitHub repository, along with the completed code for this tutorial.

最后,在名为“ video ”的app.py目录内创建一个文件夹。 我们将在这里存储输入的视频。 您可以在GitHub存储库中找到一些示例视频,以及本教程的完整代码。

应用程式 (App.py)

Now, we’ll add the content to app.py. There are a few different moving parts here, but we’ll walk through the code top to bottom and explain them as we go!

现在,我们将内容添加到app.py中 。 这里有一些不同的活动部分,但是我们将逐步讲解代码,并逐步进行解释!

First, make sure the following lines are at the top of app.py:

首先,请确保以下行位于app.py的顶部:

import time
import edgeiq

This will import the necessary libraries (edgeiq is the alwaysAI python API). Then, replace the contents of ‘main()’ with the following:

这将导入必要的库(edgeiq是alwaysAI python API)。 然后,将“ main()”的内容替换为以下内容:

def main():
# The current frame index
frame_idx = 0 # The number of frames to skip before running detector
detect_period = 30 obj_detect = edgeiq.ObjectDetection(
"alwaysai/vehicle_license_mobilenet_ssd")
obj_detect.load(engine=edgeiq.Engine.DNN) print("Loaded model:\n{}\n".format(obj_detect.model_id))
print("Engine: {}".format(obj_detect.engine))
print("Accelerator: {}\n".format(obj_detect.accelerator))
print("Labels:\n{}\n".format(obj_detect.labels)) tracker = edgeiq.CorrelationTracker(max_objects=5) fps = edgeiq.FPS()

What we’ve done here is pretty standard if you’ve used any of alwaysAI’s starter or example apps before: we’re just setting up the main method, creating an object detector using the alwaysai/vehicle_license_mobilenet_ssd model, and we’re printing the object detector’s configuration to the console.

如果您之前使用过alwaysAI的任何启动器或示例应用程序,那么我们在这里所做的工作都是非常标准的:我们只是设置主要方法,使用alwaysai / vehicle_license_mobilenet_ssd模型创建对象检测器,然后打印对象检测器的配置到控制台。

We’re also creating three important variables: frame_idx, detect_period, and tracker.

我们还将创建三个重要的变量: frame_idxdetect_periodtracker

The variable tracker is the correlation tracker object. Using a tracker, such as the correlation tracker in the edge IQ library, reduces the CPU usage and inference time.

变量跟踪器是相关跟踪器对象。 使用跟踪器(例如边缘IQ库中的相关跟踪器)可以减少CPU使用率和推理时间。

The variable frame_idx tracks how many iterations we’ve done, and detect_period defines how often we’ll perform object detection. If the frame count is not evenly divisible by 30, we instead check if the tracker is currently tracking any objects (using ‘count’), and if so, we set the ‘predictions’ variable to be the tracked predictions. To reduce overhead, increase detect_period, and vice versa.

变量frame_idx跟踪我们完成了多少次迭代, detect_period定义了我们执行对象检测的频率。 如果帧计数不能被30整除,我们将检查跟踪器当前是否正在跟踪任何对象(使用“计数”),如果是,则将“ predictions”变量设置为跟踪的预测。 为了减少开销,请增加detect_period,反之亦然。

NOTE: comparing the value returned by using modulo (e.g. m % n) to an integer (e.g. 0) in a loop statement is a common way of performing a task every n iterations and can be applied in your other projects. If you set n to 2 and compare the result to 0, your loop task will execute every even iteration, for example.

注意:在循环语句中将通过使用模(例如m%n)返回的值与整数(例如0)进行比较是每 n 次迭代 执行任务的一种常用方法, 并且可以在您的其他项目中应用。 例如,如果将 n 设置 为2并将结果与​​0进行比较,则循环任务将执行每个 偶数 迭代。

Next, copy all the code contents directly under what you just added. This instantiates a ‘try’ block, along with a ‘finally’ counterpart, which will always be executed regardless of the ‘try’ execution and will bring down the streamer, stop tracking the frames per second and print closing messages.

接下来,将所有代码内容直接复制到刚添加的内容下。 这将实例化一个“ try”块以及一个“ finally”对应块,无论“ try”执行如何,该块始终执行,并将关闭流媒体,停止跟踪每秒的帧并打印关闭消息。

    try:
# blank for now, will fill in later!finally:
fps.stop()
streamer.close()
print("elapsed time {:.2f}".format(fps.get_elapsed_seconds()))
print("approx. FPS: {:.2f}".format(fps.compute_fps())) print("Program Ending")

Now that the configuration is done and we have a skeleton of an app to work with, we’ll fill in the object tracking and file import portions. All of the rest of the code will go into the ‘try’ block we created in the last step.

现在已经完成了配置,并且有了应用程序的框架,我们将填写对象跟踪和文件导入部分。 其余所有代码将进入上一步中创建的“ try”块。

Inside the ‘try’ block, paste the following code:

在“ try”块中 ,粘贴以下代码:

        video_paths = edgeiq.list_files(base_path="./video/", valid_exts=".mp4")
streamer = edgeiq.Streamer().setup() for video_path in video_paths:
with edgeiq.FileVideoStream(video_path)as video_stream:

This code uses the edge IQ command ‘list_files’ to get a list of all the files you’ve stored in the ‘video’ folder that have the file extension ‘mp4’. Then, it iterates over each of the file paths in that returned list and runs the nested code on each, which we’ll cover in the following section.

这段代码使用Edge IQ命令'list_files'来获取存储在'video'文件夹中且文件扩展名为'mp4'的所有文件的列表。 然后,对返回的列表中的每个文件路径进行迭代,并在每个文件路径上运行嵌套的代码,我们将在下一部分中进行介绍。

Add in as many .mp4 files to the ‘video’ folder as you would like to test your new app on!

您可以在“视频”文件夹中添加尽可能多的.mp4文件,以在其上测试新应用程序!

Next, we’ll add in the tracking logic. For every file in the ‘video’ folder, we’ll use that video as an input stream and detect and track license plates and vehicles.

接下来,我们将添加跟踪逻辑。 对于“视频”文件夹中的每个文件,我们将使用该视频作为输入流,并检测和跟踪车牌和车辆。

NOTE: the variable called ‘predictions’ that is defined at the beginning of the ‘while’ loop in the code below is a list that will be used to store the predictions sent to the streamer. It will be updated every ‘detect_period’, otherwise it will hold the ‘tracker’ predictions.

注意:在以下代码的“ while”循环的开头定义的名为“ predictions”的变量是一个列表,将用于存储发送到流媒体的预测。 它将在每个“ detect_period”进行更新,否则将保留“ tracker”预测。

Add the following code directly under the for loop statement initializing the streamer (for continuity, all the code is given at once, however the code comments provide additional information for each step):

将以下代码直接添加到初始化流程序的for循环语句下(为了连续性,所有代码都立即给出,但是代码注释为每个步骤提供了附加信息):

                # Allow Webcam to warm up
time.sleep(2.0)
fps.start() # loop detection
while video_stream.more(): frame = video_stream.read()
predictions = [] # if using new detections, update 'predictions'
if frame_idx % detect_period == 0:
results = obj_detect.detect_objects(frame, confidence_level=.5) # Generate text to display on streamer
text = ["Model: {}".format(obj_detect.model_id)]
text.append(
"Inference time: {:1.3f} s".format(results.duration))
text.append("Objects:")
# Stop tracking old objects
if tracker.count:
tracker.stop_all() # Set predictions to the new predictions
predictions = results.predictions if not predictions:
text.append("no predictions") # use 'number' to identify unique objects
number = 0
for prediction in predictions:
number = number + 1
text.append("{}_{}: {:2.2f}%".format(
prediction.label, number, prediction.confidence * 100))
tracker.start(frame, prediction) else:
# otherwise, set 'predictions' to those
# stored in the correlation tracker object
if tracker.count:
predictions = tracker.update(frame) # either way, use 'predictions' to mark up the image and update text
frame = edgeiq.markup_image(
frame, predictions, show_labels=True, show_confidences=False, colors=obj_detect.colors)
streamer.send_data(frame, text)
frame_idx += 1 fps.update() if streamer.check_exit():
break

That’s it!

而已!

You can now build the app and start it (see this blog if you need assistance), and you should see output similar to that shown below

现在,您可以构建应用程序并启动它(如果需要帮助,请参阅此博客 ),并且您应该会看到类似于以下所示的输出

Image for post

If you’d like to build your own license plate model and tracker from scratch you can find the survey link, dataset, test videos, and the app code on GitHub.

如果您想从头开始构建自己的车牌模型和跟踪器,则可以在GitHub上找到调查链接,数据集,测试视频和应用程序代码。

Contributions to the article made by Todd Gleed and Jason Koo

对Todd Gleed和Jason Koo的文章的贡献

升级编码 (Level Up Coding)

Thanks for being a part of our community! Subscribe to our YouTube channel or join the Skilled.dev coding interview course.

感谢您加入我们的社区! 订阅我们的YouTube频道或参加Skilled.dev编码面试课程

翻译自: https://levelup.gitconnected.com/using-alwaysais-model-training-tool-to-build-a-license-plate-tracker-dd5f6185047a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值