У меня проблема заключается в том, что символы обнаруживаются в белом цвете. Цвет может быть виден для некоторых, но не для других. Есть ли способ изменить этот цвет? Я очень новичок в Python. Я уже предсказал изображения всего набора (более 100 000 изображений).
Я обучил модель YOLOv8, используя изображения, на которых интересующие символы помечены с помощью LabelStudio. При маркировке я использовал цвета, например, красный, синий и зеленый. Но каким-то образом после получения окончательной модели YOLOv8 (best.pt) и запуска прогнозирования обнаруженные символы имеют цвета, отличные от тех, которые использовались изначально.
Вопрос 1) Как я могу изменить белые цвета обнаруженных объектов?
Вопрос 2) Есть ли способ гарантировать, что цвета, используемые при маркировке в labelstudio, сохранятся в прогнозируемых изображениях?
Код: Выделить всё
import cv2
import numpy as np
# Define a function to replace the color of complete rectangular white bounding boxes
def replace_white_rectangles_with_green(image_path, output_path):
# Read the image
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
# Convert the image to grayscale for contour detection
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Threshold the grayscale image to create a binary mask for white areas
_, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)
# Find contours to detect shapes
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
# Approximate the contour to check if it forms a rectangle
epsilon = 0.02 * cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, epsilon, True)
# Check if the contour has four edges (rectangle or square)
if len(approx) == 4:
# Verify if the shape is convex (to confirm it's a rectangle/square)
if cv2.isContourConvex(approx):
# Replace the white color of the bounding box with bright green
cv2.drawContours(image, [contour], -1, (0, 255, 0), thickness=cv2.FILLED)
# Save the modified image
cv2.imwrite(output_path, image)
# Paths for input and output images
input_image_path = "path_to_your_image.jpg" # Replace with your input image path
output_image_path = "path_to_save_modified_image.jpg" # Replace with your desired output path
# Apply the function to the image
replace_white_rectangles_with_green(input_image_path, output_image_path)
print("The image has been processed, and white rectangular bounding boxes are replaced with green.")
Подробнее здесь: https://stackoverflow.com/questions/793 ... ted-images
Мобильная версия