[Practical project] Realize street flow statistics based on AidLux+YOLOv5+ByteTrack

The content of this blog is mainly based on the implementation of AidLux, and it is also the experience of participating in the "Aidlux Smart Security AI Practical Training Camp".

With the continuous development of modern technology, artificial intelligence will have a great impact on all walks of life. In the large security market of civil defense, physical defense and technical defense, artificial intelligence is also playing a role, and it has become a popular trend and will be is the future direction of development. This training camp is to realize one of the small scenes.

Pedestrian flow statistics

1. Project overview
In public places such as railway stations, airports, subway stations, and scenic spots, it is necessary to detect the number of people in real time, give early warning when the density of people is too high, and implement measures such as diversion and flow limitation to prevent potential safety hazards.
In public places with a high density of people, Yolov5 + ByteTrackthe multi-target tracking solution used can realize the statistics of the number of people in different scenarios, and help the staff in the place to make corresponding management plans.

2. Realize the effect

3. Environment configuration

To realize this project, you must first configure the environment

  • 3.1 Download AidLux from
    the app stores of major Android phones, search for Aidlux to
    download and install, and register and log in

  • 3.2 AidLux computer projection
    insert image description here
    Open the address of the computer browser, enter Cloud_ipthe corresponding ip in , and the default login password aidluxto enter the computer page
    insert image description here

  • 3.3 Download and install VSCode and related plug-ins
    Go to the VSCode official website and select the appropriate version to download and install. When installing, remember to check the following two items and
    insert image description here
    then install Python and SSH plug-ins
    insert image description here
    insert image description here
    . Since opencv will be used, use the menu bar terminal → new terminalor use the shortcut key Ctrl + Shift + `

    Open the terminal and enter pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/sim ple, you can quickly download and install successfully.

    Of course, there is a foundation in itself. Students who already have a Python environment installed on this machine can directly open or create a python file and choose their own local environment.
    insert image description here

  • 3.4 Use VSCode to remotely debug AidLux on the PC side.
    First upload the local project file to AidLux (open the file browser, enter home, and finally upload the file).
    insert image description here
    Next, configure the ssh connection
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    insert image description here
    and select the path to upload the file. In the window that pops up, enter the password aidlux, that is You can open the Lesson5_code folder that we have uploaded
    insert image description here
    . After completing the above operations, you can start the code operation. By the way, remember to replace the Python interpreter with the AidLuxabove Python
    insert image description here
    . Go here, and all the configuration processes are over.

4. Implementation steps

  • 4.1 Detection of pedestrians Pedestrian
    detection mainly uses the target detection algorithm YOLOv5. For the specific model training implementation, you can refer to Mr. Jiang Dabai’s article: Super detailed tutorial on Yolov5’s own data set training in simple terms.
    You can also search for YOLOv5 training on major platforms to obtain corresponding tutorials
    . The AidLux platform is deployed, so it needs to be export.pyconverted pt文件into tflite文件
    insert image description here
    model initialization and loading, which mainly uses two function interfaces, one is aidlite_gpu.aidlite()andaidlite.ANNMode()
    # aidlux相关
    from cvs import *
    import aidlite_gpu
    from utils import detect_postprocess, preprocess_img, draw_detect_res
    
    import time
    import cv2
    
    # AidLite初始化:调用AidLite进行AI模型的加载与推理,需导入aidlite
    aidlite = aidlite_gpu.aidlite()
    # Aidlite模型路径
    model_path = '/home/lesson4_codes/aidlux/yolov5n_best-fp16.tflite'
    # 定义输入输出shape
    in_shape = [1 * 640 * 640 * 3 * 4]
    out_shape = [1 * 25200 * 6 * 4]
    # 加载Aidlite检测模型:支持tflite, tnn, mnn, ms, nb格式的模型加载
    aidlite.ANNModel(model_path, in_shape, out_shape, 4, 0)
    
  • 4.2 Pedestrian Tracking
    Pedestrian tracking mainly uses the target tracking algorithm Bytetrack,
    the algorithm principle can be viewed: MOT classic algorithm of target tracking: ByteTrack algorithm principle and multi-category tracking
  • 4.3 Pedestrian Flow Statistics
    • Draw monitoring area
      # 1.绘制行人流量统计线
      lines = [[186, 249], [1235, 366]]
      cv2.line(res_img, lines[0], lines[1], (255, 255, 0), 3)
      
    • Set up human monitoring points
      # 2.计算得到人体下方中心点的位置(人体检测监测点调整)
      pt = [tlwh[0] + 1 / 2 * tlwh[2], tlwh[1] + 1 / 2 * tlwh[3]]
      
    • Pedestrian area location judgment
      # utils.py
      def is_passing_line(point, polyline):
        # 在直线下方,status =-1
        # 在直线上方,status =1
        status = 1
        # 点映射在直线的高度
        poly_x = ((polyline[1][0] - polyline[0][0]) * (point[1] - polyline[0][1])) / (polyline[1][1] - polyline[0][1]) + \
                  polyline[0][0]
        if point[0] > poly_x:
            status = -1
        return status
      
      # 3. 人体和违规区域的判断(人体状态追踪判断)
      track_info = is_passing_line(pt, lines)
      if tid not in track_id_status.keys():
          track_id_status.update( {
              
              tid:[track_info]})
      else:
          if track_info != track_id_status[tid][-1]:
              track_id_status[tid].append(track_info)
      
    • statistics
      # 4. 判断是否有track_id越界,有的话保存成图片
      # 当某个track_id的状态,上一帧是-1,但是这一帧是1时,说明越界了
      if track_id_status[tid][-1] == 1 and len(track_id_status[tid]) > 1:
          # 判断上一个状态是否是-1,是否的话说明越界,为了防止继续判别,随机的赋了一个3的值
          if track_id_status[tid][-2] == -1:
              track_id_status[tid].append(3)
              # cv2.imwrite("overstep.jpg", res_img)
              person_up += 1
      if track_id_status[tid][-1] == -1 and len(track_id_status[tid]) > 1:
          if track_id_status[tid][-2] == 1:
              track_id_status[tid].append(-3)
              person_down += 1
      
    • The system reminds
      you to pay attention to 喵提醒the WeChat public account, register an account, select the menu bar 提醒, select 新建, fill in the relevant information and save it, the corresponding 喵码and will be generated 网址, and you can receive the relevant information 喵码by filling in the following codeid
      # 5.越界识别+喵提醒
      # 填写对应的喵码
      id = 'tP48aPC'
      # 填写喵提醒中,发送的消息,这里放上前面提到的图片外链
      text = "有人越界识别!!"
      ts = str(time.time())  # 时间戳
      type = 'json'  # 返回内容格式
      request_url = "http://miaotixing.com/trigger?"
      headers = {
              
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36 Edg/87.0.664.47'}
      result = requests.post(request_url + "id=" + id + "&text=" + text + "&ts=" + ts + "&type=" + type,headers=headers)
      

After the above operations, the statistics of the flow of people in the specified area can be completed.

5. Experience of using AidLux
After learning AidLux, it is not difficult to find that the deployment of AidLux is relatively friendly and easy for those who are just getting started, and the entire process can be completed only by using Python. Aidlux uses the entire development process through the Aidlux platform to quickly apply the code written on the PC to the Android system, which greatly reduces the cumbersome process of traditional model deployment and reduces the consumption of personnel.

Guess you like

Origin blog.csdn.net/weixin_42166222/article/details/127548520