Beispiel #1
0
 def init_pedestrian(self):
     size = Sizes['pedestrian']
     i = 0
     while (i < self.num_pedestrians):
         x = np.random.randint(size, self.width - size)
         y = np.random.randint(size, self.height - size)
         r = Rect(x, y, x + size, y + size)
         if r.within_any(self.pedestrians_rectangles):
             p = Pedestrian(r, size=size, env=self)
             p.draw(self.drawing, color=Colors['pedestrian'])
             self.pedestrians.append(p)
             i += 1
Beispiel #2
0
 def init_vehicle(self):
     size = Sizes['vehicle']
     i = 0
     while (i < self.num_vehicles):
         x = np.random.randint(size, self.width - size)
         y = np.random.randint(size, self.height - size)
         r = Rect(x, y, x + size, y + size)
         if not r.intersects_any(self.pedestrians_rectangles):
             v = Vehicle(r, size=size, env=self)
             v.draw(self.drawing, color=Colors['vehicle'])
             self.vehicles.append(v)
             i += 1
Beispiel #3
0
 def __enter__(self):
     from utils import Rect
     from pyglet.gl import GLint, glGetIntegerv, GL_SCISSOR_BOX, glScissor
     ob = (GLint * 4)()
     glGetIntegerv(GL_SCISSOR_BOX, ob)
     ob = list(ob)
     box = [int(i) for i in self.box]
     nb = Rect(*ob).intersect(Rect(*box))
     if nb:
         glScissor(nb.x, nb.y, nb.width, nb.height)
     self.ob, self.nb = ob, nb
     return self
Beispiel #4
0
    def draw_cross_road(self):
        self.background = Image.new('RGB', (self.width, self.height),
                                    color=Colors['background'])
        self.drawing = ImageDraw.Draw(self.background)
        p1 = (self.width * 0.4, self.height * 0.4)
        p2 = (self.width * 0.4, self.height * 0.6)
        p3 = (self.width * 0.6, self.height * 0.4)
        p4 = (self.width * 0.6, self.height * 0.6)

        crossing_area_width = 10
        self.pedestrians_rectangles.clear()
        self.pedestrians_rectangles.append(Rect(0, 0, p1[0], p1[1]))
        self.pedestrians_rectangles.append(Rect(0, p2[1], p2[0], self.height))
        self.pedestrians_rectangles.append(Rect(p3[0], 0, self.width, p3[1]))
        self.pedestrians_rectangles.append(
            Rect(p4[0], p4[1], self.width, self.height))

        self.shared_rectangles.clear()
        self.shared_rectangles.append(
            Rect(p1[0] - crossing_area_width, p1[1], p2[0], p2[1]))
        self.shared_rectangles.append(
            Rect(p2[0], p2[1], p4[0], p4[1] + crossing_area_width))
        self.shared_rectangles.append(
            Rect(p3[0], p3[1], p4[0] + crossing_area_width, p4[1]))
        self.shared_rectangles.append(
            Rect(p1[0], p1[1] - crossing_area_width, p3[0], p3[1]))

        for r in self.pedestrians_rectangles:
            r.draw(self.drawing, color=Colors['pedestrian_area'])
        for r in self.shared_rectangles:
            r.draw(self.drawing, color=Colors['shared_area'])
Beispiel #5
0
 def get_sidebar_coords(self, ui_el):
     if self.top_o is None and self.bottom_o is None \
       and self.left_o is None and self.right_o is None:
         # Let's see if we're being applied on a GridMenu-like thing, fail otherwise
         entry_width = getattr(ui_el.view, "entry_width", None)
         cols = getattr(ui_el, "cols", None)
         if not entry_width and not cols:
             return self.get_coords_for_unrecognized_ui_el(ui_el)
         else:
             # Simplified case - GridMenu with a sidebar on the right
             left_offset = ui_el.view.entry_width*ui_el.cols
             sidebar_width = ui_el.o.width-left_offset
             return Rect(left_offset, 0, ui_el.o.width-1, ui_el.o.height)
     else:
         return Rect(self.left_o, self.top_o, self.right_o, self.bottom_o)
def get_accuracy(net, inputs, net_config, threshold=0.9):
    bbox_list, conf_list = forward(net, inputs, net_config)
    anno = inputs['anno']
    image = inputs['raw']
    count_anno = 0.0
    for r in anno:
        count_anno += 1
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]

    all_rects = [[[] for x in range(net_config["grid_width"])]
                 for y in range(net_config["grid_height"])]
    for n in range(len(bbox_list)):
        for k in range(net_config["grid_height"] * net_config["grid_width"]):
            y = int(k / net_config["grid_width"])
            x = int(k % net_config["grid_width"])
            bbox = bbox_list[n][k]
            conf = conf_list[n][k, 1].flatten()[0]
            abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
            abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
            w = bbox[2, 0, 0]
            h = bbox[3, 0, 0]
            if conf < threshold:
                continue
            all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    acc_rects = stitch_rects(all_rects, net_config)
    count_cover = 0.0
    count_error = 0.0
    count_pred = 0.0
    for rect in acc_rects:
        if rect.true_confidence < threshold:
            continue
        else:
            count_pred += 1
            x1 = rect.cx - rect.width / 2.
            x2 = rect.cx + rect.width / 2.
            y1 = rect.cy - rect.height / 2.
            y2 = rect.cy + rect.height / 2.
            iscover = False
            for r in anno:
                if overlap_union(x1, y1, x2, y2, r.x1, r.y1, r.x2,
                                 r.y2) >= 0.5:  # 0.2 is for bad annotation
                    iscover = True
                    break
            if iscover:
                count_cover += 1
            else:
                count_error += 1

    #         cv2.rectangle(image, (int(x1),int(y1)),(int(x2),int(y2)),
    #                                     (255,0,0), 2)
    #         cv2.rectangle(image, (int(r.x1),int(r.y1)),(int(r.x2),int(r.y2)),
    #                                     (0,255,0), 2)
    # if count_pred!=0 and count_anno!=0:
    #     print 1-count_cover/count_pred, count_cover/count_anno
    # plt.imshow(image)
    # plt.show()

    return (count_cover, count_error, count_anno, count_pred)
Beispiel #7
0
    def draw_bar(self, c, top_y):
        # type: (Canvas, int) -> None
        outline_coords = Rect(
            self.margin, top_y, self.o.width - self.margin,
            min(top_y + self.bar_height, self.o.height - self.margin))

        bar_width = outline_coords.right - outline_coords.left - self.padding * 2
        bar_width *= (self.progress / 100.0)

        bar_coords = Rect(outline_coords.left + self.padding,
                          outline_coords.top + self.padding,
                          outline_coords.left + self.padding + int(bar_width),
                          outline_coords.bottom - self.padding)

        c.rectangle(outline_coords, fill=False, outline=True)
        c.rectangle(bar_coords, fill=True, outline=False)
Beispiel #8
0
 def add_to_mine(self, mine):
     super().add_to_mine(mine)
     hole_size = random.randint(4, 8)
     mine.create_cave(self.x, self.y, hole_size)
     mine.set_visibility(self.x, self.y, True)
     self.search_area = Rect.from_center(self.target.x, self.target.y,
                                         math.ceil(self.mine.width / 6),
                                         math.ceil(self.mine.height / 6))
Beispiel #9
0
    def __init__(self, parent, width=800, height=600):
        super().__init__(parent, 'tooltip', 'window title', True, width,
                         height)

        from button import Button

        self.ui = []
        Button(self, self.ui, 'save', Rect(742, 560, 10, 10),
               lambda: self.hide(), '')
        self.show()
Beispiel #10
0
def find_intersection_rect(rect_one, rect_two):
    """ Given two rectangles, find the common section. """

    new_tl_x = max(rect_one.tl_x, rect_two.tl_x)
    new_tl_y = max(rect_one.tl_y, rect_two.tl_y)
    new_br_x = min(rect_one.br_x, rect_two.br_x)
    new_br_y = min(rect_one.br_y, rect_two.br_y)

    new_rect = Rect(new_tl_x, new_tl_y, new_br_x, new_br_y)

    return new_rect
def get_page_size(page, offset=50):
    rects = [Rect((p['area']['x'], p['area']['y'],
                   p['area']['w'], p['area']['h']))
             for p in page]
    bottom_right = [[r.bottom, r.right] for r in rects]
    bottom_right = np.array(bottom_right)
    max_br = bottom_right.max(axis=0) + offset
    for idx, (p, rect) in enumerate(zip(page, rects)):
        page[idx]['rect'] = rect

    return int(max_br[0]), int(max_br[1]), page
Beispiel #12
0
    def get_svg_bounding_box(self):
        '''
        Return a Rect describing the bounding box of coords in the SVG file. 
        [TODO: is this already a function in the SVG lib?]
        '''
        svg_bounding_box = Rect.far_extents()
        for elem in self.svg_root.iter():
            elem_extents = self._get_elem_extents(elem)
            svg_bounding_box = svg_bounding_box.expand_to(elem_extents)

        print(f'SVG extents: {svg_bounding_box}')
        return svg_bounding_box
Beispiel #13
0
    def get_transform(self):
        '''
        Map the SVG bounding box onto the plot area.
        '''
        # Shrink either x_size or y_size to match aspect ratio of SVG        
        svg_ratio = self.svg_bounding_box.ratio
        plot_ratio = 1.0 * self.plot_size_mm.y / self.plot_size_mm.x

        if svg_ratio > plot_ratio:
            # svg is taller than plot - shrink the x
            self.plot_size_mm.x = self.plot_size_mm.y / svg_ratio
        else:
            # svg is wider than plot - shrink the y
            self.plot_size_mm.y = self.plot_size_mm.x * svg_ratio

        if self.plot_from_origin:
            # output extents are (0, 0 -> x_size, y_size) + offset, if plot_from_origin
            self.plot_extents = Rect(self.offset_mm, self.offset_mm + self.plot_size_mm)
        else:
            # output extents are (bed_centre +/- size / 2) + offset, if plot_from_centre
            self.plot_extents = Rect((self.plot_bed_mm.size - self.plot_size_mm), (self.plot_bed_mm.size + self.plot_size_mm)) / 2.0 + self.offset_mm
        # output extents are clipped to bed_size
        self.plot_extents = self.plot_extents.clip(self.plot_bed_mm)

        print(f'Plot extents: {self.plot_extents}')

        # Transform SVG bounding box to plot extents

        # flip the SVG y values, since the coordinate systems are different
        temp = self.svg_bounding_box.corner0.y
        self.svg_bounding_box.corner0.y = self.svg_bounding_box.corner1.y
        self.svg_bounding_box.corner1.y = temp

        scale = self.plot_extents.size / self.svg_bounding_box.size
        offset = self.plot_extents.corner0 - scale * self.svg_bounding_box.corner0

        print(f'scale: {scale}; offset: {offset}')

        return scale, offset
Beispiel #14
0
def get_accuracy(net, inputs, net_config, threshold=0.9):
    bbox_list, conf_list = forward(net, inputs, net_config, True)
    anno = inputs['anno']
    count_anno = 0.0
    for r in anno:
        count_anno += 1
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]

    all_rects = [[[] for x in range(net_config["grid_width"])]
                 for y in range(net_config["grid_height"])]
    for n in range(len(bbox_list)):
        for k in range(net_config["grid_height"] * net_config["grid_width"]):
            y = int(k / net_config["grid_width"])
            x = int(k % net_config["grid_width"])
            bbox = bbox_list[n][k]
            conf = conf_list[n][k, 1].flatten()[0]
            abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
            abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
            w = bbox[2, 0, 0]
            h = bbox[3, 0, 0]
            if conf < threshold:
                continue
            all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    acc_rects = stitch_rects(all_rects, net_config)
    count_cover = 0.0
    count_error = 0.0
    count_pred = 0.0
    for rect in acc_rects:
        if rect.true_confidence < threshold:
            continue
        else:
            count_pred += 1
            x1 = rect.cx - rect.width / 2.
            x2 = rect.cx + rect.width / 2.
            y1 = rect.cy - rect.height / 2.
            y2 = rect.cy + rect.height / 2.
            iscover = False
            for r in anno:
                if overlap_union(x1, y1, x2, y2, r.x1, r.y1, r.x2,
                                 r.y2) >= 0.5:
                    iscover = True
                    break
            if iscover:
                count_cover += 1
            else:
                count_error += 1

    return (count_cover, count_error, count_anno, count_pred)
Beispiel #15
0
    def get_aoi(self):
        """
        Get the current area of interest.

        Returns
        =======
        rect: Rect object
            Area of interest
        """
        rect_aoi = ueye.IS_RECT()
        ueye.is_AOI(self.h_cam, ueye.IS_AOI_IMAGE_GET_AOI, rect_aoi,
                    ueye.sizeof(rect_aoi))
        return Rect(rect_aoi.s32X.value, rect_aoi.s32Y.value,
                    rect_aoi.s32Width.value, rect_aoi.s32Height.value)
    def __init__(self):
        super().__init__(None, 'ergodox-gui-configurator')

        self.viewing_layer = 0

        self.keys = []

        scale = 4
        offset = 50

        self.read_data('dvorak.json')
        self.read_data()

        for key in self.data['matrix']:
            Button(
                self,
                self.keys,
                key['layers'][0]['label'],
                Rect(
                    offset + key['x'] * scale,
                    offset + key['y'] * scale,
                    key['w'] * scale,
                    key['h'] * scale
                    ),
                lambda: KeycapConfigWindow(self).show()
                )

        self.active_layer = 0
        self.ui = []
        Checkbox(self, self.ui, 'LED 1', Rect(500, 500, 70, 50), self.data['layers'][self.active_layer]['leds']['led1'] * 2, self.check_led_button1_state)
        Checkbox(self, self.ui, 'LED 1', Rect(550, 500, 70, 50), self.data['layers'][self.active_layer]['leds']['led2'] * 2, self.check_led_button2_state)
        Checkbox(self, self.ui, 'LED 1', Rect(600, 500, 70, 50), self.data['layers'][self.active_layer]['leds']['led3'] * 2, self.check_led_button3_state)
        #lb = LayerButton(self, 0, 0, self.layers)
        #Button(self, 'write', Rect(742, 560, button_size[0], button_size[1]),
        #       lambda: self.write(), self.ui, '')

        self.show()
def get_some_nonfaces(imgs, labels, num_examples):
    '''Get some random nonface images of size BOX_SIZE'''
    nonfaces = []
    pbar = tqdm(total=num_examples, desc='Extracting some nonfaces')
    while True:
        for img_id, img in imgs.items():
            # random scale
            scale = choice(SCALES)
            scaled_img = rescale(img, scale, multichannel=False)
            
            # random position
            try:
                y = randrange(scaled_img.shape[0] - BOX_SIZE.h)
                x = randrange(scaled_img.shape[1] - BOX_SIZE.w)
            except ValueError:  # images to small to extract a rect
                continue

            random_rect = Rect(x, y, BOX_SIZE.w, BOX_SIZE.h)
            
            if not random_rect.overlap(*labels[img_id], threshold=0.4):
                nonfaces.append(random_rect.extract_from_img(scaled_img))
                pbar.update()
                if len(nonfaces) >= num_examples:
                    return nonfaces
Beispiel #18
0
    def _get_elem_extents(self, elem):
        elem_extents = Rect.far_extents()
        tag_suffix = elem.tag.split('}')[-1]
        if tag_suffix not in SVG_TAGS:
            return elem_extents

        shape_class = getattr(shapes, tag_suffix)
        shape_obj = shape_class(elem)
        path = shape_obj.d_path()
        mtx = shape_obj.transformation_matrix()
        if path:
            points = shapes.point_generator(path, mtx, self.settings.smoothness)
            for point in points:
                elem_extents = elem_extents.expand_to(rotate(Vector2(point), self.rotate_rads))

        return elem_extents
Beispiel #19
0
    def checkControlLine(self, img):
        print('[INFO] checkControlLine')
        hls = cv.cvtColor(img, cv.COLOR_RGBA2RGB)
        hls = cv.cvtColor(hls, cv.COLOR_RGB2HLS)
        # show_image(hls)

        kernelErode = np.ones((5, 5), np.uint8)
        kernelDilate = np.ones((20, 20), np.uint8)
        # Threshold the HSV image to get only blue colors
        threshold = cv.inRange(hls, CONTROL_LINE_COLOR_LOWER,
                               CONTROL_LINE_COLOR_UPPER)
        # show_image(threshold)
        threshold = cv.erode(threshold, kernelErode, iterations=1)
        threshold = cv.dilate(threshold, kernelDilate, iterations=1)
        threshold = cv.GaussianBlur(threshold, (5, 5), 2, 2)
        # show_image(threshold)
        im2, contours, hierarchy = cv.findContours(threshold, cv.RETR_EXTERNAL,
                                                   cv.CHAIN_APPROX_SIMPLE)
        # cv.drawContours(img, contours, -1, (0,255,0), 3)
        # show_image(img)
        # show_image(im2)
        controlLineRect = None
        for contour in contours:
            x, y, w, h = cv.boundingRect(contour)
            # rect = cv.minAreaRect(contour)
            # ((x, y) , (w, h), angle) = rect
            print('[INFO] boundingRect', x, y, w, h)
            # TODO: maybe ask CJ should we change the constants control line?s
            # ('[INFO] boundingRect', 1344, 0, 70, 112)
            if (CONTROL_LINE_POSITION_MIN < x and x < CONTROL_LINE_POSITION_MAX
                    and CONTROL_LINE_MIN_HEIGHT < h
                    and CONTROL_LINE_MIN_WIDTH < w
                    and w < CONTROL_LINE_MAX_WIDTH):
                print('[INFO] controlLine found!')
                # cv.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
                # show_image(img)
                controlLineRect = Rect(x, y, w, h)
                # DEPRECATED ROTATED RECTANGLE WAY
                # box = cv.boxPoints(rect)
                # box = np.int0(box)
                # cv.drawContours(img,[box],0,(0,0,255),2)
                # show_image(img)

                # controlLineRect = rect

        return controlLineRect
Beispiel #20
0
    def bounding_rect(self):
        N = len(self.positions)

        xs = np.empty(N)
        ys = np.empty(N)

        for i, position in enumerate(self.positions):
            xs[i] = position.center().x
            ys[i] = position.center().y

        rect = Rect(
            min_x=np.min(xs),
            min_y=np.min(ys),
            max_x=np.max(xs),
            max_y=np.max(ys),
        )

        return rect
Beispiel #21
0
    def _find_shapes_for_nodes_by_page(self, page_no):
        translated_page_no = self._page_no_translation(page_no)
        shapes = []
        if translated_page_no in self.data.pages.keys(
        ):  #get_inherited dictionary
            shapes += self.data.db_manipulator.fetch_shapes(
                self.data.pages[translated_page_no])
        for dictionary in self.data.shape_dictionaries:
            if dictionary.page == translated_page_no:
                shapes += self.data.db_manipulator.fetch_shapes(
                    dictionary.db_id)
        self.data.add_shapes(shapes)
        #page_dictionaries = self.data.db_manipulator.fetch_dictionaries_for_page(self.data.current_document.db_id, self._page_no_translation(page_no))
        self.data.add_blits(
            self.data.db_manipulator.fetch_blits(
                self.data.current_document.db_id, translated_page_no),
            translated_page_no)

        blit_rects = []
        for blit in self.data.blits[translated_page_no]:
            rect = Rect(blit.x, blit.y, blit.w, blit.h)
            rect.shape = blit.shape
            blit_rects.append(rect)
        #for node in self.hocr_pages[translated_page_no].chars:
        for node in self.text_model[translated_page_no].chars:
            node_rect = Rect(*node.rect)
            for blit_rect in blit_rects:
                if node_rect.intersect(blit_rect):
                    node.shapes.append(blit_rect.shape)
                    node.blits.append(blit_rect)
        for node in self.text_model[translated_page_no].lines:
            node_rect = Rect(*node.rect)
            for blit_rect in blit_rects:
                if node_rect.intersect(blit_rect):
                    #node.shapes.append(blit_rect.shape)
                    node.blits.append(blit_rect)
Beispiel #22
0
 def select_shape(self, node):
     if not node.shape_selected:
         node_rect = Rect(*node.rect)
         shapes_by_coverage_and_size = {}
         for i in range(len(node.shapes)):
             blit = node.blits[i]
             shapes_by_coverage_and_size[(blit.coverage_percent(node_rect),
                                          blit.size)] = (node.shapes[i],
                                                         blit)
         shapes = []
         blits = []
         for shape_key in sorted(shapes_by_coverage_and_size.keys(),
                                 key=itemgetter(0, 1),
                                 reverse=True):
             shapes.append(shapes_by_coverage_and_size[shape_key][0])
             blits.append(shapes_by_coverage_and_size[shape_key][1])
         node.shapes = shapes
         node.blits = blits
         node.shape_selected = True
     return node.shapes[0]
Beispiel #23
0
    def __init__(
        self,
        settings,
        svg_path,
        gcode_path,
        plot_from_origin,
        x_offset_mm,
        y_offset_mm,
        x_size_mm,
        y_size_mm,
        rotate,
    ):

        # Check File Validity
        if not os.path.isfile(svg_path):
            raise ValueError('File \''+svg_path+'\' not found.')

        if not svg_path.endswith('.svg'):
            raise ValueError('File \''+svg_path+'\' is not an SVG file.')

        self.settings = settings
        self.svg_path = svg_path
        self.gcode_path = gcode_path
        self.gcode_file = GCodeFile(self.gcode_path, self.settings)

        # Get the svg Input File
        input_file = open(self.svg_path, 'r')
        self.svg_root = ET.parse(input_file).getroot()
        input_file.close()

        self.rotate_rads = rotate * pi / 180

        self.svg_bounding_box = self.get_svg_bounding_box()

        bed_area_mm = Vector2(self.settings.bed_area_mm)
        self.plot_bed_mm = Rect(Vector2(), bed_area_mm)
        self.plot_size_mm = Vector2(x_size_mm or bed_area_mm.x, y_size_mm or bed_area_mm.y)
        self.offset_mm = Vector2(x_offset_mm, y_offset_mm)
        self.plot_from_origin = plot_from_origin

        self.scale, self.offset = self.get_transform()
def forward_test(net, inputs, net_config, enable_ip_split):
    net.phase = 'test'
    bbox_list, conf_list = forward(net,
                                   inputs,
                                   net_config,
                                   deploy=True,
                                   enable_ip_split=enable_ip_split)

    img = np.copy(inputs["raw"])
    all_rects = [[[] for x in range(net_config["grid_width"])]
                 for y in range(net_config["grid_height"])]
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]
    for n in range(len(bbox_list)):
        for k in range(net_config["grid_height"] * net_config["grid_width"]):
            y = int(k / net_config["grid_width"])
            x = int(k % net_config["grid_width"])
            bbox = bbox_list[n][k]
            conf = conf_list[n][k, 1].flatten()[0]
            abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
            abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
            w = bbox[2, 0, 0]
            h = bbox[3, 0, 0]
            if conf < 0.9:
                continue
            all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    acc_rects = stitch_rects(all_rects, net_config)

    for rect in acc_rects:
        if rect.true_confidence < 0.9:
            continue
        cv2.rectangle(
            img,
            (rect.cx - int(rect.width / 2), rect.cy - int(rect.height / 2)),
            (rect.cx + int(rect.width / 2), rect.cy + int(rect.height / 2)),
            (255, 0, 0), 2)
    return img
Beispiel #25
0
 def rect(self):
     '''Returns a Rect suitable for collision detection'''
     return Rect(self.x - self.hw, self.y - self.hh, self.img.width,
                 self.img.height)
Beispiel #26
0
 def rect(self):
     '''Returns a Rect suitable for collision'''
     x, y = self.world.world_to_screen(self.x, self.y)
     x += self.offset[0]
     y += self.offset[1] - self.world.half_stack
     return Rect(x, y, self.world.tile_size[0], -self.world.tile_size[1])
def convert_deploy_2_train(boot_deploy_list,
                           data_mean,
                           net_config,
                           max_heads=999999,
                           threshold=0.9,
                           jitter=True,
                           if_random=True):
    annos = []
    cnt = 0
    pix_per_w = net_config["img_width"] / net_config["grid_width"]
    pix_per_h = net_config["img_height"] / net_config["grid_height"]
    for dic in boot_deploy_list:
        anno = Annotation()
        anno.imageName = dic["imname"]
        bbox_list = dic["bbox"]
        conf_list = dic["conf"]

        all_rects = [[[] for x in range(net_config["grid_width"])]
                     for y in range(net_config["grid_height"])]
        for n in range(len(bbox_list)):
            for k in range(net_config["grid_height"] *
                           net_config["grid_width"]):
                y = int(k / net_config["grid_width"])
                x = int(k % net_config["grid_width"])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k, 1].flatten()[0]
                abs_cx = pix_per_w / 2 + pix_per_w * x + int(bbox[0, 0, 0])
                abs_cy = pix_per_h / 2 + pix_per_h * y + int(bbox[1, 0, 0])
                w = bbox[2, 0, 0]
                h = bbox[3, 0, 0]
                if conf < threshold:
                    continue
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

        acc_rects = stitch_rects(all_rects, net_config)
        for rect in acc_rects:
            if rect.true_confidence >= threshold:
                r = AnnoRect()
                r.x1 = int(rect.cx - rect.width / 2.)
                r.x2 = int(rect.cx + rect.width / 2.)
                r.y1 = int(rect.cy - rect.height / 2.)
                r.y2 = int(rect.cy + rect.height / 2.)
                anno.rects.append(r)
        annos.append(anno)
        cnt += len(anno.rects)
        if cnt >= max_heads:
            break
    print 'deployed', len(annos), 'images with', cnt, 'heads'

    while True:
        if if_random:
            random.shuffle(annos)
        for anno in annos:
            if jitter:
                jit_image, jit_anno = annotation_jitter(
                    anno,
                    target_width=net_config["img_width"],
                    target_height=net_config["img_height"])
            else:
                jit_image = imread(anno.imageName)
                jit_anno = anno
            image = image_to_h5(jit_image, data_mean, image_scaling=1.0)
            boxes, box_flags = annotation_to_h5(jit_anno,
                                                net_config["grid_width"],
                                                net_config["grid_height"],
                                                net_config["region_size"],
                                                net_config["max_len"])
            yield {
                "num": len(annos),
                "imname": anno.imageName,
                "raw": jit_image,
                "image": image,
                "boxes": boxes,
                "box_flags": box_flags,
                'anno': jit_anno
            }
Beispiel #28
0
class App:
    # restart BUTTON
    IMG_ID = 0
    BTN_W, BTN_H = 32, 32
    BTN_RESTART = Rect(0, 96, BTN_W, BTN_H, COLKEY)

    def __init__(self):
        global WINDOW_HEIGHT, WINDOW_WIDTH, CAPTION, FPS, COLKEY
        pyxel.init(WINDOW_WIDTH, WINDOW_HEIGHT, caption=CAPTION, fps=FPS)

        # === Generate Instances ===
        self.player = Player()
        self.enemy = Enemy()
        self.backgnd = BackGround()
        # TODO: make game title
        self.player.beGameover()  # not good way ...

        pyxel.load("asset/asset.pyxel")
        pyxel.run(self.update, self.draw)

    def update(self):
        self.backgnd.update()  # execution order: back layer to front layer
        self.enemy.update()
        self.player.update()

        # score update
        if Player.getState() != "IDLE":
            Score.update()

        # === Restart ===
        if Player.getState() == "IDLE":
            if pyxel.btnp(pyxel.KEY_SPACE):
                Score.saveHighScore()
                # initialize state
                Score.initialize()
                self.backgnd.initialize()
                self.enemy.initialize()
                self.player.initialize()
                music.start_music()

    def draw(self):
        global WINDOW_HEIGHT, WINDOW_WIDTH, DEBUG
        pyxel.cls(ColPal.gray_dark)
        self.backgnd.blt()
        self.enemy.blt()
        self.player.blt()

        # show score
        pyxel.text(WINDOW_WIDTH - 50, 5,
                   "HI {}  {}".format(Score.getHighScore(),
                                      Score.getScore()), ColPal.white)
        # if gameover, show restart button
        if Player.getState() == "IDLE":
            pyxel.text(WINDOW_WIDTH // 2 - 5, WINDOW_HEIGHT // 2 - 5,
                       "GAME OVER\n[SPACE] TO CONTINUE", ColPal.white)
            pyxel.blt(WINDOW_WIDTH // 2 - 48, WINDOW_HEIGHT // 2 - 16,
                      App.IMG_ID, *App.BTN_RESTART.getRect())

        if DEBUG:
            pyxel.text(0, 20, "Frame: {}".format(pyxel.frame_count),
                       ColPal.orange)
Beispiel #29
0
 def get_char_rect(self):
     node = self.text_model[self.text_model.current_page].current_char_node
     rect = Rect(node.x, node.y, node.w, node.h)
     return rect
Beispiel #30
0
def test(config):
    """Test the model and output to test/output."""

    net = apollocaffe.ApolloNet()

    net_config = config["net"]
    data_config = config["data"]
    solver = config["solver"]

    image_mean = load_data_mean(
        data_config["idl_mean"], net_config["img_width"],
        net_config["img_height"], image_scaling=1.0)

    input_gen_test = load_idl(data_config["test_idl"],
                                   image_mean, net_config, jitter=False)

    forward(net, input_gen_test.next(), config["net"])

    try:
        net.load(solver["weights"])
    except:
        pass

    net.phase = 'test'
    test_loss = []
    for i in range(solver["test_iter"]):
        input_test = input_gen_test.next()
        image = input_test["raw"]
        tic = time.time()
        bbox, conf = forward(net, input_test, config["net"], True)
        print "forward deploy time", time.time() - tic
        bbox_list = bbox
        conf_list = conf
        pix_per_w = 32
        pix_per_h = 32

        all_rects = [[[] for x in range(net_config['grid_width'])] for y in range(net_config['grid_height'])]
        for n in range(len(bbox_list)):
            for k in range(net_config['grid_width'] * net_config['grid_height']):
                y = int(k / net_config['grid_width'])
                x = int(k % net_config['grid_width'])
                bbox = bbox_list[n][k]
                conf = conf_list[n][k,1].flatten()[0]
                abs_cx = pix_per_w/2 + pix_per_w*x + int(bbox[0,0,0])
                abs_cy = pix_per_h/2 + pix_per_h*y+int(bbox[1,0,0])
                w = bbox[2,0,0]
                h = bbox[3,0,0]
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))
        acc_rects = stitch_rects(all_rects)
        #print acc_rects

        for idx, rect in enumerate(acc_rects):
            if rect.true_confidence < 0.9:
                print 'rejected', rect.true_confidence
                continue
            else:
                print 'found', rect.true_confidence
                cv2.rectangle(image, (rect.cx-int(rect.width/2), rect.cy-int(rect.height/2)),
                                   (rect.cx+int(rect.width/2), rect.cy+int(rect.height/2)),
                                   (255,0,0),
                                   2)
        cv2.imwrite("test_output/img_out%s.jpg" % i, image)