Python 3.11.2 | YOLOv8 – 如何保存到自己的文件夹

huangapple go评论119阅读模式
英文:

Python 3.11.2 | YOLOv8 - how save to own folder

问题

如何将结果保存到自己的文件夹?我在使用YOLOv8。

我尝试这样做:

from ultralytics import YOLO

model = YOLO('yolov8n.pt', 'v8')

# 输入视频路径
input_path = r"C:\Users\user\PycharmProjects\Gen\Users\Per\pre_video\BAACAgIAAxkBAAICPGSP5ivdrC9mmwABPTyoVDnBtEAhswACHCcAAu5agEgK3IT3udd3Yy8E.mp4"

# 输出目录
output_dir = r"C:\Users\user\PycharmProjects\Gen\Users\Pre\post_video"

results = model.predict(source=input_path, conf=0.45)

# 将带注释的帧保存到输出目录
results.save(save_dir=output_dir)

但是遇到了错误:

video 1/1 (135/135) C:\Users\user\PycharmProjects\Gen\Users\Pre\pre_video\BAACAgIAAxkBAAICPGSP5ivdrC9mmwABPTyoVDnBtEAhswACHCcAAu5agEgK3IT3udd3Yy8E.mp4: 384x640 1 person, 77.6ms
Speed: 2.2ms preprocess, 67.7ms inference, 1.2ms postprocess per image at shape (1, 3, 640, 640)
Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\Gen\Yolo.py", line 15, in <module>
    results.save(save_dir=output_dir)
    ^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'save'

Process finished with exit code 1

我在互联网上搜索了解决此问题的方法,所以我的代码应该能够工作,但实际上并没有。

英文:

How can I save results to my own folder? I using YOLOv8

I tried to do this :

from ultralytics import YOLO


model = YOLO(&#39;yolov8n.pt&#39;, &#39;v8&#39;)

# input video path
input_path = r&quot;C:\Users\user\PycharmProjects\Gen\Users\Per\pre_video\BAACAgIAAxkBAAICPGSP5ivdrC9mmwABPTyoVDnBtEAhswACHCcAAu5agEgK3IT3udd3Yy8E.mp4&quot;

# output directory
output_dir = r&quot;C:\Users\user\PycharmProjects\Gen\Users\Pre\post_video&quot;

results = model.predict(source=input_path, conf=0.45)

# Save annotated frames to the output directory
results.save(save_dir = input_path)

but got an error :

video 1/1 (135/135) C:\Users\user\PycharmProjects\Gen\Users\Pre\pre_video\BAACAgIAAxkBAAICPGSP5ivdrC9mmwABPTyoVDnBtEAhswACHCcAAu5agEgK3IT3udd3Yy8E.mp4: 384x640 1 person, 77.6ms
Speed: 2.2ms preprocess, 67.7ms inference, 1.2ms postprocess per image at shape (1, 3, 640, 640)
Traceback (most recent call last):
  File &quot;C:\Users\user\PycharmProjects\Gen\Yolo.py&quot;, line 15, in &lt;module&gt;
    results.save(save_dir = input_path)
    ^^^^^^^^^^^^
AttributeError: &#39;list&#39; object has no attribute &#39;save&#39;

Process finished with exit code 1

I search in the internet of solve this problem,so my code must work but it's not

答案1

得分: 2

你应该将它保存为以下内容:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
    model.predict(source=input_path, project="output_dir", name="smth")

<!-- end snippet -->

它将保存在output_dir/smth。

英文:

You should save it as following

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

model.predict(source=input_path, project=&quot;output_dir&quot;, name=&quot;smth&quot;)

<!-- end snippet -->

It will be saved as output_dir/smth

答案2

得分: 0

只翻译代码部分,不包括问题或其他内容:

# 你只需要添加一个参数,即项目路径,并更新你的代码为以下内容
from ultralytics import YOLO

model = YOLO('yolov8n.pt', 'v8')

# 输入视频路径
input_path = r"path\to\folder\filename.extension"

# 输出目录
output_dir = r"path\to\output"

results = model.predict(source=input_path, conf=0.45, project="path to output folder")

# 将带有注释的帧保存到输出目录
results.save(save_dir=input_path)
英文:

You had done perfect just add one parameter which is project and update your code to

from ultralytics import YOLO


model = YOLO(&#39;yolov8n.pt&#39;, &#39;v8&#39;)

# input video path
input_path = r&quot;path\to\folder\filename.extension&quot;

# output directory
output_dir = r&quot;path\to\output&quot;

results = model.predict(source=input_path, conf=0.45, **project=&quot;path to output folder&quot;**)

# Save annotated frames to the output directory
results.save(save_dir = input_path)

huangapple
  • 本文由 发表于 2023年6月19日 14:10:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504009.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定