def gen(): while True: (grabbed, frame) = vs.read() if not grabbed: break frame = imutils.resize(frame, width=700) results = detect_people(frame, net, ln, personIdx=LABELS.index("person")) violate = set() if len(results) >= 2: centroids = np.array([r[2] for r in results]) D = dist.cdist(centroids, centroids, metric="euclidean") # loop over the upper triangular of the distance matrix for i in range(0, D.shape[0]): for j in range(i + 1, D.shape[1]): if D[i, j] < config.MIN_DISTANCE: violate.add(i) violate.add(j) # loop over the results for (i, (prob, bbox, centroid)) in enumerate(results): (startX, startY, endX, endY) = bbox (cX, cY) = centroid color = (0, 255, 0) if i in violate: color = (0, 0, 255) cv2.rectangle(frame, (startX, startY), (endX, endY), color, 2) cv2.circle(frame, (cX, cY), 5, color, 1) text = "Social Distancing Violations: {}".format(len(violate)) cv2.putText(frame, text, (10, frame.shape[0] - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.85, (0, 0, 255), 3) cv2.imwrite("1.jpg",frame) (flag, encodedImage) = cv2.imencode(".jpg", frame) yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n')
print("[INFO] accessing video stream...") vs = cv2.VideoCapture(r"pedestrian.mp4" if "pedestrian.mp4" else 0) writer = None # loop over the frames from the video stream while True: # read the next frame from the file (grabbed, frame) = vs.read() # if the frame was not grabbed, then we have reached the end # of the stream if not grabbed: break # resize the frame and then detect people (and only people) in it frame = imutils.resize(frame, width=700) results = detect_people(frame, net, ln, personIdx=LABELS.index("person")) # initialize the set of indexes that violate the minimum social # distance violate = set() # ensure there are *at least* two people detections (required in # order to compute our pairwise distance maps) if len(results) >= 2: # extract all centroids from the results and compute the # Euclidean distances between all pairs of the centroids centroids = np.array([r[2] for r in results]) D = dist.cdist(centroids, centroids, metric="euclidean") # loop over the upper triangular of the distance matrix for i in range(0, D.shape[0]): for j in range(i + 1, D.shape[1]):