GXYM / TextBPN-Plus-Plus

Arbitrary Shape Text Detection via Boundary Transformer;The paper at: https://arxiv.org/abs/2205.05320, which has been accepted by IEEE Transactions on Multimedia (T-MM 2023).
172 stars 37 forks source link

ValueError: A LinearRing must have at least 3 coordinate tuples #17

Open aamna2401 opened 1 year ago

aamna2401 commented 1 year ago

Why is this error happening? Here is a log

cropped

GXYM commented 1 year ago

Why is this error happening? Here is a log

cropped

This is because the number of annotation points in the label is less than 4 or coincidence of annotation points. Please remove these annotations with less than 4 annotation points during data loading。

such as in Icdar15_Text.py:

 for line in lines:
            line = strs.remove_all(line.strip('\ufeff'), '\xef\xbb\xbf')
            gt = line.split(',')
            x1, y1, x2, y2, x3, y3, x4, y4 = list(map(int, gt[:8]))
            xx = [x1, x2, x3, x4]
            yy = [y1, y2, y3, y4]

            label = gt[-1].strip().replace("###", "#")

            pts = np.stack([xx, yy]).T.astype(np.int32)
            d1 = norm2(pts[0] - pts[1])
            d2 = norm2(pts[1] - pts[2])
            d3 = norm2(pts[2] - pts[3])
            d4 = norm2(pts[3] - pts[0])
            if min([d1, d2, d3, d4]) < 2:
                 continue
            polygons.append(TextInstance(pts, 'c', label))
GXYM commented 1 year ago

We can't guarantee that the code can cover all situations. Data differences may cause some bugs. Please fix it yourself.

123cjjjj commented 1 year ago

Why is this error happening? Here is a log

cropped

你好,请问您解决了吗?我在评估totaltxt数据集的时候也遇到了这种问题

123cjjjj commented 1 year ago

image

一般来说,文本检测的结果都会把面积过小的文本过滤掉,所以文本轮廓的点数不应该小于3,也就通常不会报这个错误。点数小于3,无法组成一个封闭的轮廓,所以报这个错误。建议对检测的结果进行检查过滤。

GXYM commented 1 year ago

Why is this error happening? Here is a log cropped

你好,请问您解决了吗?我在评估totaltxt数据集的时候也遇到了这种问题

请检查边界上点的个数,如果少于四个点[shape(4,2)],过滤掉; 或者用Polygon(inst.points).is_valid:判断轮廓是否是有效的,过滤掉无效的轮廓。在total-text的数据加载里,我们有添加这个过代码 https://github.com/GXYM/TextBPN-Plus-Plus/blob/34a7949966a0f1a95a67495368a86529a21e2772/dataset/Total_Text.py#L57, 为什还会报这个错呢?

 if len(x) < 4:  # too few points
    continue
123cjjjj commented 1 year ago

Why is this error happening? Here is a log cropped

你好,请问您解决了吗?我在评估totaltxt数据集的时候也遇到了这种问题

请检查边界上点的个数,如果少于四个点[shape(4,2)],过滤掉; 或者用Polygon(inst.points).is_valid:判断轮廓是否是有效的,过滤掉无效的轮廓。在total-text的数据加载里,我们有添加这个过代码 https://github.com/GXYM/TextBPN-Plus-Plus/blob/34a7949966a0f1a95a67495368a86529a21e2772/dataset/Total_Text.py#L57, 为什还会报这个错呢?

 if len(x) < 4:  # too few points
    continue

是的,还存在这个问题,我学习的是您的TextPM模型,在使用训练得到的模型对totaltxt数据集进行评估的过程中出现这种问题:detect 298 / 300 images: img391.jpg. (6.42 fps); detect 299 / 300 images: img661.jpg. (6.42 fps); detect 300 / 300 images: img898.jpg. (6.41 fps); Computing DetEval in output/Totaltext 80%|##################################################################9 | 239/300 [00:12<00:03, 19.28it/s] Traceback (most recent call last): File "dataset/total_text/Evaluation_Protocol/Python_scripts/Deteval.py", line 106, in detections = detection_filtering(detections, groundtruths) # filters detections overlapping with DC area File "dataset/total_text/Evaluation_Protocol/Python_scripts/Deteval.py", line 65, in detection_filtering det_gt_iou = iod(det_x, det_y, gt_x, gt_y) File "/opt/data/private/TextPMs-master/dataset/total_text/Evaluation_Protocol/Python_scripts/polygon_fast.py", line 70, in iod return area_of_intersection(det_x, det_y, gt_x, gt_y) / (area(det_x, det_y) + 1.0) File "/opt/data/private/TextPMs-master/dataset/total_text/Evaluation_Protocol/Python_scripts/polygon_fast.py", line 52, in area_of_intersection p1 = Polygon(np.stack([det_x, det_y], axis=1)).buffer(0) File "/usr/local/anaconda3/lib/python3.6/site-packages/shapely/geometry/polygon.py", line 261, in init ret = geos_polygon_from_py(shell, holes) File "/usr/local/anaconda3/lib/python3.6/site-packages/shapely/geometry/polygon.py", line 539, in geos_polygon_from_py ret = geos_linearring_from_py(shell) File "shapely/speedups/_speedups.pyx", line 257, in shapely.speedups._speedups.geos_linearring_from_py ValueError: A LinearRing must have at least 3 coordinate tuples 80%|##################################################################9 | 239/300 [00:12<00:03, 19.29it/s] Traceback (most recent call last): File "dataset/total_text/Evaluation_Protocol/Python_scripts/Deteval.py", line 106, in detections = detection_filtering(detections, groundtruths) # filters detections overlapping with DC area File "dataset/total_text/Evaluation_Protocol/Python_scripts/Deteval.py", line 65, in detection_filtering det_gt_iou = iod(det_x, det_y, gt_x, gt_y) File "/opt/data/private/TextPMs-master/dataset/total_text/Evaluation_Protocol/Python_scripts/polygon_fast.py", line 70, in iod return area_of_intersection(det_x, det_y, gt_x, gt_y) / (area(det_x, det_y) + 1.0) File "/opt/data/private/TextPMs-master/dataset/total_text/Evaluation_Protocol/Python_scripts/polygon_fast.py", line 52, in area_of_intersection p1 = Polygon(np.stack([det_x, det_y], axis=1)).buffer(0) File "/usr/local/anaconda3/lib/python3.6/site-packages/shapely/geometry/polygon.py", line 261, in init ret = geos_polygon_from_py(shell, holes) File "/usr/local/anaconda3/lib/python3.6/site-packages/shapely/geometry/polygon.py", line 539, in geos_polygon_from_py ret = geos_linearring_from_py(shell) File "shapely/speedups/_speedups.pyx", line 257, in shapely.speedups._speedups.geos_linearring_from_py ValueError: A LinearRing must have at least 3 coordinate tuples End.

GXYM commented 1 year ago

ValueError: A LinearRing must have at least 3 coordinate tuples

那你对最终的检测结果,再存txt文件之前,过滤一下吧。把点数小于4的检测结果和Polygon(inst.points).is_valid ==False的检测结果都过滤掉,应该就不会报错了

123cjjjj commented 1 year ago

ValueError: A LinearRing must have at least 3 coordinate tuples

那你对最终的检测结果,再存txt文件之前,过滤一下吧。把点数小于4的检测结果和Polygon(inst.points).is_valid ==False的检测结果都过滤掉,应该就不会报错了 ` if cfg.expname == "Icdar2015": fname = "res" + meta['image_id'][idx].replace('jpg', 'txt') contours = data_transfer_ICDAR(contours) write_to_file(contours, os.path.join(output_dir, fname)) elif cfg.exp_name == "MLT2017": out_dir = os.path.join(outputdir, "{}{}{}{}_{}".format(str(cfg.checkepoch), str(cfg.threshold), str(cfg.score_i), str(cfg.test_size[0]), str(cfg.test_size[1]))) if not os.path.exists(out_dir): mkdirs(out_dir) fname = meta['image_id'][idx].split("/")[-1].replace('ts', 'res') fname = fname.split(".")[0] + ".txt" data_transfer_MLT2017(contours, os.path.join(out_dir, fname)) elif cfg.expname == "TD500": fname = "res" + meta['image_id'][idx].split(".")[0]+".txt" data_transfer_TD500(contours, os.path.join(output_dir, fname))

    elif cfg.exp_name == "Totaltext":
        fname = meta['image_id'][idx].replace('jpg', 'txt')
        data_transfer_Totaltext(contours, os.path.join(output_dir, fname))

    else:
        fname = meta['image_id'][idx].replace('jpg', 'txt')
        write_to_file(contours, os.path.join(output_dir, fname))`

在这里修改 https://github.com/GXYM/TextBPN-Plus-Plus/blob/a4963ac3f0f604598d075f0dbd6d195831bc3541/eval_textBPN.py#L42

def write_to_file(contours, file_path):
    """
    :param contours: [[x1, y1], [x2, y2]... [xn, yn]]
    :param file_path: target file path
    """
    with open(file_path, 'w') as f:
        for cont in contours:
            cont = np.stack([cont[:, 0], cont[:, 1]], 1)
            if cv2.contourArea(cont) <= 0:
                continue
            cont = cont.flatten().astype(str).tolist()
            cont = ','.join(cont)
            f.write(cont + '\n')