Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

python - I got this error ValueError: not enough values to unpack (expected 2, got 1)

Python

Iam trying to extract frames from UCF-101 dataset, but I got this error: ValueError: not enough values to unpack (expected 2, got 1). Iam using Anaconda 1.7.2

This line shows the argument error: sequence_type, sequence_name = video_path.split(".avi")[0].split("/")[-2:]

import av
import glob
import os
import time
import tqdm
import datetime
import argparse


def extract_frames(video_path):
    frames = []
    video = av.open(video_path)
    for frame in video.decode(0):
        yield frame.to_image()


prev_time = time.time()
if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--dataset_path", type=str, default="UCF-101", help="Path to UCF-101 dataset")
    opt = parser.parse_args()
    print(opt)

    time_left = 0
    video_paths = glob.glob(os.path.join(opt.dataset_path, "*", "*.avi"))
    for i, video_path in enumerate(video_paths):
        sequence_type, sequence_name = video_path.split(".avi")[0].split("/")[-2:]
        sequence_path = os.path.join(f"{opt.dataset_path}-frames", sequence_type, sequence_name)

        if os.path.exists(sequence_path):
            continue

        os.makedirs(sequence_path, exist_ok=True)

        # Extract frames
        for j, frame in enumerate(
            tqdm.tqdm(
                extract_frames(video_path, time_left),
                desc=f"[{i}/{len(video_paths)}] {sequence_name} : ETA {time_left}",
            )
        ):
            frame.save(os.path.join(sequence_path, f"{j}.jpg"))

        # Determine approximate time left
        videos_left = len(video_paths) - (i + 1)
        time_left = datetime.timedelta(seconds=videos_left * (time.time() - prev_time))
        prev_time = time.time()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your responses to advice and questions in the comments suggest to me that this code is not really yours and you have no clear idea what this line

sequence_type, sequence_name = video_path.split(".avi")[0].split("/")[-2:]

is supposed to do. So let me explain. It takes video_path, which is a full path to an .avi file, for example '~/folder/subfolder/subsubfolder/myfile.avi'. It splits off the extension .avi, and from what remains, that is, '~/folder/subfolder/subsubfolder/myfile', it takes the last two elements, that is subsubfolder and myfile, and uses those as values for sequence_type and sequence_name.

But for that to work, video_path has to contain both subsubfolder and myfile, with a slash between, so, at a minimum, 'subsubfolder/myfile.avi'.

One of the pathnames returned by glob.glob() does not contain even one / character. That is why split("/") is returning a 1-list, which is not enough to unpack into the 2-tuple (sequence_type, sequence_name), resulting in the error Not enough values to unpack.

The only reason that occurs to me why this might be happening is that you are working on Windows where the path separator is not /. That would mean that the pathname your code is getting back from glob.glob() would be something like 'C:Usersfoldersubfoldersubsubfoldermyfile.avi'. No slashes: there couldn't be, because Windows filenames can't contain slashes.

That is the reason why commenters asked you to show us the value you were trying to unpack, instead of just telling us about it. Programming problems are very often about tiny specific details. So, in the absence of an explicit response from you, this is still only a guess, but if the guess is correct, the line that is giving you the error should be

sequence_type, sequence_name = video_path.split(".avi")[0].split("\")[-2:]

(note, you have to double the backslash) or, if you want to the code to be platform-independent,

sequence_type, sequence_name = video_path.split(".avi")[0].split(os.path.sep)[-2:]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...