检测
一种目标检测深度学习算法 ,通过对 图片中目 标的标注,训练.得到模型,下载运行模型从而检测目标
应用场景
对于不同手机,不同分辨率情况下的目标检测. 能达到很好的效果.
例如 微信跳一跳检测 , 多分辨率兼容的图像检测 , 物品检测, 带口罩检测 等等...
# 导包
from ascript.ios.screen import yolov11
方法
初始化模型
加载模型,并初始化
- 函数
yolov11.load(param_path, bin_path, yaml_path=None, nc=0, use_gpu=False)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| param_path | str | 是 | ncnn模型的param文件的绝对路径 |
| bin_path | str | 是 | ncnn模型的bin文件绝对路径 |
| yaml_path | str | 否 | data.yaml文件路径,用于自动读取标签名称 |
| nc | int | 否 | 模型的标签数量,默认0(自动检测) |
| use_gpu | bool | 否 | 是否启动gpu,默认用cpu |
- 返回值
bool 是否初始化成功
- 示例
from ascript.ios.system import R
from ascript.ios.screen import yolov11
# 基本用法
is_init = yolov11.load(R.res("custom.param"), R.res("custom.bin"))
if is_init:
print("模型初始化成功")
# 指定 yaml 文件,自动读取标签名称
is_init = yolov11.load(
R.res("model.ncnn.param"),
R.res("model.ncnn.bin"),
yaml_path=R.res("data.yaml")
)
检测目标
- 函数
yolov11.detect(img=None, target_size=640, threshold=0.4, nms_threshold=0.5, rect=None)
- 参数
| 参数 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| img | Union[Image, str] | 否 | 默认None,用当前屏幕检测, 可以填入PilImage或者http图片地址,或文件地址 |
| target_size | int | 否 | 图像进入后缩放的检测大小,模型训练一般都用640,因此这里的640一般不要改 |
| threshold | float | 否 | 置信度,默认0.4 小于这个值的会被过滤掉 |
| nms_threshold | float | 否 | 重叠阈值,一般是0.5不需要更改 |
| rect | list | 否 | 圈定检测范围 [x1, y1, x2, y2],默认全屏 |
- 返回值
数组[]:
[
{"class_id": 1, "confidence": 0.96, "rect": [551, 948, 1008, 1214], "tag": "label_name"},
{"class_id": 0, "confidence": 0.91, "rect": [296, 1151, 370, 1364], "tag": "label_name"}
]
-
class_id: 标签序号,可通过标签数组拿到对应的标签名称
-
confidence: 置信度
-
rect: 位置信息 [左,上,右,下] 4个点的坐标
-
tag: 标签名称(需要在 load 时传入 yaml_path,否则为 null)
-
示例
from ascript.ios.system import R
from ascript.ios.screen import yolov11
is_init = yolov11.load(
R.res("model.ncnn.param"),
R.res("model.ncnn.bin"),
yaml_path=R.res("data.yaml")
)
if is_init:
print("模型初始化成功")
res = yolov11.detect()
for r in res:
print("标签:", r["tag"])
print("标签ID:", r["class_id"])
print("置信度:", r["confidence"])
print("目标坐标范围:", r["rect"])
# 指定区域检测
res = yolov11.detect(rect=[0, 0, 500, 500])
获取类别数
获取当前模型的类别数量
- 函数
yolov11.nc()
- 返回值
int 模型类别数量
释放模型
- 函数
yolov11.free()