Repository navigation
Expand file tree
/
Copy pathopencv.py
More file actions
107 lines (62 loc) · 2.03 KB
/
Copy pathopencv.py
File metadata and controls
107 lines (62 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from utils import daynight_helper
import random
import matplotlib.pyplot as plt
import cv2
import numpy as np
training_data_path = 'data/day_night_images/training'
training_data = daynight_helper.load_dataset(training_data_path)
std_training_data = daynight_helper.standardize_inputs(training_data)
img = std_training_data[0][0]
label = std_training_data[0][1]
plt.imshow(img)
print('Shape ', img.shape)
print('label: ', label)
def brightness_value(img):
hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
# get 2nd channel values of hsv values
v_values = np.sum(hsv_img[:,:,2])
# dimensions
area = img.shape[0] * img.shape[1]
avg_brightness = v_values/area
return avg_brightness
def estimate_label(img, threshold):
avg_brightness = brightness_value(img)
predicted_label = 0
threshold = threshold
if avg_brightness > threshold:
predicted_label = 1
return predicted_label, avg_brightness
def optimize_threshold(img_data, threshold):
img = img_data[0]
label = img_data[1]
threshold = threshold
pred_label, avg_brightness = estimate_label(img, threshold)
if pred_label == label:
return threshold
else:
return round((avg_brightness + threshold)/2)
# 120 is initial value
threshold = 120
for i in range(0, len(std_training_data)):
img_data = std_training_data[i]
threshold = optimize_threshold(img_data, threshold)
#116
print('threshold = ', threshold)
# test the data
test_data_path = "data/day_night_images/test"
test_data = daynight_helper.load_dataset(test_data_path)
test_data = daynight_helper.standardize_inputs(test_data)
correctly_classfied = []
misclassified = []
for i in range(0, len(test_data)):
img_data = test_data[i]
pred, avg_brightness = estimate_label(img_data[0], threshold)
label = img_data[1]
if pred == label:
correctly_classfied.append(img_data)
else:
misclassified.append(img_data)
print("Total = ", len(test_data))
print("Correct predictions = ", len(correctly_classfied))
print("Misclassified = ", len(misclassified))
print("Accuracy = ", len(correctly_classfied)/len(test_data))