コード例 #1
0
    def run(self):
        results = []
        idx_frame = 0
        plt.figure()
        while self.vdo.grab():
            self.telegram_bot.update_database()
            #########################################
            # Load the config for the top-down view #
            #########################################
            #  print(bcolors.WARNING + "[ Loading config file for the bird view transformation ] " + bcolors.ENDC)
            with open("configs/config_birdview.yml", "r") as ymlfile:
                cfg = yaml.safe_load(ymlfile)
            width_og, height_og = 0, 0
            corner_points = []
            for section in cfg:
                corner_points.append(cfg["image_parameters"]["p1"])
                corner_points.append(cfg["image_parameters"]["p2"])
                corner_points.append(cfg["image_parameters"]["p3"])
                corner_points.append(cfg["image_parameters"]["p4"])
                width_og = int(cfg["image_parameters"]["width_og"])
                height_og = int(cfg["image_parameters"]["height_og"])
                img_path = cfg["image_parameters"]["img_path"]
                size_frame = cfg["image_parameters"]["size_frame"]
            #   print(bcolors.OKGREEN + " Done : [ Config file loaded ] ..." + bcolors.ENDC)

            #########################################
            #     Compute transformation matrix		#
            #########################################
            # Compute  transformation matrix from the original frame
            matrix, imgOutput = compute_perspective_transform(
                corner_points, width_og, height_og, cv2.imread(img_path))
            height, width, _ = imgOutput.shape
            blank_image = np.zeros((height, width, 3), np.uint8)
            height = blank_image.shape[0]
            width = blank_image.shape[1]
            dim = (width, height)

            # Load the image of the ground and resize it to the correct size
            img = cv2.imread("img/chemin_1.png")
            bird_view_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

            idx_frame += 1
            if idx_frame % self.args.frame_interval:
                continue

            start = time.time()
            _, ori_im = self.vdo.retrieve()
            im = cv2.cvtColor(ori_im, cv2.COLOR_BGR2RGB)
            cv2.imwrite("img_calib.bmp", im)
            # # Draw the green rectangle to delimitate the detection zone
            # draw_rectangle(ori_im, corner_points)

            # do detection
            bbox_xywh, cls_conf, cls_ids = self.detector(im)

            # select person class
            mask = cls_ids == 0

            bbox_xywh = bbox_xywh[mask]
            # bbox dilation just in case bbox too small, delete this line if using a better pedestrian detector
            bbox_xywh[:, 3:] *= 1
            cls_conf = cls_conf[mask]

            # do tracking
            outputs = self.deepsort.update(bbox_xywh, cls_conf, im)

            # draw boxes for visualization
            if len(outputs) > 0:
                bbox_tlwh = []
                bbox_xyxy = outputs[:, :4]
                track_identities = outputs[:, -3]
                track_aruco = outputs[:, -2]
                ori_im = draw_boxes(ori_im, bbox_xyxy, track_identities,
                                    track_aruco)

                # check statuses (acquired):
                for person in outputs:
                    stat = self.status_dict[person[-1]]
                    if stat != "OK":
                        print([
                            str(time.time()) + ":" + stat + ":" +
                            str(person[-2]) + ":" + str(person[-2])
                        ])
                        telegram_msg = [
                            str(time.time()) + ":" + stat + ":" +
                            str(person[-2]) + ":" + str(person[-2])
                        ]
                        self.telegram_bot.send_message(telegram_msg)

                array_centroids, array_groundpoints = get_centroids_and_groundpoints(
                    bbox_xyxy)
                # Use the transform matrix to get the transformed coordonates
                transformed_downoids = compute_point_perspective_transformation(
                    np.linalg.inv(self.H), array_groundpoints)

                # Show every point on the top view image
                plt.cla()
                plt.xlim((-500, 100))
                plt.ylim((-100, 500))
                plt.grid()
                plt.xlabel('[m]')
                plt.ylabel('[m]')
                for point in transformed_downoids:
                    print(point)
                    x, y = point
                    # cv2.circle(bird_view_img, (x, y), BIG_CIRCLE, COLOR_GREEN, 2)
                    # cv2.circle(bird_view_img, (x, y), SMALL_CIRCLE, COLOR_GREEN, -1)

                    plt.scatter(x, y, marker='+', color='b')
                    plt.scatter(x,
                                y,
                                marker='o',
                                color='g',
                                alpha=0.2,
                                s=1500 * 2)

                list_indexes = list(
                    itertools.combinations(range(len(transformed_downoids)),
                                           2))

                for i, pair in enumerate(
                        itertools.combinations(transformed_downoids, r=2)):
                    # Check if the distance between each combination of points is less than the minimum distance chosen
                    distance_minimum = 110
                    if math.sqrt(
                        (pair[0][0] - pair[1][0])**2 +
                        (pair[0][1] - pair[1][1])**2) < distance_minimum:
                        # Change the colors of the points that are too close from each other to red
                        # if not (pair[0][0] > width or pair[0][0] < 0 or pair[0][1] > height + 200 or pair[0][
                        #     1] < 0 or
                        #         pair[1][0] > width or pair[1][0] < 0 or pair[1][1] > height + 200 or pair[1][
                        #             1] < 0):
                        change_color_on_topview(bird_view_img, pair)
                        # Get the equivalent indexes of these points in the original frame and change the color to red
                        index_pt1 = list_indexes[i][0]
                        index_pt2 = list_indexes[i][1]
                        cv2.rectangle(
                            ori_im,
                            (bbox_xyxy[index_pt1][0], bbox_xyxy[index_pt1][1]),
                            (bbox_xyxy[index_pt1][2], bbox_xyxy[index_pt1][3]),
                            COLOR_RED, 3)
                        cv2.rectangle(
                            ori_im,
                            (bbox_xyxy[index_pt2][0], bbox_xyxy[index_pt2][1]),
                            (bbox_xyxy[index_pt2][2], bbox_xyxy[index_pt2][3]),
                            COLOR_RED, 3)

                        outputs[index_pt1][-1] = 2
                        outputs[index_pt2][-1] = 2

                        plt.scatter(pair[0][0],
                                    pair[0][1],
                                    marker='o',
                                    color='r',
                                    alpha=0.2,
                                    s=1500 * 2)
                        plt.scatter(pair[1][0],
                                    pair[1][1],
                                    marker='o',
                                    color='r',
                                    alpha=0.2,
                                    s=1500 * 2)

                        telegram_msg = [
                            str(time.time()) + ":" + "VIOLATION" + ":" +
                            str(outputs[index_pt1][-2]) + ":" +
                            str(outputs[index_pt2][-2])
                        ]
                        self.telegram_bot.send_message(telegram_msg)

                plt.pause(0.05)

                for bb_xyxy in bbox_xyxy:
                    bbox_tlwh.append(self.deepsort._xyxy_to_tlwh(bb_xyxy))

                results.append(
                    (idx_frame - 1, bbox_tlwh, track_identities, track_aruco))

            end = time.time()

            cv2.imshow("Bird view", bird_view_img)

            if self.args.display:
                cv2.imshow("test", ori_im)
                cv2.waitKey(1)

            if self.args.save_path:
                self.writer.write(ori_im)

            # save results
            # write_results(self.save_results_path, results, 'mot')

            # logging
            self.logger.info("time: {:.03f}s, fps: {:.03f}, detection numbers: {}, tracking numbers: {}" \
                             .format(end - start, 1 / (end - start), bbox_xywh.shape[0], len(outputs)))
コード例 #2
0
vs = cv2.VideoCapture(video_path)
output_video_1,output_video_2 = None,None

while True:
	img = cv2.imread("../img/chemin_1.png")
	bird_view_img = cv2.resize(img,dim,interpolation = cv2.INTER_AREA)	

	frame_exists,frame = vs.read()
	if not frame_exists:
		break
	else:
		frame = imutils.resize(frame,width=int(size_frame))
		(boxes,scores,classes) = model.predict(frame)
		array_boxes_detected = get_human_box_detection(boxes,scores[0].tolist(),classes[0].tolist(),frame.shape[0],frame.shape[1])
		array_centroids , array_groundpoints = get_centroids_and_groundpoints(array_boxes_detected)
		transformed_downoids = compute_point_perspective_transformation(matrix,array_groundpoints)
		
		for point in transformed_downoids:
			(x,y)= point
			cv2.circle(bird_view_img,(x,y),60,(0,255,0),2)
			cv2.circle(bird_view_img,(x,y),3,(0,255,0),-1)

		if len(transformed_downoids) >=2:
			for index,downoid in enumerate(transformed_downoids):
				if not (downoid[0] > width or downoid[0] < 0 or downoid[1] > height+200 or downoid[1] < 0 ):
						cv2.rectangle(frame,(array_boxes_detected[index][1],array_boxes_detected[index][0]),(array_boxes_detected[index]			[3],array_boxes_detected[index][2]),(0,255,0),2)			


			list_indexes = list(itertools.combinations(range(len(transformed_downoids)),2))
			for i,pair in enumerate(itertools.combinations(transformed_downoids,2)):
				if np.linalg.norm(np.array(pair[0])-np.array(pair[1])) < int(distance_minimum):
def run(v_path):
    #########################################
    # Load the config for the top-down view #
    #########################################
    with open("../conf/config_birdview.yml", "r") as ymlfile:
        cfg = yaml.load(ymlfile)
    width_og, height_og = 0, 0
    corner_points = []
    for section in cfg:
        corner_points.append(cfg["image_parameters"]["p1"])
        corner_points.append(cfg["image_parameters"]["p2"])
        corner_points.append(cfg["image_parameters"]["p3"])
        corner_points.append(cfg["image_parameters"]["p4"])
        width_og = int(cfg["image_parameters"]["width_og"])
        height_og = int(cfg["image_parameters"]["height_og"])
        img_path = cfg["image_parameters"]["img_path"]
        size_frame = cfg["image_parameters"]["size_frame"]

    #########################################
    #		     Select the model 			#
    #########################################
    model_names_list = [
        name for name in os.listdir("../models/.") if name.find(".") == -1
    ]
    for index, model_name in enumerate(model_names_list):
        print(" - {} [{}]".format(model_name, index))
    model_num = ""
    if model_num == "":
        model_path = "../models/faster_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb"
    else:
        model_path = "../models/" + model_names_list[int(
            model_num)] + "/frozen_inference_graph.pb"
    model = Model(model_path)

    #########################################
    #		     Select the video 			#
    #########################################
    video_names_list = [
        name for name in os.listdir("../video/")
        if name.endswith(".mp4") or name.endswith(".avi")
    ]
    for index, video_name in enumerate(video_names_list):
        print(" - {} [{}]".format(video_name, index))
        video_path = v_path

    #########################################
    #		    Minimal distance			#
    #########################################
    distance_minimum = "110"

    #########################################
    #     Compute transformation matrix		#
    #########################################
    # Compute  transformation matrix from the original frame
    blank_image = np.zeros((height, width, 3), np.uint8)
    matrix, imgOutput = compute_perspective_transform(corner_points, width,
                                                      height, blank_image)
    cv2.imwrite("../img/chemin_1.png", imgOutput)

    matrix, imgOutput = compute_perspective_transform(corner_points, width_og,
                                                      height_og,
                                                      cv2.imread(img_path))
    blank_image = np.zeros((height, width, 3), np.uint8)
    height, width, _ = imgOutput.shape
    height = blank_image.shape[0]
    width = blank_image.shape[1]
    dim = (width, height)

    ######################################################
    #########									 #########
    # 				START THE VIDEO STREAM               #
    #########									 #########
    ######################################################
    vs = cv2.VideoCapture(video_path)
    output_video_1, output_video_2 = None, None
    # Loop until the end of the video stream
    while True:
        # Load the image of the ground and resize it to the correct size
        img = cv2.imread("../img/chemin_1.png")
        bird_view_img = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)

        # Load the frame
        (frame_exists, frame) = vs.read()
        # Test if it has reached the end of the video
        if not frame_exists:
            break
        else:
            # Resize the image to the correct size
            frame = imutils.resize(frame, width=int(size_frame))

            # Make the predictions for this frame
            (boxes, scores, classes) = model.predict(frame)

            # Get the human detected in the frame and return the 2 points to build the bounding box
            array_boxes_detected = get_human_box_detection(
                boxes, scores[0].tolist(), classes[0].tolist(), frame.shape[0],
                frame.shape[1])

            # Both of our lists that will contain the centroïds coordonates and the ground points
            array_centroids, array_groundpoints = get_centroids_and_groundpoints(
                array_boxes_detected)

            # Use the transform matrix to get the transformed coordonates
            transformed_downoids = compute_point_perspective_transformation(
                matrix, array_groundpoints)

            # Show every point on the top view image
            for point in transformed_downoids:
                x, y = point
                cv2.circle(bird_view_img, (x, y), BIG_CIRCLE, COLOR_GREEN, 2)
                cv2.circle(bird_view_img, (x, y), SMALL_CIRCLE, COLOR_GREEN,
                           -1)

            # Check if 2 or more people have been detected (otherwise no need to detect)
            if len(transformed_downoids) >= 2:
                for index, downoid in enumerate(transformed_downoids):
                    if not (downoid[0] > width or downoid[0] < 0
                            or downoid[1] > height + 200 or downoid[1] < 0):
                        cv2.rectangle(frame, (array_boxes_detected[index][1],
                                              array_boxes_detected[index][0]),
                                      (array_boxes_detected[index][3],
                                       array_boxes_detected[index][2]),
                                      COLOR_GREEN, 2)

                # Iterate over every possible 2 by 2 between the points combinations
                list_indexes = list(
                    itertools.combinations(range(len(transformed_downoids)),
                                           2))
                for i, pair in enumerate(
                        itertools.combinations(transformed_downoids, r=2)):
                    # Check if the distance between each combination of points is less than the minimum distance chosen
                    if math.sqrt(
                        (pair[0][0] - pair[1][0])**2 +
                        (pair[0][1] - pair[1][1])**2) < int(distance_minimum):
                        # Change the colors of the points that are too close from each other to red
                        if not (pair[0][0] > width or pair[0][0] < 0
                                or pair[0][1] > height + 200 or pair[0][1] < 0
                                or pair[1][0] > width or pair[1][0] < 0 or
                                pair[1][1] > height + 200 or pair[1][1] < 0):
                            change_color_on_topview(pair)
                            # Get the equivalent indexes of these points in the original frame and change the color to red
                            index_pt1 = list_indexes[i][0]
                            index_pt2 = list_indexes[i][1]
                            cv2.rectangle(frame,
                                          (array_boxes_detected[index_pt1][1],
                                           array_boxes_detected[index_pt1][0]),
                                          (array_boxes_detected[index_pt1][3],
                                           array_boxes_detected[index_pt1][2]),
                                          COLOR_RED, 2)
                            cv2.rectangle(frame,
                                          (array_boxes_detected[index_pt2][1],
                                           array_boxes_detected[index_pt2][0]),
                                          (array_boxes_detected[index_pt2][3],
                                           array_boxes_detected[index_pt2][2]),
                                          COLOR_RED, 2)

        # Draw the green rectangle to delimitate the detection zone
        draw_rectangle(corner_points)
        # Show both images
        # cv2.imshow("Bird view", bird_view_img)
        # cv2.imshow("Original picture", frame)

        key = cv2.waitKey(1) & 0xFF

        # Write the both outputs video to a local folders
        if output_video_1 is None and output_video_2 is None:
            fourcc1 = cv2.VideoWriter_fourcc(*"MJPG")
            output_video_1 = cv2.VideoWriter("../output/video.avi", fourcc1,
                                             25,
                                             (frame.shape[1], frame.shape[0]),
                                             True)
            fourcc2 = cv2.VideoWriter_fourcc(*"MJPG")
            output_video_2 = cv2.VideoWriter(
                "../output/bird_view.avi", fourcc2, 25,
                (bird_view_img.shape[1], bird_view_img.shape[0]), True)
        elif output_video_1 is not None and output_video_2 is not None:
            output_video_1.write(frame)
            output_video_2.write(bird_view_img)

        # Break the loop
        if key == ord("q"):
            break

    return "Done"