【守护工地安全】YOLOv8实现安全帽检测

简介: 【守护工地安全】YOLOv8实现安全帽检测

数据集

该图像数据集包含8000张图像,两个类别分别是安全帽与人、以其中200多张图像为验证集,其余为训练集。

模型训练

准备好数据集以后,直接按下面的命令行运行即可:

yolo train model=yolov8s.pt data=hat_dataset.yaml epochs=50 imgsz=640 batch=4

导出与测试

下面的命令行,导出模型为ONNX格式,同时预测模型的实际推理能力

yolo export model=hat_best.pt format=onnx
yolo predict model=hat_best.pt source=./hats

### 部署推理

转成ONNX格式文件以后,基于OpenVINO-Python部署推理,相关代码如下

# Read IR
model = ie.read_model(model="hat_best.onnx")
compiled_model = ie.compile_model(model=model, device_name="CPU")
output_layer = compiled_model.output(0)

capture = cv.VideoCapture("D:/images/video/hat_test.mp4")
while True:
    _, frame = capture.read()
    if frame is None:
        print("End of stream")
        break
    bgr = format_yolov8(frame)
    img_h, img_w, img_c = bgr.shape

    start = time.time()
    image = cv.dnn.blobFromImage(bgr, 1 / 255.0, (640, 640), swapRB=True, crop=False)

    res = compiled_model([image])[output_layer] # 1x84x8400
    rows = np.squeeze(res, 0).T
    class_ids = []
    confidences = []
    boxes = []
    x_factor = img_w / 640
    y_factor = img_h / 640

    for r in range(rows.shape[0]):
        row = rows[r]
        classes_scores = row[4:]
        _, _, _, max_indx = cv.minMaxLoc(classes_scores)
        class_id = max_indx[1]
        if (classes_scores[class_id] > .25):
            confidences.append(classes_scores[class_id])
            class_ids.append(class_id)
            x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
            left = int((x - 0.5 * w) * x_factor)
            top = int((y - 0.5 * h) * y_factor)
            width = int(w * x_factor)
            height = int(h * y_factor)
            box = np.array([left, top, width, height])
            boxes.append(box)

    indexes = cv.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
    for index in indexes:
        box = boxes[index]
        color = colors[int(class_ids[index]) % len(colors)]
        cv.rectangle(frame, box, color, 2)
        cv.rectangle(frame, (box[0], box[1] - 20), (box[0] + box[2], box[1]), color, -1)
        cv.putText(frame, class_list[class_ids[index]], (box[0], box[1] - 10), cv.FONT_HERSHEY_SIMPLEX, .5, (0, 0, 0))
    end = time.time()
    inf_end = end - start
    fps = 1 / inf_end
    fps_label = "FPS: %.2f" % fps
    cv.putText(frame, fps_label, (20, 45), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

    cv.imshow("YOLOv8 hat Detection", frame)
    cc = cv.waitKey(1)
    if cc == 27:
        break
cv.destroyAllWindows()

认真学习 YOLOv8 点这里。

相关文章
|
6月前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
11059 21
|
4月前
|
机器学习/深度学习 数据采集 人工智能
无人机行人精准检测数据集分享(适用于YOLO系列深度学习分类检测任务)
本数据集含6990张无人机航拍图像,专为YOLO系列模型优化,聚焦行人(person)单类高精度检测。涵盖城市道路、广场、景区等多场景,提供YOLO格式标注与标准目录结构,支持快速训练部署,助力公共安全、应急响应与城市管理智能化。(239字)
263 0
|
算法框架/工具
成功解决INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is c
成功解决INFO: pip is looking at multiple versions of keras-preprocessing to determine which version is c
|
6月前
|
人工智能 IDE 机器人
给阿里产品团队的真心话:千问很强,但我们需要它能“干活”!
作为一名一线程序员,建议千问尽快打通项目目录读取功能,并实现手机电脑端同步,做真正能干活的AI工具。
|
8月前
|
人工智能 IDE API
从Prompt工程到Skill工程:Agent Skills开放标准彻底改变了AI协作方式
本文介绍了 Agent Skill 这一新兴的 AI 开放标准,将带你从是什么到怎么用在实际工作中,彻底掌握这个比 Prompt 更高级、比 MCP 更易用的 AI 编程神器。
3275 2
|
10月前
|
Linux Docker 容器
docker下部署 vLLM 启动Qwen3-VL-32B-Instruct模型
本文介绍在CentOS系统、A10 6×24G显卡环境下,通过Docker部署vLLM并启动Qwen3-VL-32B-Instruct大模型的完整流程,涵盖镜像拉取、容器配置、多卡并行与显存优化设置,支持32K上下文,附带启动脚本及调用验证示例。
9581 2
|
12月前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
8185 12
|
机器学习/深度学习 监控 安全
基于YOLOv8的有无戴安全帽检测识别项目
本项目通过集成 YOLOv8 强大的目标检测能力与 PyQt5 的可视化界面,构建了一个 实用性强、易于部署、安全帽自动识别系统。无论是单张图片、视频监控,还是实时摄像头输入,该系统均可稳定工作,准确判断佩戴与未佩戴状态,极大减轻了传统人工巡查压力。
基于YOLOv8的有无戴安全帽检测识别项目