Hi!
- User
Hi!
- Assistant
Hello! How can I assist you today?
- User
I want to change my script below to do the same thing and this to add: please save all the images in "isolate/" folder, if doesn't exist create it, and write an image file with the plygon contours, the masks drawn with a very subtle line and without the class name drwan on the image. Here is the code: `from pathlib import Path
import cv2 import numpy as np
from ultralytics import YOLO
m = YOLO("runs/segment/train10/weights/best.pt") res = m.predict("newtestamentinsy0000unse_g7r6_0227.jpg")
Iterate detection results
for r in res: img = np.copy(r.orig_img) img_name = Path(r.path).stem
# Iterate each object contour print(r) for ci, c in enumerate(r): label = c.names[c.boxes.cls.tolist().pop()] b_mask = np.zeros(img.shape[:2], np.uint8) # Create contour mask contour = c.masks.xy.pop().astype(np.int32).reshape(-1, 1, 2) _ = cv2.drawContours(b_mask, [contour], -1, (255, 255, 255), cv2.FILLED) # Choose one: # OPTION-1: Isolate object with black background mask3ch = cv2.cvtColor(b_mask, cv2.COLOR_GRAY2BGR) isolated = cv2.bitwise_and(mask3ch, img) # OPTION-2: Isolate object with transparent background (when saved as PNG) #isolated = np.dstack([img, b_mask]) # OPTIONAL: detection crop (from either OPT1 or OPT2) x1, y1, x2, y2 = c.boxes.xyxy.cpu().numpy().squeeze().astype(np.int32) iso_crop = isolated[y1:y2, x1:x2] cv2.imwrite(f"{img_name}_{ci}.jpg", iso_crop) # TODO your actions go here` - Assistant
- User
I don't need a contour for every match, please write all the contours in a single file
- Assistant
- User
How can a change the contour line in the drawn image to light green?
- Assistant
To change the contour line color to light green in the drawn image, you can modify the color parameter in the
cv2.drawContours()function. In OpenCV, colors are typically represented as BGR (Blue, Green, Red) tuples.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.