ValueError: not enough values to unpack (expected3, got1) error

Asked 2 years ago, Updated 2 years ago, 19 views

The following error appears, but I am not sure where to improve.
It seems that the pixels2.append(tmp) part of line 72 is an error.

error messages:

ValueError: not enough values to unpack(expected3,got1)

#Read, process and list files from each folder one at a time

I don't understand the meaning of the code after this, so I'd like to ask for an explanation.
Thank you for your cooperation.

source code:

#%%
import cv2
import numpy as np

importos
os.chdir("c:\\Users\\ume\\Desktop\\Python Exercise\\Introduction to Preprocessing for Machine Learning\\ants")
os.chdir("c:\\Users\\ume\\Desktop\\Python Exercise\\Introduction to Preprocessing for Machine Learning\\bees")

# Specifying the Image Folder
dirs=['ants', 'bees']

# Generate a list containing pixel values and labels for images
pixels = [ ]# Description variable
labels=[]#objective variable

# Read, process and list files from each folder one at a time
for i, din enumerate (dirs):
    # Retrieving Files
    os.chdir("c:\\Users\\ume\\Desktop\\Python Exercise\\Introduction to Preprocessing for Machine Learning\\"+d)
    files=os.listdir("c:\\Users\\ume\\Desktop\\Python Exercise\\Introduction to Preprocessing for Machine Learning\\"+d)

    For fin files:
        # Grayscale loading of images
        img=cv2.imread(f,cv2.IMREAD_GRAYSCALE)

        # Resize image
        img = cv2.resize(img, (128,128))
        # pixel value storage
        img=np.array(img).flatten().tolist()
        pixels.append(img)
        
        # Store image labels in a list 
        labels.append(i)
        
# %%
import pandas aspd

pixels_df=pd.DataFrame(pixels)
pixels_df = pixels_df / 255

labels_df = pd.DataFrame(labels)
labels_df = labels_df.rename (columns={0:"label"})

img_set=pd.concat([pixels_df, labels_df], axis=1)
img_set.head()

#%%
# Generate a list containing pixel values and labels for images
pixels2 = [ ]# Description variable
labels2 = [ ]# objective variable
tmp = [ ]

# Read, process and list files from each folder one at a time
for i, din enumerate (dirs):
    # Retrieving Files
      os.chdir("c:\\Users\\ume\\Desktop\\Python Exercise\\Introduction to Preprocessing for Machine Learning\\"+d)
      files=os.listdir("c:\\Users\\ume\\Desktop\\Python Exercise\\Introduction to Preprocessing for Machine Learning\\"+d)
    
      For fin files:
        # Grayscale loading of images
        img2 = cv2.imread(f,cv2.IMREAD_GRAYSCALE)
        # Resize image
        img2 = cv2.resize(img2,(128,128)))
        # Split pixel array by B, G, and R
        b,g,r = cv2.split(img2)        
        # pixel value storage
        b=np.array(b).flatten().tolist()
        g=np.array(g).flatten().tolist()
        r=np.array(r).flatten().tolist()
        tmp = b + g + r
        pixels2.append(tmp)
        
        # Store image labels in a list 
        labels2.append(i)

python

2022-09-29 22:06

1 Answers

If you don't present the error message without abbreviating it, you won't get the correct advice and answers, or as @Takahiro Funahashi commented, it will take extra time.
The problem will be reproduced, so please try again and add the error message exactly as it appears in the text.

And in the error message for the amount presented, it seems that the problem is not pixels2.append(tmp), but b,g,r=cv2.split(img2).

Perhaps the reason is that the img2 data is loaded as grayscale, as shown in the comments and processing of the image loading above.

for fin files:
    # Grayscale loading of images
    img2 = cv2.imread(f,cv2.IMREAD_GRAYSCALE)

There is an article like this, and it says that if you enter a grayscale image, you get an error.
Channel split cv2.split()

for RGB images

It is important to note that the order of variables should be BGR instead of RGB, and that color images should be used for img.

If you put an image grayed out with cv2.cvtColor() in cv2.split(), you will get an error because the image has only one channel.

Although the above does not provide details of the error, considering the error message in the question, the return value for a color image is three, but the return value for a grayscale image is only one.

Since the subsequent processing of the questioning program is also considered to be programmed with color image data, it is recommended that you do not specify cv2.IMREAD_GRAYSCALE when reading the image file.

for fin files:
    # Image loaded in color (default)
    img2 = cv2.imread(f)

Read and write image files with OpenCV (Python)

The cv2.imread() method can be flagged as the second argument. Specifically, you can specify the following flags:

cv2. Read the IMREAD_COLOR image in color (RGB). Default value for the argument.


2022-09-29 22:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.