def publishCharacterImage(self):
        from PIL import Image, ImageDraw, ImageFilter

        # Publish Image with label
        img = self.img
        for x, y, label in zip(self.person_x, self.person_y, self.labels):
            # Draw character image --->
            os.chdir(self.PICTURE_URL)
            char = ch.CHARACTER_PICTURE[int(label)]

            # Load and resize
            src = pil_image.cv2pil(img)
            forward = Image.open( char["filename"])
            h, w = forward.size
            forward = forward.resize((int(h*char["size"]), int(w*char["size"])))

            # For convert
            src = src.convert('RGBA')

            # Paste
            c = Image.new('RGBA', src.size, (255, 255, 255, 0))
            #src.paste(forward, ( char["x"], char["y"]))
            #c.paste(forward, ( char["x"] + x, char["y"] + y), forward)
            c.paste(forward, ( char["x"] + x - int(char["size"]*h/2), char["y"] + y - int(char["size"]*w/2)), forward)

            result = Image.alpha_composite(src, c)
            result.save('tmp.jpg', quality=95)
            img = cv.imread('tmp.jpg')

            # Write label name to img
            cv.putText(img, ch.CHARACTER_NAME[int(label)], (x-50,y), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0,0,150), 2, cv.LINE_AA)
        msg = self.bridge.cv2_to_imgmsg(img, encoding="rgb8")
        self.image_pub.publish(msg)
Beispiel #2
0
 def resize_keep_ratio(self, img, width, height, fill_color=(0, 0, 0, 0)):
     #======= Make sure image is smaller than background =======
     h, w, channel = img.shape
     h_ratio = float(float(height) / float(h))
     w_ratio = float(float(width) / float(w))
     #if h_ratio <= 1 or w_ratio <= 1:
     ratio = h_ratio
     if h_ratio > w_ratio:
         ratio = w_ratio
     img = cv2.resize(img, (int(ratio * w), int(ratio * h)))
     #======== Paste image to background =======
     im = Image.fromarray(np.uint8(img))
     x, y = im.size
     new_im = Image.new('RGBA', (width, height), fill_color)
     new_im.paste(im, ((width - x) / 2, (height - y) / 2))
     return np.asarray(new_im)
def print_road_map(image, left_line, right_line):
    """ print simple road map """
    img = cv2.imread('images/top_view_car.png', -1)
    img = cv2.resize(img, (120, 246))

    rows, cols = image.shape[:2]
    window_img = np.zeros_like(image)

    window_margin = left_line.window_margin
    left_plotx, right_plotx = left_line.allx, right_line.allx
    ploty = left_line.ally
    lane_width = right_line.startx - left_line.startx
    lane_center = (right_line.startx + left_line.startx) / 2
    lane_offset = cols / 2 - (2 * left_line.startx + lane_width) / 2
    car_offset = int(lane_center - 360)
    # Generate a polygon to illustrate the search window area
    # And recast the x and y points into usable format for cv2.fillPoly()
    left_pts_l = np.array([
        np.transpose(
            np.vstack([
                right_plotx - lane_width + lane_offset - window_margin / 4,
                ploty
            ]))
    ])
    left_pts_r = np.array([
        np.flipud(
            np.transpose(
                np.vstack([
                    right_plotx - lane_width + lane_offset + window_margin / 4,
                    ploty
                ])))
    ])
    left_pts = np.hstack((left_pts_l, left_pts_r))
    right_pts_l = np.array([
        np.transpose(
            np.vstack([right_plotx + lane_offset - window_margin / 4, ploty]))
    ])
    right_pts_r = np.array([
        np.flipud(
            np.transpose(
                np.vstack(
                    [right_plotx + lane_offset + window_margin / 4, ploty])))
    ])
    right_pts = np.hstack((right_pts_l, right_pts_r))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(window_img, np.int_([left_pts]), (140, 0, 170))
    cv2.fillPoly(window_img, np.int_([right_pts]), (140, 0, 170))

    # Recast the x and y points into usable format for cv2.fillPoly()
    pts_left = np.array([
        np.transpose(
            np.vstack([
                right_plotx - lane_width + lane_offset + window_margin / 4,
                ploty
            ]))
    ])
    pts_right = np.array([
        np.flipud(
            np.transpose(
                np.vstack(
                    [right_plotx + lane_offset - window_margin / 4, ploty])))
    ])
    pts = np.hstack((pts_left, pts_right))

    # Draw the lane onto the warped blank image
    cv2.fillPoly(window_img, np.int_([pts]), (0, 160, 0))

    #window_img[10:133,300:360] = img
    road_map = Image.new('RGBA', image.shape[:2], (0, 0, 0, 0))
    window_img = Image.fromarray(window_img)
    img = Image.fromarray(img)
    road_map.paste(window_img, (0, 0))
    road_map.paste(img, (300 - car_offset, 590), mask=img)
    road_map = np.array(road_map)
    road_map = cv2.resize(road_map, (95, 95))
    road_map = cv2.cvtColor(road_map, cv2.COLOR_BGRA2BGR)
    return road_map