|
| 1 | +import cv2 |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +class Kordinat: |
| 5 | + def __init__(self,x,y): |
| 6 | + self.x=x |
| 7 | + self.y=y |
| 8 | + |
| 9 | +class Sensor: |
| 10 | + def __init__(self,kordinat1,kordinat2,frame_weight,frame_lenght): |
| 11 | + self.kordinat1=kordinat1 |
| 12 | + self.kordinat2=kordinat2 |
| 13 | + self.frame_weight=frame_weight |
| 14 | + self.frame_lenght =frame_lenght |
| 15 | + self.mask=np.zeros((frame_weight,frame_lenght,1),np.uint8)*abs(self.kordinat2.y-self.kordinat1.y) |
| 16 | + self.full_mask_area=abs(self.kordinat2.x-self.kordinat1.x) |
| 17 | + cv2.rectangle(self.mask,(self.kordinat1.x,self.kordinat1.y),(self.kordinat2.x,self.kordinat2.y),(255),thickness=cv2.FILLED) |
| 18 | + self.stuation=False |
| 19 | + self.car_number_detected=0 |
| 20 | + |
| 21 | + |
| 22 | +Sensor1 = Sensor(Kordinat(1, 425), Kordinat(1080, 430), 500, 1080) |
| 23 | +video=cv2.VideoCapture("video2.mp4") |
| 24 | +fgbg=cv2.createBackgroundSubtractorMOG2() |
| 25 | +#fgbg=cv2.createBackgroundSubtractorMOG2() |
| 26 | +kernel=np.ones((5,5),np.uint8) |
| 27 | +font=cv2.FONT_HERSHEY_TRIPLEX |
| 28 | +while (1): |
| 29 | + ret,frame=video.read() |
| 30 | + # resize frame |
| 31 | + cut_image=frame[100:600,100:1180] |
| 32 | + # make morphology for frame |
| 33 | + deleted_background=fgbg.apply(cut_image) |
| 34 | + opening_image=cv2.morphologyEx(deleted_background,cv2.MORPH_OPEN,kernel) |
| 35 | + ret,opening_image=cv2.threshold(opening_image,125,255,cv2.THRESH_BINARY) |
| 36 | + |
| 37 | + # detect moving anything |
| 38 | + _, cnts,_=cv2.findContours(opening_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) |
| 39 | + result=cut_image.copy() |
| 40 | + |
| 41 | + zeros_image=np.zeros((cut_image.shape[0],cut_image.shape[1],1),np.uint8) |
| 42 | + |
| 43 | + # detect moving anything with loop |
| 44 | + for cnt in cnts: |
| 45 | + x,y,w,h=cv2.boundingRect(cnt) |
| 46 | + if (w>100 and h>100 ): |
| 47 | + cv2.rectangle(result,(x,y),(x+w,y+h),(255,0,0),thickness=2) |
| 48 | + cv2.rectangle(zeros_image,(x,y),(x+w,y+h),(255),thickness=cv2.FILLED) |
| 49 | + |
| 50 | + # detect whether there is car via bitwise_and |
| 51 | + mask1=np.zeros((zeros_image.shape[0],zeros_image.shape[1],1),np.uint8) |
| 52 | + mask_result=cv2.bitwise_or(zeros_image,zeros_image,mask=Sensor1.mask) |
| 53 | + white_cell_number=np.sum(mask_result==255) |
| 54 | + |
| 55 | + # detect to control whether car is passing under the red line sensor |
| 56 | + sensor_rate=white_cell_number/Sensor1.full_mask_area |
| 57 | + if sensor_rate>0: |
| 58 | + print(sensor_rate) |
| 59 | + |
| 60 | + # if car is passing under the red line sensor . red line sensor is yellow color. |
| 61 | + if (sensor_rate >= 1.8 and sensor_rate<2.9 and Sensor1.stuation == False): |
| 62 | + # draw the red line |
| 63 | + cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y), |
| 64 | + (0, 0, 255), thickness=cv2.FILLED) |
| 65 | + Sensor1.stuation = False |
| 66 | + Sensor1.car_number_detected += 2 |
| 67 | + if (sensor_rate>=0.6 and sensor_rate<1.8 and Sensor1.stuation==False): |
| 68 | + # draw the red line |
| 69 | + cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y), |
| 70 | + (0,255, 0,), thickness=cv2.FILLED) |
| 71 | + Sensor1.stuation = True |
| 72 | + elif (sensor_rate<0.6 and Sensor1.stuation==True) : |
| 73 | + # draw the red line |
| 74 | + cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y), |
| 75 | + (0, 0,255), thickness=cv2.FILLED) |
| 76 | + Sensor1.stuation = False |
| 77 | + Sensor1.car_number_detected+=1 |
| 78 | + else : |
| 79 | + # draw the red line |
| 80 | + cv2.rectangle(result, (Sensor1.kordinat1.x, Sensor1.kordinat1.y), (Sensor1.kordinat2.x, Sensor1.kordinat2.y), |
| 81 | + (0, 0, 255), thickness=cv2.FILLED) |
| 82 | + |
| 83 | + |
| 84 | + cv2.putText(result,str(Sensor1.car_number_detected),(Sensor1.kordinat1.x,Sensor1.kordinat1.y+60),font,2,(255,255,255)) |
| 85 | + |
| 86 | + |
| 87 | + cv2.imshow("video", result) |
| 88 | + #cv2.imshow("mask_result", mask_result) |
| 89 | + #cv2.imshow("zeros_image", zeros_image) |
| 90 | + #cv2.imshow("opening_image", opening_image) |
| 91 | + |
| 92 | + k=cv2.waitKey(30) & 0xff |
| 93 | + if k == 27 : |
| 94 | + break |
| 95 | + |
| 96 | +video.release() |
| 97 | +cv2.destroyAllWindows() |
0 commit comments