Skip to content

Instantly share code, notes, and snippets.

@AdroitAnandAI
Last active September 16, 2021 12:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdroitAnandAI/313243481c4db81c19b4849db426103d to your computer and use it in GitHub Desktop.
Save AdroitAnandAI/313243481c4db81c19b4849db426103d to your computer and use it in GitHub Desktop.
Monocular Depth Estimation
# Detections contains bounding boxes using object detection model
boxcount = 0
depths = []
bboxMidXs = []
bboxMidYs = []
# This is computed to reflect real distance during initial camera calibration
scalingFactor = 1000
# Depth scaling factor is based on one-time cam calibration
for detection in detections:
xmin, ymin, xmax, ymax = detection
depths.append(np.median(disp[ymin:ymax, xmin:xmax]))
bboxMidXs.append((xmin+xmax)/2)
bboxMidYs.append((ymin+ymax)/2)
size = disp.shape[:2]
# disp = draw_detections(disp, detection)
xmin = max(int(detection[0]), 0)
ymin = max(int(detection[1]), 0)
xmax = min(int(detection[2]), size[1])
ymax = min(int(detection[3]), size[0])
boxcount = boxcount + 1
cv2.rectangle(disp, (xmin, ymin), (xmax, ymax), (0,255,0), 2)
cv2.putText(disp, '{} {}'.format('person', boxcount),
(xmin, ymin - 7), cv2.FONT_HERSHEY_COMPLEX, 0.6, (0,255,0), 1)
for i in range(len(bboxMidXs)):
for j in range(i+1, len(bboxMidXs)):
dist = np.square(bboxMidXs[i] - bboxMidXs[j]) +
np.square((depths[i]-depths[j])*scalingFactor)
# check whether less than 200 to detect
# social distance violations
if np.sqrt(dist) < 200:
color = (0, 0, 255)
thickness = 3
else:
color = (0, 255, 0)
thickness = 1
cv2.line(original_img, (int(bboxMidXs[i]), int(bboxMidYs[i])),
(int(bboxMidXs[j]), int(bboxMidYs[j])), color, thickness)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment