首页 > 代码库 > python 使用记录

python 使用记录

1. python 加载 caffe mean.binaryproto

#### mean_file ####
proto_data = http://www.mamicode.com/open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]

 

2. python 加载caffe 图片输入

#### load input and configure preprocessing type 1 ####
im = caffe.io.load_image("tmp.jpg") # 读进来是RGB格式和0~1
transformer = caffe.io.Transformer({data: net_full_conv.blobs[data].data.shape})
#transformer.set_mean(‘data‘, np.load(caffe_root + ‘python/caffe/imagenet/ilsvrc_2012_mean.npy‘).mean(1).mean(1))
transformer.set_transpose(data, (2, 0, 1)) # channel width(cols) height(cows)
transformer.set_channel_swap(data, (2, 1, 0)) # 将RGB变换到BGR
transformer.set_raw_scale(data, 255.0) # 缩放至0~255

 

#### load input and configure preprocessing type 2 ####
cv_im = cv2.imread("tmp.jpg")
transformer.set_transpose(data, (2, 0, 1)) # channel 

 

3. python opencv

####python opencv####

img.shape[0] = img.rows = 高
img.shape[1] = img.cols = 宽
Python: dst = cv2.resize(src, NewShape[1], NewShape[0], interpolation = cv2.INTER_LINEAR)
C++: resize(midImage, tmpImage, cv::Size(ratio*midImage.cols, ratio*midImage.rows), (0, 0), (0, 0), cv::INTER_AREA);

 

4. python Matplotlib

####python Matplotlib####

import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np

lena = mpimg.imread(lena.png) # 读取和代码处于同一目录下的 lena.png
# 此时 lena 就已经是一个 np.array 了,可以对它进行任意处理
lena.shape #(512, 512, 3)

plt.imshow(lena) # 显示图片
plt.axis(off) # 不显示坐标轴
plt.show()

fig = plt.figure() # 新图 0
plt.savefig() # 保存
plt.close(all) # 关闭图 0

 

python 使用记录