def plot_polygon(polygon, color=None, draw_labels=True, label_direction=1, indexing="xy", axis=None): if python_utils.module_exists("matplotlib.pyplot"): import matplotlib.pyplot as plt if axis is None: axis = plt.gca() polygon_closed = np.append(polygon, [polygon[0, :]], axis=0) if indexing == "xy=": axis.plot(polygon_closed[:, 0], polygon_closed[:, 1], color=color, linewidth=3.0) elif indexing == "ij": axis.plot(polygon_closed[:, 1], polygon_closed[:, 0], color=color, linewidth=3.0) else: print("WARNING: Invalid indexing argument") if draw_labels: labels = range(1, polygon.shape[0] + 1) for label, x, y in zip(labels, polygon[:, 0], polygon[:, 1]): axis.annotate(label, xy=(x, y), xytext=(-20 * label_direction, 20 * label_direction), textcoords='offset points', ha='right', va='bottom', bbox=dict(boxstyle='round,pad=0.25', fc=color, alpha=0.75), arrowprops=dict(arrowstyle='->', color=color, connectionstyle='arc3,rad=0'))
import os import sys import numpy as np import cv2 current_filepath = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(current_filepath, "../../utils")) import python_utils import polygon_utils # --- Useful when code is executed inside Docker without a display: --- # # Try importing pyplot: display_is_available = python_utils.get_display_availability() use_pyplot = None if display_is_available: if python_utils.module_exists("matplotlib.pyplot"): # This means everything works with the default matplotlib backend import matplotlib.pyplot as plt use_pyplot = True else: # matplotlib.pyplot is just not available we cannot plot anything use_pyplot = False else: # Try switching backend import matplotlib matplotlib.use('Agg') if python_utils.module_exists("matplotlib.pyplot"): # The Agg backend works, pyplot is available, we just can't display plots to the screen (they'll be saved to file anyway) import matplotlib.pyplot as plt use_pyplot = True # --- --- #
import numpy as np import time import sklearn.datasets import skimage.transform import python_utils import image_utils # if python_utils.module_exists("matplotlib.pyplot"): # import matplotlib.pyplot as plt CV2 = False if python_utils.module_exists("cv2"): import cv2 CV2 = True # import multiprocessing # # import python_utils # # if python_utils.module_exists("joblib"): # from joblib import Parallel, delayed # JOBLIB = True # else: # JOBLIB = False # def plot_field_map(field_map): # from mpl_toolkits.mplot3d import Axes3D # # row = np.linspace(0, 1, field_map.shape[0]) # col = np.linspace(0, 1, field_map.shape[1])
import math import random import numpy as np import scipy.spatial from PIL import Image, ImageDraw, ImageFilter import skimage import python_utils if python_utils.module_exists("skimage.measure"): from skimage.measure import approximate_polygon if python_utils.module_exists("shapely"): from shapely import geometry def is_polygon_clockwise(polygon): rolled_polygon = np.roll(polygon, shift=1, axis=0) double_signed_area = np.sum((rolled_polygon[:, 0] - polygon[:, 0]) * (rolled_polygon[:, 1] + polygon[:, 1])) if 0 < double_signed_area: return True else: return False def orient_polygon(polygon, orientation="CW"): poly_is_orientated_cw = is_polygon_clockwise(polygon) if (poly_is_orientated_cw and orientation == "CCW") or (not poly_is_orientated_cw and orientation == "CW"): return np.flip(polygon, axis=0) else:
import tensorflow as tf import sys import math import numpy as np sys.path.append("../../polygon") import python_utils import polygon_utils if python_utils.module_exists("skimage.io"): import skimage.io as io # --- Params --- # SEED = 0 RANDOM_CROP_AMPLITUDE = 0 # --- --- # def polygon_flip_up_down(polygon, vertex_count, im_res): invert_mat = np.array([[1, 0], [0, -1]], dtype=np.float16) translate_mat = np.tile(np.array([0, im_res]), (vertex_count, 1)) polygon = tf.add(tf.matmul(polygon, invert_mat), translate_mat) return polygon def read_and_decode(tfrecords_filename, im_res,