├── .github └── FUNDING.yml ├── README.md ├── carsVideo.mp4 ├── file.xml ├── speed_detector.py └── vech.xml /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: code 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vehicle-speed-detection-using-opencv-python 2 | xml file vehicle detection 3 | -------------------------------------------------------------------------------- /carsVideo.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/noorkhokhar99/vehicle-speed-detection-using-opencv-python/9ba196ce74ead8a6fe072e768b032b79ac1d88ab/carsVideo.mp4 -------------------------------------------------------------------------------- /speed_detector.py: -------------------------------------------------------------------------------- 1 | #Importing Libraries 2 | 3 | import cv2 4 | import dlib 5 | import time 6 | import math 7 | 8 | #Classifier File 9 | carCascade = cv2.CascadeClassifier("vech.xml") 10 | 11 | #Video file capture 12 | video = cv2.VideoCapture("carsVideo.mp4") 13 | 14 | # Constant Declaration 15 | WIDTH =1280 16 | HEIGHT = 720 17 | 18 | #estimate speed function 19 | def estimateSpeed(location1, location2): 20 | d_pixels = math.sqrt(math.pow(location2[0] - location1[0], 2) + math.pow(location2[1] - location1[1], 2)) 21 | ppm = 8.8 22 | d_meters = d_pixels / ppm 23 | fps = 18 24 | speed = d_meters * fps * 3.6 25 | return speed 26 | 27 | #tracking multiple objects 28 | def trackMultipleObjects(): 29 | rectangleColor = (0, 255, 255) 30 | frameCounter = 0 31 | currentCarID = 0 32 | fps = 0 33 | 34 | carTracker = {} 35 | carNumbers = {} 36 | carLocation1 = {} 37 | carLocation2 = {} 38 | speed = [None] * 1000 39 | 40 | out = cv2.VideoWriter('outTraffic.avi', cv2.VideoWriter_fourcc('M','J','P','G'), 10, (WIDTH, HEIGHT)) 41 | 42 | while True: 43 | start_time = time.time() 44 | rc, image = video.read() 45 | if type(image) == type(None): 46 | break 47 | 48 | image = cv2.resize(image, (WIDTH, HEIGHT)) 49 | resultImage = image.copy() 50 | 51 | frameCounter = frameCounter + 1 52 | carIDtoDelete = [] 53 | 54 | for carID in carTracker.keys(): 55 | trackingQuality = carTracker[carID].update(image) 56 | 57 | if trackingQuality < 7: 58 | carIDtoDelete.append(carID) 59 | 60 | 61 | for carID in carIDtoDelete: 62 | print("Removing carID " + str(carID) + ' from list of trackers. ') 63 | print("Removing carID " + str(carID) + ' previous location. ') 64 | print("Removing carID " + str(carID) + ' current location. ') 65 | carTracker.pop(carID, None) 66 | carLocation1.pop(carID, None) 67 | carLocation2.pop(carID, None) 68 | 69 | 70 | if not (frameCounter % 10): 71 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 72 | cars = carCascade.detectMultiScale(gray, 1.1, 13, 18, (24, 24)) 73 | 74 | for (_x, _y, _w, _h) in cars: 75 | x = int(_x) 76 | y = int(_y) 77 | w = int(_w) 78 | h = int(_h) 79 | 80 | x_bar = x + 0.5 * w 81 | y_bar = y + 0.5 * h 82 | 83 | matchCarID = None 84 | 85 | for carID in carTracker.keys(): 86 | trackedPosition = carTracker[carID].get_position() 87 | 88 | t_x = int(trackedPosition.left()) 89 | t_y = int(trackedPosition.top()) 90 | t_w = int(trackedPosition.width()) 91 | t_h = int(trackedPosition.height()) 92 | 93 | t_x_bar = t_x + 0.5 * t_w 94 | t_y_bar = t_y + 0.5 * t_h 95 | 96 | if ((t_x <= x_bar <= (t_x + t_w)) and (t_y <= y_bar <= (t_y + t_h)) and (x <= t_x_bar <= (x + w)) and (y <= t_y_bar <= (y + h))): 97 | matchCarID = carID 98 | 99 | if matchCarID is None: 100 | print(' Creating new tracker' + str(currentCarID)) 101 | 102 | tracker = dlib.correlation_tracker() 103 | tracker.start_track(image, dlib.rectangle(x, y, x + w, y + h)) 104 | 105 | carTracker[currentCarID] = tracker 106 | carLocation1[currentCarID] = [x, y, w, h] 107 | 108 | currentCarID = currentCarID + 1 109 | 110 | for carID in carTracker.keys(): 111 | trackedPosition = carTracker[carID].get_position() 112 | 113 | t_x = int(trackedPosition.left()) 114 | t_y = int(trackedPosition.top()) 115 | t_w = int(trackedPosition.width()) 116 | t_h = int(trackedPosition.height()) 117 | 118 | cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w, t_y + t_h), rectangleColor, 4) 119 | 120 | carLocation2[carID] = [t_x, t_y, t_w, t_h] 121 | 122 | end_time = time.time() 123 | 124 | if not (end_time == start_time): 125 | fps = 1.0/(end_time - start_time) 126 | 127 | for i in carLocation1.keys(): 128 | if frameCounter % 1 == 0: 129 | [x1, y1, w1, h1] = carLocation1[i] 130 | [x2, y2, w2, h2] = carLocation2[i] 131 | 132 | carLocation1[i] = [x2, y2, w2, h2] 133 | 134 | if [x1, y1, w1, h1] != [x2, y2, w2, h2]: 135 | if (speed[i] == None or speed[i] == 0) and y1 >= 275 and y1 <= 285: 136 | speed[i] = estimateSpeed([x1, y1, w1, h1], [x1, y2, w2, h2]) 137 | 138 | if speed[i] != None and y1 >= 180: 139 | cv2.putText(resultImage, str(int(speed[i])) + "km/h", (int(x1 + w1/2), int(y1-5)), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 100) ,2) 140 | 141 | cv2.imshow('result', resultImage) 142 | 143 | out.write(resultImage) 144 | 145 | if cv2.waitKey(1) == 27: 146 | break 147 | 148 | 149 | cv2.destroyAllWindows() 150 | out.release() 151 | 152 | if __name__ == '__main__': 153 | trackMultipleObjects() --------------------------------------------------------------------------------