<jupyter_start><jupyter_text>Traffic Light Classifier - EvaluationNotebook to evaluate the pretrained models in https://github.com/ThomasHenckel/CarND-Capstone/tree/master/Traffic_Light_Detection  Detection<jupyter_code>import numpy as np
import os
import sys
import tensorflow as tf
import time
from matplotlib import pyplot as plt
from PIL import Image
%matplotlib inline
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util<jupyter_output>/mnt/d/the_workspace/models/research/object_detection/utils/visualization_utils.py:25: UserWarning: matplotlib.pyplot as already been imported, this call will have no effect.
  import matplotlib; matplotlib.use('Agg')  # pylint: disable=multiple-statements<jupyter_text>Make sure to use converted model: `models/converted_to_TFv14/ssd_mobilenet/frozen_inference_graph.pb`<jupyter_code>MODELS_DIR = os.path.join('..', 'models', 'exported') #'exported')
model_file = 'ssd_mobilenet/frozen_inference_graph.pb' #'ssd_mobilenet/frozen_inference_graph.pb'

MODEL_PATH = os.path.join(MODELS_DIR, model_file)
LABELS_MAP_PATH = os.path.join('..', 'config', 'labels_map.pbtxt') #PATH_TO_LABELS = 'labels_map.pbtxt'

NUM_CLASSES = 3<jupyter_output><empty_output><jupyter_text>Loadig label map<jupyter_code>label_map = label_map_util.load_labelmap(LABELS_MAP_PATH)
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)
print(category_index)
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)
from glob import glob<jupyter_output><empty_output><jupyter_text>Detection<jupyter_code>detection_graph = tf.Graph()

with detection_graph.as_default():
    od_graph_def = tf.GraphDef()

    with tf.gfile.GFile(MODEL_PATH, 'rb') as fid:
        
        serialized_graph = fid.read()
        od_graph_def.ParseFromString(serialized_graph)
        tf.import_graph_def(od_graph_def, name='')
DATA_DIR = os.path.join('..', 'test_images/190304_StAn_Udacity_Real_Traffic_Lights_Training') 

print(os.path.join(DATA_DIR, '*.jpg'))
DATA_PATHS = glob(os.path.join(DATA_DIR, '*.jpg'))
print("Length of test images:", len(DATA_PATHS))

# Size, in inches, of the output images. (original size: 350, 280)
IMAGE_SIZE = (10, 8) 
image_path_1 = os.path.join('..', 'test_images', 'uda_loop_r.jpg')
image_1 = Image.open(image_path_1)
image_1.size

image_path_2 = os.path.join('..', 'test_images', 'sim_y.jpg')
image_2 = Image.open(image_path_1)
image_2.size<jupyter_output><empty_output><jupyter_text>Testing classification in Yellow Lights<jupyter_code>%matplotlib inline
# Testing Yellow Lights:

DATA_DIR = os.path.join('..', 'test_images/1190304_StAn_Udacity_Simulator_Traffic_Lights_Training') 
DATA_PATHS = sorted(glob(os.path.join(DATA_DIR, '*.png')))
print("Length of test images:", len(DATA_PATHS))

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 DATA_PATHS:
            image = Image.open(image_path).convert("RGB")
            # 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)

            time0 = time.time()

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

            time1 = time.time()

            boxes = np.squeeze(boxes)
            scores = np.squeeze(scores)
            classes = np.squeeze(classes).astype(np.int32)
            
            # Visualization of the results of a detection.
            vis_util.visualize_boxes_and_labels_on_image_array(
                image_np, boxes, classes, scores,
                category_index,
                use_normalized_coordinates=True,
                line_thickness=4)
            
            print("Image name", image_path)
            plt.figure(figsize=IMAGE_SIZE)
            plt.imshow(image_np)
            plt.show()
            #im = Image.fromarray(image_np)
            #im.save("../test_images_result/190304_StAn_Udacity_Real_Traffic_Lights_Training/"+image_path.split('/')[-1], "JPEG", quality=80, optimize=True, progressive=True)

            min_score_thresh = .30
            for i in range(boxes.shape[0]):
                if scores is None or scores[i] > min_score_thresh:

                    class_name = category_index[classes[i]]['name']
                    print('{}'.format(class_name), scores[i])
                    
                    fx =  0.97428  #1345.200806
                    fy =  1.73205  #1353.838257
                    perceived_width_x = (boxes[i][3] - boxes[i][1]) * 800
                    perceived_width_y = (boxes[i][2] - boxes[i][0]) * 600

                    # ymin, xmin, ymax, xmax = box
                    # depth_prime = (width_real * focal) / perceived_width
                    perceived_depth_x = ((.1 * fx) / perceived_width_x)
                    perceived_depth_y = ((.3 * fy) / perceived_width_y )

                    estimated_distance = round((perceived_depth_x + perceived_depth_y) / 2)
                    print("Distance (metres)", estimated_distance)
                    print("Time in milliseconds", (time1 - time0) * 1000, "\n")<jupyter_output>/mnt/d/the_workspace/SelfDrivingCar/Traffic_Light_Detection/notebooks
Length of test images: 0
{'fraction_non_alphanumeric': 0.10149750415973377, 'fraction_numerical': 0.017243987293904098, 'mean_word_length': 3.3243950294310007, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '436190', 'license': 'MIT', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2095, 'n_words': 415, 'path': 'Traffic_Light_Detection/notebooks/.ipynb_checkpoints/Traffic-Light-Classifier-evaluation-yellow-lights-checkpoint.ipynb', 'repo_name': 'ilopezfr/CarND-Capstone', 'size': '6611'}