import cv2
import numpy as np
Video_TrafficFlow = cv2.VideoCapture("video.mp4")
fgbg = cv2.createBackgroundSubtractorMOG2()
kernel = np.ones((5,5),np.uint8)
font = cv2.FONT_HERSHEY_SIMPLEX
class Coordinate:
def __init__(self,x,y):
self.x=x
self.y=y
class Sensor:
def __init__(self, Coordinate1, Coordinate2, Square_Width, Square_Length):
self.Coordinate1 = Coordinate1
self.Coordinate2 = Coordinate2
self.Square_Width = Square_Width
self.Square_Length = Square_Length
self.Mask_Area = abs(self.Coordinate2.x-Coordinate1.x)*abs(self.Coordinate2.y-self.Coordinate1.y)
self.Mask = np.zeros((Square_Length, Square_Width, 1), np.uint8)
cv2.rectangle(self.Mask, (self.Coordinate1.x, self.Coordinate1.y), (self.Coordinate2.x, self.Coordinate2.y), (255), thickness=cv2.FILLED)
self.Case = False
self.Numberof_Detected_Cars = 0
"""def Shadow_Del(Picture):
rgb_planes = cv2.split(Picture)
dst = np.zeros(shape = (5, 2))
result_planes = []
result_norm_planes = []
for plane in rgb_planes:
dilated_img = cv2.dilate(plane, np.ones((7, 7), np.uint8))
bg_img = cv2.medianBlur(dilated_img, 21)
diff_img = 255 - cv2.absdiff(plane, bg_img)
norm_img = cv2.normalize(diff_img,None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
result_planes.append(diff_img)
result_norm_planes.append(norm_img)
result = cv2.merge(result_planes)
result_norm = cv2.merge(result_norm_planes)
return result_norm""" # This side is created for shadow and brilliant area but it didnt finished yet
Sensor1 = Sensor (Coordinate(310,180),Coordinate (420,240), 1080, 250)
#cv2.imshow("Mask", Sensorr1.Mask)
while (1):
ret, Trafficflow = Video_TrafficFlow.read()
CuttingSquare = Trafficflow[350:600, 100:1180]
# CuttingSquare = Shadow_Del(CuttingSquare)
Black_White_Screen = fgbg.apply(CuttingSquare)
Black_White_Screen_MorOpening = cv2.morphologyEx(Black_White_Screen , cv2.MORPH_OPEN, kernel)
ret, Black_White_Screen_MorOpening = cv2.threshold(Black_White_Screen_MorOpening, 127, 255, cv2.THRESH_BINARY)
cnts, hierarchy = cv2.findContours(Black_White_Screen_MorOpening , cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
Result = CuttingSquare.copy()
Filled_Picture = np.zeros ((CuttingSquare.shape [0], CuttingSquare.shape[1], 1), np.uint8)
for cnt in cnts:
x, y, w, h = cv2.boundingRect(cnt)
if(w>30 and h>30):
cv2.rectangle(Result, (x,y), (x+w, y+h), (0,255,0), thickness=4)
cv2.rectangle(Filled_Picture, (x,y), (x+w, y+h), (255), thickness= cv2.FILLED)
Sensor1_Mask_Result = cv2.bitwise_and(Filled_Picture, Filled_Picture, mask=Sensor1.Mask)
Sensor1_Numberof_White_Pixel = np.sum(Sensor1_Mask_Result==255)
Sensor1_Oran = Sensor1_Numberof_White_Pixel/Sensor1.Mask_Area
if(Sensor1_Oran>=0.75 and Sensor1.Case == False):
cv2.rectangle(Result, (Sensor1.Coordinate1.x, Sensor1.Coordinate1.y),
(Sensor1.Coordinate2.x, Sensor1.Coordinate2.y), (0, 255, 0), thickness=cv2.FILLED)
Sensor1.Case = True
elif (Sensor1_Oran<=0.75 and Sensor1.Case == True):
cv2.rectangle(Result, (Sensor1.Coordinate1.x, Sensor1.Coordinate1.y),
(Sensor1.Coordinate2.x, Sensor1.Coordinate2.y), (0, 0, 255), thickness=cv2.FILLED)
Sensor1.Case=False
Sensor1.Numberof_Detected_Cars +=1
else:
cv2.rectangle(Result, (Sensor1.Coordinate1.x, Sensor1.Coordinate1.y),
(Sensor1.Coordinate2.x, Sensor1.Coordinate2.y), (0, 0, 255), thickness=cv2.FILLED)
cv2.putText (Result, str(Sensor1.Numberof_Detected_Cars), (Sensor1.Coordinate1.x, Sensor1.Coordinate1.y+60), font, 3, (255,255,255),3,cv2.LINE_AA)
#cv2.imshow("Traffic Flow", Trafficflow)
cv2.imshow("Black White Screen Morphologia Opening", Black_White_Screen_MorOpening )
#cv2.imshow("Cutting Square", CuttingSquare)
cv2.imshow("Result", Result)
cv2.imshow("Filled Picture", Filled_Picture)
cv2.imshow("Sensor1 Mask Result", Sensor1_Mask_Result)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
Video_TrafficFlow.release()
cv2.destroyAllWindows()
31 Mayıs 2020 Pazar
Counting car in Traffic Flow with OpenCV Python
Kaydol:
Kayıt Yorumları (Atom)
Ros2 çalışmaları
1) Her saniye yazı yazdırma. Eklediğim kod öncelikle Hello Cpp Node yazdıracak ardınca Hello ekleyecek. benim .cpp dosyamın adı my_first_no...
-
Udemy Problems Solve Exercise-1 Quiz: Average Electricity Bill It's time to try a calculation in Python! My electricity bil...
-
I've started programming python but Education videos didn't help me for learning and remembering python command. That's w...
Hiç yorum yok:
Yorum Gönder