• python把ChestX-Det-Dataset的json样本转为COCO数据集的json格式


    ChestX-Det-Dataset数据集网址:https://github.com/Deepwise-AILab/ChestX-Det-Dataset/tree/main
    在这里插入图片描述
    数据集JSON内容:

    [
        {
            "file_name": "36199.png",
            "syms": [],
            "boxes": [],
            "polygons": []
        },
        {
            "file_name": "36302.png",
            "syms": [
                "Effusion"
            ],
            "boxes": [
                [
                    799,
                    666,
                    937,
                    761
                ]
            ],
            "polygons": [
                [
                    [
                        799,
                        678
                    ],
                    [
                        799,
                        678
                    ],
                    [
                        799,
                        680
                    ],
                    [
                        801,
                        681
                    ],
                    [
                        805,
                        684
                    ],
                    [
                        807,
                        684
                    ],
                    [
                        809,
                        685
                    ],
                    [
                        811,
                        686
                    ],
                    [
                        813,
                        686
                    ],
                    [
                        814,
                        686
                    ],
                    [
                        817,
                        687
                    ],
                    [
                        820,
                        687
                    ],
                    [
                        824,
                        690
                    ],
                    [
                        827,
                        690
                    ],
                    [
                        830,
                        691
                    ],
                    [
                        832,
                        691
                    ],
                    [
                        833,
                        691
                    ],
                    [
                        836,
                        693
                    ],
                    [
                        837,
                        693
                    ],
                    [
                        840,
                        695
                    ],
                    [
                        844,
                        696
                    ],
                    [
                        848,
                        696
                    ],
                    [
                        851,
                        697
                    ],
                    [
                        854,
                        697
                    ],
                    [
                        855,
                        697
                    ],
                    [
                        856,
                        698
                    ],
                    [
                        861,
                        699
                    ],
                    [
                        864,
                        699
                    ],
                    [
                        870,
                        701
                    ],
                    [
                        872,
                        703
                    ],
                    [
                        875,
                        704
                    ],
                    [
                        878,
                        705
                    ],
                    [
                        881,
                        705
                    ],
                    [
                        886,
                        707
                    ],
                    [
                        890,
                        709
                    ],
                    [
                        894,
                        711
                    ],
                    [
                        896,
                        713
                    ],
                    [
                        897,
                        714
                    ],
                    [
                        899,
                        714
                    ],
                    [
                        902,
                        716
                    ],
                    [
                        903,
                        717
                    ],
                    [
                        906,
                        720
                    ],
                    [
                        908,
                        721
                    ],
                    [
                        910,
                        725
                    ],
                    [
                        912,
                        726
                    ],
                    [
                        914,
                        728
                    ],
                    [
                        916,
                        731
                    ],
                    [
                        916,
                        732
                    ],
                    [
                        917,
                        733
                    ],
                    [
                        918,
                        734
                    ],
                    [
                        921,
                        738
                    ],
                    [
                        922,
                        740
                    ],
                    [
                        924,
                        741
                    ],
                    [
                        925,
                        744
                    ],
                    [
                        927,
                        745
                    ],
                    [
                        929,
                        747
                    ],
                    [
                        930,
                        751
                    ],
                    [
                        931,
                        752
                    ],
                    [
                        934,
                        753
                    ],
                    [
                        935,
                        755
                    ],
                    [
                        935,
                        756
                    ],
                    [
                        935,
                        757
                    ],
                    [
                        936,
                        758
                    ],
                    [
                        937,
                        759
                    ],
                    [
                        937,
                        761
                    ],
                    [
                        937,
                        759
                    ],
                    [
                        937,
                        757
                    ],
                    [
                        937,
                        756
                    ],
                    [
                        937,
                        752
                    ],
                    [
                        937,
                        750
                    ],
                    [
                        937,
                        747
                    ],
                    [
                        937,
                        745
                    ],
                    [
                        937,
                        744
                    ],
                    [
                        937,
                        743
                    ],
                    [
                        937,
                        741
                    ],
                    [
                        937,
                        740
                    ],
                    [
                        937,
                        739
                    ],
                    [
                        937,
                        738
                    ],
                    [
                        937,
                        737
                    ],
                    [
                        937,
                        735
                    ],
                    [
                        937,
                        733
                    ],
                    [
                        937,
                        731
                    ],
                    [
                        937,
                        729
                    ],
                    [
                        937,
                        728
                    ],
                    [
                        937,
                        726
                    ],
                    [
                        937,
                        723
                    ],
                    [
                        937,
                        720
                    ],
                    [
                        937,
                        717
                    ],
                    [
                        937,
                        716
                    ],
                    [
                        936,
                        714
                    ],
                    [
                        935,
                        710
                    ],
                    [
                        935,
                        709
                    ],
                    [
                        935,
                        708
                    ],
                    [
                        934,
                        705
                    ],
                    [
                        934,
                        704
                    ],
                    [
                        934,
                        703
                    ],
                    [
                        934,
                        702
                    ],
                    [
                        933,
                        701
                    ],
                    [
                        933,
                        698
                    ],
                    [
                        933,
                        696
                    ],
                    [
                        931,
                        695
                    ],
                    [
                        931,
                        692
                    ],
                    [
                        931,
                        691
                    ],
                    [
                        930,
                        690
                    ],
                    [
                        930,
                        686
                    ],
                    [
                        930,
                        685
                    ],
                    [
                        929,
                        681
                    ],
                    [
                        929,
                        680
                    ],
                    [
                        929,
                        679
                    ],
                    [
                        929,
                        677
                    ],
                    [
                        928,
                        674
                    ],
                    [
                        928,
                        673
                    ],
                    [
                        927,
                        672
                    ],
                    [
                        927,
                        671
                    ],
                    [
                        925,
                        671
                    ],
                    [
                        924,
                        668
                    ],
                    [
                        924,
                        666
                    ],
                    [
                        924,
                        667
                    ],
                    [
                        924,
                        669
                    ],
                    [
                        924,
                        672
                    ],
                    [
                        924,
                        674
                    ],
                    [
                        923,
                        677
                    ],
                    [
                        923,
                        678
                    ],
                    [
                        923,
                        679
                    ],
                    [
                        923,
                        680
                    ],
                    [
                        922,
                        681
                    ],
                    [
                        921,
                        683
                    ],
                    [
                        921,
                        684
                    ],
                    [
                        920,
                        685
                    ],
                    [
                        918,
                        685
                    ],
                    [
                        918,
                        686
                    ],
                    [
                        915,
                        689
                    ],
                    [
                        912,
                        690
                    ],
                    [
                        910,
                        691
                    ],
                    [
                        909,
                        692
                    ],
                    [
                        908,
                        692
                    ],
                    [
                        906,
                        692
                    ],
                    [
                        905,
                        693
                    ],
                    [
                        904,
                        693
                    ],
                    [
                        902,
                        695
                    ],
                    [
                        900,
                        695
                    ],
                    [
                        900,
                        695
                    ],
                    [
                        899,
                        695
                    ],
                    [
                        898,
                        695
                    ],
                    [
                        896,
                        695
                    ],
                    [
                        894,
                        693
                    ],
                    [
                        891,
                        693
                    ],
                    [
                        887,
                        693
                    ],
                    [
                        886,
                        693
                    ],
                    [
                        884,
                        693
                    ],
                    [
                        881,
                        692
                    ],
                    [
                        879,
                        692
                    ],
                    [
                        876,
                        691
                    ],
                    [
                        874,
                        691
                    ],
                    [
                        870,
                        690
                    ],
                    [
                        867,
                        690
                    ],
                    [
                        866,
                        690
                    ],
                    [
                        863,
                        690
                    ],
                    [
                        861,
                        689
                    ],
                    [
                        860,
                        689
                    ],
                    [
                        857,
                        689
                    ],
                    [
                        856,
                        687
                    ],
                    [
                        854,
                        687
                    ],
                    [
                        851,
                        687
                    ],
                    [
                        848,
                        686
                    ],
                    [
                        845,
                        686
                    ],
                    [
                        842,
                        686
                    ],
                    [
                        840,
                        686
                    ],
                    [
                        839,
                        685
                    ],
                    [
                        837,
                        685
                    ],
                    [
                        834,
                        684
                    ],
                    [
                        828,
                        683
                    ],
                    [
                        825,
                        683
                    ],
                    [
                        822,
                        681
                    ],
                    [
                        819,
                        680
                    ],
                    [
                        815,
                        679
                    ],
                    [
                        814,
                        679
                    ],
                    [
                        812,
                        679
                    ],
                    [
                        811,
                        679
                    ],
                    [
                        811,
                        678
                    ],
                    [
                        808,
                        677
                    ],
                    [
                        806,
                        675
                    ],
                    [
                        803,
                        675
                    ],
                    [
                        802,
                        675
                    ],
                    [
                        801,
                        675
                    ]
                ]
            ]
        },
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
    • 372
    • 373
    • 374
    • 375
    • 376
    • 377
    • 378
    • 379
    • 380
    • 381
    • 382
    • 383
    • 384
    • 385
    • 386
    • 387
    • 388
    • 389
    • 390
    • 391
    • 392
    • 393
    • 394
    • 395
    • 396
    • 397
    • 398
    • 399
    • 400
    • 401
    • 402
    • 403
    • 404
    • 405
    • 406
    • 407
    • 408
    • 409
    • 410
    • 411
    • 412
    • 413
    • 414
    • 415
    • 416
    • 417
    • 418
    • 419
    • 420
    • 421
    • 422
    • 423
    • 424
    • 425
    • 426
    • 427
    • 428
    • 429
    • 430
    • 431
    • 432
    • 433
    • 434
    • 435
    • 436
    • 437
    • 438
    • 439
    • 440
    • 441
    • 442
    • 443
    • 444
    • 445
    • 446
    • 447
    • 448
    • 449
    • 450
    • 451
    • 452
    • 453
    • 454
    • 455
    • 456
    • 457
    • 458
    • 459
    • 460
    • 461
    • 462
    • 463
    • 464
    • 465
    • 466
    • 467
    • 468
    • 469
    • 470
    • 471
    • 472
    • 473
    • 474
    • 475
    • 476
    • 477
    • 478
    • 479
    • 480
    • 481
    • 482
    • 483
    • 484
    • 485
    • 486
    • 487
    • 488
    • 489
    • 490
    • 491
    • 492
    • 493
    • 494
    • 495
    • 496
    • 497
    • 498
    • 499
    • 500
    • 501
    • 502
    • 503
    • 504
    • 505
    • 506
    • 507
    • 508
    • 509
    • 510
    • 511
    • 512
    • 513
    • 514
    • 515
    • 516
    • 517
    • 518
    • 519
    • 520
    • 521
    • 522
    • 523
    • 524
    • 525
    • 526
    • 527
    • 528
    • 529
    • 530
    • 531
    • 532
    • 533
    • 534
    • 535
    • 536
    • 537
    • 538
    • 539
    • 540
    • 541
    • 542
    • 543
    • 544
    • 545
    • 546
    • 547
    • 548
    • 549
    • 550
    • 551
    • 552
    • 553
    • 554
    • 555
    • 556
    • 557
    • 558
    • 559
    • 560
    • 561
    • 562
    • 563
    • 564
    • 565
    • 566
    • 567
    • 568
    • 569
    • 570
    • 571
    • 572
    • 573
    • 574
    • 575
    • 576
    • 577
    • 578
    • 579
    • 580
    • 581
    • 582
    • 583
    • 584
    • 585
    • 586
    • 587
    • 588
    • 589
    • 590
    • 591
    • 592
    • 593
    • 594
    • 595
    • 596
    • 597
    • 598
    • 599
    • 600
    • 601
    • 602
    • 603
    • 604
    • 605
    • 606
    • 607
    • 608
    • 609
    • 610
    • 611
    • 612
    • 613
    • 614
    • 615
    • 616
    • 617
    • 618
    • 619
    • 620
    • 621
    • 622
    • 623
    • 624
    • 625
    • 626
    • 627
    • 628
    • 629
    • 630
    • 631
    • 632
    • 633
    • 634
    • 635
    • 636
    • 637
    • 638
    • 639
    • 640
    • 641
    • 642
    • 643
    • 644
    • 645
    • 646
    • 647
    • 648
    • 649
    • 650
    • 651
    • 652
    • 653
    • 654
    • 655
    • 656
    • 657
    • 658
    • 659
    • 660
    • 661
    • 662
    • 663
    • 664
    • 665
    • 666
    • 667
    • 668
    • 669
    • 670
    • 671
    • 672
    • 673
    • 674
    • 675
    • 676
    • 677
    • 678
    • 679
    • 680
    • 681
    • 682
    • 683
    • 684
    • 685
    • 686
    • 687
    • 688
    • 689
    • 690
    • 691
    • 692
    • 693
    • 694
    • 695
    • 696
    • 697
    • 698
    • 699
    • 700
    • 701
    • 702
    • 703
    • 704
    • 705
    • 706
    • 707
    • 708
    • 709
    • 710
    • 711
    • 712
    • 713
    • 714
    • 715
    • 716
    • 717
    • 718
    • 719
    • 720
    • 721
    • 722
    • 723
    • 724
    • 725
    • 726
    • 727
    • 728
    • 729
    • 730
    • 731
    • 732
    • 733
    • 734
    • 735
    • 736
    • 737
    • 738
    • 739
    • 740
    • 741
    • 742
    • 743
    • 744
    • 745
    • 746
    • 747
    • 748
    • 749
    • 750
    • 751
    • 752
    • 753
    • 754
    • 755
    • 756
    • 757
    • 758
    • 759
    • 760
    • 761
    • 762
    • 763
    • 764
    • 765
    • 766

    转化后coco格式样本json

    在这里插入图片描述
    使用的python代码如下:

    import json
    import os
    import sys
    import cv2
    from tqdm import tqdm
    import math
    
    che_json = './chetrain.json'
    dst_json = './chestrain_coco.json'
    test_img = './train_data/train'
    
    # che_json = './chetest.json'
    # dst_json = './chetest_coco.json'
    # test_img = './test_data/test'
    
    def polygon_area(vertices):
        n = len(vertices)
        area = 0.0
        for i in range(n):
            x1, y1 = vertices[i]
            x2, y2 = vertices[(i + 1) % n]
            area += (x1 * y2 - x2 * y1)
        return abs(area) / 2.0
    
    def main():
        coco_data = {
            "info": {},
            "licenses": [],
            "categories": [],
            "images": [],
            "annotations": []
        }
    	category_mapping = {}
        category_id = 1
        image_id = 1
        annotation_id = 1
        with open(che_json,'r',encoding='utf-8') as js:
            json_info = json.load(js)
        
        image = {}
        annotation = {}
        boxid = 0
        for jsfo in tqdm(json_info):
            image['file_name'] = jsfo['file_name']
            img =cv2.imread(test_img + '/' + jsfo['file_name']) 
            image['height'] = img.shape[0]
            image['width'] = img.shape[1]
            image['id'] = image_id
            coco_data["images"].append(image)
            image = {}
            category_name = jsfo['syms']
            for ii in range(len(category_name)):
                if category_name[ii] not in category_mapping:
                    category_mapping[category_name[ii]] = category_id
                    coco_data["categories"].append({
                        "supercategory": category_name[ii],
                        "id": category_id,
                        "name": category_name[ii]
                    })
                    category_id += 1
            box_cnt = len(jsfo['boxes'])
            for i in range(box_cnt):
                boxid = boxid + 1
                segpnts = []
                segtmp = jsfo['polygons'][i]
                for segt in segtmp:
                    segpnts.append(segt[0])
                    segpnts.append(segt[1])
                segarea = polygon_area(segtmp)
                annotation['segmentation'] = [segpnts]
                annotation['image_id'] = image_id
                annotation['area'] = segarea
                boxtmp = jsfo['boxes'][i]
                x_left,y_left,x_br,y_br = boxtmp
                box_w,box_h = x_br-x_left, y_br-y_left
                annotation['bbox'] = [x_left,y_left,box_w,box_h]
                annotation['category_id'] = category_mapping[category_name[i]]
                annotation['id'] = boxid
            coco_data["annotations"].append(annotation)
            annotation = {}
            image_id = image_id + 1
        with open(dst_json,'w') as jsout:
            json.dump(coco_data, jsout)
    
    if __name__=='__main__':
        main()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    先跑训练集或先跑验证集会生成category_mapping 的字典内容,生成后统一用同一个,就可以保证训练集和验证集的标签一致

  • 相关阅读:
    java毕业设计智慧防疫上报系统服务端Mybatis+系统+数据库+调试部署
    方块栈问题
    渗透测试-红队从资产收集到打点
    离散数学 --- 命题逻辑 --- 基本推理形式和自然演绎法推理
    5种常用格式的数据输出,手把手教你用Pandas实现
    猿创征文 | 剑指offer 36. 复杂链表的复刻
    【数字IC设计】DC自动添加门控时钟
    LeetCode --- 2068. Check Whether Two Strings are Almost Equivalent 解题报告
    java ssm空巢老年人社区服务系统
    数据结构 栈和队列上
  • 原文地址:https://blog.csdn.net/qq_44442727/article/details/134046854