I have such a code:
我有这样的代码:
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))
import tempfil