Skip to content
Advertisement

A question regarding plots with matplotlib in python

i hope i can find someone among you who can help me to master the first steps with matplot.

the following scenario is currently bothering me: I am using a custom trained YoloV5 model to apply object detection to a video.

now i can detect n objects per frame, which i generate via screenshot from the video. These objects I would like to plot in a chess-like field as this is applied to a video so it is also necessary to have something like a live-graph.

i am already so far that i get the information and also the positions on the detected objects (x-axis and y-axis) extracted, however i fail miserably at plotting this information…

i actually thought that the graph should have an x-axis which should get the height of the image and the y-axis the width of the image…

could someone here help me with this?

to be more precise – here’s the loop for the inferecing including the pandas array with the detections. for each detected object i want to plot the position on the image to the figure

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)

Advertisement

Answer

I have solved it myself in the meantime. The problem was of course the layer 8 ;-)

here the code i wrote for this… :

# 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()

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement