【Tensorflow】SSD_Mobilenet_v2实现目标检测(三):测试(补充)

ssd_mobilenet 模型训练后,测试结果(补充)

测试图片并保存测试结果 (步骤4)

【Tensorflow】SSD_Mobilenet_v2实现目标检测(二):测试,博客中介绍了,模型训练后,进行结果测试的全部过程,但该篇博客中介绍的测试代码对图片的位深度有一定要求,必须为8位深度,其他位深度则会出错,于是笔者做如下修改:

1. 在读取图片后,先对图片进行预处理,修改图片的位深度,然后再进行后续的测试过程。

注意:该方法导致结果图片失去色彩,变成灰度图像,但不影响测试结果
笔者再原先的测试代码中修改如下:(以笔者代码为例:76-81行)

######################################################
将原始的输入 图片全部转换为8位深度
image = Image.fromarray(np.uint8(image))
t = image.convert('L')
image = Image.fromarray(np.uint8(t))  
######################################################

完整代码如下:

"""
2020.09.23:alian
获得测试结果
修改参数:28;29;31;32;33
"""
# -*- coding: utf-8 -*-
import os
from PIL import Image
import time
import tensorflow as tf
from PIL import Image
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import zipfile
import time

from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
# plt.switch_backend('Agg')
from utils import label_map_util

from utils import visualization_utils as vis_util

PATH_TO_TEST_IMAGES = "/home/lianlirong/models-master/research/object_detection/images/facility/test/"  # 测试图片的路径
MODEL_NAME = '/home/lianlirong/models-master/logs/facility/model'  #  模型的储存路径
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'  # 模型文件
PATH_TO_LABELS = '/home/lianlirong/models-master/research/object_detection/data/facility/facility_label_map.pbtxt'  #  标签文件的路径
NUM_CLASSES = 2  # 检测的目标数
PATH_TO_RESULTS = "/home/lianlirong/models-master/logs/facility/results_imgs/"  # 测试结果图的保存路径


# 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 load_image_into_numpy_array(image):
    # The function supports only grayscale images
    last_axis = -1
    dim_to_repeat = 2
    repeats = 3
    grscale_img_3dims = np.expand_dims(image, last_axis)
    training_image = np.repeat(grscale_img_3dims, repeats, dim_to_repeat).astype('uint8')
    assert len(training_image.shape) == 3
    assert training_image.shape[-1] == 3
    return training_image



def save_object_detection_result():
    IMAGE_SIZE = (12, 8)
    # Load a (frozen) Tensorflow model into memory.
    detection_graph = tf.Graph()
    with detection_graph.as_default():
        od_graph_def = tf.GraphDef()
        # loading ckpt file to graph
        with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:  ## tf.gfile.GFile更新为tf.io.gfile.GFile,否则会出错‘utf8’
            serialized_graph = fid.read()
            od_graph_def.ParseFromString(serialized_graph)
            tf.import_graph_def(od_graph_def, name='')
    # Loading label map
    label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
    categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                                use_display_name=True)
    category_index = label_map_util.create_category_index(categories)
    # Helper code
    with detection_graph.as_default():
        with tf.Session(graph=detection_graph) as sess:
            start = time.time()
            for test_image in os.listdir(PATH_TO_TEST_IMAGES):
                print(test_image)
                image = Image.open(PATH_TO_TEST_IMAGES + test_image)
                ######################################################
                # 将原始的输入 图片全部转换为8位深度
                image = Image.fromarray(np.uint8(image))
                t = image.convert('L')
                image = Image.fromarray(np.uint8(t))
                ######################################################
                # the array based representation of the image will be used later in order to prepare the
                # result image with boxes and labels on it.
                image_np = load_image_into_numpy_array(image)
                # Expand dimensions since the model expects images to have shape: [1, None, None, 3]
                image_np_expanded = np.expand_dims(image_np, axis=0)
                image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
                # Each box represents a part of the image where a particular object was detected.
                boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
                # Each score represent how level of confidence for each of the objects.
                # Score is shown on the result image, together with the class label.
                scores = detection_graph.get_tensor_by_name('detection_scores:0')
                classes = detection_graph.get_tensor_by_name('detection_classes:0')
                num_detections = detection_graph.get_tensor_by_name('num_detections:0')
                # Actual detection.
                (boxes, scores, classes, num_detections) = sess.run(
                    [boxes, scores, classes, num_detections],
                    feed_dict={image_tensor: image_np_expanded})
                # Visualization of the results of a detection.
                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,
                    use_normalized_coordinates=True,
                    line_thickness=8)

                final_score = np.squeeze(scores)
                count = 0
                for i in range(100):
                    if scores is None or final_score[i] > 0.5:
                        count = count + 1
                print()
                print("the count of objects is: ", count)
                (im_width, im_height) = image.size
                for i in range(count):
                    # print(boxes[0][i])
                    y_min = boxes[0][i][0] * im_height
                    x_min = boxes[0][i][1] * im_width
                    y_max = boxes[0][i][2] * im_height
                    x_max = boxes[0][i][3] * im_width
                    x = int((x_min + x_max) / 2)
                    y = int((y_min + y_max) / 2)
                    if category_index[classes[0][i]]['name'] == "tower":
                        print("this image has a tower!")
                        y = int((y_max - y_min) / 4 * 3 + y_min)
                    print("object{0}: {1}".format(i, category_index[classes[0][i]]['name']),
                          ',Center_X:', x, ',Center_Y:', y)
                    # print(x_min,y_min,x_max,y_max)
                plt.figure(figsize=IMAGE_SIZE)
                plt.imshow(image_np)
                picName = test_image.split('/')[-1]
                # print(picName)
                plt.savefig(PATH_TO_RESULTS + picName)
                print(test_image + ' succeed')

            end = time.time()
            seconds = end - start
            print("Time taken : {0} seconds".format(seconds))


save_object_detection_result()

结果如下:
在这里插入图片描述

方法2. 利用cv2读取图片

上述代码显示:方法一是利用PIL库中中的Image模块读取图片,在方法二中,采用cv2读取图片进行测试。
完整代码如下所示:(需修改的部分已在代码中注明)

"""
2020.09.30:alian
tensorflow 数据检测
修改参数:
"""
# Imports
import time

start = time.time()
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
import cv2

from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image

os.chdir('/home/username/models-master/research/object_detection')

# Env setup
# This is needed to display the images.
# %matplotlib inline

# This is needed since the notebook is stored in the object_detection folder.
sys.path.append("..")

# Object detection imports
from utils import label_map_util

from utils import visualization_utils as vis_util

# Model preparation
# What model to download.

# 这是我们刚才训练的模型
MODEL_NAME = '/home/username/models-master/logs/facility/model'

# 对应的Frozen model位置
# Path to frozen detection graph. This is the actual model that is used for the object detection.
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'

# List of the strings that is used to add correct label for each box.
PATH_TO_LABELS = os.path.join('/home/lianlirong/models-master/research/object_detection/data/facility', 'facility_label_map.pbtxt')

# 改成自己例子中的类别数,5
NUM_CLASSES = 5

# Load a (frozen) Tensorflow model into memory.
detection_graph = tf.Graph()
with detection_graph.as_default():
    od_graph_def = tf.compat.v1.GraphDef()
    with tf.io.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='')

    # Loading label map
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
                                                            use_display_name=True)
category_index = label_map_util.create_category_index(categories)


# Helper code
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)


# Detection

# If you want to test the code with your images, just add path to the images to the TEST_IMAGE_PATHS.
# 测试图片位置
PATH_TO_TEST_IMAGES_DIR = '/home/username/models-master/research/object_detection/images/facility/test'
os.chdir(PATH_TO_TEST_IMAGES_DIR)
TEST_IMAGE_PATHS = os.listdir(PATH_TO_TEST_IMAGES_DIR)

# Size, in inches, of the output images.
IMAGE_SIZE = (12, 8)
# 结果图的输出路径
output_path = ('/home/lianlirong/models-master/logs/facility/results_imgs/')

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        # Definite input and output Tensors for detection_graph
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        # Each box represents a part of the image where a particular object was detected.
        detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        # Each score represent how level of confidence for each of the objects.
        # Score is shown on the result image, together with the class label.
        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:
            image = cv2.imread(image_path, 0)
            image_RGB = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)#BGR
            image_np = image_RGB
            # image_np= cv2.imread(image_path)


            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_np1,
                np.squeeze(boxes),
                np.squeeze(classes).astype(np.int32),
                np.squeeze(scores),
                category_index,
                use_normalized_coordinates=True,
                line_thickness=8)

            cv2.imwrite(output_path + image_path.split('\\')[-1], image_np)

end = time.time()
print("Execution Time: ", end - start)

注意:
方法二存在的问题:
利用cv2.imread(image_path, 0)将图片强行改为位深度8,再利用cv2.cvtColor(image, cv2.COLOR_GRAY2BGR),其实无法恢复图像的原色彩,导致原图色彩丢失,变成灰度图:
在这里插入图片描述
说明:笔者上述的两种方法,虽然可以实现检测,但都存在图像失去色彩的问题,后续笔者会针对上述问题进行研究,寻找解决方法。当然,若有读者已解决上述问题,笔者期待你的分享!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值