当前位置:网站首页>Convert multiple pictures into one NPY file storage

Convert multiple pictures into one NPY file storage

2022-06-11 06:12:00 Glutinous rice balls

  In my recent experiment, I need to convert a data set that is all pictures into numpy stored , Then put in the model training .

First of all, there must be a .csv file , This file stores the names and categories of all pictures in the training set , There are two columns , When downloading some data sets, this file will be brought or handled for you to download , But it can also be generated by itself , You can see here ( Main points three )

import pandas as pd
import cv2
import numpy as np
# Read the file containing the picture name and the corresponding class alias .csv file 
train = pd.read_csv('train_new.csv')
#print(train)

# Create an empty list , The back is used to store every picture in the training set 
train_image = []

'''
csv There are two columns in the document , A list of picture names , A list of corresponding class aliases , Here is the name of the traversal image , in total 2408 Zhang 
'''
for i in range(train.shape[0]):#2408
    # Load image , Here go to the folder , Traverse csv The column name in the file is 'image' The column of , Picture name , Then read the picture 
    img_cv2 = cv2.imread('train_1_50/'+train['image'][i])
    # Set the target size to (224,224,3)
    image  = cv2.resize(img_cv2,(224,224),interpolation=cv2.INTER_AREA)
    # Pixel value normalization 
    img = image / 255
    # Add the image to train_image list 
    train_image.append(img)

#  Turn the list to numpy Array 
X = np.array(train_image)#train_image There is 2408 A picture , All are numpy Format 

#  Check the size of the array 
print(X.shape)

#  View array 
print(X)

Refer to the blog post where the boss loads the image and sets the image size in one line of code , But with image.load_img Only pictures are read , And then turn it into numpy form , and cv2.imread It can be done in one step .( Why should I change it , Because I have been reporting mistakes in the way of big men , I can't solve , The private letter boss ignored me ,555...)

#  Load image , And set the target size to (224,224,3)
img = image.load_img('train_1/'+train['image'][i], target_size=(224,224,3))
#  Convert it to an array 
img = image.img_to_array(img)

Look at the effect :

There are so many here 0 Because my pictures are extracted from the video , Some of the extracted pictures are just the switching between the previous frame and the next frame , So it's black , No value , In addition, the array display is incomplete .

view list , It all shows up ...

Refer to the post

原网站

版权声明
本文为[Glutinous rice balls]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020529569398.html