英文:
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('yolov8n.pt', 'v8')
# input video path
input_path = r"C:\Users\user\PycharmProjects\Gen\Users\Per\pre_video\BAACAgIAAxkBAAICPGSP5ivdrC9mmwABPTyoVDnBtEAhswACHCcAAu5agEgK3IT3udd3Yy8E.mp4"
# output directory
output_dir = r"C:\Users\user\PycharmProjects\Gen\Users\Pre\post_video"
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 "C:\Users\user\PycharmProjects\Gen\Yolo.py", line 15, in <module>
results.save(save_dir = input_path)
^^^^^^^^^^^^
AttributeError: 'list' object has no attribute 'save'
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="output_dir", name="smth")
<!-- 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('yolov8n.pt', 'v8')
# input video path
input_path = r"path\to\folder\filename.extension"
# output directory
output_dir = r"path\to\output"
results = model.predict(source=input_path, conf=0.45, **project="path to output folder"**)
# Save annotated frames to the output directory
results.save(save_dir = input_path)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论