错误代码
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_rcnn',image_tag): """ Run MATLAB Selective Search code on the given image filenames to generate window proposals. Parameters ---------- image_filenames: strings Paths to images to run on. cmd: string selective search function to call: - 'selective_search' for a few quick proposals - 'selective_seach_rcnn' for R-CNN configuration for more coverage. """ # Form the MATLAB script command that processes images and write to # temporary results file. f, output_filename = tempfile.mkstemp(prefix = '{}'.format(image_tag),suffix='.mat',dir='/home/bnrc/formatm') os.close(f) fnames_cell = '{' + ','.join("'{}'".format(x) for x in image_fnames) + '}' command = "{}({}, '{}')".format(cmd, fnames_cell, output_filename) print(command) # Execute command in MATLAB. 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!") # Read the results and undo Matlab's 1-based indexing. 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] # Remove temporary file, and return. #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__': """ Run a demo. """ import time image_tag = '/000015.jpg' image_filenames = [ script_dirname + '/000015.jpg', script_dirname + '/cat.jpg' ] t = time.time() #boxes = get_windows(image_filenames) boxes = get_windows(image_filenames,cmd='selective_search_rcnn',image_tag) print(boxes[:2]) print("Processed {} images in {:.3f} s".format( len(image_filenames), time.time() - t))
报的错误:
非默认的参数在默认参数之后。image_tag这个参数在cmd之后,但cmd是默认参数的,image_tag不是默认的。
修改代码
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, image_tag,cmd='selective_search_rcnn'): """ Run MATLAB Selective Search code on the given image filenames to generate window proposals. Parameters ---------- image_filenames: strings Paths to images to run on. cmd: string selective search function to call: - 'selective_search' for a few quick proposals - 'selective_seach_rcnn' for R-CNN configuration for more coverage. """ # Form the MATLAB script command that processes images and write to # temporary results file. f, output_filename = tempfile.mkstemp(prefix = '{}'.format(image_tag),suffix='.mat',dir='/home/bnrc/formatm') os.close(f) fnames_cell = '{' + ','.join("'{}'".format(x) for x in image_fnames) + '}' command = "{}({}, '{}')".format(cmd, fnames_cell, output_filename) print(command) # Execute command in MATLAB. 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!") # Read the results and undo Matlab's 1-based indexing. 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] # Remove temporary file, and return. #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__': """ Run a demo. """ import time image_tag = '/000015.jpg' image_filenames = [ script_dirname + '/000015.jpg', script_dirname + '/cat.jpg' ] t = time.time() #boxes = get_windows(image_filenames) boxes = get_windows(image_filenames,image_tag) print(boxes[:2]) print("Processed {} images in {:.3f} s".format( len(image_filenames), time.time() - t))
之后又报错:
这个把image_tag里的/删除掉就好了