《Aidlux智慧安防AI实战训练营》作业心得
1 训练营课程链接
智慧安防实战训练营的课程:AidLux智慧安防AI实战训练营活动正式开启
可以做相关的只是扩充,同时学习AidLux的使用,将安卓手机利用起来。
2 作业题目
(1)题目:
在学习了越界识别的功能后,采用人体检测+人体追踪+业务功能的方式实现 人流统计。
(2)实现效果:
https://zhuanlan.zhihu.com/p/578560984
(3)代码实现:
其它相关内容关注AidLux公众号回复lesson1、lesson2...、lesson5即可下载,下边的是lesson5的内容
# aidlux相关 from cvs import * import aidlite_gpu # from utils import detect_postprocess, preprocess_img, draw_detect_res, scale_coords,process_points,isInsidePolygon,is_in_poly from utils import detect_postprocess, preprocess_img, draw_detect_res, scale_coords,process_points,is_in_poly,is_passing_line import cv2 # bytetrack from track.tracker.byte_tracker import BYTETracker from track.utils.visualize import plot_tracking import requests import time # 加载模型 model_path = '/home/lesson4_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/lesson4_codes/aidlux/video.mp4") frame_id = 0 count_person = 0 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) # 目标追踪相关功能 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) ### 人流量统计功能实现 ### # 1.绘制统计人流线 lines = [[186,249],[1235,366]] cv2.line(res_img,(186,249),(1235,366),(255,255,0),3) # 2.计算得到人体下方中心点的位置(人体检测监测点调整) pt = [tlwh[0]+1/2*tlwh[2],tlwh[1]+tlwh[3]] # 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) # 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) count_person += 1 cv2.putText(res_img,"-1 to 1 person_count:"+str(count_person),(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,0,255),2) cvs.imshow(res_img) if count_person>10: # 5.越界识别+喵提醒 # 填写对应的喵码 公众号关注喵提醒,注册账号,然后首页创建,填好标题,其它默认,再将喵码填到下边 id = 'xxxx' # 填写喵提醒中,发送的消息,这里放上前面提到的图片外链 text = "人流统计数:" + str(count_person) 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)
3 学习心得&使用心得
AidLux安卓版本兼容还需要继续加强,我的vivo手机重装了四遍才能使用的[狗头];目前使用vscode ssh连接到AidLux也还存在断线和版本配置问题。不过能利用起家里的旧手机也还不错,使用AidLux开发一些小的应用程序还不错,基本在本地调试完稍作修改即可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!