自动检测图像和视频文件以进行进一步处理 [英] Automatically detecting Image and Video Files for further processing

查看:107
本文介绍了自动检测图像和视频文件以进行进一步处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想编写一个脚本,该脚本只提供文件夹或文件的路径,然后它将检测所有视频和图像文件,并将它们放在视频列表和图像列表中.对于每个视频,我将编辑帧并生成输出视频.然后,我将对所有图像执行相同的操作.但是,确定文件是有效图像还是视频的最佳方法是什么?还是我必须做这样的事情:

So I want to write a script which I only give a path to either a folder or a file, and it will then detect all video and image files and put them in a list of videos and a list of images. For each video I'll edit the frames and produce an output video. Then I'll do the same with all images. But what is the best way to determine whether a file is a valid image or video? Or do I have to do something like this:

# if os.path.isdir(path_to_folder)
video_list = []
image_list = []
for file in os.listdir(path_to_folder):
  try:
    cv2.VideoCapture(file)
    video_list.append(file)
    continue
  except:
    pass

  try:
    cv2.imread(file)
    image_list.append(file)
    continue
  except:
    pass

我真的希望有更好的方法可以做到这一点.我什至没有测试这段代码,它是如此草率,我不想不必诉诸这种方法.

I really hope there's a better way to do this. I didn't even test this code, it's so sloppy I'd prefer not to have to resort to this method.

推荐答案

有一个专门用于此目的的库.它肯定会简化任务.

There is a library available just for this purpose. It surely will simplify the task.

此处安装库文件类型使用pip.还提到了支持的文件类型列表.

Install the library filetype from HERE which can be easily installed using pip. The list of supported file types are also mentioned.

import filetype
file = filetype.guess(r'C:\Users\Jackson\Desktop\car.png')
if file is None:
    print('Cannot guess file type!')

elif:
    print('File extension: %s' % file.extension)
    print('File MIME type: %s' % file.mime)

我传入了.png文件,结果是:

I passed in a .png file which resulted in:

File extension: png
File MIME type: image/png

使用mime属性,您可以确定文件的类型.

Using the mime attribute you can determine what type your file is.

这篇关于自动检测图像和视频文件以进行进一步处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆