-
Notifications
You must be signed in to change notification settings - Fork 26
/
tl_classifier.py
39 lines (33 loc) · 1.28 KB
/
tl_classifier.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
from styx_msgs.msg import TrafficLight
import cv2
import tensorflow as tf
import numpy as np
class TLClassifier(object):
def __init__(self):
self.model = None
self.width = 0
self.height = 0
self.channels = 3
def setup_classifier(self, model, width, height, channels=3):
self.width = width
self.height = height
self.model = model
self.channels = channels
# necessary work around to avoid troubles with keras
self.graph = tf.get_default_graph()
def get_classification(self, image):
"""Determines the color of the traffic light in the image
Args:
image (cv::Mat): image containing the traffic light
Returns:
int: ID of traffic light color (specified in styx_msgs/TrafficLight)
"""
resized = cv2.resize(image, (self.width,self.height))
resized = resized / 255.; # Normalization
# necessary work around to avoid troubles with keras
with self.graph.as_default():
predictions = self.model.predict(resized.reshape((1, self.height, self.width, self.channels)))
color = predictions[0].tolist().index(np.max(predictions[0]))
tl = TrafficLight()
tl.state = color
return tl.state