BruceFeng98 commited on
Commit
2b59f5c
·
verified ·
1 Parent(s): 9f3980c

Upload metric.py

Browse files
Files changed (1) hide show
  1. metric/metric.py +31 -71
metric/metric.py CHANGED
@@ -228,7 +228,7 @@ def extract_numbers(s):
228
  def extract_numbers_float(s):
229
 
230
  numbers = []
231
- for num_str in re.findall(r'\d+\.\d+|\d+', s): # 匹配小数或整数
232
  if '.' in num_str:
233
  numbers.append(float(num_str))
234
  else:
@@ -236,50 +236,34 @@ def extract_numbers_float(s):
236
  return numbers
237
 
238
  def extract_numbers_float2(s):
239
- """提取字符串中的所有浮点数(小数),忽略整数"""
240
  numbers = []
241
- for num_str in re.findall(r'\d+\.\d+', s): # 只匹配小数(必须包含小数点)
242
  numbers.append(float(num_str))
243
  return numbers
244
  def group_numbers_into_fours(num_list):
245
- """
246
- 将数字列表按每4个元素分组,并验证总长度是否为4的倍数
247
-
248
- 参数:
249
- num_list -- 全数字组成的列表,如 [1,2,3,4,5,6,7,8]
250
 
251
- 返回:
252
- 分组后的二维列表,如 [[1,2,3,4], [5,6,7,8]]
253
-
254
- 异常:
255
- ValueError -- 当输入列表长度不是4的倍数时抛出
256
- """
257
  n = len(num_list)
258
 
259
- # 验证长度是否为4的倍数
260
- # if n % 4 != 0:
261
- # raise ValueError(f"数字个数 {n} 不是4的倍数,无法完整分组")
262
-
263
- # 按步长4切割列表
264
  result = [num_list[i:i + 4] for i in range(0, n-3, 4)]
265
  return result
266
 
267
  def clean_string_for_box(input_str):
268
- # 正则表达式匹配:保留方括号 []、数字、空格和逗号
269
  return re.sub(r'[^\[\], .\d]', '', input_str)
270
  def parse_bbox_string(bbox_str):
271
 
272
  """
273
  """
274
  try:
275
- # 使用literal_eval将字符串解析为Python对象
276
  bbox_str = clean_string_for_box(bbox_str)
277
  bbox_nums = extract_numbers_float(bbox_str)
278
  bboxes = group_numbers_into_fours(bbox_nums)
279
  # bboxes = ast.literal_eval(bbox_str)
280
  return bboxes
281
  except Exception as e:
282
- print("解析bbox字符串时出错:", e)
283
  return []
284
 
285
  def parse_bbox_string2(bbox_str):
@@ -287,27 +271,27 @@ def parse_bbox_string2(bbox_str):
287
  """
288
  """
289
  try:
290
- # 使用literal_eval将字符串解析为Python对象
291
  bbox_str = clean_string_for_box(bbox_str)
292
  bbox_nums = extract_numbers_float2(bbox_str)
293
  bboxes = group_numbers_into_fours(bbox_nums)
294
  # bboxes = ast.literal_eval(bbox_str)
295
  return bboxes
296
  except Exception as e:
297
- print("解析bbox字符串时出错:", e)
298
  return []
299
 
300
  def calculate_iou(box1, box2):
301
  """
302
  """
303
 
304
- # 解析坐标
305
  # print("box 1",box1)
306
  # print("box 2",box2)
307
  x1_1, y1_1, x2_1, y2_1 = box1
308
  x1_2, y1_2, x2_2, y2_2 = box2
309
 
310
- # 计算交集区域坐标
311
  x_left = max(x1_1, x1_2)
312
  y_top = max(y1_1, y1_2)
313
  x_right = min(x2_1, x2_2)
@@ -319,21 +303,21 @@ def calculate_iou(box1, box2):
319
  if x1_2 > x2_2: return 0.0
320
  if y1_2 > y2_2: return 0.0
321
 
322
- # 处理无交集情况
323
  if x_right < x_left or y_bottom < y_top:
324
  return 0.0
325
 
326
- # 计算交集面积
327
  intersection_area = (x_right - x_left) * (y_bottom - y_top)
328
 
329
- # 计算各自面积
330
  box1_area = (x2_1 - x1_1) * (y2_1 - y1_1)
331
  box2_area = (x2_2 - x1_2) * (y2_2 - y1_2)
332
 
333
- # 计算并集面积
334
  union_area = box1_area + box2_area - intersection_area
335
 
336
- # 计算IoU
337
  iou = intersection_area / union_area
338
  return iou
339
 
@@ -344,11 +328,11 @@ def calculate_centerpoint(norm_gt_bboxs, norm_pre_bbox):
344
  cx1 = (x1_1 + x2_1) / 2.0
345
  cy1 = (y1_1 + y2_1) / 2.0
346
 
347
- # 计算第二个框的中心
348
  cx2 = (x1_2 + x2_2) / 2.0
349
  cy2 = (y1_2 + y2_2) / 2.0
350
 
351
- # 欧氏距离
352
  dist = math.hypot(cx1 - cx2, cy1 - cy2)
353
 
354
  return dist
@@ -357,13 +341,11 @@ def calculate_area_ratio(box1, box2):
357
  """
358
  """
359
 
360
- # 解析坐标
361
- # print("box 1",box1)
362
- # print("box 2",box2)
363
  x1_1, y1_1, x2_1, y2_1 = box1
364
  x1_2, y1_2, x2_2, y2_2 = box2
365
 
366
- # 计算各自面积
367
  box1_area = (x2_1 - x1_1) * (y2_1 - y1_1)
368
  box2_area = (x2_2 - x1_2) * (y2_2 - y1_2)
369
  if box1_area <= 0:
@@ -395,16 +377,12 @@ def norm_bbox(norm_bbox,size):
395
 
396
  def bbox_number_types(bboxes):
397
  """
398
- 判断一组 bbox 列表中的每个数字是整数还是小数。
399
-
400
- :param bboxes: List[List[float]],每个 bbox 为 [x1, y1, x2, y2]
401
- :return: List[List[str]],与 bboxes 结构相同,每个位置返回 "int" 或 "float"
402
  """
403
  result = []
404
  for box in bboxes:
405
  types = []
406
  for num in box:
407
- # 如果它和自身取整后相等,就当作整数
408
  if isinstance(num, (int,)) or (isinstance(num, float) and num.is_integer()):
409
  types.append("int")
410
  else:
@@ -870,7 +848,6 @@ def task_poster_detection(data, max_box_num=30):
870
  # print(f"{ratio:3f} boxes: {len(gt_bboxs)}")
871
  wrong_recall += 1
872
  # else:
873
- """最多算5个bbox"""
874
  incount_bbox_num = min(len(gt_bboxs), len(pre_bboxs), max_box_num)
875
  for i in range(incount_bbox_num):
876
  # print(pre_bboxs[i])
@@ -1043,48 +1020,31 @@ def task_layout_generation(data):
1043
 
1044
  def extract_last_bracket_list(s: str) -> list:
1045
  """
1046
- 在字符串中定位最后一个 '[' 和最后一个 ']',并将它们之间的内容提取、
1047
- 按逗号拆分后返回为 Python 列表。
1048
-
1049
- Args:
1050
- s (str): 输入字符串
1051
-
1052
- Returns:
1053
- list: 拆分后的元素列表(去除两端空白),如果未找到匹配的括号,则返回空列表
1054
  """
1055
- # 查找最后一个 '[' 和最后一个 ']'
1056
  last_open = s.rfind('[')
1057
  last_close = s.rfind(']')
1058
 
1059
- # 如果任意一个不存在,或顺序不对,则返回空列表
1060
  if last_open == -1 or last_close == -1 or last_open > last_close:
1061
  return []
1062
 
1063
- # 提取中间的子串
1064
  content = s[last_open + 1:last_close]
1065
 
1066
- # 按逗号分割,并去除每个元素的首尾空白
1067
- # 如果希望支持空元素,也可以改用 content.split(',')
1068
  items = [int(item.strip()) for item in content.split(',') if item.strip()]
1069
 
1070
  return items
1071
 
1072
  def list_iou(list1, list2):
1073
  """
1074
- 计算两个列表(或任何可迭代对象)中元素的交并比(IoU)。
1075
-
1076
- Args:
1077
- list1 (list): 第一个列表
1078
- list2 (list): 第二个列表
1079
-
1080
- Returns:
1081
- float: IoU 值,范围 [0, 1]。如果二者都为空,则返回 1.0。
1082
  """
1083
  set1 = set(list1)
1084
  set2 = set(list2)
1085
 
1086
  if not set1 and not set2:
1087
- return 1.0 # 两个都为空,定义 IoU 为 1
1088
 
1089
  intersection = set1 & set2
1090
  union = set1 | set2
@@ -1206,10 +1166,10 @@ if __name__=="__main__":
1206
  print(os.path.basename(json_item))
1207
  data = read_json_file(json_item)
1208
  """ocr"""
1209
- # logo_ac = task_logo_cor(data)
1210
- # poster_ac = task_poster_ocr(data)
1211
- # pc_wr1, pc_wr2, pc_r, cc_wr1, cc_wr2, cc_r, pw_wr1, pw_wr2, pw_r, cw_wr1, cw_wr2, cW_r = task_4_ocr(data)
1212
- # print(f"& {logo_ac:.3f} & {poster_ac:.3f} & {pc_wr1:.3f} & {pc_wr2:.3f} & {pc_r:.3f} & {cc_wr1:.3f} & {cc_wr2:.3f} & {cc_r:.3f} & {pw_wr1:.3f} & {pw_wr2:.3f} & {pw_r:.3f} & {cw_wr1:.3f} & {cw_wr2:.3f} & {cW_r:.3f}")
1213
  """font size ocr"""
1214
  # mean, std, mean3, std3, mean_r = task_font_size(data)
1215
  # print(f"& {mean:.3f} & {std:.3f} & {mean3:.3f} & {std3:.3f} & {mean_r:.3f}")
@@ -1251,5 +1211,5 @@ if __name__=="__main__":
1251
  # bias, area_rate, rate = task_layout_generation(data)
1252
  # print(f"{bias:.3f} & {area_rate:.3f} & {rate:.3f}")
1253
  """advertisement understanding"""
1254
- points = task_ads(data)
1255
- print(f"& {points:.3f}")
 
228
  def extract_numbers_float(s):
229
 
230
  numbers = []
231
+ for num_str in re.findall(r'\d+\.\d+|\d+', s):
232
  if '.' in num_str:
233
  numbers.append(float(num_str))
234
  else:
 
236
  return numbers
237
 
238
  def extract_numbers_float2(s):
239
+
240
  numbers = []
241
+ for num_str in re.findall(r'\d+\.\d+', s):
242
  numbers.append(float(num_str))
243
  return numbers
244
  def group_numbers_into_fours(num_list):
 
 
 
 
 
245
 
 
 
 
 
 
 
246
  n = len(num_list)
247
 
 
 
 
 
 
248
  result = [num_list[i:i + 4] for i in range(0, n-3, 4)]
249
  return result
250
 
251
  def clean_string_for_box(input_str):
252
+
253
  return re.sub(r'[^\[\], .\d]', '', input_str)
254
  def parse_bbox_string(bbox_str):
255
 
256
  """
257
  """
258
  try:
259
+
260
  bbox_str = clean_string_for_box(bbox_str)
261
  bbox_nums = extract_numbers_float(bbox_str)
262
  bboxes = group_numbers_into_fours(bbox_nums)
263
  # bboxes = ast.literal_eval(bbox_str)
264
  return bboxes
265
  except Exception as e:
266
+ print("error", e)
267
  return []
268
 
269
  def parse_bbox_string2(bbox_str):
 
271
  """
272
  """
273
  try:
274
+
275
  bbox_str = clean_string_for_box(bbox_str)
276
  bbox_nums = extract_numbers_float2(bbox_str)
277
  bboxes = group_numbers_into_fours(bbox_nums)
278
  # bboxes = ast.literal_eval(bbox_str)
279
  return bboxes
280
  except Exception as e:
281
+ print("error:", e)
282
  return []
283
 
284
  def calculate_iou(box1, box2):
285
  """
286
  """
287
 
288
+
289
  # print("box 1",box1)
290
  # print("box 2",box2)
291
  x1_1, y1_1, x2_1, y2_1 = box1
292
  x1_2, y1_2, x2_2, y2_2 = box2
293
 
294
+
295
  x_left = max(x1_1, x1_2)
296
  y_top = max(y1_1, y1_2)
297
  x_right = min(x2_1, x2_2)
 
303
  if x1_2 > x2_2: return 0.0
304
  if y1_2 > y2_2: return 0.0
305
 
306
+
307
  if x_right < x_left or y_bottom < y_top:
308
  return 0.0
309
 
310
+
311
  intersection_area = (x_right - x_left) * (y_bottom - y_top)
312
 
313
+
314
  box1_area = (x2_1 - x1_1) * (y2_1 - y1_1)
315
  box2_area = (x2_2 - x1_2) * (y2_2 - y1_2)
316
 
317
+
318
  union_area = box1_area + box2_area - intersection_area
319
 
320
+
321
  iou = intersection_area / union_area
322
  return iou
323
 
 
328
  cx1 = (x1_1 + x2_1) / 2.0
329
  cy1 = (y1_1 + y2_1) / 2.0
330
 
331
+
332
  cx2 = (x1_2 + x2_2) / 2.0
333
  cy2 = (y1_2 + y2_2) / 2.0
334
 
335
+
336
  dist = math.hypot(cx1 - cx2, cy1 - cy2)
337
 
338
  return dist
 
341
  """
342
  """
343
 
344
+
 
 
345
  x1_1, y1_1, x2_1, y2_1 = box1
346
  x1_2, y1_2, x2_2, y2_2 = box2
347
 
348
+
349
  box1_area = (x2_1 - x1_1) * (y2_1 - y1_1)
350
  box2_area = (x2_2 - x1_2) * (y2_2 - y1_2)
351
  if box1_area <= 0:
 
377
 
378
  def bbox_number_types(bboxes):
379
  """
 
 
 
 
380
  """
381
  result = []
382
  for box in bboxes:
383
  types = []
384
  for num in box:
385
+
386
  if isinstance(num, (int,)) or (isinstance(num, float) and num.is_integer()):
387
  types.append("int")
388
  else:
 
848
  # print(f"{ratio:3f} boxes: {len(gt_bboxs)}")
849
  wrong_recall += 1
850
  # else:
 
851
  incount_bbox_num = min(len(gt_bboxs), len(pre_bboxs), max_box_num)
852
  for i in range(incount_bbox_num):
853
  # print(pre_bboxs[i])
 
1020
 
1021
  def extract_last_bracket_list(s: str) -> list:
1022
  """
 
 
 
 
 
 
 
 
1023
  """
1024
+ #
1025
  last_open = s.rfind('[')
1026
  last_close = s.rfind(']')
1027
 
1028
+ #
1029
  if last_open == -1 or last_close == -1 or last_open > last_close:
1030
  return []
1031
 
1032
+
1033
  content = s[last_open + 1:last_close]
1034
 
1035
+
 
1036
  items = [int(item.strip()) for item in content.split(',') if item.strip()]
1037
 
1038
  return items
1039
 
1040
  def list_iou(list1, list2):
1041
  """
 
 
 
 
 
 
 
 
1042
  """
1043
  set1 = set(list1)
1044
  set2 = set(list2)
1045
 
1046
  if not set1 and not set2:
1047
+ return 1.0
1048
 
1049
  intersection = set1 & set2
1050
  union = set1 | set2
 
1166
  print(os.path.basename(json_item))
1167
  data = read_json_file(json_item)
1168
  """ocr"""
1169
+ logo_ac = task_logo_cor(data)
1170
+ poster_ac = task_poster_ocr(data)
1171
+ pc_wr1, pc_wr2, pc_r, cc_wr1, cc_wr2, cc_r, pw_wr1, pw_wr2, pw_r, cw_wr1, cw_wr2, cW_r = task_4_ocr(data)
1172
+ print(f"& {logo_ac:.3f} & {poster_ac:.3f} & {pc_wr1:.3f} & {pc_wr2:.3f} & {pc_r:.3f} & {cc_wr1:.3f} & {cc_wr2:.3f} & {cc_r:.3f} & {pw_wr1:.3f} & {pw_wr2:.3f} & {pw_r:.3f} & {cw_wr1:.3f} & {cw_wr2:.3f} & {cW_r:.3f}")
1173
  """font size ocr"""
1174
  # mean, std, mean3, std3, mean_r = task_font_size(data)
1175
  # print(f"& {mean:.3f} & {std:.3f} & {mean3:.3f} & {std3:.3f} & {mean_r:.3f}")
 
1211
  # bias, area_rate, rate = task_layout_generation(data)
1212
  # print(f"{bias:.3f} & {area_rate:.3f} & {rate:.3f}")
1213
  """advertisement understanding"""
1214
+ # points = task_ads(data)
1215
+ # print(f"& {points:.3f}")