资源简介
再生成模型后,通常我们都是对小图进行检测,但是最终应用很多时候是对大图检测,输出大图,代码调用deeplabv3的pb文件对大幅图像进行检测,并输出大图。
代码片段和文件信息
#时间:2018.12.11
#功能:将大图送入网络进行检测
#输入:大幅图像
#输出:大幅图像检测结果
#运行环境:python3.5tensorflow1.6win10
#存在问题:未生成彩色图像
import tensorflow as tf
import numpy as np
import cv2
import os
import math
from PIL import Image
from keras.preprocessing.image import load_img img_to_arrayarray_to_img
imagedir = ‘E:/1204/1211test/menghaimosaic/‘
savepath = ‘E:/1204/1211test/3/‘
cropsize = 500
def cutimage(imgrowcol):
newimg = img[col*cropsize:(col+1)*cropsizerow*cropsize:(row+1)*cropsize]
return newimg
img = tf.placeholder(tf.uint8 [1cropsize cropsize 3] name=“img“)
with open(“E:/models-master/research/deeplab/output/model.pb“ “rb“) as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
output = tf.import_graph_def(graph_def input_map={“ImageTensor:0“: img}
return_elements=[“SemanticPredictions:0“])
sess = tf.Session()
imagelist = os.listdir(imagedir)
for i in range(0len(imagelist)):
path = os.path.join(imagedirimagelist[i])
filename = os.path.splitext(imagelist[i])[0]
filetype = os.path.splitext(imagelist[i])[1]
image = cv2.imread(path)
imagesize = image.shape
width = imagesize[1]
height = imagesize[0]
newwidth = (math.ceil(width/cropsize))*cropsize
newheight = (math.ceil(height/cropsize))*cropsize
deltaW = newwidth - width
deltaH = newheight - height
augimage = cv2.copyMakeBorder(image0deltaH0deltaW
cv2.BORDER_CONSTANTvalue = [000])
rgbim = np.zeros((newheightnewwidth3) ‘uint8‘)
R = rgbim[::0]
G = rgbim[::1]
B = rgbim[::2]
newimage = np.zeros((newheightnewwidth)‘uint8‘)
print(newimage.shape)
for col in range(math.ceil(height/cropsize)):
for row in range (math.ceil(width/cropsize)):
cutimg = cutimage(augimagerowcol)
cutimg = np.asarray(cutimg)
cutimg = np.expand_dims(cutimg axis=0).astype(np.uint8)
result = sess.run(output feed_dict = {img:cutimg})
grayim = np.squeeze(result[0])
newimage[col*cropsize:(col+1)*cropsizerow*cropsize:(row+1)*cropsize] = grayim[::]
print(col+row)
newimage = Image.fromarray(np.uint8(newimage))
savedir = os.path.join(savepathfilename + ‘.png‘)
newimage.save(savedir)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2304 2018-12-12 11:03 det.py
文件 80 2018-12-12 11:03 使用说明.txt
- 上一篇:deeplabv3调用pb模型批量预测图片
- 下一篇:6s.exe 大气 纠正 软件
评论
共有 条评论