资讯分类
全部资讯
最新活动
商城公告
行业信息
新品推荐
元器件知识
资讯标签
艾迈斯欧司朗(126) 艾为awinic(118) 英伟达NVIDIA(80) iCEasy商城(76) 萤火工场(61) 飞腾派V3(40) 兆易创新(31) 罗彻斯特电子(28) Jetson Orin Nano SUPER 开发套件(21) 资料下载专区(18) 赛昉科技(14) 小华半导体(13) 赛昉科技(StarFive)(13) 芯佰微(12) 灵动微电子(11) Seeed矽递科技(11) 日清纺微电子(9) NVIDIA(8) 飞腾派(8) 上海雷卯Leiditech(8) 微源半导体 LPSemi(8) 龙芯中科(7) GD32VW553-IOT(7) 物奇WuQi(7) 庆科(5) 航顺芯片(5) 日清纺微电子Nisshinbo(5) TROQ创捷电子(4) 创捷电子(4) 开源口碑分享(4) Geehy极海半导体(4) CEM5826-M11(4) 飞腾派eMMC(3) FTHR-G0001(3) GD32VW553-IOT-V2(3) 罗彻斯特(3) MM32SPIN060G(3) 中国星坤(3) 峰岹科技(3) 杭晶(HCI)(3) 野火(2) 小华EVB-HC32F4A0开发板(2) 极海半导体(2) 国产开源硬件(2) 极海(2) 社区评奖活动(2) 芯佰微电子(2) NVIDIA品牌专区(2) 技术问答专区(2) Mini-SPIN0230(2) 飞腾官网飞腾派(2) 飞腾派下载专区(2) 口碑评测(2) 教育优惠申请链接(2) 华润微电子(2) EVB-L0130(2) BeagleBoard.org®(2) iCEsay商城(2) 英伟达初创加速计划(2) 启明云端(2) 弈安云(2) 英迪芯微(2) 村田(2) GD32无线生态专区(1) 一件免邮专区(1) TagoreTech(1) SFH 7018B(1) 一件包邮专区(1) 英伟达 NVIDIA品牌专区(1) GD32VW553-IOT V2版本(1) 618大促专区(1) 欧达可(1) 飞腾派V3版本上新(1) 飞虹半导体(1) GD32VW553-IOT V2(1) 微碧半导体(1) 8月7日竞拍竞买(1) 教育优惠(1) 萤火工场GD32VW553-IOT(1) 迈巨微(1) 内存(1) VF202040-A0(1) CEM5825F(1) COMBO模组 EMC3020-PZI5(1) 松科智能(1) 台容积电(1) 乐动机器人(1) 英伟达 NVIDIA(1) 格力新元(1) 周年抽奖活动(1) 庆科信息(MXCHIP)品牌专区(1) 附件(1) Jetson AGX Xavier(32GB)(1) 润石(1) NVIDIA DGX™ Spark:桌面级AI超级计算机(1) 飞腾派PRO(1) 微源半导体(1) QCS6490 SOM EVB开发板(1) 附件下载地址(1) B站风扇视频(1) 国产自主可控(1) Mini-F5333(1) JETSON_AGX_ORIN_64GB_DEVELOPER_KIT(1) QCS8550 SOM EVB开发板(1) 申请样片:高光效红光OSCONIQ® P 3030, GR QSSPA1.23(1) 华大北斗(1) 开源社区(1) NVIDIA Jetson Orin Nano 开发套件(1) 萤火工场品牌专区(1) 商城下载专区(1) 瑞隆源产品(1) 雷克沙(1) CEM5881-M11(1) Jetson AGX Orin 64GB开发套件(1) 荣湃半导体(2Pai Semi)品牌专区(1) 下载专区(1) 飞腾派专链(1) 世野(Khadas)(1) 中科云(1) Arduino UNO Rev 4 Minima开发板(1) NVIDIA NIM(1) 微雪电子(1) 海芯微(1) NVIDIA Jetson Orin Nano 开发套件(1) Arduino品牌专区(1) 飞腾派调研问卷链接(1) 小脚丫(1) CEM5861G-M11(1) 赛昉科技9折活动券(1) Jetson AGX Orin 64GB开发套件(1) 24GHz毫米波雷达模块(1) 品牌清仓(1) QCS8550 SOM EVB(1) 天微电子商家页(1) 国产毫米波雷达模块(1) 海凌科电子(Hi-Link)(1) NVIDIA Jetson Nano 模组(1) QCS6490 SOM EVB(1) EV Board (MM32L0136C7P)(1) 迈巨微电子品牌专区(1) 样片申请(1) 龙芯教育派(1) 轮趣科技(1) CEK8902-S905D3(1) 雷卯(1) 飞腾派试用申请链接(1) BeagleBone Black快速入门指南(1) Jetson AGX Thor 开发套件/模组(1) 618大促(1) 竞买活动(1) 极海半导体(Geehy)(1) Arduino品牌页(1) 野火RK3566开发板(1)

施工场景安全帽检测识别方案,轻松应对多人同框场景!

发布时间:2026-02-12

安全帽是建筑、工地等场景保护工人安全的重要装备,但部分人员难免会有疏忽未佩戴的情况,且人工巡查难以全程监管。安全帽识别技术可智能检测工人佩戴情况,有效提升安全生产监管水平。本文将在飞腾派上部署目标检测算法,用于实时检测人员佩戴安全帽的情况。

飞腾派检测安全帽应用教程

目标检测是计算视觉中一项常见的任务,其具体是指将检测图像中目标目标,并用带标签的边界框框出来。(如下图所示),本次教程将会用YOLO作为安全帽识别的检测算法。

项目准备

硬件规格:飞腾派

操作系统:飞腾OS

飞腾派OS系统下载:‍

https://www.iceasy.com/cloud/Phytium?pid=1877647545995448322

安装PyTorch

使用Win + x,唤起开始菜单,选择运行,键入pwsh,进入powershell。进入miniconda安装位置,激活conda

$cd D:\miniconda\shell\condabin$ .\conda-hook.ps1$ conda activate base

创建torch环境

(base) PS C:\Users> conda create --name tf python=3.10(base) PS C:\Users> conda activate torch(torch) PS C:\Users>

安装PyTorch并验证

(torch) PS C:\Users> conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia(torch) PS C:\Users> python -c"import torch; print(torch.cuda.is_available())"True

安装Ultralytics

(torch) PS C:\Users> python -m pip install ultralytics -i https://mirror.baidu.com/pypi/simple

转换数据

本文使用百度AI Studio提供的安全帽数据集,转换代码如下:

import osimport refrom tqdm.auto import tqdmfrom shutil importcopyimport yamlimportxml.etree.ElementTree as ETdef convert_box(size, box):    """    convert into (x, y, w, h) from (xmin, ymin, xmax, ymax)    """    dw, dh = 1. / size[0], 1. / size[1]    x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2]    return x * dw, y * dh, w * dw, h * dhdef convert_label(path, lb_path, year, image_id):    in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml')    out_file = open(lb_path, 'w')    tree = ET.parse(in_file)    root = tree.getroot()    size = root.find('size')    w = int(size.find('width').text)    h = int(size.find('height').text)    names = list(yaml['names'].values())  # names list    for obj in root.iter('object'):        cls = obj.find('name').text        if cls in names andint(obj.find('difficult').text) != 1:            xmlbox = obj.find('bndbox')            bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])            cls_id = names.index(cls)  # class id            out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')# Pathto images and annotationspath_images = "./HelmetDetection/images/"path_annot = "./HelmetDetection/annotations/"target_dir = "./dataset"# GetallXML file paths in path_annot and sort themxml_files = sorted(    [        os.path.join(path_annot, file_name)        for file_name in os.listdir(path_annot)        if file_name.endswith(".xml")    ])# Getall JPEG image file paths in path_images and sort themjpg_files = sorted(    [        os.path.join(path_images, file_name)        for file_name in os.listdir(path_images)        if file_name.endswith(".jpg")    ])# split datasetratio = 0.8N = len(xml_files)train_set = xml_files[:int(N * ratio)]val_set = xml_files[int(N*ratio):]dataset = {    "train": train_set,    "val": val_set}# convert cococlass_ids = [    "helmet",    "head",    "person",]class_mapping = dict(zip(range(len(class_ids)), class_ids))for k, v in dataset.items():    for xml_file in tqdm(v):                tree = ET.parse(xml_file)        root = tree.getroot()        image_names = root.find("filename").text        # write coco label        label_txt = re.sub("png", "txt", image_names)        out_file = open(f"{target_dir}/labels/{k}/{label_txt}", 'w+')        size = root.find('size')        w = int(size.find('width').text)        h = int(size.find('height').text)        for obj in root.iter("object"):            cls = obj.find("name").text            xmlbox = obj.find('bndbox')            bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])            cls_id = class_ids.index(cls)  # class id            out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')                copy(os.path.join(path_images, image_names), os.path.join(target_dir, 'images', k, image_names))        out_file.close()

转换之后,数据集格式如下:

dataset|--- images       |--- train        |--- xx.png|--- val        |--- xx.png|--- labels       |--- train        |--- xx.txt|--- val        |--- xx.txt

训练YOLOv8模型

创建数据集描述文件helmet.yml,helmet.yml描述了数据集的存放位置以及数据类别信息

train:C:/Users/dataset/images/train  # train images (relative to 'path') 4 imagesval:C:/Users/dataset/images/val  # val images (relative to 'path') 4 imagestest:  # test images (optional)# Classesnames:  0:helmet  1:head  2:person

模型训练

$ yolo task=detect mode=train model=yolov8n.pt data=C:/Users/helmet.yml device=0 batch=16 epochs=50Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size       1/10      1.24G      1.624      1.789      1.276         31        640: 100%|██████████| 500/500 [00:57<00:00,  8.62it/s]                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 63/63 [00:09<00:00,  6.52it/s]                   all       1000       5272      0.867      0.492      0.534      0.302      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size       2/10      1.35G      1.548      1.206      1.239         18        640: 100%|██████████| 500/500 [00:53<00:00,  9.37it/s]                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 63/63 [00:09<00:00,  6.77it/s]                   all       1000       5272      0.919      0.515      0.575      0.318      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size       3/10      1.34G      1.525      1.058      1.234         30        640: 100%|██████████| 500/500 [00:49<00:00, 10.05it/s]                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 63/63 [00:08<00:00,  7.32it/s]                   all       1000       5272      0.928      0.528      0.587      0.335      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size       4/10      1.33G      1.498     0.9742      1.218         42        640: 100%|██████████| 500/500 [00:49<00:00, 10.05it/s]                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 63/63 [00:08<00:00,  7.66it/s]                   all       1000       5272      0.928      0.535      0.592      0.322      Epoch    GPU_mem   box_loss   cls_loss   dfl_loss  Instances       Size       5/10      1.33G      1.489     0.9235      1.214         30        640: 100%|██████████| 500/500 [00:47<00:00, 10.59it/s]                 Class     Images  Instances      Box(P          R      mAP50  mAP50-95): 100%|██████████| 63/63 [00:08<00:00,  7.49it/s]

导出tflite模型

安装依赖项

$ python -m pip install tensorflow>2.4.1 onnx>=1.12.0 onnxsim>=0.4.1'sng4onnx>=1.0.1''onnx_graphsurgeon>=0.3.26'\$'onnx2tf>=1.15.4,<=1.17.5''tflite_support''onnxruntime' -i https://mirror.baidu.com/pypi/simple

导出模型

$ yolo exportmodel=./runs/detect/train/weights/best.pt format=tflite

模型已部署,现在让飞腾派检测安全帽吧!

安装Ultralytics

user@phytiumpi:~/Documents/cv/dl$ python -m pip install ultralytics -i https://mirror.baidu.com/pypi/simple

推理

user@phytiumpi:~/Documents/cv/dl/yolo$ yolo detect predict model=/home/user/Documents/cv/dl/yolo/best_float16.tflite source=/home/user/Documents/cv/dl/yolo/hard_hat_workers1.pngUltralytics YOLOv8.0.196 🚀 Python-3.9.2 torch-2.1.0 CPU (aarch64)Loading /home/user/Documents/cv/dl/yolo/best_float16.tflite for TensorFlow Lite inference...INFO: Created TensorFlow Lite XNNPACK delegate for CPU.image1/1 /home/user/Documents/cv/dl/yolo/hard_hat_workers1.png: 640x640 9 helmets, 959.0msSpeed: 17.2ms preprocess, 959.0ms inference, 6.7ms postprocess per image at shape (1, 3, 640, 640)Results saved to runs/detect/predict

结果可以看到即使在人多聚集的地方,飞腾派也能成功的识别安全帽!