wzzanthony7 commited on
Commit
a0661eb
·
verified ·
1 Parent(s): a9cc0fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -1
app.py CHANGED
@@ -134,6 +134,29 @@ def filter_overlapping_boxes(filter_box_info, iou_threshold=0.5):
134
 
135
  return other_boxes + kept_boxes
136
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
  def packFilterBoxInfo(filter_box_info):
138
  # 数字类别映射
139
  digit_classes = {
@@ -207,7 +230,26 @@ def getOverlap(box1, box2):
207
  b2_area = abs(b2_x2 - b2_x1) * abs(b2_y2 - b2_y1)
208
 
209
  return inter_area / b2_area
210
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211
  def generate_textual_description(box_info):
212
  fraction_values = packFilterBoxInfo(box_info)
213
  # Create a dictionary to store information by class ID
@@ -271,6 +313,14 @@ def generate_textual_description(box_info):
271
  textual_description += f"(({box[0]:.2f}, {box[1]:.2f}), ({box[2]:.2f}, {box[3]:.2f})), "
272
  if (class_name == "fraction"):
273
  textual_description += f"\nThe fraction numbers from left to right are: {fraction_values}. "
 
 
 
 
 
 
 
 
274
  return textual_description
275
  def drawWithAllBox_info(pil_image, box_info):
276
  colors = ['red', 'green', 'blue', 'orange', 'purple', 'cyan', 'magenta', 'yellow', 'brown', 'pink', 'gray', 'lime', 'navy']
 
134
 
135
  return other_boxes + kept_boxes
136
 
137
+ def to_ordinal(n):
138
+ """
139
+ Converts an integer to its ordinal string representation.
140
+ e.g., 1 -> "1st", 2 -> "2nd", 13 -> "13th"
141
+ """
142
+ if not isinstance(n, int):
143
+ raise TypeError("Input must be an integer.")
144
+ # Check for 11th, 12th, 13th, which are special cases
145
+ if 11 <= (n % 100) <= 13:
146
+ suffix = 'th'
147
+ else:
148
+ # Check the last digit for all other cases
149
+ last_digit = n % 10
150
+ if last_digit == 1:
151
+ suffix = 'st'
152
+ elif last_digit == 2:
153
+ suffix = 'nd'
154
+ elif last_digit == 3:
155
+ suffix = 'rd'
156
+ else:
157
+ suffix = 'th'
158
+ return f"{n}{suffix}"
159
+
160
  def packFilterBoxInfo(filter_box_info):
161
  # 数字类别映射
162
  digit_classes = {
 
230
  b2_area = abs(b2_x2 - b2_x1) * abs(b2_y2 - b2_y1)
231
 
232
  return inter_area / b2_area
233
+
234
+ def tick2fraction(ticks, fractions):
235
+ ret = []
236
+ used_ticks = set()
237
+ for fi, frac in enumerate(fractions):
238
+ all_dis = []
239
+ for ti, tick in enumerate(ticks):
240
+ if ti not in used_ticks:
241
+ dis = getCenterXDis(tick, frac)
242
+ all_dis.append((dis, ti))
243
+ if len(all_dis) == 0:
244
+ #print(f"no tick found for fraction {fi}")
245
+ #no tick found for this fraction
246
+ break
247
+ all_dis.sort(key=lambda x: x[0])
248
+ min_dis_index = all_dis[0][1]
249
+ used_ticks.add(min_dis_index)
250
+ ret.append(f"T{min_dis_index}-F{fi}")
251
+ return ret
252
+
253
  def generate_textual_description(box_info):
254
  fraction_values = packFilterBoxInfo(box_info)
255
  # Create a dictionary to store information by class ID
 
313
  textual_description += f"(({box[0]:.2f}, {box[1]:.2f}), ({box[2]:.2f}, {box[3]:.2f})), "
314
  if (class_name == "fraction"):
315
  textual_description += f"\nThe fraction numbers from left to right are: {fraction_values}. "
316
+ tick2fra = tick2fraction(class_summary['tick'], class_summary['fraction'])
317
+ tick2fraction_des = ""
318
+ for cor in tick2fra:
319
+ tick_part, fraction_part = cor.split('-')
320
+ fraction_idx = int(fraction_part[1:])
321
+ tick_idx = int(tick_part[1:])
322
+ tick2fraction_des += f"{to_ordinal(fraction_idx + 1)} fraction is associated with {to_ordinal(tick_idx + 1)} tick. "
323
+ textual_description += tick2fraction_des
324
  return textual_description
325
  def drawWithAllBox_info(pil_image, box_info):
326
  colors = ['red', 'green', 'blue', 'orange', 'purple', 'cyan', 'magenta', 'yellow', 'brown', 'pink', 'gray', 'lime', 'navy']