|
| 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 |
0 commit comments