paddle尝试PP-Vehicle-属性识别

本文介绍了在Windows11与conda环境中,如何使用PP-Vehicle和PP-Human进行车辆和行人的属性识别。涉及安装PaddleDetection、下载模型、配置文件修改及运行代码进行视频和图片测试。在处理过程中,遇到了缺少库的问题,通过安装requirements.txt文件解决。此外,还提供了行人属性的中文标签转化功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PP-Vehicle-属性识别

windows11环境,conda虚拟环境中运行。
官网地址:PP-Vehicle

  1. 首先安装环境->

    • conda create -n paddle python=3.7
    • conda activate paddle
      两种方式,一种是缺啥装即:
    • pip install paddlepaddle-gpu
    • pip install pyyaml
    • pip install opencv-python
    • pip install scipy
    • pip install matplotlib
    • pip install scikit-learn
    • pip install pandas
    • pip install tqdm
    • pip install shapely
    • pip install requests
    • pip install lab

    另外一种是:pip install -r requirements.txt

  2. 下载PaddleDetection代码,网址为PaddleDetection

  3. 进入到文件夹内,准备测试的视频cd C:\Users\10106\Desktop\PaddleDetection-release-2.6,随便下载一个汽车的视频,放到了demo/test.mp4

  4. 下载模型,放到pretrained/vehicle_attribute_model下,predtrain是自己创建的。其他模型最好也都下载好,解压一下

  5. 编辑配置文件deploy/pipeline/config/infer_cfg_ppvehicle.yml中VEHICLE_ATTR是用于属性识别的,将其中的model_dir替换为下载的模型。

  6. 修改配置文件中deploy/pipeline/config/infer_cfg_ppvehicle.yml 的VEHICLE_ATTR项的enable: True,以启用该功能。

  7. 运行代码python deploy/pipeline/pipeline.py --config deploy/pipeline/config/infer_cfg_ppvehicle.yml --video_file=demo/test.mp4 --device=gpu,出现了个以下的问题。

  8. 测试单张图片python deploy/pipeline/pipeline.py --config deploy/pipeline/config/infer_cfg_ppvehicle.yml --device=gpu --image_file=demo/car.jpg

  9. 测试文件夹中的图片python deploy/pipeline/pipeline.py --config deploy/pipeline/config/infer_cfg_ppvehicle.yml --device=gpu --image_dir=demo

问题解决

  1. 第一次检测视频运行卡在了这里,单张图片可以。将paddle换成gpu版本后,提示Unable to use JDE/FairMOT/ByteTrace提示要安装lab。主要原因为环境安装时出现问题。最好使用pip install -r requirements.txt进行安装,要不然会缺包。
    在这里插入图片描述

PP-Human-行人属性识别

1.使用与上面相同的环境
2.下载对应的模型
3.运行单张图片python deploy/pipeline/pipeline.py --config deploy/pipeline/config/examples/infer_cfg_human_attr.yml --device=gpu --image_file=demo/hrnet_demo.jpg
4. 属性介绍

  • 性别:男、女
  • 年龄:小于18、18-60、大于60
  • 朝向:朝前、朝后、侧面
  • 配饰:眼镜、帽子、无
  • 正面持物:是、否
  • 包:双肩包、单肩包、手提包
  • 上衣风格:带条纹、带logo、带格子、拼接风格
  • 下装风格:带条纹、带图案
  • 短袖上衣:是、否
  • 长袖上衣:是、否
  • 长外套:是、否
  • 长裤:是、否
  • 短裤:是、否
  • 短裙&裙子:是、否
  • 穿靴:是、否
    可在该路径下查看标签内容PaddleDetection-release-2.6\deploy\pipeline\pphuman\attr_infer.py
    5.写了个函数把里面部分转化为中文,并且保存为txt
    def save_txt(self,result,files):
        # - Gender
        # - Age: Less than 18; 18-60; Over 60
        # - Orientation: Front; Back; Side
        # - Accessories: Glasses; Hat; None
        # - HoldObjectsInFront: Yes; No
        # - Bag: BackPack; ShoulderBag; HandBag
        # - TopStyle: UpperStride; UpperLogo; UpperPlaid; UpperSplice
        # - BottomStyle: LowerStripe; LowerPattern
        # - ShortSleeve: Yes; No
        # - LongSleeve: Yes; No
        # - LongCoat: Yes; No
        # - Trousers: Yes; No
        # - Shorts: Yes; No
        # - Skirt&Dress: Yes; No
        # - Boots: Yes; No
        txt = []
        attr_map = {'Male':'男','Female':'女',"Glasses: True":"眼镜","AgeLess18":"小于18岁","Age18-60":"18到60岁之间","AgeOver60":"大于60岁",
                    "Hat: True":"帽子","Backpack":"背包","ShoulderBag":"肩包","HandBag":"手提包","Upper: LongSleeve":"长袖上衣","Upper: ShortSleeve":"短袖上衣",
                    "LongCoat":"长外套","Trousers":"长裤","Shorts":"短裤","Skirt&Dress":"短裙","Boots":"靴子"}
        human_attr_res = result.get('attr')
        print(human_attr_res['output'])
        for human_attr in human_attr_res['output']:
            for i in range(len(human_attr)):
                if human_attr[i] in attr_map.keys():
                    # human_attr[i]  = attr_map[human_attr[i]]
                    txt.append(attr_map[human_attr[i]])
                elif "Upper:" in human_attr[i] or "Lower:" in human_attr[i]:
                    temps = human_attr[i].split(" ")
                    for temp in temps:
                        if temp in attr_map.keys():
                            txt.append(attr_map[temp])
            files[0] = files[0].replace("\\","/")
            txt_name = (files[0].split("/")[-1].split(".")[-2] + '.txt') if "." in files[0] else (files[0] + '.txt')
            with open(txt_name,"w") as file:
                # 将字典转换为字符串,以空格隔开键和值
                dict_str = ' '.join(set(txt))
                file.write(dict_str)
### PP-LCNet 的源码实现与示例 以下是基于 PaddleClas 库中 PP-LCNet 模型的一个完整代码实现示例。此模型可以用于图像分类任务,例如车辆颜色及车型属性识别。 #### 导入必要的库和初始化模型 为了加载并使用 PP-LCNet 模型,需要先导入 `paddle` 和 `PaddleClas` 库,并指定模型名称和其他必要参数: ```python import paddle from paddleclas import PaddleClas # 初始化PP-LCNet模型 paddleclas = PaddleClas("pp-lcnet", num_classes=15, device="cuda") ``` 这里,`num_classes=15` 表明该模型被配置为支持 15 种不同的类别预测[^3]。如果实际应用场景中的类别数量不同,则需调整这一参数以匹配具体需求。 #### 加载预训练权重 在实例化模型之后,通常还需要加载对应的预训练权重文件来提升性能。注意,在实际操作过程中应将默认路径下的 `.pdparams` 文件替换为自己所需的具体权重文件位置[^1]。 ```python # 假设我们已经下载好了预训练权重到本地目录 ./pretrained_models/pp_lcnet.pdparams model_path = "./pretrained_models/pp_lcnet.pdparams" paddleclas.load_pretrained_weights(model_path=model_path) ``` #### 数据前处理与推理过程 对于输入图片数据,一般要经过标准化、缩放等一系列预处理步骤后再送入网络进行推断计算。下面展示了一个简单的例子,演示如何读取一张测试图片并对它执行分类预测: ```python from PIL import Image import numpy as np def preprocess_image(image_path): img = Image.open(image_path).convert('RGB') # Resize image to fixed size (e.g., 224x224 pixels), normalize pixel values between [0,1], etc. transform = paddle.vision.transforms.Compose([ paddle.vision.transforms.Resize((224, 224)), paddle.vision.transforms.ToTensor(), paddle.vision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) return transform(img) image_path = 'test_vehicle.jpg' input_tensor = preprocess_image(image_path) input_batch = input_tensor.unsqueeze(0) # Add batch dimension output = paddleclas(input_batch) predicted_class_idx = output.argmax().item() print(f'Predicted class index: {predicted_class_idx}') ``` 以上脚本展示了完整的端到端流程——从原始图片加载直至最终获得预测结果索引号。 #### 解决依赖项缺失问题 当尝试运行上述代码片段时可能会碰到缺少某些 Python 包的情况,比如提示未找到名为 **Polygon** 的第三方库。此时可以通过 pip 工具快速安装所缺组件解决此类错误消息[^2]: ```bash pip install polygon-package-name==version_number ``` 当然,确切的包名及其版本信息取决于具体的环境设置以及报错详情描述内容。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值