Comments (5)
from speed-camera.
Hii sir thank you for your feedback , : i relied on this PDF for the calculation of the speed "" https://pdfs.semanticscholar.org/efb3/7b45f4c9241c7d4ddf1295450dba6cf5cd06.pdf to calculate the speed""
you can find the code as below :
`import cv2
import dlib
import time
import threading
import math
#carCascade = cv2.CascadeClassifier('myhaar.xml')
video = cv2.VideoCapture('0')
WIDTH = 1280
HEIGHT = 720
speed = K * d_pixels / time // ( time = 1 / fps ) between 2 frames
def estimateSpeed(x1, x2):
d_pixels = math.sqrt(math.pow(x2[0] - x1[0], 2) + math.pow(x2[1] - x1[1], 2))
K = 15.13 # K is coeficient in relation with FOV and angle of view , u can find how to calculate the speed in this link
#https://pdfs.semanticscholar.org/efb3/7b45f4c9241c7d4ddf1295450dba6cf5cd06.pdf
d_meters = d_pixels * K
#print("d_pixels=" + str(d_pixels), "d_meters=" + str(d_meters))
fps = 18
speed = d_meters * fps * 3.6
return speed
def trackMultipleObjects():
rectangleColor = (0, 255, 0)
frameCounter = 0
currentCarID = 0
fps = 0
carTracker = {}
carNumbers = {}
carx1 = {}
carx2 = {}
speed = [None] * 1000
# Write output to video file
out = cv2.VideoWriter('outpy.avi',cv2.VideoWriter_fourcc('M','J','P','G'), 10, (WIDTH,HEIGHT))
while True:
start_time = time.time()
rc, image = video.read()
if type(image) == type(None):
break
image = cv2.resize(image, (WIDTH, HEIGHT))
resultImage = image.copy()
frameCounter = frameCounter + 1
carIDtoDelete = []
for carID in carTracker.keys():
trackingQuality = carTracker[carID].update(image)
if trackingQuality < 7:
carIDtoDelete.append(carID)
for carID in carIDtoDelete:
print ('Removing carID ' + str(carID) + ' from list of trackers.')
print ('Removing carID ' + str(carID) + ' previous x.')
print ('Removing carID ' + str(carID) + ' current x.')
carTracker.pop(carID, None)
carx1.pop(carID, None)
carx2.pop(carID, None)
if not (frameCounter % 10):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cars = carCascade.detectMultiScale(gray, 1.1, 13, 18, (24, 24))
for (_x, _y, _w, _h) in cars:
x = int(_x)
y = int(_y)
w = int(_w)
h = int(_h)
x_bar = x + 0.5 * w
y_bar = y + 0.5 * h
matchCarID = None
for carID in carTracker.keys():
trackedPosition = carTracker[carID].get_position()
t_x = int(trackedPosition.left())
t_y = int(trackedPosition.top())
t_w = int(trackedPosition.width())
t_h = int(trackedPosition.height())
t_x_bar = t_x + 0.5 * t_w
t_y_bar = t_y + 0.5 * t_h
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))):
matchCarID = carID
if matchCarID is None:
print ('Creating new tracker ' + str(currentCarID))
tracker = dlib.correlation_tracker()
tracker.start_track(image, dlib.rectangle(x, y, x + w, y + h))
carTracker[currentCarID] = tracker
carx1[currentCarID] = [x, y, w, h]
currentCarID = currentCarID + 1
#cv2.line(resultImage,(0,480),(1280,480),(255,0,0),5)
for carID in carTracker.keys():
trackedPosition = carTracker[carID].get_position()
t_x = int(trackedPosition.left())
t_y = int(trackedPosition.top())
t_w = int(trackedPosition.width())
t_h = int(trackedPosition.height())
cv2.rectangle(resultImage, (t_x, t_y), (t_x + t_w, t_y + t_h), rectangleColor, 4)
# speed estimation
carx2[carID] = [t_x, t_y, t_w, t_h]
end_time = time.time()
if not (end_time == start_time):
fps = 1.0/(end_time - start_time)
#cv2.putText(resultImage, 'FPS: ' + str(int(fps)), (620, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
for i in carx1.keys():
if frameCounter % 1 == 0:
[x1, y1, w1, h1] = carx1[i]
[x2, y2, w2, h2] = carx2[i]
# print 'previous x: ' + str(carx1[i]) + ', current x: ' + str(carx2[i])
carx1[i] = [x2, y2, w2, h2]
# print 'new previous x: ' + str(carx1[i])
if [x1, y1, w1, h1] != [x2, y2, w2, h2]:
if (speed[i] == None or speed[i] == 0) and y1 >= 275 and y1 <= 285:
speed[i] = estimateSpeed([x1, y1, w1, h1], [x2, y2, w2, h2])
#if y1 > 275 and y1 < 285:
if speed[i] != None and y1 >= 180:
cv2.putText(resultImage, str(int(speed[i])) + " km/hr", (int(x1 + w1/2), int(y1-5)),cv2.FONT_HERSHEY_SIMPLEX, 0.75, (255, 255, 255), 2)
#print ('CarID ' + str(i) + ': speed is ' + str("%.2f" % round(speed[i], 0)) + ' km/h.\n')
#else:
# cv2.putText(resultImage, "Far Object", (int(x1 + w1/2), int(y1)),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
#print ('CarID ' + str(i) + ' x1: ' + str(carx1[i]) + ' x2: ' + str(carx2[i]) + ' speed is ' + str("%.2f" % round(speed[i], 0)) + ' km/h.\n')
cv2.imshow('result', resultImage)
# Write the frame into the file 'output.avi'
#out.write(resultImage)
if cv2.waitKey(33) == 27:
break
cv2.destroyAllWindows()
if name == 'main':
trackMultipleObjects()`
from speed-camera.
from speed-camera.
Yes Sir, im using my laptop not RPI ,i tested it but the value of speed not true , as you see in this picture:
like you see the value of speed indicated is 28 Kps but the real value is 54 Kps , Could explain how can i play with value of parameters to get the estimation speed. Thanks
from speed-camera.
from speed-camera.
Related Issues (20)
- No Window to view camera HOT 6
- Windows not displaying on host machine HOT 1
- Clarification on calibration HOT 4
- Frames dropped due to contour out of bounds? HOT 2
- Docker Install always 11.22 HOT 1
- Calibration with different Screen Ratio and Framerate HOT 1
- No detections or speed calcs HOT 2
- No images
- How do I manually trigger the camera? HOT 4
- Can I reposition calibration marks? HOT 1
- Capture being triggered by small objects and missing all vehicles. HOT 1
- Warnings in startup log: "Unsupported V4L2 pixel format" HOT 1
- Vehicles at higher speeds being missed. HOT 4
- search-speed / Not compatible with the major rewrite? HOT 2
- Bigger resolution - `picam720` plugin - `speed_camera Out - 0/6` messages HOT 1
- Vehicles detected twice: Ideal `MO_TRACK_EVENT_COUNT`
- `MO_MIN_AREA_PX` is not the area of the rectangle
- Capture video HOT 2
- RPI 5, Bookworm - speed_cam.py process starts, then stops HOT 1
- Over reading speed on large vehicles HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from speed-camera.