qq_42519463 2021-05-06 23:49 采纳率: 62.5%
浏览 345
已采纳

PyCharm运行出现AttributeError

出现这个问题怎么解决?网上找找不到。

module 'subprocess' has no attribute 'src'

  • 写回答

2条回答 默认 最新

  • GitCode 官方 企业官方账号 2021-05-07 17:37
    关注

    我在本地跑了下2_extract_files.py,我的系统也是windows

    昨天我已经回答了你的1_move_files.py问题,如果你操作成功了,直接把我下面的代码复制过去替换掉原始代码(2_extract_files.py)就能直接跑起来了

    首先提醒下,别忘记安装ffmpeg这个python包,我用的是Anaconda,直接一条指令就可以安装

    conda install ffmpeg

    先上代码:

    """
    After moving all the files using the 1_ file, we run this one to extract
    the images from the videos and also create a data file we can use
    for training and testing later.
    """
    import csv
    import glob
    import os
    import os.path
    from subprocess import call
    
    def extract_files():
        """After we have all of our videos split between train and test, and
        all nested within folders representing their classes, we need to
        make a data file that we can reference when training our RNN(s).
        This will let us keep track of image sequences and other parts
        of the training process.
    
        We'll first need to extract images from each of the videos. We'll
        need to record the following data in the file:
    
        [train|test], class, filename, nb frames
    
        Extracting can be done with ffmpeg:
        `ffmpeg -i video.mpg image-%04d.jpg`
        """
        data_file = []
        folders = ['./train/', './test/']
    
        for folder in folders:
            class_folders = glob.glob(folder + '*')
    
            for vid_class in class_folders:
                class_files = glob.glob(vid_class + '/*.avi')
    
                for video_path in class_files:
                    # Get the parts of the file.
                    video_parts = get_video_parts(video_path)
    
                    train_or_test, classname, filename_no_ext, filename = video_parts
    
                    # Only extract if we haven't done it yet. Otherwise, just get
                    # the info.
                    if not check_already_extracted(video_parts):
                        # Now extract it.
                        src = train_or_test + '/' + classname + '/' + \
                            filename
                        dest = train_or_test + '/' + classname + '/' + \
                            filename_no_ext + '-%04d.jpg'
                        call(["ffmpeg", "-i", src, dest])
    
                    # Now get how many frames it is.
                    nb_frames = get_nb_frames_for_video(video_parts)
    
                    data_file.append([train_or_test, classname, filename_no_ext, nb_frames])
    
                    print("Generated %d frames for %s" % (nb_frames, filename_no_ext))
    
        with open('data_file.csv', 'w') as fout:
            writer = csv.writer(fout)
            writer.writerows(data_file)
    
        print("Extracted and wrote %d video files." % (len(data_file)))
    
    def get_nb_frames_for_video(video_parts):
        """Given video parts of an (assumed) already extracted video, return
        the number of frames that were extracted."""
        train_or_test, classname, filename_no_ext, _ = video_parts
        generated_files = glob.glob(train_or_test + '/' + classname + '/' +
                                    filename_no_ext + '*.jpg')
        return len(generated_files)
    
    def get_video_parts(video_path):
        """Given a full path to a video, return its parts."""
        print(video_path, type(video_path))
        parts = video_path.split('\\')
        parts.append(parts[0].split('/'))
        print(parts)
        filename = parts[2]
        filename_no_ext = filename.split('.')[0]
        classname = parts[1]
        train_or_test = parts[3][1]
    
        return train_or_test, classname, filename_no_ext, filename
    
    def check_already_extracted(video_parts):
        """Check to see if we created the -0001 frame of this file."""
        train_or_test, classname, filename_no_ext, _ = video_parts
        return bool(os.path.exists(train_or_test + '/' + classname +
                                   '/' + filename_no_ext + '-0001.jpg'))
    
    def main():
        """
        Extract images from videos and build a new file that we
        can use as our data input file. It can have format:
    
        [train|test], class, filename, nb frames
        """
        extract_files()
    
    if __name__ == '__main__':
        main()
    

    下面我说下我修改的地方,如下图所示,其实如果你是在Linux上跑这个代码,完全不需要更改,windows再次表示悲哀,斜杆与反斜杠。。。太坑

    下面是我跑起来的截图,不过需要一段时间,毕竟是把视频切成一帧帧图片

     

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程