我应该如何将数据集文件夹路径放在此python代码中

时间:2019-03-29 17:06:36

标签: python path gesture-recognition

所以我正在git hub上进行这个手语手势识别python项目。 我按照自述文件进行操作,并将所有数据集文件保存在(位于项目的根文件夹中)两个单独的文件夹中,分别名为train_videos和test_videos以进行机器学习。

现在我遇到以下错误:

usage: video-to-frame.py [-h] gesture_folder target_folder
video-to-frame.py: error: the following arguments are required: gesture_folder, target_folder

以下是“ video-to-frame.py”文件中的代码。 我不知道将数据集设置程序的路径放在何处。

import cv2
import os
import pickle
from os.path import join, exists
import handsegment as hs
import argparse
from tqdm import tqdm

hc = []


def convert(gesture_folder, target_folder):
    rootPath = os.getcwd()
    majorData = os.path.abspath(target_folder)

    if not exists(majorData):
        os.makedirs(majorData)

    gesture_folder = os.path.abspath(gesture_folder)

    os.chdir(gesture_folder)
    gestures = os.listdir(os.getcwd())

    print("Source Directory containing gestures: %s" % gesture_folder)
    print("Destination Directory containing frames: %s\n" % majorData)

    for gesture in tqdm(gestures, unit='actions', ascii=True):
        gesture_path = os.path.join(gesture_folder, gesture)
        os.chdir(gesture_path)

        gesture_frames_path = os.path.join(majorData, gesture)
        if not os.path.exists(gesture_frames_path):
            os.makedirs(gesture_frames_path)

        videos = os.listdir(os.getcwd())
        videos = [video for video in videos if(os.path.isfile(video))]

        for video in tqdm(videos, unit='videos', ascii=True):
            name = os.path.abspath(video)
            cap = cv2.VideoCapture(name)  # capturing input video
            frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            lastFrame = None

            os.chdir(gesture_frames_path)
            count = 0

            # assumption only first 200 frames are important
            while count < 201:
                ret, frame = cap.read()  # extract frame
                if ret is False:
                    break
                framename = os.path.splitext(video)[0]
                framename = framename + "_frame_" + str(count) + ".jpeg"
                hc.append([join(gesture_frames_path, framename), gesture, frameCount])

                if not os.path.exists(framename):
                    frame = hs.handsegment(frame)
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                    lastFrame = frame
                    cv2.imwrite(framename, frame)

                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
                count += 1

            # repeat last frame until we get 200 frames
            while count < 201:
                framename = os.path.splitext(video)[0]
                framename = framename + "_frame_" + str(count) + ".jpeg"
                hc.append([join(gesture_frames_path, framename), gesture, frameCount])
                if not os.path.exists(framename):
                    cv2.imwrite(framename, lastFrame)
                count += 1

            os.chdir(gesture_path)
            cap.release()
            cv2.destroyAllWindows()

    os.chdir(rootPath)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Extract Individual Frames from gesture videos.')
    parser.add_argument('gesture_folder', help='Path to folder containing folders of videos of different gestures.')
    parser.add_argument('target_folder', help='Path to folder where extracted frames should be kept.')
    args = parser.parse_args()
    convert(args.gesture_folder, args.target_folder)

This是指向项目的git hub存储库的链接。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

因此,我对python来说还比较陌生,但是您共享的代码段是一个带有gesture_folder和target_folder输入的函数,因此您需要查看在整个代码中调用了convert()函数的位置,然后检查函数调用中有哪些输入。

因此,如果看起来像这样,请替换函数调用中的路径

ANAME record

但是看起来像这样

convert("C:\\User\gesturefolder","C:\\User\targetfolder" )

然后,您需要向后工作,找到在其中声明了gf和tf的位置并替换其中的路径

编辑:

convert(gf,tf)

如果用您的路径替换它不起作用,请尝试使用\或/

替换所有反斜杠。

EDIT2:

因此,我浏览了Github,并显示了在调用python代码时提供的路径

parser.add_argument('C:\\User\gesturefolder', help='Path to folder containing folders of videos of different gestures.')
parser.add_argument('C:\\User\targetfolder', help='Path to folder where extracted frames should be kept.')

您尝试过这样的事情

video-to-frame.py [-h] gesture_folder target_folder

EDIT3:

我能想到的最后一个建议是,因为您只是直接运行它的代码而不是调用它,然后仅在import和hc之间的代码开头定义路径

gesture_folder="C:\\..." #gesture folder path
target_folder="C:\\..." #target folder path

video-to-frame.py [-h] gesture_folder target_folder