我正在使用imagemagick来识别文件夹中截断的图像的过早结束。我写的脚本可以成功识别图像,但是速度很慢。这可能是因为它必须将整个映像加载到内存中,但是考虑到我将文件复制到磁盘所花的时间,这应该不会增加操作的时间。我正在分析700,000张图像,以目前的速度,该操作将花费一个多月才能完成,更不用说CPU使用率极高了。
foreach (string f in files)
{
Tuple<int, string> result = ImageCorrupt(f);
int exitCode = result.Item1;
if (exitCode != 0)...
}
public static Tuple<int, string> ImageCorrupt(string pathToImage)
{
var cmd = "magick identify -regard-warnings -verbose \"" + pathToImage + "\"";
var startInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C " + cmd,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var process = new Process
{
StartInfo = startInfo
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
if (!process.WaitForExit(30000))
{
process.Kill();
}
return Tuple.Create(process.ExitCode, process.StandardError.ReadToEnd());
}
这是我要在图像中识别的问题的example。
有没有一种方法可以优化脚本的性能?还是有更快的方法来识别图像问题?
答案 0 :(得分:3)
您可以尝试net-vips。它不会发现像imagemagick一样多的图像格式,但是可以完成基本的TIF / JPG / PNG / GIF等格式,而且速度要快得多。
我将通过计算平均像素值来测试图像。这样,您可以保证读取每个像素,并且操作便宜。
我实际上没有在这里安装C#,但是在pyvips(Python绑定到与net-vips相同的库)中,它将是:
import sys
import pyvips
for filename in sys.argv[1:]:
try:
# the fail option makes pyvips throw an exception on a file
# format error
# sequential access means libvips will stream the image rather than
# loading it into memory
image = pyvips.Image.new_from_file(filename,
fail=True, access="sequential")
avg = image.avg()
except pyvips.Error as e:
print("{}: {}".format(filename, e))
我可以这样运行它:
$ for i in {1..1000}; do cp ~/pics/k2.jpg $i.jpg; done
$ cp ~/pics/k2_broken.jpg .
$ vipsheader 1.jpg
1.jpg: 1450x2048 uchar, 3 bands, srgb, jpegload
那是一张损坏的图像,一千张OK图像,全部为1450x2048。然后:
$ time ../sanity.py *.jpg
k2_broken.jpg: unable to call avg
VipsJpeg: Premature end of JPEG file
VipsJpeg: out of order read at line 48
real 0m23.424s
因此,在这台普通的笔记本电脑上,它在23秒内发现了损坏的图像。
您的带有身份标识的循环(尽管仅测试100张图像)将是:
$ time for i in {1..100}; do if ! identify -regard-warnings -verbose $i.jpg > /dev/null; then echo $i: error; fi; done
real 0m21.454s
大约相同的时间长度,因此在此测试中,net-vip的速度要快10倍左右。
由于net-vip相对节省内存,因此您还可以一次运行很多,具体取决于您拥有多少个内核。这应该给出几乎线性的加速。
在这两核,四线程的笔记本电脑上,我看到:
$ time parallel --jobs 10 -m ../sanity.py ::: *.jpg
k2_broken.jpg: unable to call avg
VipsJpeg: Premature end of JPEG file
VipsJpeg: out of order read at line 48
real 0m10.828s
现在下降到11s,可处理1,001张图像。