-
Notifications
You must be signed in to change notification settings - Fork 6
/
prediction.py
87 lines (69 loc) · 2.39 KB
/
prediction.py
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
80
81
82
83
84
85
86
87
## Imports for making predictions
import os
import numpy as np
from PIL import Image
from keras.applications.imagenet_utils import preprocess_input
from keras.models import load_model
from keras.preprocessing import image
import cv2
from create_data import show_images
import pickle
from edge_detection import final_spot_dict
cwd = os.getcwd()
top_model_weights_path = 'car1.h5'
class_dictionary = {}
class_dictionary[0] = 'empty'
class_dictionary[1] = 'occupied'
from PIL import Image
model = load_model(top_model_weights_path)
from create_data import test_images
# pickel load
def make_prediction(image):
#Rescale image
img = image/255.
#Convert to a 4D tensor
image = np.expand_dims(img, axis=0)
#print(image.shape)
# make predictions on the preloaded model
class_predicted = model.predict(image)
inID = np.argmax(class_predicted[0])
label = class_dictionary[inID]
return label
empty_slots = []
def predict_on_image(image, spot_dict = final_spot_dict, make_copy=True, color = [0, 255, 0], alpha=0.5):
if make_copy:
new_image = np.copy(image)
overlay = np.copy(image)
cnt_empty = 0
all_spots = 0
for spot in spot_dict:
all_spots += 1
(x1, y1, x2, y2) = spot
(x1, y1, x2, y2) = (int(x1), int(y1), int(x2), int(y2))
#crop this image
spot_img = image[y1:y2, x1:x2]
spot_img = cv2.resize(spot_img, (48, 48))
label = make_prediction(spot_img)
# print(label)
if label == 'empty':
empty_slots.append(spot)
cv2.rectangle(overlay, (int(x1),int(y1)), (int(x2),int(y2)), color, -1)
cnt_empty += 1
cv2.addWeighted(overlay, alpha, new_image, 1 - alpha, 0, new_image)
cv2.putText(new_image, "Available: %d spots" %cnt_empty, (30, 95),
cv2.FONT_HERSHEY_SIMPLEX,
0.7, (255, 255, 255), 2)
cv2.putText(new_image, "Total: %d spots" %all_spots, (30, 125),
cv2.FONT_HERSHEY_SIMPLEX,
0.7, (255, 255, 255), 2)
save = False
if save:
filename = 'with_marking.jpg'
cv2.imwrite(filename, new_image)
return new_image
def sample_func():
predicted_images = list(map(predict_on_image, test_images))
show_images(predicted_images)
with open('free_spots.pickle', 'wb') as handle:
pickle.dump(empty_slots, handle, protocol=pickle.HIGHEST_PROTOCOL)
sample_func()