import tempfile
import subprocess
import shlex
import os
import numpy as np
import scipy.io
script_dirname = os.path.abspath(os.path.dirname(__file__))
def get_windows(image_fnames, cmd='selective_search'):
f, output_filename = tempfile.mkstemp(suffix='.mat')
os.close(f)
fnames_cell = '{' + ','.join("'{}'".format(x) for x in image_fnames) + '}'
command = "{}({}, '{}')".format(cmd, fnames_cell, output_filename)
print(command)
mc = "matlab -nojvm -r \"try; {}; catch; exit; end; exit\"".format(command)
pid = subprocess.Popen(
shlex.split(mc), stdout=open('/dev/null', 'w'), cwd=script_dirname)
retcode = pid.wait()
if retcode != 0:
raise Exception("Matlab script did not exit successfully!")
all_boxes = list(scipy.io.loadmat(output_filename)['all_boxes'][0])
subtractor = np.array((1, 1, 0, 0))[np.newaxis, :]
all_boxes = [boxes - subtractor for boxes in all_boxes]
os.remove(output_filename)
if len(all_boxes) != len(image_fnames):
raise Exception("Something went wrong computing the windows!")
return all_boxes
if __name__ == '__main__':
import time
image_filenames = [
script_dirname + '/000015.jpg',
script_dirname + '/cat.jpg'
] * 4
t = time.time()
boxes = get_windows(image_filenames)
print(boxes[:2])
print("Processed {} images in {:.3f} s".format(
len(image_filenames), time.time() - t))
代码已经测试过,应该是可以正常工作的。
但当我运行代码时,出现了以下错误:
Traceback (most recent call last):
File "selective_search.py", line 62, in <module>
boxes = get_windows(image_filenames)
File "selective_search.py", line 42, in get_windows
all_boxes = list(scipy.io.loadmat(output_filename)['all_boxes'][0])
File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 131, in loadmat
MR = mat_reader_factory(file_name, appendmat, **kwargs)
File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/mio.py", line 55, in mat_reader_factory
mjv, mnv = get_matfile_version(byte_stream)
File "/usr/lib/python2.7/dist-packages/scipy/io/matlab/miobase.py", line 218, in get_matfile_version
buffer = fileobj.read(4))
TypeError: buffer is too small for requested array
我正在使用MATLAB 2015。
我该如何解决这个问题?
python/numpy
的错误,而不是 MATLAB 代码产生的。除了提供错误回调堆栈外,您还需要提供一些打印输出(如果需要,请添加诊断打印)。帮助我们确定错误的来源。 - hpaulj