def caffe_preprocess_and_compute(pimg, caffe_transformer=None, caffe_net=None, output_layers=None):
 if (caffe_net is not None):
  if (output_layers is None):
   output_layers = caffe_net.outputs
  img_data_rs = resize_image(pimg, sz=(256, 256))
  image = caffe.io.load_image(StringIO(img_data_rs))
  (H, W, _) = image.shape
  (_, _, h, w) = caffe_net.blobs['data'].data.shape
  h_off = max(((H - h) / 2), 0)
  w_off = max(((W - w) / 2), 0)
  crop = image[h_off:(h_off + h), w_off:(w_off + w), :]
  transformed_image = caffe_transformer.preprocess('data', crop)
  transformed_image.shape = ((1,) + transformed_image.shape)
  input_name = caffe_net.inputs[0]
  all_outputs = caffe_net.forward_all(blobs=output_layers, **{input_name: transformed_image})
  outputs = all_outputs[output_layers[0]][0].astype(float)
  return outputs
 else:
  return []

def caffe_preprocess_and_compute ( pimg , caffe_transformer = None , caffe_net = None , output_layers = None ) : if ( caffe_net is not None ) : if ( output_layers is None ) : output_layers = caffe_net . outputs img_data_rs = resize_image ( pimg , sz = ( 256 , 256 ) ) image = caffe . io . load_image ( StringIO ( img_data_rs ) ) ( H , W , _ ) = image . shape ( _ , _ , h , w ) = caffe_net . blobs [ 'data' ] . data . shape h_off = max ( ( ( H - h ) / 2 ) , 0 ) w_off = max ( ( ( W - w ) / 2 ) , 0 ) crop = image [ h_off : ( h_off + h ) , w_off : ( w_off + w ) , : ] transformed_image = caffe_transformer . preprocess ( 'data' , crop ) transformed_image . shape = ( ( 1 , ) + transformed_image . shape ) input_name = caffe_net . inputs [ 0 ] all_outputs = caffe_net . forward_all ( blobs = output_layers , ** { input_name : transformed_image } ) outputs = all_outputs [ output_layers [ 0 ] ] [ 0 ] . astype ( float ) return outputs else : return [ ]
run a caffe network on an input image after preprocessing it to prepare it for caffe .