繁体   English   中英

关于 python 中 matplotlib 绘图的问题

[英]A question regarding plots with matplotlib in python

我希望我能在你们中找到能帮助我掌握 matplot 第一步的人。

以下场景目前困扰着我:我正在使用经过自定义训练的 YoloV5 模型将对象检测应用于视频。

现在我可以每帧检测 n 个对象,这是我通过视频截图生成的。 这些对象我想在类似国际象棋的领域中绘制,因为这适用于视频,因此还需要有像实时图这样的东西。

到目前为止,我已经获得了信息以及提取的检测到的对象(x 轴和 y 轴)上的位置,但是我在绘制这些信息时失败了......

我实际上认为图形应该有一个 x 轴,它应该得到图像的高度,y 轴应该得到图像的宽度......

这里有人可以帮我吗?

更准确地说 - 这是推理的循环,包括带有检测的 pandas 数组。 对于每个检测到的对象,我想将图像上的位置绘制到图中

while True:
    current_screen_shot = pyautogui.screenshot(
        region=(window.left, window.top, window.width, window.height)
    )

    # start inference
    results = model(current_screen_shot, size=640)

    if results.pandas().xyxy[0].size != 0:
        # we have results

        results.xyxy[0]  # img1 predictions (tensor)
        results.pandas().xyxy[0]  # img1 predictions (pandas)
        print("Got results!")
        time.sleep(2)

        for i in range(results.n):
            ## function to plot each detected object

    else:
        # we have no results
        print("No results!")
        time.sleep(2)

在此期间我自己解决了它。 问题当然是第 8 层 ;-)

这是我为此编写的代码...:

# load the pretrained model and run the inference in a loop

import torch
import pyautogui
import matplotlib
import matplotlib.pyplot as plt

from tools.screenshot_maker.methods import get_window_object


yolov5_path = "\\yolov5\\"
model_path  = "\\models\\latest.pt"

model = torch.hub.load(yolov5_path, "custom", path=model_path, source="local")

if not torch.cuda.is_available():
    print("No GPU detected. Exiting...")
    exit()

if not model:
    print("Model not found. Exiting...")
    exit()


window = get_window_object("Your Window Name")
window.moveTo(10, 10)


matplotlib.use("TkAgg")
plt.ion()
plt.style.use("dark_background")

fig = plt.figure("Your Window Name")
fig.show()


def get_inferenced_object(result_object):
    centerX = result_object["xmin"] + (
        (result_object["xmax"] - result_object["xmin"]) / 2
    )
    centerY = result_object["ymin"] + (
        (result_object["ymax"] - result_object["ymin"]) / 2
    )
    return centerX, centerY


while True:
    plt.xlim(0, window.width)
    plt.ylim(0, window.height)
    plt.gca().invert_yaxis()
    plt.grid(
        which="major", axis="both", linestyle="-", linewidth=0.5, color="white"
    )
    plt.xlabel("X")
    plt.ylabel("Y")

    current_screen_shot = pyautogui.screenshot(
        region=(window.left, window.top, window.width, window.height)
    )

    # start interference
    results = model(current_screen_shot, size=640)

    if results.pandas().xyxy[0].size != 0:
        # we have results

        for i in range(results.n):
            print(results.pandas().xyxy[i])
            x, y = get_inferenced_object(results.pandas().xyxy[i])
            # draw_object(x, y)
            plt.scatter(x, y, color="red")

        print("Got results!")
        plt.pause(0.0001)
    else:
        # we have no results
        print("No results!")
        plt.pause(0.0001)

    plt.clf()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM