【问题标题】:Learning Object Detection Detected result showed in discolouration学习对象检测检测结果显示变色
【发布时间】:2021-10-22 00:38:52
【问题描述】:

简要说明

最近开始学习物体检测,刚开始学习PyTorch,YOLOv5。所以我想为什么不建一个小项目来学习呢?用它来训练检测皮卡丘。

问题

我已经成功地用皮卡丘训练了模型,然后使用训练过的权重和我自己编写的 Python 脚本/代码来使用测试图像检测皮卡丘,现在问题来了,皮卡丘可以成功检测到,但是 所有结果都显示了在蓝色的变色中,应该是黄色的,都变成了蓝色,蓝色变成了黄色

Fig-1 Result images showed in blue discolouration(few example outputs)

其他信息

我已将此项目推送到 GitHub,欢迎下载或拉取调试。

GitHub repository where it contains all the files

任何解决方案/建议都会有所帮助,谢谢。

守则

"""Object detection using YOLOv5

Pokemon Pikachu detecting

"""

# import os, sys to append YOLOv5 folder path
import os, sys

# import object detection needed modules and libraries
# pillow
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import torch # PyTorch


# YOLOv5 folder path and related folder path settings
cwd = os.getcwd()
root_dir = (cwd + "/yolov5_stable")
sys.path.append(root_dir)


# import methods, functions from YOLOv5
from models.experimental import attempt_load
from utils.datasets import LoadImages
from utils.general import non_max_suppression, scale_coords
from utils.plots import colors



# define a function to show detected pikachu
def show_pikachu(img, det):
    labels = ["pikachu"]
    img = Image.fromarray(img)
    draw = ImageDraw.Draw(img)
    font_size = max(round(max(img.size)/40), 12)
    font = ImageFont.truetype(cwd + "/yolov5_stable/fonts/times.ttf")

    for info in det:
        color = colors(1)
        target, prob = int(info[5].cpu().numpy()), np.round(info[4].cpu().numpy(), 2)
        x_min, y_min, x_max, y_max = info[0], info[1], info[2], info[3]
        draw.rectangle([x_min, y_min, x_max, y_max], width = 3, outline = color)
        draw.text((x_min, y_min), labels[target] + ':' + str(prob), fill = color, font = font)

    # Bug unresolved, pikachu shown in blue discolouration

    return img


if __name__ == "__main__":
    device = "cuda:0" if torch.cuda.is_available() else "cpu"
    print("GPU State: ", device)
    
    data_path = (cwd + "/test_data/")
    weight_path = (cwd + "/yolov5_stable/weights/best_v1.pt")
    dataset = LoadImages(data_path)
    model = attempt_load(weight_path, map_location = device)
    model.to(device)
    
    for path, img, im0s, _ in dataset:
        img = torch.from_numpy(img).to(device)
        img = img.float() # uint8 to fp16/32
        img /= 255.0 # 0-255 to 0.0-1.0
        if img.ndimension() == 3:
            img = img.unsqueeze(0)
            
        pred = model(img)[0]
        pred = non_max_suppression(pred, 0.25, 0.45)
        for i, det in enumerate(pred):
            im0 = im0s.copy()
            det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round()
            result = show_pikachu(im0, det)
            result.show()

【问题讨论】:

    标签: python colors pytorch object-detection yolov5


    【解决方案1】:

    问题在于Image.fromarray 需要 RGB 格式的图像,而您以 BGR 格式提供它们。你只需要改变它。您可以在多个地方执行此操作,例如:

    Image.fromarray(img[...,::-1])  # assuming `img` is channel-last
    

    有证据表明鼠标的红色部分(红色为RGB(255, 0, 0))显示为蓝色(RGB(0, 0, 255))。仅供参考,黄色是RGB(255, 255, 0),青色是RGB(0, 255, 255),你也可以在你的情况下看到。

    【讨论】:

    • 非常感谢。我无法相信我在浏览 Pillow 文档时错过了这一点。我最初认为并专注于ImageDrawColor,并认为我可能错过了ImageColor 的导入。
    猜你喜欢
    • 2021-05-29
    • 2020-07-06
    • 2023-04-01
    • 2021-03-27
    • 2018-02-27
    • 2019-12-13
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    相关资源
    最近更新 更多