'path, angle = line.strip().split() ValueError: too many values to unpack (expected 2)
Code:
from __future__ import division
import cv2
import os
import numpy as np
import scipy
import pickle
import matplotlib.pyplot as plt
from itertools import islice
LIMIT = None
DATA_FOLDER = 'driving_dataset'
TRAIN_FILE = os.path.join(DATA_FOLDER, 'data.txt')
def preprocess(img):
resized = cv2.resize((cv2.cvtColor(img, cv2.COLOR_RGB2HSV))[:, :, 1], (100, 100))
return resized
def return_data():
X = []
y = []
features = []
with open(TRAIN_FILE) as fp:
for line in islice(fp, LIMIT):
path, angle = line.strip().split()
full_path = os.path.join(DATA_FOLDER, path)
X.append(full_path)
# using angles from -pi to pi to avoid rescaling the atan in the network
y.append(float(angle) * scipy.pi / 180)
for i in range(len(X)):
img = plt.imread(X[i])
features.append(preprocess(img))
features = np.array(features).astype('float32')
labels = np.array(y).astype('float32')
with open("features", "wb") as f:
pickle.dump(features, f, protocol=4)
with open("labels", "wb") as f:
pickle.dump(labels, f, protocol=4)
return_data()
Error:
path, angle = line.strip().split()
ValueError: too many values to unpack (expected 2)
Ready I got an Autopilot code when I use the code to extract the data I'm Getting An Error Like This I Don't Know What To Do Exactly My Python Version Latest Version Thanks in advance
Solution 1:[1]
That means there is a line in your data.txt file with more than two space separated values. You are trying to put more than two values into two variables, which causes an error.
If you only want the first two values try this:
path, angle, *_ = line.strip().split()
This will assign the remaining values into _.
If this is not what you want then either your data.txt file is the problem, or you need to add more variables, for example:
path, angle, and, more, variables = line.strip().split()
EDIT
If i understand correctly, a single line looks like this
0.jpg 0.000000,2018-07-01 17:09:44:912
and you are trying to get '0.jpg' as path, and 0.00000 as angle. To achieve this you first have to get rid of everything after the comma, then split the remaining string by spaces. For example
line = line.strip().split(',')[0] # get rid of everything after the comma
path, angle = line.strip().split() # split the rest on spaces
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
