<jupyter_start><jupyter_text>Detecting red lights in the simulatorThe goal of this jupyter notebook is to detect red red lights using opencv functions and methods. In order to do so we will use the HSV color model. Furthermore this method works because there is not other red thing in the simulation<jupyter_code>import cv2
import os
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

images_dir = "./simulation_images/"
minimum_red_image = "red_light_images/camera_image--1546656127.6.jpeg"
yellow_image = "yellow_light_images/camera_image--1546658160.44.jpeg"


red_min_image = cv2.imread(os.path.join(images_dir, minimum_red_image))
yellow_image = cv2.imread(os.path.join(images_dir, yellow_image))


RED_PIXEL_MIN_NUMBER = 100 # For 600x800 images


def get_red_mask(image):
    
    # Convert BGR to HSV
    img_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

    # red lower mask (0-10)
    lower_red = np.array([0,100,100])
    upper_red = np.array([10,255,255])
    mask0 = cv2.inRange(img_hsv, lower_red, upper_red)

    # Red upper mask
    lower_red = np.array([170,100,100])
    upper_red = np.array([180,255,255])
    mask1 = cv2.inRange(img_hsv, lower_red, upper_red)

    # join my masks
    mask = mask0+mask1
    return mask


def get_masked_image(image):
    
    mask = get_red_mask(image)

    # set my output img to zero everywhere except my mask
    output_img = image.copy()
    output_img[np.where(mask==0)] = 0
    
    cv2.imshow('my image',output_img)
    cv2.waitKey(10)
    
    
def get_red_pixel_number(image):
    mask = get_red_mask(image)
    return np.count_nonzero(mask)
def red_light_in_folder(folder):
    images_folder = os.path.join(images_dir, folder)
    for filename in os.listdir(images_folder):
        if ".jpeg" in filename:
            img = cv2.imread(os.path.join(images_folder, filename))
            red_pixels = get_red_pixel_number(img)
            print("{} : {}".format(filename, red_pixels))
            
red_light_in_folder("red_light_images")<jupyter_output>camera_image--1546656153.08.jpeg : 376
camera_image--1546656155.17.jpeg : 150
camera_image--1546656152.91.jpeg : 376
camera_image--1546656152.29.jpeg : 357
camera_image--1546656142.28.jpeg : 204
camera_image--1546656151.43.jpeg : 337
camera_image--1546656150.81.jpeg : 333
camera_image--1546656127.6.jpeg : 55
camera_image--1546656149.2.jpeg : 293
camera_image--1546656148.18.jpeg : 266
camera_image--1546656154.99.jpeg : 150
camera_image--1546656150.35.jpeg : 310
camera_image--1546656143.72.jpeg : 70
camera_image--1546656128.22.jpeg : 55
camera_image--1546656142.03.jpeg : 204
camera_image--1546656154.06.jpeg : 403
camera_image--1546656154.47.jpeg : 403
camera_image--1546656147.57.jpeg : 271
camera_image--1546656152.74.jpeg : 357
camera_image--1546656127.89.jpeg : 53
camera_image--1546656150.09.jpeg : 310
camera_image--1546656145.67.jpeg : 240
camera_image--1546656153.62.jpeg : 384
camera_image--1546656150.6.jpeg : 310
camera_image--1546656148.65.jpeg : 266
camera_image--1546656151.12.jpeg[...]
{'fraction_non_alphanumeric': 0.10950345281157514, 'fraction_numerical': 0.15356790529431108, 'mean_word_length': 4.003289473684211, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '662485', 'license': 'MIT', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1097, 'n_words': 216, 'path': 'traffic_light_detection/simulation/simulator.ipynb', 'repo_name': 'tekisetsu/udacity_capstone', 'size': '3041'}