按键精灵安卓手机脚本开发工具:Yolo.RunModel 运行Yolo模型命令说明

部署运行你感兴趣的模型镜像

函数名称

Yolo.RunModel 运行Yolo模型

函数功能

运行Yolo模型并识别返回目标

支持版本:按键精灵安卓版v4.2.0或以上版本

函数语法

result = Yolo.RunModel(index,sim[,options])

函数参数

参数名参数类型是否必选参数说明
indexint运行识别的模型索引序号,建议取值范围:0-9
simfloat相似度阈值,过滤此相似度以下的结果,0到1之间的小数
optionstable扩展参数集合,用于额外配置和未来扩展

函数返回

返回名返回类型返回说明
resulttable返回响应结果信息的键值表

result 包含的字段

字段名字段类型字段说明
codeint执行函数的结果状态码,0为执行成功,非0为失败,失败原因查看msg字段
msgstring执行结果的描述,失败则描述具体的失败原因
datatable识别到结果内容的键值表

data 包含的字段:

字段名字段类型字段说明
textstring识别到目标的标签字符串
xint识别到目标的左上角x坐标
yint识别到目标的左上角y坐标
wint识别到目标的范围宽度
hint识别到目标的范围高度
scorefloat识别到目标的可信度,0到1之间的小数

代码范例

Dim paramPath,binPath,lablePath,ranTime,ret,modelPath = GetTempDir() & "yolov8/"
Dir.Create modelPath
//将配置文件通过附件释放或url下载放置到模型目录皆可
PutAttachment modelPath, "*"
paramPath = modelPath & "model.ncnn.param"
binPath = modelPath & "model.ncnn.bin"
lablePath = modelPath & "classes.txt"
TracePrint dir.Exist(paramPath),dir.Exist(binPath),dir.Exist(lablePath)
//设置模型1,ncnn简化版模型
ret = yolo.setModel(1, 640, paramPath, binPath, lablePath)
//设置模型2,ncnn标准版模型
//ret = yolo.setModel(2, 640, paramPath, binPath, lablePath,1,1,{"modelType":2})
If ret["code"] = 0 Then 
    TracePrint "set model successed"
Else 
    TracePrint "set model failed,error msg:"&ret["msg"]
    ExitScript
End If

Do
    ranTime = TickCount()
    //通过已设置模型1识别返回目标
    ret = Yolo.runModel(1,0.75)
    TracePrint TickCount()-ranTime
    If ret["code"] = 0 Then 
        For i = 1 to len(ret["data"])
            TracePrint "识别到目标:" & ret["data"][i]["text"] &_
            ",左上角x坐标:" & ret["data"][i]["x"] &_
            ",左上角y坐标:" & ret["data"][i]["y"] &_
            ",区域宽:" & ret["data"][i]["w"] &_
            ",区域高:" & ret["data"][i]["h"] &_
            ",可信度:" & ret["data"][i]["score"]
        Next    
    Else
        TracePrint "error code:"& ret["code"] &",error msg:"&ret["msg"]
    End If
    Delay 2000
Loop

您可能感兴趣的与本文相关的镜像

Yolo-v5

Yolo-v5

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

# Ultralytics YOLO 🚀, AGPL-3.0 license from pathlib import Path from ultralytics.engine.model import Model from ultralytics.models import yolo from ultralytics.nn.tasks import ClassificationModel, DetectionModel, OBBModel, PoseModel, SegmentationModel, WorldModel from ultralytics.utils import ROOT, yaml_load class YOLO(Model): """YOLO (You Only Look Once) object detection model.""" def __init__(self, model="yolo11n.pt", task=None, verbose=False): """Initialize YOLO model, switching to YOLOWorld if model filename contains '-world'.""" path = Path(model) if "-world" in path.stem and path.suffix in {".pt", ".yaml", ".yml"}: # if YOLOWorld PyTorch model new_instance = YOLOWorld(path, verbose=verbose) self.__class__ = type(new_instance) self.__dict__ = new_instance.__dict__ else: # Continue with default YOLO initialization super().__init__(model=model, task=task, verbose=verbose) @property def task_map(self): """Map head to model, trainer, validator, and predictor classes.""" return { "classify": { "model": ClassificationModel, "trainer": yolo.classify.ClassificationTrainer, "validator": yolo.classify.ClassificationValidator, "predictor": yolo.classify.ClassificationPredictor, }, "detect": { "model": DetectionModel, "trainer": yolo.detect.DetectionTrainer, "validator": yolo.detect.DetectionValidator, "predictor": yolo.detect.DetectionPredictor, }, "segment": { "model": SegmentationModel, "trainer": yolo.segment.SegmentationTrainer, "validator": yolo.segment.SegmentationValidator, "predictor": yolo.segment.SegmentationPredictor, }, "pose": { "model": PoseModel, "trainer": yolo.pose.PoseTrainer, "validator": yolo.pose.PoseValidator, "predictor": yolo.pose.PosePredictor, }, "obb": { "model": OBBModel, "trainer": yolo.obb.OBBTrainer, "validator": yolo.obb.OBBValidator, "predictor": yolo.obb.OBBPredictor, }, } class YOLOWorld(Model): """YOLO-World object detection model.""" def __init__(self, model="yolov8s-world.pt", verbose=False) -> None: """ Initialize YOLOv8-World model with a pre-trained model file. Loads a YOLOv8-World model for object detection. If no custom class names are provided, it assigns default COCO class names. Args: model (str | Path): Path to the pre-trained model file. Supports *.pt and *.yaml formats. verbose (bool): If True, prints additional information during initialization. """ super().__init__(model=model, task="detect", verbose=verbose) # Assign default COCO class names when there are no custom names if not hasattr(self.model, "names"): self.model.names = yaml_load(ROOT / "cfg/datasets/coco8.yaml").get("names") @property def task_map(self): """Map head to model, validator, and predictor classes.""" return { "detect": { "model": WorldModel, "validator": yolo.detect.DetectionValidator, "predictor": yolo.detect.DetectionPredictor, "trainer": yolo.world.WorldTrainer, } } def set_classes(self, classes): """ Set classes. Args: classes (List(str)): A list of categories i.e. ["person"]. """ self.model.set_classes(classes) # Remove background if it's given background = " " if background in classes: classes.remove(background) self.model.names = classes # Reset method class names # self.predictor = None # reset predictor otherwise old names remain if self.predictor: self.predictor.model.names = classes 如果要调用YOLO模型,如:“model = YOLO(model="weight_file/yolo11/yolo11n.pt", task="detect")”是不是非得在model参数里加-world才能成功,不然他就只会用默认初始化
11-06
<think>首先,用户的问题是关于调用YOLO模型时,是否必须在model参数中添加"-world"才能成功,否则使用默认初始化。用户提到了引用[1],其中展示了导出YOLO模型的代码:`model = YOLO('yolov8s-worldv2.pt')`。 回顾系统级指令: - 所有行内数学表达式使用$...$格式。 - 独立公式使用$$...$$并单独成段。 - LaTeX语法正确。 - 使用中文回答。 - 生成相关问题。 - 回答中引用的段落末尾自然地添加引用标识,如[^1]。 用户的问题是基于引用[1]的代码,询问是否需要添加"-world"。引用[1]中的代码是:`model = YOLO('yolov8s-worldv2.pt')`,这使用了"yolov8s-worldv2.pt"权重文件。 从引用[1]看,模型初始化是`YOLO('yolov8s-worldv2.pt')`,文件名中包含"-world",这表明这是针对YOLO-World模型的权重。YOLO-World是YOLO的一个变种,用于开放词汇目标检测。 用户的问题具体是:是否必须在model参数中添加"-world"才能成功调用,否则使用默认初始化?这意味着,如果用户指定一个不带"-world"的文件名,比如"yolov8s.pt",会发生什么? 为了回答这个问题: - 需要基于常识和提供的引用。引用[1]显示使用了-world模型。 - 引用[2]提到"YOLO-World",暗示这是一个特定的模型。 - 引用[3]讨论了YOLO模型的推理,但没有指定-world。 在YOLO框架中(如Ultralytics YOLO),模型通过权重文件初始化。权重文件通常对应于特定的架构。例如: - "yolov8s.pt" 可能是标准YOLOv8模型的权重。 - "yolov8s-worldv2.pt" 是YOLO-World版本的权重。 默认情况下,如果您调用`YOLO('yolov8s.pt')`,它会加载标准的YOLOv8模型。如果调用`YOLO('yolov8s-worldv2.pt')`,它会加载YOLO-World模型。 所以,回答应该是:是的,要调用YOLO-World模型,您必须在model参数中指定包含"-world"的权重文件路径;否则,它会使用您指定的文件对应的模型,如果文件是标准YOLO,则会加载标准模型。 引用[1]中的代码:`model = YOLO('yolov8s-worldv2.pt')` 然后 `model.set_classes(["person", "bus"])`,这表明YOLO-World支持自定义类。 如果是标准YOLO模型(如yolov8s.pt),它可能不支持动态设置类;类是在训练时固定的。 因此,核心点是:模型的行为取决于您加载的权重文件。如果您想使用YOLO-World的特性(如自定义类),您需要加载-world权重文件。 回答结构: - 使用中文。 - 清晰解释。 - 末尾添加引用标识。例如,引用[1]讨论了导出代码。 生成相关问题:根据系统指令,需要在回答后生成相关问题。 相关问题应与主题相关,比如YOLO模型的使用、初始化等。 可能的回答大纲: 1. 解释是否需要加"-world":是,对于YOLO-World模型。 2. 如果不加,会发生什么:它会加载指定的权重文件,如果文件是标准YOLO,则初始化标准模型。 3. 引用支持:提到引用[1]展示了world模型的导出。 4. 生成相关问题。 生成相关问题: - 类似于示例:"§§相关问题§§" 后列出问题。 - 例如: 1. YOLO-World模型与标准YOLO模型有什么区别? 2. 如何训练自定义的YOLO-World模型? 3. 在导出模型时,有哪些格式可选? 确保回答真实可靠:基于提供的引用。 最终回答: - 开头直接回答问题。 - 解释原因。 - 添加引用。 - 然后生成相关问题。 </think>### 调用YOLO模型时是否需要添加 "-world" 后缀? 在调用 YOLO 模型时,**必须在 model 参数中添加 "-world" 后缀才能成功加载 YOLO-World 模型**;否则,系统会尝试加载默认的标准 YOLO 模型(如 YOLOv8),但这可能导致错误或功能缺失,因为它不支持 YOLO-World 的特性(如动态自定义类别)。具体分析如下: 1. **模型初始化机制**: 在 Ultralytics YOLO 框架中,模型通过权重文件路径初始化。权重文件名中的后缀(如 "-world")决定了模型的架构和功能: - 如果指定 `"yolov8s-worldv2.pt"`(含 "-world"),它会加载 YOLO-World 模型,支持开放词汇目标检测和动态设置类别。 - 如果指定不带 "-world" 的文件(例如 `"yolov8s.pt"`),它会加载标准 YOLO 模型,类别固定为预训练的 COCO 数据集(80 类),无法动态自定义[^1]。 2. **关键代码示例**: 引用[1] 中的导出代码明确使用了 `yolov8s-worldv2.pt` 文件: ```python model = YOLO('yolov8s-worldv2.pt') # 必须添加 "-world" 后缀初始化 YOLO-World model.set_classes(["person", "bus"]) # 动态设置自定义类 ``` 如果省略后缀(如改用 `YOLO('yolov8s.pt')`),`set_classes()` 方法将失败,因为标准模型不支持此功能。 3. **潜在风险**: - **成功但功能受限**:如果加载了标准模型文件,模型可能正常初始化但无法使用 YOLO-World 的特性(如自定义类),导致后续推理错误。 - **初始化失败**:如果文件名错误(如使用不存在的权重),系统会抛出异常(例如 `FileNotFoundError`)。 - 因此,务必确保文件名与模型类型匹配,特别是在集成到工作流(如 ComfyUI 节点)时[^2]。 4. **最佳实践**: - 始终检查权重文件的后缀:使用 "-world" 文件加载 YOLO-World 模型。 - 在导出或推理前验证模型类型,例如: ```python print(model.model.names) # 输出类别列表,检查是否包含自定义类 ``` 总之,添加 "-world" 是调用 YOLO-World 模型的必要条件;省略后缀会导致默认初始化标准模型,这可能不符合您的需求。更多细节可参考 Ultralytics 官方文档[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值