Home > Mobile >  TypeError: 'int' object is not iterable
TypeError: 'int' object is not iterable

Time:12-15

The Project is basically Digit Recognition using Machine Learning in Python. For the Digit input there's an interface created using PyGame. Also, I am trying to create a dynamic size rectangle around the digit which I draw on the Output screen. Like shown in the Image

I am getting error on this line:

rect_min_Y, rect_max_Y = max(number_ycord[0]-BOUNDRYINC, ), min(number_ycord[-1] BOUNDRYINC, WINDOWSIZEX)

What am I doing wrong here?

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

WINDOWSIZEX = 640
WINDOWSIZEY = 480

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

IMAGESAVE = False

MODEL = load_model("bestmodel.h5")

LABELS = {0:"Zero", 1:"One",
        2:"Two", 3:"Three",
        4:"Four", 5:"Five",
        6:"Six", 7:"Seven",
        8:"Eight", 9:"Nine"}

#Initialize our pygame
pygame.init()

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

pygame.display.set_caption("Digit Board")

number_xcord = []
number_ycord =[]

iswriting = False

imag_cnt = 1
PREDICT = True

while True:
    for event in pygame.event.get():
        if event.type == pygame.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]-BOUNDRYINC, 0 ), min(WINDOWSIZEX, number_xcord[-1] BOUNDRYINC)
            rect_min_Y, rect_max_Y = max(number_ycord[0]-BOUNDRYINC, ), min(number_ycord[-1] BOUNDRYINC, 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 event.type==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)))])

                textSurface = FONT.render(label, True, RED, WHITE)
                textRecObj = testing.get_rect();


                textRecObj.left , textRecObj.bottom = rect_min_x, rect_max_Y

                DISPLAYSURF.blit(textSurface, textRectObj)
            
            if event.type == KEYDOWN:
                if event.unicode == "n":
                    DISPLAYSURF.fill(BLACK)
    
    pygame.display.update()

CodePudding user response:

You're missing the other argument, probably a zero in your max() function call.

The line should read:

rect_min_Y, rect_max_Y = max(number_ycord[0]-BOUNDRYINC, 0), min(number_ycord[-1] BOUNDRYINC, WINDOWSIZEX)

Or you could separate the lines for readability, it mihgt have made the error more obvious:

rect_min_Y = max(number_ycord[0]-BOUNDRYINC, 0)
rect_max_Y = min(number_ycord[-1] BOUNDRYINC, WINDOWSIZEX)

CodePudding user response:

You are missing a parameter where you calculate rect_min_Y and rect_max_Y. Try this:

rect_min_Y, rect_max_Y = max(number_ycord[0]-BOUNDRYINC, 0), min(number_ycord[-1] BOUNDRYINC, WINDOWSIZEX)
  • Related