- demo.py
from io import BytesIO
from pathlib import Path
# pip install opencv-python pillow ultralytics
import cv2
from PIL import Image
from ultralytics import YOLO # type:ignore[import-untyped]
filename = "test.jpg"
model = YOLO("yolov8s.pt")
img = Image.open(BytesIO(Path(filename).read_bytes()))
result = model.predict(img, save=False, show=False, verbose=False, classes=[0])[0]
has_person = bool(result.summary())
if has_person:
print(f"file: {filename}, result: {has_person}")
content = cv2.imencode(".jpg", result.plot())[1].tobytes() # 检测结果转成jpg图片
Path("test_result.jpg").write_bytes(content) # 保存到本地
result.show() # 打开画框了的图片
else:
print(f"{filename=}, result: {result.verbose()}")
标签:detect,jpg,filename,person,only,result,Path,import
From: https://www.cnblogs.com/waketzheng/p/18399044