Spaces:
Sleeping
Sleeping
Update app.py
Browse files29 unmodified lines3031323334353637383940414243444546474849505152535455565729 unmodified lines# Invert black to whiteimage = np.where(image == 0, 255, image) # Black to white# Resize to 28x28image = Image.fromarray(image.astype('uint8'))image = image.resize((28, 28))# Convert back to numpy arrayimage = np.array(image)# Add black borderbordered_image = np.ones((30, 30)) * 0 # Black backgroundbordered_image[1:29, 1:29] = image # Place the image in the center# Invert colors for model if necessary (depends on model expectations)bordered_image = 1 - (bordered_image / 255.0)# Add channel and batch dimensionsbordered_image = np.expand_dims(bordered_image, axis=-1)bordered_image = np.expand_dims(bordered_image, axis=0)# Final shape checkprint(f"Image shape after preprocessing: {bordered_image.shape}")return bordered_imageexcept Exception as e:print(f"Error in preprocessing: {str(e)}")return None29 unmodified lines30313233343536373839404142434445464748495051525329 unmodified lines# Invert black to whiteimage = np.where(image == 0, 255, image) # Black to white# Resize to 28x28 (this should match the model input shape)image = Image.fromarray(image.astype('uint8'))image = image.resize((28, 28))# Convert back to numpy arrayimage = np.array(image)# Add channel and batch dimensionsimage = np.expand_dims(image, axis=-1) # Shape (28, 28, 1)image = np.expand_dims(image, axis=0) # Shape (1, 28, 28, 1)# Normalize if needed (depends on model expectations)image = 1 - (image / 255.0)# Final shape checkprint(f"Image shape after preprocessing: {image.shape}")return imageexcept Exception as e:print(f"Error in preprocessing: {str(e)}")return None
app.py+8-12