Beispiel #1
0
def visualize_segmentation(input_img,
                           seg_arr,
                           n_classes,
                           class_number=2,
                           display=False,
                           output_file=None):
    seg_img = predict.segmented_image_from_prediction(
        seg_arr, n_classes=n_classes, input_shape=input_img.shape)
    overlay_img = cv2.addWeighted(input_img, 0.7, seg_img, 0.3, 0)

    # Reshaping the Lanes Class into binary array and Upscaling the image as input image
    dummy_img = np.zeros(seg_arr.shape)
    dummy_img += ((seg_arr[:, :] == class_number) * (255)).astype(
        'uint8')  # Class Number 2 belongs to Lanes
    original_h, original_w = overlay_img.shape[0:2]
    upscaled_img = cv2.resize(dummy_img,
                              (original_w, original_h)).astype('uint8')
    upscaled_img_rgb = cv2.cvtColor(upscaled_img, cv2.COLOR_GRAY2RGB)

    # Stack input and segmentation in one video
    vis_img = np.vstack((
        np.hstack((input_img, seg_img)),
        np.hstack((
            overlay_img,
            upscaled_img_rgb))  # np.ones(overlay_img.shape,dtype=np.uint8)*128
    ))

    return upscaled_img  #vis_img #vis_img
def visualize_segmentation(input_img,
                           seg_arr,
                           n_classes,
                           class_number=2,
                           display=False,
                           output_file=None):
    seg_img = predict.segmented_image_from_prediction(
        seg_arr, n_classes=n_classes, input_shape=input_img.shape)
    overlay_img = cv2.addWeighted(input_img, 0.7, seg_img, 0.3, 0)

    # Reshaping the Lanes Class into binary array and Upscaling the image as input image
    dummy_img = np.zeros(seg_arr.shape)
    dummy_img += ((seg_arr[:, :] == class_number) * (255)).astype(
        'uint8')  # Class Number 2 belongs to Lanes
    original_h, original_w = overlay_img.shape[0:2]
    upscaled_img = cv2.resize(dummy_img,
                              (original_w, original_h)).astype('uint8')
    upscaled_img_rgb = cv2.cvtColor(upscaled_img, cv2.COLOR_GRAY2RGB)

    # centerline_arr = np.mean(upscaled_img, axis = 1)
    # for c_r in range(len(centerline_arr)):
    #     cv2.circle(upscaled_img_rgb, (int(centerline_arr[c_r]), c_r), 5, (0, 0, 255), -1)

    # for c_r in range(upscaled_img_rgb.shape[0]):
    # 	nonzero_xy = upscaled_img_rgb[c_r][1:].nonzero()
    # 	nonzero_x = np.array(nonzero_xy[0])
    #
    # 	centerline_arr = np.mean(nonzero_x)
    #
    # 	if np.isnan(centerline_arr)==0:
    # 		# print centerline_arr #img[c_r][1:].nonzero(),
    #
    # 		cv2.circle(upscaled_img_rgb, (int(centerline_arr), c_r), 5, (0, 0, 255), -1)

    # Stack input and segmentation in one video
    vis_img = np.vstack((
        np.hstack((input_img, seg_img)),
        np.hstack((
            overlay_img,
            upscaled_img_rgb))  # np.ones(overlay_img.shape,dtype=np.uint8)*128
    ))

    return upscaled_img_rgb  #upscaled_img
    def visualize_segmentation(self, seg_arr, display=False, output_file=None):

        seg_img = predict.segmented_image_from_prediction(
            seg_arr, self.n_classes, input_shape=self.image.shape)

        # Reshaping the Lanes Class into binary array and Upscaling the image as input image
        self.overlay_img = cv2.addWeighted(self.image, 0.7, seg_img, 0.3, 0)
        dummy_img = np.zeros(seg_arr.shape)
        dummy_img += ((seg_arr[:, :] == self.label_no) * (255)).astype('uint8')
        original_h, original_w = self.overlay_img.shape[0:2]
        self.pred_img = cv2.resize(dummy_img,
                                   (original_w, original_h)).astype('uint8')
        upscaled_img_rgb = cv2.cvtColor(self.pred_img, cv2.COLOR_GRAY2RGB)

        # Stack input and segmentation in one video
        vis_img = np.vstack((
            np.hstack((self.image, seg_img)),
            np.hstack((self.overlay_img, upscaled_img_rgb
                       ))  # np.ones(overlay_img.shape,dtype=np.uint8)*128
        ))
    def visualize_segmentation(
        self
    ):  #input_img, seg_arr, n_classes, display = False, output_file = None, class_number = 2
        seg_img = predict.segmented_image_from_prediction(
            self.seg_arr,
            n_classes=self.model.n_classes,
            input_shape=self.image.shape)
        overlay_img = cv2.addWeighted(self.image, 0.7, seg_img, 0.3, 0)

        # Reshaping the Lanes Class into binary array and Upscaling the image as input image
        dummy_img = np.zeros(self.seg_arr.shape)
        dummy_img += ((self.seg_arr[:, :] == self.class_number) *
                      (255)).astype('uint8')  # Class Number 2 belongs to Lanes
        original_h, original_w = overlay_img.shape[0:2]
        upscaled_img = cv2.resize(dummy_img,
                                  (original_w, original_h)).astype('uint8')
        upscaled_img_rgb = cv2.cvtColor(upscaled_img, cv2.COLOR_GRAY2RGB)

        # Stack input and segmentation in one video
        vis_img = np.vstack((
            np.hstack((self.image, seg_img)),
            np.hstack((overlay_img, upscaled_img_rgb
                       ))  #np.ones(overlay_img.shape,dtype=np.uint8)*128))
        ))

        #try:
        (trans, rot) = self.listener.lookupTransform('utm', 'base_link',
                                                     rospy.Time(0))
        #except (tf.LookupException, tf.ConnectivityException, tf.ExtrapolationException):
        #continue

        robot_lat_utm = trans[1]  # Latitude
        robot_long_utm = trans[0]  # Longitude

        a = np.array((robot_lat_utm, robot_long_utm))
        b = np.array((self.gt_lat_utm[1], self.gt_long_utm[1]))
        self.dist_0 = np.linalg.norm(a - b)
        #yaw_gt = math.atan2(gt_long_utm[1],gt_lat_utm[1])

        for i in range(2, len(self.gt_lat_utm)):
            b = np.array((self.gt_lat_utm[i], self.gt_long_utm[i]))
            dist = np.linalg.norm(a - b)
            if dist < self.dist_0:
                self.dist_0 = dist

        # #self.dist_0 = self.dist_0+1
        print self.dist_0

        font = cv2.FONT_HERSHEY_SIMPLEX
        bottomLeftCornerOfText = (100, 70)
        fontScale = 2
        fontColor = (255, 0, 0)
        lineType = 2

        cv2.putText(vis_img, 'Lateral Offset:' + str(self.dist_0),
                    bottomLeftCornerOfText, font, fontScale, fontColor,
                    lineType)

        # cv2.imshow('preview', vis_img)
        #
        # # Press Q on keyboard to  exit
        # if cv2.waitKey(25) & 0xFF == ord('q'):
        #  print('Q pressed, breaking')

        return vis_img