コード例 #1
0
 def test_object_boxes(self):
     camera_raw = Camera('../data/videos/2019-06-01.avi', save=False, num_skip=0, draw=False)
     camera_labeled = Camera(None, save=False, window_name='labeled')
     weight_path = '../model/custom_tiny_yolov3.weights'
     network_config_path = '../cfg/custom-tiny.cfg'
     object_config_path = '../cfg/custom.data'
     detector = Detector(weight_path, network_config_path, object_config_path, auto_id=True)
     while True:
         image = camera_raw.get_image()
         if image is None:
             break
         image = camera_raw.rgb_to_bgr(image)
         object_list = detector.detect_objects(image)
         boxes = camera_raw.draw_boxes(image, object_list)
         camera_labeled.display(boxes)
     del camera_raw
     del camera_labeled
コード例 #2
0
    'policeman1': (0, 255, 0),
    'policeman2': (0, 0, 255)
}
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 1
lineType = 2
window_name = 'test'
cv2.namedWindow(window_name)
while True:
    ret, frame = cap.read()
    if frame is None:
        break
    else:
        image = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
    frame = cv2.resize(frame, (1024, 768))
    object_list = detector.detect_objects(image)
    print(object_list)
    if len(object_list) > 0:
        for key, value in object_list.items():
            height, width = frame.shape[0], frame.shape[1]
            x = value['center'][0] * width
            y = value['center'][1] * height
            size_width = value['size'][0] * width
            size_height = value['size'][1] * height
            x1 = int(x - size_width / 2)
            y1 = int(y - size_height / 2)
            x2 = int(x + size_width / 2)
            y2 = int(y + size_height / 2)
            color = colors[key]
            cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
            cv2.putText(frame, key,
コード例 #3
0
ファイル: main.py プロジェクト: ramk94/Thief_Policemen
class Game:
    """
    Each game is an instance of class Game.
    """
    def __init__(self, weight_path, network_config_path, object_config_path,
                 robots_config_path):
        """
        Load necessary modules and files.

        Parameters
        ----------
        weight_path: str
            file path of YOLOv3 network weights
        network_config_path: str
            file path of YOLOv3 network configurations
        object_config_path: str
            file path of object information in YOLOv3 network
        robots_config_path: str
            file path of robots' remote server configuration
        """

        # fix robot movement order
        self.orders = ['thief', 'policeman1']
        # self.orders = ['policeman1', 'policeman2']
        # self.orders = ['thief', 'policeman1', 'policeman2']

        # initialize internal states
        self.graph = None
        self.objects_on_graph = None
        self.instructions = None

        # set up escape nodes
        self.escape_nodes = set()

        # construct the camera system
        self.camera = Camera(1)

        # construct the object detector
        self.detector = Detector(weight_path, network_config_path,
                                 object_config_path)

        # load gaming board image and get centers' coordinates of triangles
        self.gaming_board_image = self.camera.get_image()
        self.centers = self.detector.detect_gaming_board(
            self.gaming_board_image)

        # construct the graph builder
        self.graph_builder = GraphBuilder(self.centers)

        # construct the strategy module
        self.strategy = Strategy(self.orders)

        # construct the control system
        self.controller = Controller(self.detector, self.camera.get_image,
                                     robots_config_path)

        # connect to each robot
        self.controller.connect()

    def is_over(self):
        """
        Check if the game is over.

        Returns
        -------
        game_over: bool
            True if the thief is at the escape point or the policemen have caught the thief, otherwise False.
        """
        game_over = False
        if self.instructions is None or self.objects_on_graph is None or self.graph is None:
            return game_over
        if 'thief' in self.objects_on_graph:
            if self.objects_on_graph['thief'] in self.escape_nodes:
                game_over = True
                logger.info('The thief wins!')
            else:
                for name, instruction in self.instructions.items():
                    if name != 'thief':
                        if self.instructions['thief'][1] == instruction[1]:
                            game_over = True
                            logger.info('The policemen win!')
        return game_over

    def shuffle(self):
        random.randint(5, 10)

    def forward(self):
        """
        Push the game to the next step.
        """
        # get objects' coordinates and categories
        image = self.camera.get_image()
        object_list = self.detector.detect_objects(image)

        # build a graph based on object list
        graph, objects_on_graph = self.graph_builder.build(object_list)
        self.graph = graph
        self.objects_on_graph = objects_on_graph

        # generate instructions based on the graph
        instructions = self.strategy.get_next_steps_shortest_path(
            graph, objects_on_graph)
        self.instructions = instructions
        logger.info('instructions:{}'.format(instructions))

        if self.is_over():
            return
        # move robots until they reach the right positions
        while not self.controller.is_finished(self.centers, object_list,
                                              instructions):
            # obtain feedback from camera
            image = self.camera.get_image()
            object_list = self.detector.detect_objects(image)

            # calculate control signals
            control_signals = self.controller.calculate_control_signals(
                self.centers, object_list, instructions)

            # cut extra signals
            real_signals = []
            for name in self.orders:
                for signal in control_signals:
                    if signal['name'] == name:
                        # if True:
                        real_signals.append(signal)
                if len(real_signals) > 0:
                    break

            # update internal states
            self.controller.update_state(object_list)

            # move robots
            self.controller.move_robots(real_signals)

            # obtain feedback from camera
            image = self.camera.get_image()
            object_list = self.detector.detect_objects(image)

            # update internal states
            self.controller.update_state(object_list)

    def get_report(self):
        """
        Generate a game report(json, xml or plain text).

        Returns
        -------
        game_report: object or str
            a detailed record of the game
        """
        game_report = None
        return game_report