0. 项目概述
基于Aidlux平台,通过vscode远程调试,最终实现在一个设定的监控区域内,如果同时停留的人数超过设定的阈值,即认定为聚集,并会在对应的视频上进行显示,保存聚集时图片,并将通知以及检测到的聚集图片发送到管理员的手机上。
1. 环境配置
1. 1 跨平台应用系统Aidlux
AIdlux主打的是基于ARM架构的跨生态(Android/鸿蒙+Linux)一站式AIOT应用开发平 台。用比较简单的方式理解,我们平时编写训练模型,测试模型的时候,常用的是 Linux/window系统。而实际应用到现场的时候,通常会以几种形态:GPU服务器、嵌入式设 备(比如Android手机、人脸识别闸机等)、边缘设备。GPU服务器我们好理解,而Android 嵌入式设备的底层芯片,通常是ARM架构。而Linux底层也是ARM架构,并且Android又是 基于Linux内核开发的操作系统,两者可以共享Linux内核。因此就产生了从底层开发一套应 用系统的方式,在此基础上同时带来原生Android和原生Linux使用体验。
因此基于ARM芯片,开发了Aidlux平台,可以在安卓手机上直接下载Aidlux使用。同时基于 ARM芯片,比如高通骁龙的855芯片和865芯片,也开发了AidBox边缘设备,提供7T OPS和 15TOPS算力,可以直接在设备上使用。
1.2 手机版本Aidlux软件安装
我们先下载一下手机Aidlux的APP软件。打开安卓手机的应用商城,搜索Aidlux即可 下载安装。
打开手机版本的Aidlux软件APP,第一次进入的时候,APP自带的系统会进行初始化。 初始化好后,进入系统登录页面,这一步最好可以用手机注册一下,当然也可以直接点击“我 已阅读并同意”,然后点击跳过登录。 进入主页面后,可以点击左上角的红色叉号,将说明页面关闭。
可以点击页面最上方的 Cloud_ip可以将界面映射到电脑上操作。
题主这里是http://10.203.185.227:8000,打开电脑浏览器,并输入相应的ip,默认密码是aidlux,输入后即可进入主界面
1.3 Aidlux&VScode
下载VScode软件:
点击官网https://code.visualstudio.com/,选择Download按钮进行下载,下载后根据提示一直进行安装
环境配置:
安装本地版的python和Opencv以及Remote-SSH用于连接手机端Aidlux
2. PC端远程调试Aidlux
2.1 VScode远程连接Aidlux
点击左下角绿色部分创建连接
选择Open SSH Configuration File
选择第一个,设置如下:
之后在远程资源管理器中进行连接
连接成功后打开文件管理器并打开对应的文件夹(本文是直接用手机传输文件到对应的目录后进行运行和调试的)
3. AI案例:人流聚集检测
通过使用人体检测+人体追踪+聚集检测进行实现
3.1 准备模型
对于一般开发而非工业环境的小规模应用来说,一般是不需要自己训练模型的,许多开源的算法方案也提供了相应的训练好的模型,本文使用的模型基于yolov5,在Aidlux的课程文件夹中提供了相应的模型,将训练好的模型转换为tflite格式以便在移动端设备上进行运行,并放在文件夹目录下
3.2 导入需要的包
# aidlux相关
from cvs import *
import aidlite_gpu
from utils import detect_postprocess, preprocess_img, draw_detect_res,process_points,is_in_poly
import cv2
# bytetrack
from track.tracker.byte_tracker import BYTETracker
from track.utils.visualize import plot_tracking
from track.utils.timer_count import *
import requests
import time
3.3 加载模型
# 加载模型 model_path = '/home/lesson5_codes/aidlux/yolov5n_best-fp16.tflite' in_shape = [1 * 640 * 640 * 3 * 4] out_shape = [1 * 25200 * 6 * 4]
载入模型
aidlite = aidlite_gpu.aidlite()
载入yolov5检测模型
aidlite.ANNModel(model_path, in_shape, out_shape, 4, 0)
tracker = BYTETracker(frame_rate=30)
track_id_status = {}
cap = cvs.VideoCapture("/home/lesson5_codes/aidlux/video.mp4")
frame_id = 0
threshold = 7
3.4 读取视频并进行预处理,以便输入模型
while True:
frame = cap.read()
if frame is None:
continue
frame_id += 1
if frame_id % 3 != 0:
continue
# 预处理
img = preprocess_img(frame, target_shape=(640, 640), div_num=255, means=None, stds=None)
# 数据转换:因为setTensor_Fp32()需要的是float32类型的数据,所以送入的input的数据需为float32,大多数的开发者都会忘记将图像的数据类型转换为float32
aidlite.setInput_Float32(img, 640, 640)
# 模型推理API
aidlite.invoke()
# 读取返回的结果
pred = aidlite.getOutput_Float32(0)
# 数据维度转换
pred = pred.reshape(1, 25200, 6)[0]
# 模型推理后处理
pred = detect_postprocess(pred, frame.shape, [640, 640, 3], conf_thres=0.4, iou_thres=0.45)
# 绘制推理结果
res_img = draw_detect_res(frame, pred)
3.5 目标追踪功能相关实现
det = []
# Process predictions
for box in pred[0]: # per image
box[2] += box[0]
box[3] += box[1]
det.append(box)
if len(det):
# Rescale boxes from img_size to im0 size
online_targets = tracker.update(det, [frame.shape[0], frame.shape[1]])
online_tlwhs = []
online_ids = []
online_scores = []
# 取出每个目标的追踪信息
for t in online_targets:
# 目标的检测框信息
tlwh = t.tlwh
# 目标的track_id信息
tid = t.track_id
online_tlwhs.append(tlwh)
online_ids.append(tid)
online_scores.append(t.score)
# 针对目标绘制追踪相关信息
res_img = plot_tracking(res_img, online_tlwhs, online_ids, 0,0)
3.6 人员聚集功能实现
### 人员聚集功能实现 ### # 1.绘制监测区域 points = [[582,93],[779,114],[278,411],[17,345]] color_light_green=(144, 238, 144) ##浅绿色 res_img = process_points(res_img,points,color_light_green)
# 2.计算得到人体下方中心点的位置(人体检测监测点调整) pt = [tlwh[0]+1/2*tlwh[2],tlwh[1]+tlwh[3]] # 3. 人体和违规区域的判断(人体状态追踪判断) track_info = is_in_poly(pt, points) 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) #print(online_ids) #print(track_id_status) count_gather = 0 for value in track_id_status.values(): if value[-1] == 1: count_gather += 1
3.7 七牛云上传检测图片
from qiniu import Auth, put_file from qiniu import CdnManager
配置七牛云信息
access_key = "B_CfNLMpBzKRD0VMljTZYnRtFpwJjpv-LPAz21uU"
secret_key = "########################################"
bucket_name = "gather-aidlux"
bucket_url = "https://portal.qiniu.com/cdn/domain/rmm2ejraf.hn-bkt.clouddn.com"
q = Auth(access_key, secret_key)
cdn_manager = CdnManager(q)将本地图片上传到七牛云中
def upload_img(bucket_name, file_name, file_path):
# generate token
token = q.upload_token(bucket_name, file_name)
put_file(token, file_name, file_path)获得七牛云服务器上file_name的图片外链
def get_img_url(bucket_url, file_name):
img_url = ‘http://%s/%s’ % (bucket_url, file_name)
return img_urlcv2.imwrite("./gather.jpg",res_img)
需要上传到七牛云上面的图片的路径
image_up_name = "./gather.jpg"
上传到七牛云后,保存成的图片名称
image_qiniu_name = "gathered.jpg"
将图片上传到七牛云,并保存成image_qiniu_name的名称
upload_img(bucket_name, image_qiniu_name, image_up_name)
取出和image_qiniu_name一样名称图片的url
url_receive = get_img_url(bucket_url, image_qiniu_name)
print(url_receive)需要刷新的文件链接,由于不同时间段上传的图片有缓存,因此需要CDN清除缓存,
urls = [url_receive]
URL刷新缓存链接,一天有500次的刷新缓存机会
refresh_url_result = cdn_manager.refresh_urls(urls)
3.8 喵提醒
if(count_gather >= threshold):
cv2.imwrite("overstep.jpg",res_img)
# 喵提醒
# 填写对应的喵码
id = 'tv1OWTC'
# 填写喵提醒中,发送的消息,这里放上前面提到的图片外链
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)
4.最终效果
如图所示,当系统检测区域中的聚集人数超过指定的阈值(7)时,视频窗口会显示人员聚集,同时发送喵提醒,并在喵提醒提示的链接中,可以看到人员聚集时采集的图片。
实现对应视频效果:
https://www.bilibili.com/video/BV1yd4y1e79P/?spm_id_from=333.999.list.card_archive.click&vd_source=29046a435546cbc3c030db6053024c56
引用参考: