code: def sort_with_direction(pos_list, f_direction):
      """
      f_direction: h x w x 2
      pos_list: [[y, x], [y, x], [y, x] ...]
      """
  
      def sort_part_with_direction(pos_list, point_direction):
          pos_list = np.array(pos_list).reshape(-1, 2)
          point_direction = np.array(point_direction).reshape(-1, 2)
          average_direction = np.mean(
              point_direction, axis=0, keepdims=True
          )
          pos_proj_leng = np.sum(pos_list * average_direction, axis=1)
          sorted_list = pos_list[np.argsort(pos_proj_leng)].tolist()
          sorted_direction = point_direction[
              np.argsort(pos_proj_leng)
          ].tolist()
          return sorted_list, sorted_direction
  
      pos_list = np.array(pos_list).reshape(-1, 2)
      point_direction = f_direction[
          pos_list[:, 0], pos_list[:, 1]
      ]  # x, y
      point_direction = point_direction[:, ::-1]  # x, y -> y, x
      sorted_point, sorted_direction = sort_part_with_direction(
          pos_list, point_direction
      )
  
      point_num = len(sorted_point)
      if point_num >= 16:
          middle_num = point_num // 2
          first_part_point = sorted_point[:middle_num]
          first_point_direction = sorted_direction[:middle_num]
          (
              sorted_fist_part_point,
              sorted_fist_part_direction,
          ) = sort_part_with_direction(
              first_part_point, first_point_direction
          )
  
          last_part_point = sorted_point[middle_num:]
          last_point_direction = sorted_direction[middle_num:]
          (
              sorted_last_part_point,
              sorted_last_part_direction,
          ) = sort_part_with_direction(
              last_part_point, last_point_direction
          )
          sorted_point = sorted_fist_part_point + sorted_last_part_point
          sorted_direction = (
              sorted_fist_part_direction + sorted_last_part_direction
          )
  
      return sorted_point, np.array(sorted_direction)
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import cv2
  import math
  import numpy
  from itertools import groupby
  from skimage.morphology._skeletonize import thin
label_desc: Use image node_id 9 for calling a global function with example usage: sort_with_direction(pos_list, f_direction) and returns: sorted_point,  np, sorted_list,  sorted_direction