# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import cv2 # computer vision library
import helper

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# %matplotlib inline
# -

# Image data directories
image_dir_training = "../Data/Day-Night-Train/"
image_dir_test = "../Data/Day-Night-Test/"

# Using the load_dataset function in helper.py
# Load training data
IMAGE_LIST = helper.load_dataset(image_dir_training)

# Standardize all training images
STANDARDIZED_LIST = helper.standardize(IMAGE_LIST)

# +
# Display a standardized image and its label

# Select an image by index
image_num = 0
selected_image = STANDARDIZED_LIST[image_num][0]
selected_label = STANDARDIZED_LIST[image_num][1]

# Display image and data about it
plt.imshow(selected_image)
print("Shape: "+str(selected_image.shape))
print("Label [1 = day, 0 = night]: " + str(selected_label))


# -

# Find the average Value or brightness of an image
def avg_brightness(rgb_image):
    # Convert image to HSV
    hsv = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2HSV)

    # Add up all the pixel values in the V channel
    sum_brightness = np.sum(hsv[:,:,2])
    area = 600*1100.0  # pixels
    
    # find the avg
    avg = sum_brightness/area
    
    return avg


# +
# Testing average brightness levels
# Look at a number of different day and night images and think about 
# what average brightness value separates the two types of images

# As an example, a "night" image is loaded in and its avg brightness is displayed
image_num = 190
test_im = STANDARDIZED_LIST[image_num][0]

avg = avg_brightness(test_im)
print('Avg brightness: ' + str(avg))
plt.imshow(test_im);


# -

# # Classification and Visualizing Error
#
# In this section, we'll turn our average brightness feature into a classifier that takes in a standardized image and returns a `predicted_label` for that image. This `estimate_label` function should return a value: 0 or 1 (night or day, respectively).

# ---
# ### TODO: Build a complete classifier 
#
# Set a threshold that you think will separate the day and night images by average brightness.

# This function should take in RGB image input
def estimate_label(rgb_image):
    
    ## TODO: extract average brightness feature from an RGB image 
    # Use the avg brightness feature to predict a label (0, 1)
    predicted_label = 0
    
    ## TODO: set the value of a threshold that will separate day and night images
    
    ## TODO: Return the predicted_label (0 or 1) based on whether the avg is 
    # above or below the threshold
    
    return predicted_label    

# +
## Test out your code by calling the above function and seeing 
# how some of your training data is classified
# -



StarcoderdataJupyterScriptsDedupFiltered