def collisions_in_scene(file_path, temp_path="./temp.txt"):
    # to be called in notebook with dataframe describe for further analysis
    helpers.extract_frames(file_path, temp_path, save=True)
    nb_collisions_total = []
    nb_objects_total = []
    ious_total = []
    ious_conflict_total = []
    conflicts_total = {}
    with open(temp_path) as frames:
        for frame in frames:
            frame = json.loads(frame)
            # print(frame["0"]["bbox"])
            conflicts, nb_collisions, frame_id, nb_objects, ious, ious_conflict = collisions_in_frame(
                frame, threshold=0.5)
            nb_collisions_total.append(nb_collisions)
            nb_objects_total.append(nb_objects)

            for iou in ious:
                ious_total.append(iou)
            for iou in ious_conflict:
                ious_conflict_total.append(iou)
            conflicts_total[frame_id] = conflicts
    os.remove(temp_path)

    return conflicts_total, nb_collisions_total, nb_objects_total, ious_total, ious_conflict_total
    def extract_data(self, scene):

        helpers.extract_frames(self.original_file.format(scene),
                               self.frames_temp,
                               save=True)
        helpers.extract_trajectories(self.original_file.format(scene),
                                     self.trajectories_temp,
                                     save=True)

        sample_id = 0

        helpers.remove_file(self.samples_dest.format(scene))
        helpers.remove_file(self.labels_dest.format(scene))

        with open(self.samples_dest.format(scene), "a") as data_csv:
            data_writer = csv.writer(data_csv)
            with open(self.labels_dest.format(scene), "a") as label_csv:
                label_writer = csv.writer(label_csv)
                # with open(parameters["scene_path"],"a") as scene_csv:
                #     scene_writer = csv.writer(scene_csv)
                with open(self.trajectories_temp) as trajectories:
                    with open(self.frames_temp) as file_frames:
                        for k, trajectory in enumerate(trajectories):

                            trajectory = json.loads(trajectory)

                            scene_name = trajectory["scene"]
                            file_frames, a = tee(file_frames)

                            frames = trajectory["frames"]
                            current_id = int(trajectory["id"])
                            # if current_id == 63:
                            #     print("63")
                            continuous = self.__are_frames_continuous(frames)

                            if continuous:
                                start, stop = frames[0], frames[-1] + 1

                                ids = self.__get_neighbors(
                                    islice(a, start, stop))
                                len_traj = len(ids[current_id])

                                for i in range(0, len_traj, self.shift):

                                    features, labels = self.__features_labels(
                                        len_traj, current_id, ids, i)
                                    if features != []:
                                        sample_id = self.__persist_data(
                                            ids, current_id, i, features,
                                            labels, data_writer, label_writer,
                                            sample_id, scene_name)
                                # if sample_id == 39:
                                #     print("")
                            else:
                                print("trajectory {} discarded".format(
                                    current_id))

        os.remove(self.frames_temp)
        os.remove(self.trajectories_temp)
Exemplo n.º 3
0
def main():
    args = sys.argv
    data = json.load(open(args[1]))
    prepare = json.load(open(args[2]))

    scene_list = prepare["selected_scenes"]
    scene_path = data["preprocessed_datasets"] + "{}.csv"
    frames_temp = data["temp"] + "frames.txt"
    nb_agents_scenes = []
    for scene in scene_list:
        helpers.extract_frames(scene_path.format(scene),
                               frames_temp,
                               save=True)
        nb_agents_scene = []

        with open(frames_temp) as frames:
            for i, frame in enumerate(frames):
                # print(frame["ids"])
                frame = json.loads(frame)
                nb_agents = len(frame["ids"].keys())
                nb_agents_scene.append(nb_agents)
                nb_agents_scenes.append(nb_agents)

                # if nb_agents == 205:
                #     print(scene)
                #     print(i)

        print("{}: mean {}, std {}".format(scene, np.mean(nb_agents_scene),
                                           np.std(nb_agents_scene)))
        print(" max {}, min {}, nb_frames {}, nb_agents".format(
            np.max(nb_agents_scene), np.min(nb_agents_scene), i + 1,
            np.sum(nb_agents_scene)))

        helpers.remove_file(frames_temp)
    print(" mean {}, std {}".format(np.mean(nb_agents_scenes),
                                    np.std(nb_agents_scenes)))
    print(" max {}, min {}".format(np.max(nb_agents_scenes),
                                   np.min(nb_agents_scenes)))
    def change_rate(self, scene_name):

        former_rate = float(self.framerates_json[scene_name])

        rate_ratio = int(former_rate / self.new_rate)

        # self.destination_file = self.destination_file.format(scene_name)

        helpers.remove_file(self.destination_file.format(scene_name))

        self.counter = 0

        helpers.extract_frames(self.original_file.format(scene_name),
                               self.temp,
                               save=True)
        with open(self.temp) as frames:
            for frame in frames:
                frame = json.loads(frame)
                i = frame["frame"]
                if i % rate_ratio == 0:
                    self.__write_frame(frame["ids"], scene_name)
                    self.counter += 1
        helpers.remove_file(self.temp)
    def extract_data(self, scene):
        helpers.extract_frames(self.original_file.format(scene),
                               self.frames_temp,
                               save=True)

        helpers.remove_file(self.samples_dest.format(scene))
        helpers.remove_file(self.labels_dest.format(scene))

        with open(self.samples_dest.format(scene), "a") as data_csv:
            data_writer = csv.writer(data_csv)
            with open(self.labels_dest.format(scene), "a") as label_csv:
                label_writer = csv.writer(label_csv)

                with open(self.frames_temp) as frames:
                    observations = {}
                    sample_id = 0
                    for frame in frames:
                        delete_ids = []
                        observations[sample_id] = []
                        sample_id += 1

                        for id_ in observations:
                            if len(observations[id_]
                                   ) < self.t_obs + self.t_pred:
                                observations[id_].append(frame)
                            else:
                                features, labels, ids = self.__features_labels(
                                    observations[id_])
                                self.__persist_data(ids, features, labels,
                                                    data_writer, label_writer,
                                                    id_)

                                delete_ids.append(id_)
                        for id_ in delete_ids:
                            del observations[id_]
        helpers.remove_file(self.frames_temp)
def main():

    # file_path = CSV + "new_rates/gates8_30.0to2.5.csv"
    # file_path = CSV + "bad.csv"
    # file_path = CSV + "lankershim_inter2_10.0to1.0.csv"
    # file_path = CSV + "deathCircle1.csv"
    # file_path = CSV + "01_tracks.csv"

    args = sys.argv
    data = json.load(open(args[1]))
    original = int(args[3])

    file_path = ""
    if original:
        file_path = data["extracted_datasets"] + args[2] + ".csv"
    else:
        file_path = data["preprocessed_datasets"] + args[2] + ".csv"
    

    

    framerate = 1
    # factor_div = 3/2
    plot_last_step = True
    nb_last_steps = 200
    new_color_index = 0
    color_dict = {}
    temp_path = "./data/temp/temp.txt"
    target_size = 1000
    margin = 10

    offset = [0,0]

    helpers.extract_frames(file_path,temp_path,save = True)

    try:
        min_,max_ = vis.find_bounding_coordinates(temp_path)
        w,h = vis.get_scene_image_size(min_,max_)

        print(min_,max_)
        print(w,h)
        factor_div  = np.max([w,h]) / target_size

        w,h = int(w/factor_div) + margin,int(h/factor_div)+margin

        offset = np.divide(min_,-factor_div)

        print(factor_div)
        print(offset)

        print(w,h)
        


        # Create a black image
        img = np.zeros((h,w,3), np.uint8)

        last_frames = deque([])

        with open(temp_path) as frames:
            for frame in frames:
                frame = json.loads(frame)
                # print(frame)
                if len(last_frames) == nb_last_steps:
                    last_frames.popleft()
                last_frames.append(frame)

                


                img1 = img.copy()


                img1,color_dict,new_color_index = vis.plot_current_frame(frame,img1,color_dict,new_color_index,factor_div,offset, display_box= True)

                if plot_last_step:
                    img1 = vis.plot_last_steps(img1,frame,last_frames,color_dict,factor_div=factor_div,offset = offset)
     

                img1 = vis.plot_frame_number(w,h, img1,frame)
                
                # if file_path == CSV + "bad.csv":
                img1 = vis.plot_scene_name(w,h, img1,frame)




                cv2.imshow('image1',img1)
                cv2.waitKey(int(1000/framerate))

        cv2.destroyAllWindows()
        os.remove(temp_path)

    except KeyboardInterrupt:
        cv2.destroyAllWindows()
        os.remove(temp_path)