我正在使用Tensorflow和Flask开发一个python(3.6)项目。我需要在flask函数中运行Tensorflow模型,但它返回一个错误。
这是我的密码:
应用程序。派克
app = Flask(__name__)
@app.route('/<path:image_url>')
def grab_image(image_url):
url = str(image_url)
r = requests.get(url, allow_redirects=True)
print('Url is as: {}'.format(url))
filename = url.split('/')[-1]
open(filename, 'wb').write(r.content)
img = Image.open(filename)
# img.show()
img.save('/Users/abdul/PycharmProjects/ImgSeg/uploads/photo.jpg', 'JPEG')
# create_new_images('/Users/abdul/PycharmProjects/ImgSeg/uploads/photo.jpg')
class DeepLabModel(object):
"""Class to load deeplab model and run inference."""
INPUT_TENSOR_NAME = 'ImageTensor:0'
OUTPUT_TENSOR_NAME = 'SemanticPredictions:0'
INPUT_SIZE = 513
FROZEN_GRAPH_NAME = 'frozen_inference_graph'
def __init__(self, tarball_path):
"""Creates and loads pretrained deeplab model."""
self.graph = tf.Graph()
graph_def = None
# Extract frozen graph from tar archive.
tar_file = tarfile.open(tarball_path)
for tar_info in tar_file.getmembers():
if self.FROZEN_GRAPH_NAME in os.path.basename(tar_info.name):
file_handle = tar_file.extractfile(tar_info)
graph_def = tf.GraphDef.FromString(file_handle.read())
break
tar_file.close()
if graph_def is None:
raise RuntimeError('Cannot find inference graph in tar archive.')
with self.graph.as_default():
tf.import_graph_def(graph_def, name='')
self.sess = tf.Session(graph=self.graph)
def run(self, image):
"""Runs inference on a single image.
Args:
image: A PIL.Image object, raw input image.
Returns:
resized_image: RGB image resized from original input image.
seg_map: Segmentation map of `resized_image`.
"""
width, height = image.size
resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
target_size = (int(resize_ratio * width), int(resize_ratio * height))
resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
batch_seg_map = self.sess.run(
self.OUTPUT_TENSOR_NAME,
feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
seg_map = batch_seg_map[0]
return resized_image, seg_map
def create_pascal_label_colormap():
"""Creates a label colormap used in PASCAL VOC segmentation benchmark.
Returns:
A Colormap for visualizing segmentation results.
"""
colormap = np.zeros((256, 3), dtype=int)
ind = np.arange(256, dtype=int)
for shift in reversed(range(8)):
for channel in range(3):
colormap[:, channel] |= ((ind >> channel) & 1) << shift
ind >>= 3
return colormap
def label_to_color_image(label):
"""Adds color defined by the dataset colormap to the label.
Args:
label: A 2D array with integer type, storing the segmentation label.
Returns:
result: A 2D array with floating type. The element of the array
is the color indexed by the corresponding element in the input label
to the PASCAL color map.
Raises:
ValueError: If label is not of rank 2 or its value is larger than color
map maximum entry.
"""
if label.ndim != 2:
raise ValueError('Expect 2-D input label')
colormap = create_pascal_label_colormap()
if np.max(label) >= len(colormap):
raise ValueError('label value too large.')
return colormap[label]
def vis_segmentation(image, seg_map):
"""Visualizes input image, segmentation map and overlay view."""
plt.figure(figsize=(15, 5))
grid_spec = gridspec.GridSpec(1, 4, width_ratios=[6, 6, 6, 1])
plt.subplot(grid_spec[0])
plt.imshow(image)
plt.axis('off')
plt.title('input image')
plt.subplot(grid_spec[1])
seg_image = label_to_color_image(seg_map).astype(np.uint8)
plt.imshow(seg_image)
plt.axis('off')
plt.title('segmentation map')
plt.subplot(grid_spec[2])
plt.imshow(image)
plt.imshow(seg_image, alpha=0.7)
plt.axis('off')
plt.title('segmentation overlay')
unique_labels = np.unique(seg_map)
ax = plt.subplot(grid_spec[3])
plt.imshow(
FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation='nearest')
ax.yaxis.tick_right()
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
plt.xticks([], [])
ax.tick_params(width=0.0)
plt.grid('off')
plt.show()
return seg_image
LABEL_NAMES = np.asarray([
'background', 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike',
'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tv'
])
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
# @title Select and download models {display-mode: "form"}
MODEL_NAME = 'mobilenetv2_coco_voctrainaug' # @param ['mobilenetv2_coco_voctrainaug', 'mobilenetv2_coco_voctrainval', 'xception_coco_voctrainaug', 'xception_coco_voctrainval']
_DOWNLOAD_URL_PREFIX = 'http://download.tensorflow.org/models/'
_MODEL_URLS = {
'mobilenetv2_coco_voctrainaug':
'deeplabv3_mnv2_pascal_train_aug_2018_01_29.tar.gz',
'mobilenetv2_coco_voctrainval':
'deeplabv3_mnv2_pascal_trainval_2018_01_29.tar.gz',
'xception_coco_voctrainaug':
'deeplabv3_pascal_train_aug_2018_01_04.tar.gz',
'xception_coco_voctrainval':
'deeplabv3_pascal_trainval_2018_01_04.tar.gz',
}
_TARBALL_NAME = 'deeplab_model.tar.gz'
model_dir = tempfile.mkdtemp()
tf.gfile.MakeDirs(model_dir)
download_path = os.path.join(model_dir, _TARBALL_NAME)
print('downloading model, this might take a while...')
urllib.request.urlretrieve(_DOWNLOAD_URL_PREFIX + _MODEL_URLS[MODEL_NAME],
download_path)
print('download completed! loading DeepLab model...')
MODEL = DeepLabModel(download_path)
print('model loaded successfully!')
def run_visualization(url):
"""Inferences DeepLab model and visualizes result."""
try:
# f = urllib.request.urlopen(url)
# jpeg_str = f.read()
# original_im = Image.open(BytesIO(jpeg_str))
original_im = Image.open(url)
except IOError:
print('Cannot retrieve image. Please check url: ' + url)
return
print('running deeplab on image %s...' % url)
resized_im, seg_map = MODEL.run(original_im)
seg_img = vis_segmentation(resized_im, seg_map)
plt.imsave('img/masked_image.jpg', seg_img)
return seg_img
run_visualization(url='/Users/abdul/PycharmProjects/ImgSeg/uploads/photo.jpg')
if __name__ == '__main__':
app.run(port=8000)
但当我运行我的flask应用程序时,它返回错误为:
2018-09-05 11:08:54.952 Python[3772:69116]警告:NSWindow拖动区域只能在主线程上无效!这将在中引发异常
未来。从(0 AppKit
0x000000010fb50647-[NSWindow(NSWindow_Theme)_postWindowNeedsToResetDragMarginsUnlessPostingDisabled] 180调用
1 AppKit 0x000000010fb47a22-[NSWindow\u initContent:styleMask:backing:defer:contentView:]1488
2应用套件0x000000010fb4743e-[NSWindow initWithContentRect:styleMask:backing:defer:]45
3_macosx.cpython-36m-darwin.so0x000000010fae2230-[Window initAnd ContentRect: Style leMask: back: Defer: with Manager:] 80 4
_macosx.cpython-36m-darwin.so0x000000010fae5747FigureManager_init327
我认为问题在于vis_segmentation()
方法中的可视化/绘图代码。如果注释掉行run_visualization(url='/Users/abdul/PycharmProjects/ImgSeg/uploads/photo.jpg')
,错误应该会消失,但显然不会显示任何绘图。
已经有各种SO答案,解决如何通过烧瓶显示matplotlib图。这里有一个:Python:如何在烧瓶中显示matplotlib
问题内容: 当尝试使用str_to_date()将我的Estimates表中的字符串日期值的列CreatedDate转换为mySQL日期格式时,我一直收到错误消息。我的数据列包含m/ d / yy格式的日期(例如:1/26/16或3/3/16)。 我跑了这个查询: mySQL返回此错误消息: 我的查询出了什么问题? 问题答案: 清理数据的通常策略如下: 然后,当您确信一切都正确转换后: 适当字段的
我得到了这个错误,我知道这个问题已经在这里解决过了,因为人们没有在函数中添加返回->。我不明白为什么这仍然给我带来错误。
我相信我在执行O'Neill的PCG PRNG时在GCC中发现了一个bug。(Godbolt的编译器资源管理器上的初始代码) 生成的程序集(GCC 9.2,x86_64,-O3): 有趣的是,修改结构以使uint64_t作为第一个成员产生正确的代码,就像将两个成员都更改为uint64_t一样 x86-64 System V在rdx:rax中返回小于16字节的结构,但它们是可以复制的。在本例中,第二
任何立即的帮助都将不胜感激。 谢谢,米娜
我正在用Python作为后端编写一个应用程序,当我试图返回一个函数的变量时,我得到以下错误 这是我使用的代码 有人知道这个问题的答案吗?
问题内容: 我刚刚学习(正在学习)函数参数在Python中的工作方式,并且在没有明显原因的情况下开始进行实验: 给出了输出: 哪里来的?还有,这是什么? 问题答案: 它是函数的返回值,您可以将其打印出来。如果没有语句(或者只是没有参数的),则将隐式添加到函数的末尾。 您可能想返回函数中的值,而不是打印它们: