点击下方卡片,关注“OpenCV与AI深度学习”
视觉/图像重磅干货,第一时间送达!
pip install opencv-python ultralytics numpy
from ultralytics import YOLO
import random
import cv2
import numpy as np
model = YOLO("yolo11m-seg.pt")
https://docs.ultralytics.com/tasks/segment/#models
img = cv2.imread( "YourImagePath" )
# 如果你想要所有的类
yolo_classes = list (model.names.values())
classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
conf = 0.2
results = model.predict(img, conf=conf)
colors = [random.choices( range ( 256 ), k= 3 ) for _ in classes_ids]
print (results)
for result in results:
for mask, box in zip (result.masks.xy, result.boxes):
points = np.int32([mask])
color_number = classes_ids.index( int (box.cls[ 0 ]))
cv2.fillPoly(img, points, colors[color_number])
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.imwrite("YourSavePath", img)
from ultralytics import YOLO
import random
import cv2
import numpy as np
model = YOLO("yolo11m-seg.pt")
img = cv2.imread("00.jpg")
# if you want all classes
yolo_classes = list(model.names.values())
classes_ids = [yolo_classes.index(clas) for clas in yolo_classes]
conf = 0.2
results = model.predict(img, conf=conf)
colors = [random.choices(range(256), k=3) for _ in classes_ids]
for result in results:
for mask, box in zip(result.masks.xy, result.boxes):
points = np.int32([mask])
color_number = classes_ids.index(int(box.cls[0]))
cv2.fillPoly(img, points, colors[color_number])
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.imwrite("res.jpg", img)
—THE END—
下载1:Pytorch常用函数手册
欢迎加入CV学习交流微信群!
觉得有用,记得点个赞和在看