Есть ли способ как-нибудь преобразовать экран пигмея gui в изображение? - PullRequest
1 голос
/ 27 марта 2020

Итак, я работаю над интерактивной версией MNIST рукописного проекта по классификации изображений di git с pygame, в котором пользователь dr aws использует gui и, исходя из этого, модель, которая у меня уже есть. made будет смотреть на экран и выводить прогноз относительно того, какое число содержит изображение. Моя проблема в том, что я не уверен, какой подход предпринять, чтобы все, что отображается на gui, было введено в качестве входных данных для моей модели, чтобы предсказать (в качестве входных данных требуется изображение)

Вот код, который я сделал для gui:

import pygame as pg
#I'm importing a function that I made with my model
#Takes an image input and spits out a prediction as to what the number displayed in the image should be
from MNIST_Classification_GUI import makePrediction

pg.init()
screen = pg.display.set_mode([800, 600])
pg.display.set_caption("Draw a Number")

radius = 10
black = (0, 0, 0)
isGoing = True
screen.fill((255, 255, 255))
last_pos = (0, 0)


def roundline(srf, color, start, end, radius=1):
    dx = end[0]-start[0]
    dy = end[1]-start[1]
    distance = max(abs(dx), abs(dy))
    for i in range(distance):
        x = int( start[0]+float(i)/distance*dx)
        y = int( start[1]+float(i)/distance*dy)
        pg.draw.circle(srf, color, (x, y), radius)

#To be used for the popup text containing the prediction
pg.font.init()
myFont = pg.font.SysFont("Sans Serif", 10)

draw_on = False
while isGoing:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            isGoing = False

        if event.type == pg.MOUSEBUTTONDOWN:
            spot = event.pos
            pg.draw.circle(screen, black, spot, radius)
            draw_on = True

        #This is the part where I want to somehow obtain an image from the gui
        #So when the user stops drawing, the popup text appears with the prediction
        if event.type == pg.MOUSEBUTTONUP:
            draw_on = False
            #The makePrediction takes an image input and returns the predicted value
            prediction = makePrediction(screen)
            textSurface = myFont.render(f"The number should be {prediction}", False, black)
            screen.blit(textSurface, (0, 0))

        if event.type == pg.MOUSEMOTION:
            if draw_on:
                pg.draw.circle(screen, black, event.pos, radius)
                roundline(screen, black, event.pos, last_pos, radius)
            last_pos = event.pos
        pg.display.flip()

pg.quit()
...