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