clustering_precomputed_dbscan.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import numpy as np
  2. import pandas
  3. import csv
  4. from math import sqrt
  5. from sklearn.cluster import DBSCAN
  6. from sklearn.cluster import OPTICS
  7. import order_bounding_boxes_in_each_block
  8. def get_average_xy(list_input):
  9. csv_name = "temporary/list_to_csv_with_corner_points.csv"
  10. new_list = []
  11. resultFile = open(csv_name, 'w')
  12. wr = csv.writer(resultFile, delimiter=";")
  13. wr.writerow(["element", "xmin","ymin","xmax","ymax", "ausrichtung","point_xmi_ymi","point_xma_ymi","point_xmi_yma","point_xma_yma"])
  14. for element in list_input:
  15. xavg_elem = 0
  16. yavg_elem = 0
  17. ymin = 100000000
  18. ymax = 0
  19. xmin = 100000000
  20. xmax = 0
  21. for blub in element: #get the smallest and largest x and y value for whole block
  22. xavg_elem += (float(blub[0]) + float(blub[2]))/2
  23. yavg_elem += (float(blub[1]) + float(blub[3]))/2
  24. if float(blub[1]) < ymin:
  25. ymin = float(blub[1])
  26. #print("y_min:",y_min)
  27. if float(blub[0]) < xmin:
  28. xmin = float(blub[0])
  29. if float(blub[3]) > ymax:
  30. ymax = float(blub[3])
  31. if float(blub[2]) > xmax:
  32. xmax = float(blub[2])
  33. if float(xmax)-float(xmin) > 1.3*(float(ymax)-float(ymin)):
  34. ausrichtung = 0 #horizontal
  35. #print("horizontal")
  36. if 1.5*(float(xmax)-float(xmin)) < float(ymax)-float(ymin):
  37. ausrichtung = 1 #vertikal
  38. #print("vertikal")
  39. else:
  40. ausrichtung = 3 #sonstiges
  41. #print("sonstiges")
  42. xavg_elem = xavg_elem/len(element)
  43. #print(xavg_elem)
  44. yavg_elem = yavg_elem/len(element)
  45. #element.extend([xavg_elem, yavg_elem])
  46. #print(element)
  47. #new_list.append(element)
  48. ##### GET CORNER POINTS
  49. point_xmi_ymi = [xmin,ymin]
  50. point_xma_ymi = [xmax,ymin]
  51. point_xmi_yma = [xmin,ymax]
  52. point_xma_yma = [xmax,ymax]
  53. wr.writerow([element,xmin,ymin,xmax,ymax, ausrichtung,point_xmi_ymi,point_xma_ymi,point_xmi_yma,point_xma_yma])
  54. resultFile.close()
  55. #print(new_list)
  56. return csv_name
  57. def intersects(rectangle1, rectangle2): #using the separating axis theorem
  58. #print(rectangle2[0])
  59. #for rect in rectangle1:
  60. rect_1_min = eval(rectangle1[0])
  61. rect_1_max = eval(rectangle1[3])
  62. rect1_bottom_left_x= rect_1_min[0]
  63. rect1_top_right_x=rect_1_max[0]
  64. rect1_bottom_left_y= rect_1_max[1]
  65. rect1_top_right_y= rect_1_min[1]
  66. rect_2_min = eval(rectangle2[0])
  67. rect_2_max = eval(rectangle2[3])
  68. rect2_bottom_left_x= rect_2_min[0]
  69. rect2_top_right_x=rect_2_max[0]
  70. rect2_bottom_left_y= rect_2_max[1]
  71. rect2_top_right_y=rect_2_min[1]
  72. return not (rect1_top_right_x < rect2_bottom_left_x or rect1_bottom_left_x > rect2_top_right_x or rect1_top_right_y > rect2_bottom_left_y or rect1_bottom_left_y < rect2_top_right_y)
  73. def dist(rectangle1, rectangle2):
  74. #get minimal distance between two rectangles
  75. distance = 100000000
  76. #print(rectangle1)
  77. for point1 in rectangle1[:4]:
  78. point1 = eval(point1)
  79. #print(point1)
  80. for point2 in rectangle2[:4]:
  81. #print(point2)
  82. point2 = eval(point2)
  83. #dist1 = (float(point2[0]) - float(point1[0])) + ((float(point2[1]) - float(point1[1])))
  84. dist = sqrt(((float(point2[0]) - float(point1[0])))**2 + ((float(point2[1]) - float(point1[1])))**2)
  85. #print(dist)
  86. if dist < distance:
  87. distance = dist
  88. if rectangle1[4] != rectangle2[4]:
  89. distance = dist + 100
  90. #print(intersects(rectangle1,rectangle2))
  91. if intersects(rectangle1, rectangle2):
  92. distance = 0
  93. #print(rectangle1)
  94. return distance
  95. def clustering(distance_matrix):
  96. db = DBSCAN(eps=3, min_samples=1, metric="precomputed").fit(dm) ##3.93 until now, bei 5 shon mehr erkannt, 7 noch mehr erkannt aber auch schon zu viel; GV12 ist 4.5 gut für LH zu wenig
  97. #db = OPTICS(min_samples=1,xi=0.1, metric="precomputed").fit(dm)
  98. labels = db.labels_
  99. # Number of clusters in labels
  100. n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
  101. print('Estimated number of clusters: %d' % n_clusters_)
  102. data_df = pandas.read_csv("/home/bscheibel/PycharmProjects/dxf_reader/temporary/list_to_csv_with_corner_points.csv",
  103. sep=";")
  104. data_df["cluster"] = labels
  105. data_df.groupby(['cluster','ausrichtung'])['element'].apply(','.join).reset_index().to_csv("values_clusteredfrom_precomputed_dbscan.csv",sep=";", header=False, index=False)
  106. #file = "/home/bscheibel/PycharmProjects/dxf_reader/drawings/5152166_Rev04.html"
  107. file = "/home/bscheibel/PycharmProjects/dxf_reader/drawings/5129275_Rev01-GV12.html"
  108. result = order_bounding_boxes_in_each_block.get_bound_box(file)
  109. #print(result)
  110. get_average_xy(result)
  111. #rectangle1 = [[450,286],[464,286],[450,376],[464,376]]
  112. #rectangle2 = [[450,316],[456,316],[450,329],[456,329]]
  113. #rectangle3 = [[23,45],[35,45],[23,60],[35,60]]
  114. #print(dist(rectangle1,rectangle2))
  115. data = pandas.read_csv("/home/bscheibel/PycharmProjects/dxf_reader/temporary/list_to_csv_with_corner_points.csv", sep=";")
  116. data = data[["point_xmi_ymi","point_xma_ymi","point_xmi_yma","point_xma_yma","ausrichtung"]].replace("'","")
  117. #print(data)
  118. data.to_csv("blub.csv", sep=";", index=False, header=None)
  119. result = []
  120. with open('blub.csv') as csvfile:
  121. readCSV = csv.reader(csvfile, delimiter=';')
  122. result = list(readCSV)
  123. dm = np.asarray([[dist(p1, p2) for p2 in result] for p1 in result])
  124. with np.printoptions(threshold=np.inf):
  125. print(dm)
  126. clustering(dm)