首页 > 解决方案 > NameError:名称'testing'未在vscode中定义

问题描述

这是我的代码

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

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

标签: python

解决方案


你从来没有定义什么testing是。


推荐阅读