NameError:vscode中未定义名称“测试”

2024-09-27 22:26:28 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码

import pygame, sys 
from pygame.locals import *
import numpy as np
from keras.models import load_model
import cv2


WINDOWSIZEX = 640 
WINDOWSIZEY = 480

BOUNDARYINC = 5
WHITE = (255,255,255)
BLACK = (0,0 ,0)
RED = (255, 0 , 0 )

IMAGESAVE = False

MODEL = load_model("bestmodel.h5")
PREDICT = True
LABELS = {0:"Zero", 1:"One",
        2:"Two", 3:"Three",
        4:"Four", 5:"Five",
        6:"Six", 7:"Seven",
        8:"eEight", 9:"Nine"}

pygame.init()

FONT = pygame.font.Font("FreeSansBold.ttf", 18)


DISPLAYSURF = pygame.display.set_mode((WINDOWSIZEX, WINDOWSIZEY))

pygame.display.set_caption("Digit Boaard")

iswriting = False

number_xcord = []
number_ycord = []

image_cnt = 1


while True:

    for  event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == MOUSEMOTION and iswriting:     
            xcord, ycord = event.pos
            pygame.draw.circle(DISPLAYSURF, WHITE, (xcord,ycord), 4, 0 )

            number_xcord.append(xcord)
            number_ycord.append(ycord)

        if event.type == MOUSEBUTTONDOWN:
            iswriting = True

        if event.type == MOUSEBUTTONUP:
            iswriting = False
            number_xcord = sorted(number_xcord)
            number_ycord = sorted(number_ycord)

            rect_min_x , rect_max_x = max(number_xcord[0]-BOUNDARYINC, 0 ), min(WINDOWSIZEX, number_xcord[-1]+BOUNDARYINC)
            rect_min_Y , rect_max_Y = max(number_ycord[0]-BOUNDARYINC, 0 ), min(number_ycord[-1]+BOUNDARYINC, WINDOWSIZEX) 

            number_xcord = []
            number_ycord = []

            img_arr = np.array(pygame.PixelArray(DISPLAYSURF))[rect_min_x:rect_max_x, rect_min_Y:rect_max_Y].T.astype(np.float32)

            if IMAGESAVE:
                cv2.imwrite("image.png")
                image_cnt +=1

            if PREDICT:

                image = cv2.resize(img_arr, (28,28))
                image = np.pad(image, (10,10), 'constant', constant_values = 0 )
                image = cv2.resize(image, (28,28))/255

                label = str(LABELS[np.argmax(MODEL.predict(image.reshape(1,28,28,1)))])

                textSurface = FONT.render(label, True, RED, WHITE)
                textRecObj = testing.get_rect()
                textRecObj.left , textRecObj.bottom = rect_min_x, rect_max_Y

                DISPLAYSURF.blit(textSurface, textRecObj)

            if event.type == KEYDOWN:
                if event.unicode == "n":
                    DISPLAYSURF.fill(BLACK)


        pygame.display.update()

这就是我一直犯的错误

Traceback (most recent call last):
  File "c:\Users\dell\app.py", line 86, in <module>
    textRecObj = testing.get_rect()
NameError: name 'testing' is not defined

我在大学里修了一门“数据科学”课程,这是一个必修项目 我的讲师只给了我们一周的时间来建立一个手写数字识别模型。我三周前才开始学习这门课,所以我对这门课并不太熟悉。。。 我遵循了这个教程https://www.youtube.com/watch?v=L2cAjgc1-bo&lc=Ugy5PxPjcqMIrl93iNR4AaABAg&ab_channel=edureka%21 你能帮我吗


Tags: rectimageimporteventnumberiftypenp

热门问题