Skip to content

Instantly share code, notes, and snippets.

@shoaibmehedi7
Created July 20, 2021 11:36
Show Gist options
  • Save shoaibmehedi7/a400cb49b43106aca4760ec968917b30 to your computer and use it in GitHub Desktop.
Save shoaibmehedi7/a400cb49b43106aca4760ec968917b30 to your computer and use it in GitHub Desktop.
def main():
cap = cv2.VideoCapture('/home/python/OpenCV/soacialDistanceDetect/video .mp4')
fps_start_time = datetime.datetime.now()
fps = 0
total_frames = 0
while True:
ret, frame = cap.read()
frame = imutils.resize(frame, width=600)
total_frames = total_frames + 1
(H, W) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(frame, 0.007843, (W, H), 127.5)
detector.setInput(blob)
person_detections = detector.forward()
rects = []
for i in np.arange(0, person_detections.shape[2]):
confidence = person_detections[0, 0, i, 2]
if confidence > 0.5:
idx = int(person_detections[0, 0, i, 1])
if CLASSES[idx] != "person":
continue
person_box = person_detections[0, 0, i, 3:7] * np.array([W, H, W, H])
(startX, startY, endX, endY) = person_box.astype("int")
rects.append(person_box)
boundingboxes = np.array(rects)
boundingboxes = boundingboxes.astype(int)
rects = non_max_suppression_fast(boundingboxes, 0.3)
centroid_dict = dict()
objects = tracker.update(rects)
for (objectId, bbox) in objects.items():
x1, y1, x2, y2 = bbox
x1 = int(x1)
y1 = int(y1)
x2 = int(x2)
y2 = int(y2)
cX = int((x1 + x2) / 2.0)
cY = int((y1 + y2) / 2.0)
centroid_dict[objectId] = (cX, cY, x1, y1, x2, y2)
red_zone_list = []
for (id1, p1), (id2, p2) in combinations(centroid_dict.items(), 2):
dx, dy = p1[0] - p2[0], p1[1] - p2[1]
distance = math.sqrt(dx * dx + dy * dy)
if distance < 75.0:
if id1 not in red_zone_list:
red_zone_list.append(id1)
if id2 not in red_zone_list:
red_zone_list.append(id2)
for id, box in centroid_dict.items():
if id in red_zone_list:
cv2.rectangle(frame, (box[2], box[3]), (box[4], box[5]), (0, 0, 255), 2)
else:
cv2.rectangle(frame, (box[2], box[3]), (box[4], box[5]), (0, 255, 0), 2)
fps_end_time = datetime.datetime.now()
time_diff = fps_end_time - fps_start_time
if time_diff.seconds == 0:
fps = 0.0
else:
fps = (total_frames / time_diff.seconds)
fps_text = "FPS: {:.2f}".format(fps)
cv2.putText(frame, fps_text, (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.imshow("SocialDistancing", frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.destroyAllWindows()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment