基于paddlepaddle框架的安全帽检测

第一步:解压数据集

        在aistudio中选择安全帽检测数据集即可。

第二步:统计数据集信息

!unzip -q /home/aistudio/data/data50329/HelmetDetection.zip -d /home/aistudio/helmet

第二步:统计数据集信息

        这步是查看数据集的内容,知晓数据集后更易调参。

import re
import glob
import matplotlib.pyplot as plt
files = glob.glob("/home/aistudio/helmet/annotations/*.xml")
width_and_height_regex = re.compile(r'<width>(?P<width>.*?)</width>.*?<height>(?P<height>.*?)</height>',re.S)
class_name = re.compile(r'<name>(?P<class_name>.*?)</name>')
all_class = {}
target_num = 0
heights = []
widths = []
for file in files:
    with open(file, mode="r") as f:
        text=f.read()

        hw = width_and_height_regex.findall(text)
        heights.append(hw[0][0])
        widths.append(hw[0][1])

        names = class_name.findall(text)
        for name in names:
            if name not in all_class.keys():
                all_class.setdefault(name, 0)
            else:
                all_class[name]+=1
print(f"数据集有{len(files)}张图片")
for key,item in all_class.items():
    print(f"{key}类有{item}个")
plt.title("heights,widths")
plt.scatter(heights,widths)

第三步:下载所需套件和包

        下载完PaddleDetection套件后运行一下命令

%cd
!pip install -r PaddleDetection-2.5.0/requirements.txtglob. Glob

!pip install paddlex

        分割数据集

!paddlex --split_dataset --format voc --dataset_dir /home/aistudio/helmet/ --val_value 0.2

第四步:开始训练

%cd
%cd PaddleDetection-2.5.0
!python tools/train.py -c /home/aistudio/utils/ppyoloe_plus_crn_m_80e_coco.yml \
    --eval --amp --use_vdl=True --vdl_log_dir="../output"

第五步:预测

%cd PaddleDetection-2.5.0
!python tools/infer.py -c /home/aistudio/utils/ppyoloe_plus_crn_m_80e_coco.yml \
                      --infer_img=/home/aistudio/elsedata/1313.jpg \
                      --output_dir=/home/aistudio/inferout/\
                    -o weights=/home/aistudio/PaddleDetection-2.5.0/output/ppyoloe_plus_crn_m_80e_coco/best_model.pdparams \

详情见:https://aistudio.baidu.com/aistudio/projectdetail/6347916

安全帽检测是一种常见的计算机视觉应用,可以用于工业安全和交通安全等领域。基于 OpenCV 的安全帽检测可以通过以下步骤实现: 1. 导入 OpenCV 库和其他必要的库。 2. 加载输入图像并将其转换为灰度图像。 3. 使用 Haar 级联分类器检测人脸,并将检测到的人脸区域保存。 4. 在人脸区域内使用 Haar 级联分类器检测安全帽,并将检测到的安全帽区域保存。 5. 绘制边界框和标签,以显示检测结果。 6. 显示输出图像或保存输出图像。 以下是一个基于 OpenCV 的安全帽检测的 Python 示例代码: ```python import cv2 import numpy as np # 加载 Haar 级联分类器 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') helmet_cascade = cv2.CascadeClassifier('haarcascade_helmet.xml') # 加载输入图像 img = cv2.imread('test.jpg') # 将图像转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 使用 Haar 级联分类器检测人脸 faces = face_cascade.detectMultiScale(gray, 1.3, 5) # 在人脸区域内使用 Haar 级联分类器检测安全帽 for (x,y,w,h) in faces: roi_gray = gray[y:y+h, x:x+w] roi_color = img[y:y+h, x:x+w] helmets = helmet_cascade.detectMultiScale(roi_gray, 1.3, 5) for (hx,hy,hw,hh) in helmets: cv2.rectangle(roi_color,(hx,hy),(hx+hw,hy+hh),(0,255,0),2) cv2.putText(roi_color, 'Helmet', (hx, hy-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2) # 显示输出图像 cv2.imshow('img',img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 注意:在使用此代码之前,需要先下载并安装 Haar 级联分类器模型。可以从 OpenCV 官方网站下载预训练模型,或者使用已经训练好的模型。在上面的示例中,我们使用了两个模型:`haarcascade_frontalface_default.xml` 和 `haarcascade_helmet.xml`。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值