Python 如何修复tensorflow和matplotlib覆盖的结果?

Python 如何修复tensorflow和matplotlib覆盖的结果?,python,tensorflow,Python,Tensorflow,我正在使用TensorFlow分析一些图像,但当我运行该程序时,图像被覆盖,我不知道如何修复它,因此我应该看到多个图像,但只有一个图像显示被一次又一次覆盖 import numpy as np import os import six.moves.urllib as urllib import sys import tarfile import tensorflow as tf import zipfile import json import time import glob from io

我正在使用TensorFlow分析一些图像,但当我运行该程序时,图像被覆盖,我不知道如何修复它,因此我应该看到多个图像,但只有一个图像显示被一次又一次覆盖

import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import json
import time
import glob
from io import StringIO
from PIL import Image
import matplotlib.pyplot as plt
from utils import visualization_utils as vis_util
from utils import label_map_util
from multiprocessing.dummy import Pool as ThreadPool
import argparse, sys


parser = argparse.ArgumentParser()

parser.add_argument('--labels', help='Directorio a label_map.pbtxt', default ='D:/Downloads/deteccion_objetos-master/deteccion_objetos-master/configuracion/label_map.pbtxt')
parser.add_argument('--images', help='Directorio de las imagenes a procesar', default = 'D:/Downloads/deteccion_objetos-master/deteccion_objetos-master/img_pruebas')
parser.add_argument('--model', help='Directorio al modelo congelado', default = 'D:/Downloads/deteccion_objetos-master/deteccion_objetos-master/modelo_congelado')
args = parser.parse_args()

MAX_NUMBER_OF_BOXES = 30
MINIMUM_CONFIDENCE = 0.4

PATH_TO_LABELS = args.labels
PATH_TO_TEST_IMAGES_DIR = args.images

label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=sys.maxsize, use_display_name=True)
CATEGORY_INDEX = label_map_util.create_category_index(categories)

# Path to frozen detection graph. This is the actual model that is used for the object detection.
MODEL_NAME = args.model
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape(
        (im_height, im_width, 3)).astype(np.uint8)

def detect_objects(image_path):
    image = Image.open(image_path)
    image_np = load_image_into_numpy_array(image)
    image_np_expanded = np.expand_dims(image_np, axis=0)

    (boxes, scores, classes, num) = sess.run([detection_boxes, detection_scores, detection_classes, num_detections], feed_dict={image_tensor: image_np_expanded})

    vis_util.visualize_boxes_and_labels_on_image_array(
        image_np,
        np.squeeze(boxes),
        np.squeeze(classes).astype(np.int32),
        np.squeeze(scores),
        CATEGORY_INDEX,
        min_score_thresh=MINIMUM_CONFIDENCE,
        use_normalized_coordinates=True,
        line_thickness=8)
    fig = plt.figure()
    fig.set_size_inches(16, 9)
    ax = plt.Axes(fig, [0., 0., 1., 1.])
    ax.set_axis_off()
    fig.add_axes(ax)

    plt.imshow(image_np, aspect = 'auto')
    plt.savefig('D:/Downloads/deteccion_objetos-master/deteccion_objetos-master/output/img_pruebas/img_pruebas'.format(image_path), dpi = 62)
    print(boxes)
    plt.close(fig)

# TEST_IMAGE_PATHS = [ os.path.join(PATH_TO_TEST_IMAGES_DIR, 'image-{}.jpg'.format(i)) for i in range(1, 4) ]
TEST_IMAGE_PATHS = glob.glob(os.path.join(PATH_TO_TEST_IMAGES_DIR, '*.jpeg'))
print(TEST_IMAGE_PATHS)

# Load model into memory
print('Loading model...')
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.GraphDef()
    with tf.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')

print('detecting...')
with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
        detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
        num_detections = detection_graph.get_tensor_by_name('num_detections:0')

        for image_path in TEST_IMAGE_PATHS:
            detect_objects(image_path)

因此,应该会出现许多不同的图像,但被覆盖的图像会以循环的形式出现,直到我完成对图像的分析

 plt.savefig('D:/Downloads/deteccion_objetos-master/deteccion_objetos-master/output/img_pruebas/img_pruebas'.format(image_path), dpi = 62)
现在,无论
image\u path
的值是什么,绘图总是在
D:/Downloads/detecionu-objetos-master/detecionu-objetos-master/output/img\u-pruebas/img\u-pruebas
处创建

您可能需要以下内容:

import os

plt.savefig('D:/Downloads/deteccion_objetos-master/deteccion_objetos-master/output/img_pruebas/img_pruebas/{}'.format(os.path.basename(image_path)))

您好@BioGeek,我刚刚做了您提到的修改,但是我得到了一个错误,我将它作为另一个答案附加到了您的同一论坛上,我如何修复该错误?忘记它,我设法解决了它,非常感谢您的帮助!