比较cs行中的上一个值和下一个值

2024-09-29 23:20:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个CSV文件(.txt),包含CNN的检测结果:

CSV文件示例:

filename,type,confidence,xmin,ymin,xmax,ymax  
27cz1_SLRM_0, barrow,87, 128, 176, 176, 224  
27cz1_SLRM_101, barrow,80, 480, 400, 512, 432  
27cz1_SLRM_103, celtic_field,85, 0, 112, 96, 256  
27cz1_SLRM_103, celtic_field,80, 256, 384, 384, 544  
27cz1_SLRM_103, celtic_field,80, 160, 96, 304, 272  
27cz1_SLRM_103, barrow,85, 416, 160, 464, 208  
27cz1_SLRM_107, celtic_field,84, 96, 448, 224, 576  
27cz1_SLRM_107, barrow,94, 256, 432, 304, 480  
27cz1_SLRM_107, barrow,87, 128, 368, 176, 416  
27cz1_SLRM_107, barrow,84, 64, 304, 112, 352  
27cz1_SLRM_107, barrow,80, 64, 208, 96, 240  

坐标文件示例:

27cz1_SLRM_0, 179927.5, 475140.0
27cz1_SLRM_101, 183062.5, 476565.0
27cz1_SLRM_103, 183632.5, 476565.0
27cz1_SLRM_107, 184772.5, 476565.0

为了减少误报的数量,我想去掉类celtic_field的所有单个检测。你知道吗

在上面的例子中,来自27cz1_SLRM_103celtic_field检测应该保留,但是来自27cz1_SLRM_107celtic_field检测应该删除。你知道吗

作为进一步处理的一部分,CSV作为字典打开,并变成GEOJSON条目(见下文)。这个工作很好,但我想包括以上。你知道吗

coords = {}
coords_file = csv.reader(open(coordinate_location))
for row in coords_file:
    coords[row[0]] = [float(row[1]),float(row[2])]

# open output file
output_file = csv.DictReader(open(output_location))

# turn detections into polygons
for row in output_file:

    img_name = row['filename']
    detection_class = row['type'].strip()
    confidence = row['confidence']
    #combo = row['filename'] + row['type']

    #detection_type = detection['tool_label']

    if detection_class == 'celtic_field':
        detectionDict = {
            "type": "Feature",
            "geometry": {
                "type": "Polygon",
                "coordinates": []
            },
            "properties": {
                "detection_type": detection_class,
                "confidence": confidence
            }
        }

        polyCoords = []
        coordinate_x_1 = coords[img_name][0] + float(row['xmin']) * 0.5
        coordinate_x_2 = coords[img_name][0] + float(row['xmin']) * 0.5
        coordinate_x_3 = coords[img_name][0] + float(row['xmax']) * 0.5
        coordinate_x_4 = coords[img_name][0] + float(row['xmax']) * 0.5
        coordinate_y_1 = coords[img_name][1] - float(row['ymin']) * 0.5
        coordinate_y_2 = coords[img_name][1] - float(row['ymax']) * 0.5
        coordinate_y_3 = coords[img_name][1] - float(row['ymax']) * 0.5
        coordinate_y_4 = coords[img_name][1] - float(row['ymin']) * 0.5

        polyCoords.append([coordinate_x_1,coordinate_y_1])
        polyCoords.append([coordinate_x_2,coordinate_y_2])
        polyCoords.append([coordinate_x_3,coordinate_y_3])
        polyCoords.append([coordinate_x_4,coordinate_y_4])
        polyCoords.append([coordinate_x_1,coordinate_y_1])

        detectionDict['geometry']['coordinates'].append(polyCoords)
        output['features'].append(detectionDict)

Tags: namefieldcoordinateimgtypecoordsfloatrow
2条回答

多亏了你们很多人(尤其是比利·博纳罗斯)的帮助,我找到了一个可行的解决方案:

# remove loose celtic_fields
df = pd.read_csv(output_location)
df['count'] = df.groupby(['filename','type']).transform('count')['confidence']

for i,row in df.iterrows():
    if row['count']==1 and row['type']==' celtic_field':
        df.drop(i, inplace=True)

df.to_csv('...\csv.txt')

# open output file

output_file = csv.DictReader(open('...\csv.txt'))

非常感谢!你知道吗

试试这个:

df['count']=df.groupby(['filename','type']).transform('count')['confidence']

df=df[~((df['count']==1)&(df['type']=='celtic_field'))]

打印(df)

          filename           type  confidence  xmin  ymin  xmax  ymax    count
0     27cz1_SLRM_0         barrow          87   128   176   176     224      1
1   27cz1_SLRM_101         barrow          80   480   400   512     432      1
2   27cz1_SLRM_103   celtic_field          85     0   112    96     256      3
3   27cz1_SLRM_103   celtic_field          80   256   384   384     544      3
4   27cz1_SLRM_103   celtic_field          80   160    96   304     272      3
5   27cz1_SLRM_103         barrow          85   416   160   464     208      1
7   27cz1_SLRM_107         barrow          94   256   432   304     480      4
8   27cz1_SLRM_107         barrow          87   128   368   176     416      4
9   27cz1_SLRM_107         barrow          84    64   304   112     352      4
10  27cz1_SLRM_107         barrow          80    64   208    96     240      4

相关问题 更多 >

    热门问题