Saving an image as numpy array

Saving an image as numpy array

I am not able to load images into numpy array and getting an error like this...

I am not able to load images into numpy array and getting an error like this...

ValueError: could not broadcast input array from shape (175,217,3) into shape (100,100,3)

The function code:

import cv2
import numpy as np
import os

train_data_dir = '/home/ec2-user/SageMaker/malaria-detection-model/malaria/training' valid_data_dir = '/home/ec2-user/SageMaker/malaria-detection-model/malaria/validation'

declare the number of samples in each category

nb_train_samples = 22045 # training samples nb_valid_samples = 5513# validation samples num_classes = 2 img_rows_orig = 100 img_cols_orig = 100

def load_training_data(): labels = os.listdir(train_data_dir) total = len(labels)

X_train = np.ndarray((nb_train_samples, img_rows_orig, img_cols_orig, 3), dtype=np.uint8)
Y_train = np.zeros((nb_train_samples,), dtype='uint8')

i = 0
j = 0
for label in labels:
    image_names_train = os.listdir(os.path.join(train_data_dir, label))
    total = len(image_names_train)
    print(label, total)
    for image_name in image_names_train:
        img = cv2.imread(os.path.join(train_data_dir, label, image_name), cv2.IMREAD_COLOR)
        img = np.array([img])
        X_train[i] = img
        Y_train[i] = j

        if i % 100 == 0:
            print('Done: {0}/{1} images'.format(i, total))
        i += 1
    j += 1    
print(i)                
print('Loading done.')

np.save('imgs_train.npy', X_train, Y_train)
return X_train, Y_train

This function is part of the file load_data.py that can be found in malaria_cell_classification_code.zip file from:

https://ceb.nlm.nih.gov/repositories/malaria-datasets/


I tried to change X_train and Y_train to list instead of numpy array. The function halts at np.save method.

X_train = Y_train = list()
        X_train.append(img)
        Y_train.append(j)

What is the correct and standard way to save images in numpy?


After resizing the image, I get different error:

Done: 19400/9887 images
Done: 19500/9887 images
Done: 19600/9887 images
Done: 19700/9887 images
Done: 19800/9887 images
19842
Loading done.
Transform targets to keras compatible format.
Done: 19800/9887 images
19842
Loading done.
Transform targets to keras compatible format.


Creating validation images...

Parasitized 1098

error Traceback (most recent call last) <ipython-input-6-8008be74f482> in <module>() 2 #load data for training 3 X_train, Y_train = load_resized_training_data(img_rows, img_cols) ----> 4 X_valid, Y_valid = load_resized_validation_data(img_rows, img_cols) 5 #print the shape of the data 6 print(X_train.shape, Y_train.shape, X_valid.shape, Y_valid.shape)

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_resized_validation_data(img_rows, img_cols) 103 def load_resized_validation_data(img_rows, img_cols): 104 --> 105 X_valid, Y_valid = load_validation_data() 106 107 # Resize images

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_validation_data() 75 76 img = np.array([img]) ---> 77 img2 = cv2.resize(img, (100, 100)) 78 X_valid[i] = img2 79 Y_valid[i] = j

error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3427: error: (-215:Assertion failed) !dsize.empty() in function 'resize'


Creating validation images...

Parasitized 1098

error Traceback (most recent call last) <ipython-input-6-8008be74f482> in <module>() 2 #load data for training 3 X_train, Y_train = load_resized_training_data(img_rows, img_cols) ----> 4 X_valid, Y_valid = load_resized_validation_data(img_rows, img_cols) 5 #print the shape of the data 6 print(X_train.shape, Y_train.shape, X_valid.shape, Y_valid.shape)

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_resized_validation_data(img_rows, img_cols) 103 def load_resized_validation_data(img_rows, img_cols): 104 --> 105 X_valid, Y_valid = load_validation_data() 106 107 # Resize images

~/SageMaker/malaria-detection-model/malaria_cell_classification_code/load_data.py in load_validation_data() 75 76 img = np.array([img]) ---> 77 img2 = cv2.resize(img, (100, 100)) 78 X_valid[i] = img2 79 Y_valid[i] = j

error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/resize.cpp:3427: error: (-215:Assertion failed) !dsize.empty() in function 'resize'

The complete script can be found here...

https://gist.github.com/shantanuo/cfe0913b367647890451f5ae3f6fb691

python image numpy opencv

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Create a Virtual Pen and Eraser with Python OpenCV - Genial Code

Learn Free how to create a virtual pen and eraser with python and OpenCV with source code and complete guide. This entire application is built fundamentally on contour detection. It can be thought of as something like closed color curves on compromises that have the same color or intensity, it's like a blob. In this project we use color masking to get the binary mask of our target color pen, then we use the counter detection to find the location of this pen and the contour to find it.

Find and Draw Contours using Python OpenCV | Image Segmentation OpenCV

This video explains how to find and draw image contours using Python's Opencv. It will also explain the concept of image segmentation in opencv, a technique

Image Panorama Stitching with OpenCV and Python

In this article, we will learn how to perform image stitching using Python and OpenCV

OpenCV-Python: Reading and Writing Images and Videos

Step-by-step guide to capture live stream with camera.In addition, this tutorial will cover saving a frame as an image as well as saving it as a video in your machine. We are going to use the built-in VideoCapture and VideoWriter classes that comes with OpenCV.

NumPy Array Tutorial - Python NumPy Array Operations and Methods

Learn about NumPy Array, NumPy Array creation, various array functions, array indexing & Slicing, array operations, methods and dimensions,It also includes array splitting, reshaping, and joining of arrays. Even the other external libraries in Python relate to NumPy arrays.