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")

    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)
    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')
#ln: YOLO CNN output layer names

ln =net.getLayerNames()
ln = [ln[i[0]-1]for i in net.getUnconnectedOutLayers()]
print("[INFO] accesing video stream")

vs =cv2.VideoCapture( r"pedestrian.mp4"if "pedestrian.mp4"else 0)#r"pedestrian.mp4"if "pedestrian.mp4"else
writer =None

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:
        
        # 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]):
                if D[i,j]<config.MIN_DISTANCE:
                    violate.add(i)
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:

            # 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])
            Cendis = dist.cdist(centroids, centroids, metric="euclidean")
            D = Convertltd([r[1] for r in results])

            # Using the widths of bounding boxes and dis btwn their centroids to find violating boxes
            for i in range(len(D)):
                for j in range(len(D)):
                    if (D[i][3] - D[i][1]) >= (D[j][3] - D[j][1]):
                        if (D[i][3] - D[i][1]) * 0.8 <= (
                                D[j][3] - D[j][1]) <= (D[i][3] - D[i][1]):
                            if (D[i][2] - D[i][0]) > (D[j][2] - D[j][0]):
                                if (D[j][2] - D[j][0]) * 0.3 <= Cendis[
                                        i,
                                        j] <= (D[i][2] - D[i][0]
                                               ) * 1.4:  #(D[j][2]-D[j][0])<=
                                    violate.add(i)
                                    violate.add(j)
                            else:
                                if (D[i][2] - D[i][0]) * 0.3 <= Cendis[
                                        i,
                                        j] <= (D[j][2] - D[j][0]
                                               ) * 1.4:  #(D[i][2]-D[i][0])<=
                                    violate.add(i)
                                    violate.add(j)
                    else:
                        if (D[j][3] - D[j][1]) * 0.8 <= (
                                D[i][3] - D[i][1]) <= (D[j][3] - D[j][1]):
                            if (D[i][2] - D[i][0]) >= (D[j][2] - D[j][0]):
                                if (D[j][2] - D[j][0]) * 0.3 <= Cendis[
                                        i,
                                        j] <= (D[i][2] - D[i][0]
                                               ) * 1.4:  #(D[j][2]-D[j][0])<=
                                    violate.add(i)
                                    violate.add(j)
                            else:
                                if (D[i][2] - D[i][0]) * 0.3 <= Cendis[
                                        i,
                                        j] <= (D[j][2] - D[j][0]
                                               ) * 1.4:  #(D[i][2]-D[i][0])<=
                                    violate.add(i)
                                    violate.add(j)

        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')