def calculate_social_distancing(vid_path, net, output_dir, output_vid, ln1): count = 0 vs = cv2.VideoCapture(vid_path) # Get video height, width and fps height = int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(vs.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(vs.get(cv2.CAP_PROP_FPS)) # Set scale for birds eye view # Bird's eye view will only show ROI scale_w, scale_h = utills.get_scale(width, height) fourcc = cv2.VideoWriter_fourcc(*"XVID") output_movie = cv2.VideoWriter("./output_vid/distancing.avi", fourcc, fps, (width, height)) bird_movie = cv2.VideoWriter("./output_vid/bird_eye_view.avi", fourcc, fps, (int(width * scale_w), int(height * scale_h))) points = [] global image while True: (grabbed, frame) = vs.read() if not grabbed: print('here') break (H, W) = frame.shape[:2] # first frame will be used to draw ROI and horizontal and vertical 180 cm distance(unit length in both directions) if count == 0: while True: image = frame cv2.imshow("image", image) cv2.waitKey(1) if len(mouse_pts) == 8: cv2.destroyWindow("image") break points = mouse_pts # Using first 4 points or coordinates for perspective transformation. The region marked by these 4 points are # considered ROI. This polygon shaped ROI is then warped into a rectangle which becomes the bird eye view. # This bird eye view then has the property property that points are distributed uniformally horizontally and # vertically(scale for horizontal and vertical direction will be different). So for bird eye view points are # equally distributed, which was not case for normal view. src = np.float32(np.array(points[:4])) dst = np.float32([[0, H], [W, H], [W, 0], [0, 0]]) prespective_transform = cv2.getPerspectiveTransform(src, dst) # using next 3 points for horizontal and vertical unit length(in this case 180 cm) pts = np.float32(np.array([points[4:7]])) warped_pt = cv2.perspectiveTransform(pts, prespective_transform)[0] # since bird eye view has property that all points are equidistant in horizontal and vertical direction. # distance_w and distance_h will give us 180 cm distance in both horizontal and vertical directions # (how many pixels will be there in 180cm length in horizontal and vertical direction of birds eye view), # which we can use to calculate distance between two humans in transformed view or bird eye view distance_w = np.sqrt((warped_pt[0][0] - warped_pt[1][0]) ** 2 + (warped_pt[0][1] - warped_pt[1][1]) ** 2) distance_h = np.sqrt((warped_pt[0][0] - warped_pt[2][0]) ** 2 + (warped_pt[0][1] - warped_pt[2][1]) ** 2) pnts = np.array(points[:4], np.int32) cv2.polylines(frame, [pnts], True, (70, 70, 70), thickness=2) #################################################################################### # YOLO v3 blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) start = time.time() layerOutputs = net.forward(ln1) end = time.time() boxes = [] confidences = [] classIDs = [] for output in layerOutputs: for detection in output: scores = detection[5:] classID = np.argmax(scores) confidence = scores[classID] # detecting humans in frame if classID == 0: if confidence > confid: box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) idxs = cv2.dnn.NMSBoxes(boxes, confidences, confid, thresh) font = cv2.FONT_HERSHEY_PLAIN boxes1 = [] for i in range(len(boxes)): if i in idxs: boxes1.append(boxes[i]) x,y,w,h = boxes[i] if len(boxes1) == 0: count = count + 1 continue # Here we will be using bottom center point of bounding box for all boxes and will transform all those # bottom center points to bird eye view person_points = utills.get_transformed_points(boxes1, prespective_transform) # Here we will calculate distance between transformed points(humans) distances_mat, bxs_mat = utills.get_distances(boxes1, person_points, distance_w, distance_h) risk_count = utills.get_count(distances_mat) frame1 = np.copy(frame) # Draw bird eye view and frame with bouding boxes around humans according to risk factor bird_image = plot.bird_eye_view(frame, distances_mat, person_points, scale_w, scale_h, risk_count) img = plot.social_distancing_view(frame1, bxs_mat, boxes1, risk_count) # Show/write image and videos if count != 0: output_movie.write(img) bird_movie.write(bird_image) cv2.imshow('Bird Eye View', bird_image) cv2.imwrite(output_dir+"frame%d.jpg" % count, img) cv2.imwrite(output_dir+"bird_eye_view/frame%d.jpg" % count, bird_image) count = count + 1 if cv2.waitKey(1) & 0xFF == ord('q'): break vs.release() cv2.destroyAllWindows()
def calculate_social_distancing(vid_path, net, output_dir, output_vid, ln1): count = 0 vs = cv2.VideoCapture(vid_path) # Get video height, width and fps height = int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(vs.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(vs.get(cv2.CAP_PROP_FPS)) # Set scale for birds eye view scale_w, scale_h = utills.get_scale(width, height) fourcc = cv2.VideoWriter_fourcc(*"XVID") # Initialize writer objects output_movie = cv2.VideoWriter("Output.avi", fourcc, fps, (width, height)) output_movie2 = cv2.VideoWriter("Output2.avi", fourcc, fps, (1920, 1080)) bird_movie = cv2.VideoWriter("./output_vid/bird_eye_view.avi", fourcc, fps, (int(width * scale_w), int(height * scale_h))) points = [] global image while True: # Read frames (grabbed, frame) = vs.read() if not grabbed: print('here') break (H, W) = frame.shape[:2] if count == 0: while True: image = frame cv2.imshow("image", image) cv2.waitKey(1) if len(mouse_pts) == 8: cv2.destroyWindow("image") break points = mouse_pts src = np.float32(np.array(points[:4])) dst = np.float32([[0, H], [W, H], [W, 0], [0, 0]]) # Transform perspective using opencv method prespective_transform = cv2.getPerspectiveTransform(src, dst) # using next 3 points for horizontal and vertical unit length(in this case 6 Feets ~= 180 cm) pts = np.float32(np.array([points[4:7]])) warped_pt = cv2.perspectiveTransform(pts, prespective_transform)[0] # Calculate distance scale using marked points by user distance_w = np.sqrt((warped_pt[0][0] - warped_pt[1][0])**2 + (warped_pt[0][1] - warped_pt[1][1])**2) distance_h = np.sqrt((warped_pt[0][0] - warped_pt[2][0])**2 + (warped_pt[0][1] - warped_pt[2][1])**2) pnts = np.array(points[:4], np.int32) cv2.polylines(frame, [pnts], True, (70, 70, 70), thickness=2) # Using YOLO v3 model using dnn method blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) start = time.time() layerOutputs = net.forward(ln1) end = time.time() boxes = [] confidences = [] classIDs = [] for output in layerOutputs: for detection in output: scores = detection[5:] classID = np.argmax(scores) confidence = scores[classID] # detecting humans in frame if classID == 0: if confidence > confid: # Finding bounding boxes dimensions box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) # Applying Non Maximum Suppression to remove multiple bounding boxes around same object idxs = cv2.dnn.NMSBoxes(boxes, confidences, confid, thresh) font = cv2.FONT_HERSHEY_PLAIN boxes1 = [] for i in range(len(boxes)): if i in idxs: boxes1.append(boxes[i]) x, y, w, h = boxes[i] if len(boxes1) == 0: count = count + 1 continue # Get transformed points using perspective transform person_points = utills.get_transformed_points(boxes1, prespective_transform) # Get distances between the points distances_mat, bxs_mat = utills.get_distances(boxes1, person_points, distance_w, distance_h) # Get the risk counts risk_count = utills.get_count(distances_mat) frame1 = np.copy(frame) bird_image = plot.bird_eye_view(frame, distances_mat, person_points, scale_w, scale_h, risk_count) img = plot.social_distancing_view(frame1, bxs_mat, boxes1, risk_count) if count != 0: bird_movie.write(bird_image) cv2.imshow('Social Distancing Detect', img) output_movie.write(img) output_movie2.write(img) cv2.imwrite(output_dir + "frame%d.jpg" % count, img) cv2.imwrite(output_dir + "bird_eye_view/frame%d.jpg" % count, bird_image) count = count + 1 if cv2.waitKey(1) & 0xFF == ord('q'): break output_movie.write(img) vs.release() cv2.destroyAllWindows()
print("[INFO] loading YOLO from disk...") net = cv2.dnn.readNetFromDarknet(configPath, weightsPath) # determine only the *output* layer names that we need from YOLO ln = net.getLayerNames() ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()] # initialize the video stream and pointer to output video file print("[INFO] accessing video stream...") vs = cv2.VideoCapture(args["input"] if args["input"] else 0) writer = None writer2 = None height2 = int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT)) width2 = int(vs.get(cv2.CAP_PROP_FRAME_WIDTH)) scale_w, scale_h = utills.get_scale(width2, height2) cv2.namedWindow("image") cv2.setMouseCallback("image", get_mouse_points) points = [] global image count = 0 # loop over the frames from the video stream while True: # read the next frame from the file (grabbed, frame) = vs.read() # kalau ga ada frame berarti selesai if not grabbed: print("ga kebaca")
def calculate_social_distancing(vid_path, net, output_dir, output_vid, ln1): count = 0 vs = cv2.VideoCapture(vid_path) height = int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT)) width = int(vs.get(cv2.CAP_PROP_FRAME_WIDTH)) fps = int(vs.get(cv2.CAP_PROP_FPS)) scale_w, scale_h = utills.get_scale(width, height) fourcc = cv2.VideoWriter_fourcc(*"XVID") output_movie = cv2.VideoWriter("./output_vid/distancing.avi", fourcc, fps, (width, height)) bird_movie = cv2.VideoWriter("./output_vid/bird_eye_view.avi", fourcc, fps, (int(width * scale_w), int(height * scale_h))) points = [] global image while True: (grabbed, frame) = vs.read() if not grabbed: print('here') break (H, W) = frame.shape[:2] if count == 0: while True: image = frame cv2.imshow("image", image) cv2.waitKey(1) if len(mouse_pts) == 8: cv2.destroyWindow("image") break points = mouse_pts src = np.float32(np.array(points[:4])) dst = np.float32([[0, H], [W, H], [W, 0], [0, 0]]) prespective_transform = cv2.getPerspectiveTransform(src, dst) pts = np.float32(np.array([points[4:7]])) warped_pt = cv2.perspectiveTransform(pts, prespective_transform)[0] distance_w = np.sqrt((warped_pt[0][0] - warped_pt[1][0])**2 + (warped_pt[0][1] - warped_pt[1][1])**2) distance_h = np.sqrt((warped_pt[0][0] - warped_pt[2][0])**2 + (warped_pt[0][1] - warped_pt[2][1])**2) pnts = np.array(points[:4], np.int32) cv2.polylines(frame, [pnts], True, (70, 70, 70), thickness=2) blob = cv2.dnn.blobFromImage(frame, 1 / 255.0, (416, 416), swapRB=True, crop=False) net.setInput(blob) start = time.time() layerOutputs = net.forward(ln1) end = time.time() boxes = [] confidences = [] classIDs = [] for output in layerOutputs: for detection in output: scores = detection[5:] classID = np.argmax(scores) confidence = scores[classID] if classID == 0: if confidence > confid: box = detection[0:4] * np.array([W, H, W, H]) (centerX, centerY, width, height) = box.astype("int") x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) idxs = cv2.dnn.NMSBoxes(boxes, confidences, confid, thresh) font = cv2.FONT_HERSHEY_PLAIN boxes1 = [] for i in range(len(boxes)): if i in idxs: boxes1.append(boxes[i]) x, y, w, h = boxes[i] if len(boxes1) == 0: count = count + 1 continue person_points = utills.get_transformed_points(boxes1, prespective_transform) distances_mat, bxs_mat = utills.get_distances(boxes1, person_points, distance_w, distance_h) risk_count = utills.get_count(distances_mat) frame1 = np.copy(frame) bird_image = plot.bird_eye_view(frame, distances_mat, person_points, scale_w, scale_h, risk_count) img = plot.social_distancing_view(frame1, bxs_mat, boxes1, risk_count) if count != 0: output_movie.write(img) bird_movie.write(bird_image) cv2.imshow('Bird Eye View', bird_image) cv2.imwrite(output_dir + "frame%d.jpg" % count, img) cv2.imwrite(output_dir + "bird_eye_view/frame%d.jpg" % count, bird_image) count = count + 1 if cv2.waitKey(1) & 0xFF == ord('q'): break vs.release() cv2.destroyAllWindows()