• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
MKT-porter
博客园    首页    新随笔    联系   管理    订阅  订阅
飞桨模型识别(1)安装和测试

 

https://github.com/PaddlePaddle/PaddleClas/blob/release/2.6/docs/zh_CN/models/PULC/PULC_safety_helmet.md

python3 -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple

pip3 install paddleclas

  

git clone https://github.com/paddlepaddle/paddleclas -b develop

cd paddleclas 

pip install .

  

 

https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.8.1/docs/paddlex/quick_start.md

 

 

安装

  • 安装PaddlePaddle

安装PaddleX前请先确保您有基础的Python运行环境。(注:当前支持Python 3.8 ~ Python 3.10下运行,更多Python版本适配中)。

# cpu
python -m pip install paddlepaddle==3.0.0b1 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/

# gpu,该命令仅适用于 CUDA 版本为 11.8 的机器环境
python -m pip install paddlepaddle-gpu==3.0.0b1 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/

# gpu,该命令仅适用于 CUDA 版本为 12.3 的机器环境
python -m pip install paddlepaddle-gpu==3.0.0b1 -i https://www.paddlepaddle.org.cn/packages/stable/cu123/

  

  • 安装PaddleX
pip install https://paddle-model-ecology.bj.bcebos.com/paddlex/whl/paddlex-3.0.0b1-py3-none-any.whl

命令行使用

一行命令即可快速体验产线效果,

 

 一行命令即可快速体验产线效果,统一的命令行格式为:

paddlex --pipeline [产线名称] --input [输入图片] --device [运行设备]

paddlex --pipeline object_detection --input https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_object_detection_002.png --device gpu:0

paddlex --pipeline object_detection --input https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_object_detection_002.png --device cpu

  

 

Python脚本使用

几行代码即可完成产线的快速推理,以小目标检测产线为例:

# https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.8.1/docs/paddlex/quick_start.md
from paddlex import create_pipeline
'''
object_detection  通用目标检测
instance_segmentation   通用实例分割
small_object_detection  小目标检测
'''
pipeline = create_pipeline(pipeline="object_detection") 

output = pipeline.predict("mao.png")
for res in output:
    res.print() ## 打印预测的结构化输出
    res.save_to_img("") ## 保存结果可视化图像
    res.save_to_json("") ## 保存预测的结构化输出

  

 

 

 

{
    "input_path": "mao.png",
    "boxes": [
        {
            "cls_id": 0,
            "label": "person",
            "score": 0.9462200403213501,
            "coordinate": [
                133.7446746826172,
                467.9471740722656,
                1767.749267578125,
                2047.337646484375
            ]
        },
        {
            "cls_id": 0,
            "label": "person",
            "score": 0.8033037781715393,
            "coordinate": [
                85.89863586425781,
                999.6807861328125,
                291.049560546875,
                1494.3759765625
            ]
        },
        {
            "cls_id": 0,
            "label": "person",
            "score": 0.7918640375137329,
            "coordinate": [
                397.83929443359375,
                877.9046630859375,
                794.94775390625,
                1552.7613525390625
            ]
        },
        {
            "cls_id": 0,
            "label": "person",
            "score": 0.699224054813385,
            "coordinate": [
                1724.633544921875,
                940.7135620117188,
                1930.8173828125,
                1609.0716552734375
            ]
        },
        {
            "cls_id": 0,
            "label": "person",
            "score": 0.6661423444747925,
            "coordinate": [
                1399.666748046875,
                1009.642578125,
                1623.57763671875,
                1440.2664794921875
            ]
        },
        {
            "cls_id": 0,
            "label": "person",
            "score": 0.5388690829277039,
            "coordinate": [
                1501.3985595703125,
                967.2034912109375,
                1599.070068359375,
                1109.2591552734375
            ]
        }
    ]
}

  

2 代码测试

安装python

https://github.com/PaddlePaddle/PaddleDetection/blob/release/2.8.1/docs/tutorials/INSTALL.md


# CUDA10.2
python -m pip install paddlepaddle-gpu==2.3.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

# CPU
python -m pip install paddlepaddle==2.3.2 -i https://pypi.tuna.tsinghua.edu.cn/simple

  

测试
# check
>>> import paddle
>>> paddle.utils.run_check()

# confirm the paddle's version
python -c "import paddle; print(paddle.__version__)"

  

Install PaddleDetection

# Clone PaddleDetection repository
cd <path/to/clone/PaddleDetection>
git clone https://github.com/PaddlePaddle/PaddleDetection.git

# Install other dependencies
cd PaddleDetection
pip install -r requirements.txt

# Compile and install paddledet
python setup.py install

  

测试

# Predict an image by GPU
export CUDA_VISIBLE_DEVICES=0
python tools/infer.py -c configs/ppyolo/ppyolo_r50vd_dcn_1x_coco.yml -o use_gpu=true weights=https://paddledet.bj.bcebos.com/models/ppyolo_r50vd_dcn_1x_coco.pdparams --infer_img=demo/000000014439.jpg

  

 

测试代码

import cv2
import paddle
from ppdet.engine import Predictor
from ppdet.config import get_config
import numpy as np
import argparse


'''
python realtime_detect.py --config configs/picodet/application/helmet/picodet_s_416_coco_lcnet.yml --model_dir output_inference


'''


def draw_boxes(img, results, threshold=0.5):
    for result in results:
        for cls_id, dets in result.items():
            for det in dets:
                score = det[1]
                if score < threshold:
                    continue
                x1, y1, x2, y2 = map(int, det[2:])
                label = f"{cls_id}:{score:.2f}"
                cv2.rectangle(img, (x1, y1), (x2, y2), (0,255,0), 2)
                cv2.putText(img, label, (x1, y1 - 5),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
    return img

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--config", type=str, required=True, help="Path to config file.")
    parser.add_argument("--model_dir", type=str, required=True, help="Path to inference model.")
    args = parser.parse_args()
     

    config="configs/picodet/application/helmet/picodet_s_416_coco_lcnet.yml"
    model_dir="output_inference"

    # 加载模型配置
    cfg = get_config(config, overrides=None)
    predictor = Predictor(cfg, model_dir, device='CPU')  # 可改为 GPU

    cap = cv2.VideoCapture(0)  # 打开USB摄像头

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        results = predictor.predict([frame], visual=False)
        frame = draw_boxes(frame, results)

        cv2.imshow("Helmet/Intrusion Detection", frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

  



posted on 2025-05-25 03:05  MKT-porter  阅读(89)  评论(2)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3