Skip to content

Commit 3f5e493

Browse files
author
Takaki
committedNov 1, 2017
initial commit
1 parent 7ac6277 commit 3f5e493

11 files changed

+230
-0
lines changed
 

‎.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
videos/
2+
pics/
3+
results/
4+
*.avi
5+
*.mp4
6+
*.pyc
7+
*~

‎README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Traffic Signal Detection
2+
3+
```
4+
$ roslaunch single_camera_streaming.launch device:=/dev/video1
5+
```
6+
```
7+
$ python signal_publisher.py
8+
```
9+
```
10+
python signal_subscriber.py
11+
```

‎detection_class.py

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import cv2
2+
import numpy as np
3+
import os
4+
import copy
5+
6+
class SignalDetecor:
7+
8+
#generating diff image
9+
def diffimg(self, after, before):
10+
11+
blue_before = before[:,:,0]
12+
green_before = before[:,:,1]
13+
red_before = before[:,:,2]
14+
15+
blue_after = after[:,:,0]
16+
green_after = after[:,:,1]
17+
red_after = after[:,:,2]
18+
19+
blue_final = blue_after.astype(np.int32) - blue_before.astype(np.int32) + 255
20+
green_final = green_after.astype(np.int32)- green_before.astype(np.int32) + 255
21+
red_final = red_after.astype(np.int32) - red_before.astype(np.int32) + 255
22+
23+
output=np.zeros((after.shape[0],after.shape[1],3))
24+
output[:,:,0]=blue_final
25+
output[:,:,1]=green_final
26+
output[:,:,2]=red_final
27+
28+
return (output/2.0).astype(np.uint8)
29+
30+
31+
def __init__(self):
32+
33+
# load template images
34+
self.temp0 = cv2.imread('templates/red2green.png')
35+
self.temp1 = cv2.imread('templates/red2black.png')
36+
self.temp2 = cv2.imread('templates/black2green.png')
37+
self.temp3 = cv2.imread('templates/green2red.png')
38+
39+
self.temp0_h = self.temp0.shape[0]
40+
self.temp0_w = self.temp0.shape[1]
41+
self.temp3_h = self.temp3.shape[0]
42+
self.temp3_w = self.temp3.shape[1]
43+
44+
self.frame4=0
45+
self.frame3=0
46+
self.frame2=0
47+
self.frame1=0
48+
49+
self.ok_desu=0
50+
self.output=np.zeros((600,600,3))
51+
52+
self.green_cnt=0
53+
self.red_cnt=0
54+
55+
# threshold of template matching
56+
self.green_th = 0.65 #stable:0.65
57+
self.red_th = 0.7 #stable:0.7
58+
59+
# number of detections
60+
self.nd = 3
61+
62+
# signal states (0:unkown, 1:green, 2:red)
63+
self.state = 0
64+
65+
66+
def __call__(self, frame):
67+
68+
new_frame = frame
69+
display_frame = copy.deepcopy(new_frame)
70+
71+
if self.ok_desu>4:
72+
73+
output=self.diffimg(new_frame,self.frame4)
74+
result0=cv2.matchTemplate(output,self.temp0,method=cv2.TM_CCOEFF_NORMED)
75+
result1=cv2.matchTemplate(output,self.temp1,method=cv2.TM_CCOEFF_NORMED)
76+
result2=cv2.matchTemplate(output,self.temp2,method=cv2.TM_CCOEFF_NORMED)
77+
result3=cv2.matchTemplate(output,self.temp3,method=cv2.TM_CCOEFF_NORMED)
78+
79+
mmlr0=cv2.minMaxLoc(result0)
80+
mmlr1=cv2.minMaxLoc(result1)
81+
mmlr2=cv2.minMaxLoc(result2)
82+
mmlr3=cv2.minMaxLoc(result3)
83+
84+
# condition1: RED to GREEN
85+
if mmlr0[1]>self.green_th and mmlr0[1] > mmlr2[1]:
86+
self.green_cnt+=1
87+
else:
88+
self.green_cnt=0
89+
90+
if self.green_cnt==self.nd:
91+
self.state = 1
92+
print "The signal turned from RED to GREEN !"
93+
cv2.rectangle(display_frame,
94+
(mmlr0[3][0],mmlr0[3][1]),
95+
(mmlr0[3][0]+temp0_w,mmlr0[3][1]+temp0_h),
96+
(0,255,0),3)
97+
self.green_cnt=0
98+
99+
# condition2: GREEN to RED
100+
if mmlr3[1]>self.red_th and mmlr3[1] > mmlr1[1]:
101+
self.red_cnt+=1
102+
else:
103+
self.red_cnt=0
104+
105+
if self.red_cnt==self.nd:
106+
self.state = 2
107+
print "The signal turned from GREEN to RED !"
108+
cv2.rectangle(display_frame,
109+
(mmlr3[3][0],mmlr3[3][1]),
110+
(mmlr3[3][0]+temp3_w,mmlr3[3][1]+temp3_h),
111+
(0,0,255),3)
112+
self.red_cnt = 0
113+
114+
# display the signal state
115+
if self.state == 0:
116+
signal_state = "UNKOWN"
117+
color = (0,0,0)
118+
elif self.state == 1:
119+
signal_state = "GREEN"
120+
color = (0,255,0)
121+
else:
122+
signal_state = "RED"
123+
color = (0,0,255)
124+
125+
message = "signal state "+str(self.state)+": "+signal_state
126+
#print message
127+
cv2.putText(display_frame, signal_state, (10, 25),
128+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2)
129+
130+
# display the resulting frame
131+
cv2.imshow('frame',display_frame)
132+
cv2.imshow('diff4',output)
133+
cv2.waitKey(3)
134+
135+
self.frame4=self.frame3
136+
self.frame3=self.frame2
137+
self.frame2=self.frame1
138+
self.frame1=new_frame
139+
self.ok_desu+=1
140+
141+
return self.state

‎signal_publisher.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import rospy
2+
from std_msgs.msg import Int8
3+
from sensor_msgs.msg import Image
4+
from cv_bridge import CvBridge, CvBridgeError
5+
import cv2
6+
7+
from detection_class import *
8+
9+
class SignalDetecorNode:
10+
11+
def __init__(self):
12+
rospy.init_node('signal_detector', anonymous=True)
13+
self.bridge = CvBridge()
14+
self.image_sub = rospy.Subscriber("center_cam/image_raw",Image,self.callback)
15+
self.signal_pub = rospy.Publisher('signal_info', Int8, queue_size=10)
16+
self.detection = SignalDetecor()
17+
18+
def callback(self,data):
19+
try:
20+
cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
21+
state = self.detection(cv_image)
22+
self.signal_pub.publish(state)
23+
rospy.loginfo(str(state)+" is published.")
24+
except CvBridgeError as e:
25+
print(e)
26+
27+
def main():
28+
SignalDetecorNode()
29+
try:
30+
rospy.spin()
31+
except KeyboardInterrupt:
32+
print("Shutting down")
33+
34+
if __name__ == '__main__':
35+
main()

‎signal_subscriber.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import rospy
2+
from std_msgs.msg import Int8
3+
4+
def callback(data):
5+
6+
if data.data == 0:
7+
signal_state = "UNKOWN"
8+
elif data.data == 1:
9+
signal_state = "GREEN"
10+
else:
11+
signal_state = "RED"
12+
13+
message = " signal state : "+signal_state
14+
rospy.loginfo(rospy.get_caller_id() + '%s', message)
15+
16+
17+
def signal_subscriber():
18+
19+
rospy.init_node('signal_subscriber', anonymous=True)
20+
rospy.Subscriber('signal_info', Int8, callback)
21+
rospy.spin()
22+
23+
if __name__ == '__main__':
24+
signal_subscriber()

‎single_camera_streaming.launch

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<launch>
2+
<node name="center_cam" pkg="usb_cam" type="usb_cam_node" output="screen" >
3+
<!-- <param name="video_device" value="/dev/video0" /> -->
4+
<param name="video_device" value="$(arg device)" />
5+
<param name="image_width" value="320" />
6+
<param name="image_height" value="240" />
7+
<param name="framerate" value="20" />
8+
<param name="pixel_format" value="yuyv" />
9+
<param name="camera_frame_id" value="usb_cam" />
10+
<param name="io_method" value="mmap"/>
11+
</node>
12+
</launch>

‎templates/black2green.png

476 Bytes
Loading

‎templates/green2black.png

255 Bytes
Loading

‎templates/green2red.png

429 Bytes
Loading

‎templates/red2black.png

520 Bytes
Loading

‎templates/red2green.png

490 Bytes
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.