agrobot_base/Python/yolov8-seg/view.py

38 lines
1.0 KiB
Python
Raw Normal View History

2026-02-24 13:38:14 +00:00
from PIL import Image, ImageDraw
img = "20260126_121759_474"
img_path = f"images/train/{img}.png"
label_path = f"labels/train/{img}.txt"
img = Image.open(img_path)
w, h = img.size
with open(label_path, "r") as f:
lines = [ln.strip() for ln in f.readlines() if ln.strip()]
draw = ImageDraw.Draw(img, "RGBA")
# Cor semântica por classe (ajuste se quiser)
colors = {
0: ( 0, 128, 0, 90), # classe 0 (erva) = verde
1: ( 0, 0, 128, 90), # classe 1 (cana) = azul
}
for line in lines:
parts = line.split()
cls = int(parts[0])
coords = list(map(float, parts[1:]))
# pares (x, y) normalizados [0,1]
pts_px = []
for x, y in zip(coords[0::2], coords[1::2]):
# só pra garantir que não passa um pouquinho de 0..1
x = max(0.0, min(1.0, x))
y = max(0.0, min(1.0, y))
pts_px.append((x * w, y * h))
color = colors.get(cls, (255, 255, 255, 90))
draw.polygon(pts_px, fill=color, outline=color[:3] + (255,))
img.show() # ou img.save("overlay.png")