Skip to content

Commit

Permalink
Added distance approximation
Browse files Browse the repository at this point in the history
  • Loading branch information
daved01 committed Apr 1, 2021
1 parent 51e690e commit 41c3ec9
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
Binary file added media/ModelOutputs/ModelOutputs alias
Binary file not shown.
Binary file removed media/intersection.jpg
Binary file not shown.
9 changes: 9 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import utils2.logger as logger
import utils2.constants as constants
import utils2.lane_detection as lanes
import utils2.distance as distance


# Required for the slider
Expand Down Expand Up @@ -146,6 +147,7 @@ def runProgram(model_type, video_file, lane_detection, logs_enabled, writeOutput
# Locate objects with model if selected
if (len(sys.argv) >= 2 and model_enabled == 1 and model_type != "-detr" and model_type != "-fasterrcnn" and model_type != "-yolov5s"):
boxes, labels, conf = predictor.predict(image, 10, 0.4)

frame = pascalBoxes(image, conf, boxes, labels)
elif (len(sys.argv) >= 2 and model_enabled == 1 and model_type == "-detr"):
boxes, labels, conf = detr_predict(predictor, image)
Expand Down Expand Up @@ -180,6 +182,13 @@ def runProgram(model_type, video_file, lane_detection, logs_enabled, writeOutput
if (lane_enabled == 1):
frame = lanes.detect(frame)


# ===================== Estimate distances
frame = distance.estimate(frame, boxes)
# =====================



# Display the resulting frame
cv.imshow(windowname, frame)
if ((writeOutput == True)):
Expand Down
Binary file added utils2/Figures_dark.key
Binary file not shown.
49 changes: 49 additions & 0 deletions utils2/distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import cv2 as cv
import numpy as np

"""
TODO:
* Filter boxes for vehicle class only
* Make region of interest to filter location where distance is computed
* Tune parameters
* Think about how distance warning could work without known vehicle speed
* Write post about it
"""


def estimateDistance(boxWidth):
pass

return



def estimate(frame, boxes):
"""
Estimates distance for each box.
Uses fixed approximations for focal width [pixels] and vehicle width.
"""

#print("---")
for box in boxes:
width = None
if len(box) > 0:
x1, x2 = box[0], box[2]
y1, y2 = box[1], box[3]
width = x2 - x1
height_to_width = (y2 - y1) / (x2 - x1)

# Focal length in pixels
focallength = 1000
vehicleWidth = 2.0
if (width != None) and (height_to_width > .8):
distance = np.round((vehicleWidth * focallength) / width)
# print("Distance: {:.2f}m".format(distance))
bb_text = "{:.0f}m".format(distance)
font = cv.FONT_HERSHEY_SIMPLEX

cv.putText(frame, bb_text, (x1,int(y1-5)), font, 0.8, (255, 0, 0), 2)


return frame

0 comments on commit 41c3ec9

Please sign in to comment.