organize_drawing_according_to_details_new.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import order_bounding_boxes_in_each_block
  2. import re
  3. import csv
  4. import clustering_precomputed_dbscan
  5. def get_details(result): #search for all details in drawing and store it in list details, first need to append all text elements of one line and then check if regular expression is found in this text element
  6. reg = r"([A-Z])-\1|([A-Z]\W?[A-Z]?\s?\W\s?\d\d?\s?\s?:\s?\d\d?\s?\W)"
  7. details = []
  8. for element in result:
  9. new = []
  10. if re.search(reg,element):
  11. new.extend(result[element])
  12. new.append(element)
  13. details.append(new)
  14. number = len(details)
  15. return details, number
  16. def get_borders(details, tables):
  17. sections = []
  18. #print(coords)
  19. for first in details:
  20. x_min = -1
  21. y_min = -1
  22. x_max = -1
  23. y_max = -1
  24. firstx_max = first[2]
  25. firstx_min = first[0]
  26. firsty_max = first[3]
  27. firsty_min = first[1]
  28. distance_xmax = 100000000000
  29. distance_xmin = 100000000000
  30. distance_ymin = 100000000000
  31. distance_ymax = 100000000000
  32. for second in details:
  33. secondx_min = second[0]
  34. secondx_max = second[2]
  35. secondy_min = second[1]
  36. secondy_max = second[3]
  37. if secondx_max < firstx_min and abs(firsty_min-secondy_min) < 90 and first != second: ###check for left side, are there any other details at the left side at a certain y-span
  38. #print(first,second)
  39. if abs(firstx_min - secondx_max)/2 < distance_xmax:
  40. distance_xmax = abs(firstx_min - secondx_max)/2
  41. x_min = secondx_max + distance_xmax
  42. if secondx_min > firstx_max and abs(firsty_min-secondy_min) < 190 and first != second: ####check for right side
  43. if abs(secondx_min - firstx_max)/2 < distance_xmin:
  44. #print(first, second)
  45. distance_xmin = abs(secondx_min - firstx_max)/2
  46. x_max = firstx_max + distance_xmin
  47. if firsty_min > secondy_max and abs(firstx_min-secondx_min) < 80 and first != second: ####check above
  48. if abs(firsty_min - secondy_max)/2 < distance_ymin:
  49. #print(first, second)
  50. distance_ymin = abs(firsty_min - secondy_max)/2
  51. y_min = firsty_min
  52. if firsty_max < secondy_min and abs(firstx_min-secondx_min) < 80 and first != second: ####check below
  53. if abs(firsty_max - secondy_min)/2 < distance_ymax:
  54. #print(first, second)
  55. distance_ymax = abs(firsty_max - secondy_min)/2
  56. y_max = secondy_min
  57. if y_min == -1:
  58. y_min = firsty_min
  59. if x_min == -1:
  60. x_min = 0
  61. if x_max == -1:
  62. x_max = firstx_max + distance_xmax
  63. if y_max == -1:
  64. y_max = 1000000000
  65. ##check if it intersects with tables
  66. for table in tables:
  67. #print(table)
  68. table_xmin = table[0]
  69. if "Start drawing" in table[4]:
  70. table_xmax = 100000000
  71. else:
  72. table_xmax = table[2]
  73. table_ymin = table[1]
  74. #table_ymax = table[3]
  75. if y_max > table_ymin:
  76. if firstx_min > table_xmin and firstx_min < table_xmax:
  77. #print("blub",first,table, table_xmax)
  78. y_max = table_ymin
  79. elif x_max > table_xmin and x_max < table_xmax:
  80. #print(first,table)
  81. y_max = table_ymin
  82. sections.append((first,x_min, y_min,x_max,y_max))
  83. """for section in sections:
  84. print(section)"""
  85. return sections
  86. def intersects(detail, rectangle): #using the separating axis theorem
  87. #print(detail)
  88. rect1_bottom_left_x = float(detail[1][0])
  89. rect1_top_right_x = float(detail[1][2])
  90. rect1_bottom_left_y = float(detail[1][3])
  91. rect1_top_right_y = float(detail[1][1])
  92. rect2_bottom_left_x = float(rectangle[0])
  93. rect2_top_right_x = float(rectangle[2])
  94. rect2_bottom_left_y = float(rectangle[3])
  95. rect2_top_right_y = float(rectangle[1])
  96. 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)
  97. def main_function(result, tables):
  98. reg = r"([A-Z])-\1|([A-Z]\W?[A-Z]?\s?\W\s?\d\d?\s?\s?:\s?\d\d?\s?\W)"
  99. details, number = get_details(result)
  100. #details.extend(tables)
  101. #print(tables)
  102. #print(details)
  103. details = sorted(details, key=lambda x: x[0]) #sort by distance from 0,0
  104. sections = get_borders(details, tables)
  105. #sections.append(tables)
  106. section = []
  107. details_dict = {}
  108. for sect in sections:
  109. coord = []
  110. coord_name = sect[0][4]
  111. for sec in sect[1:]:
  112. coord.append(sec)
  113. details_dict[coord_name] = coord
  114. section.append(list((coord_name,coord)))
  115. #print(section)
  116. for table in tables:
  117. table[3] = 10000000
  118. coord = []
  119. name = "ZZZZZTable"
  120. for tab in table[:4]:
  121. coord.append(tab)
  122. details_dict[name] = coord
  123. section.append(list((name,coord)))
  124. #print(section)
  125. if number == 0 | len(section) == 0:
  126. section.append(list(("No details",list((000.000,000.000,100000000.000,10000000.000)))))
  127. dict = {}
  128. for res in result:
  129. #print(res)
  130. for det in section:
  131. help_array = []
  132. help_dict = {}
  133. #print(det)
  134. if re.match(reg, res): ###damit nicht details zu details zugeordnet werden!!!
  135. break
  136. if intersects(det,result[res]):
  137. name = det[0]
  138. #help_array.append(res)
  139. #help_array.extend(result[res])
  140. help_dict[res] = result[res]
  141. #print(name)
  142. if name in dict:
  143. dict[name].update(help_dict)
  144. else:
  145. dict[name] = help_dict
  146. break
  147. """for dic in dict:
  148. print(dic)
  149. for d in dict[dic]:
  150. print(d)"""
  151. return dict, details_dict