目标
将实例分割结果MASK以RLE格式以json形式输出方便其他工具使用
使用环境
从github上自行下载源码,按其说明配置,链接
代码
from mmdet.apis import init_detector, inference_detector, show_result_pyplot, show_result_ins
import mmcv
import os
import json
import itertools
import numpy as np
config_file = '../configs/solov2/solov2_light_512_dcn_r50_fpn_8gpu_3x.py'
# download the checkpoint from model zoo and put it in `checkpoints/`
checkpoint_file = '../SOLOv2_LIGHT_512_DCN_R50_3x.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
#read all img
path="rgb-test"
path_list=os.listdir(path)
path_list.sort()
for filename in path_list:
# test a single image
#img = 'test/000000002153.jpg'
score_thr=0.25
out_file="rgb-test-out/"+filename
img = os.path.join(path,filename)
result = inference_detector(model, img)
show_result_ins(img, result, model.CLASSES, score_thr, out_file="rgb-test-out/"+filename)
print("____________________________________________________")
print(result)
print("____________________________________________________")
cur_result = result[0]
seg_label = cur_result[0]
seg_label = seg_label.cpu().numpy().astype(np.uint8)
print("seg_label:")
print(seg_label)
print("____________________________________________________")
cate_label = cur_result[1]
cate_label = cate_label.cpu().numpy()
print("cate_label:")
print(cate_label)
print("____________________________________________________")
score = cur_result[2].cpu().numpy()
vis_inds = score > score_thr
print(vis_inds)
print("____________________________________________________")
seg_label = seg_label[vis_inds]
print(seg_label)
print("____________________________________________________")
num_mask = seg_label.shape[0]
num_mask2 = seg_label.shape[1]
num_mask3= seg_label.shape[2]
print("mask-number:",num_mask,"height:",num_mask2,"weith:",num_mask3)
print("____________________________________________________")
cate_label = cate_label[vis_inds]
print(cate_label)
print("____________________________________________________")
cate_score = score[vis_inds]
print(cate_score)
print("____________________________________________________")
data = dict()
data_mask = dict()
data['image_id'] = filename
data['score'] = cate_score.tolist()
data['category _id'] = cate_label.tolist()
data['mask_number']=num_mask
data['pic_height']=num_mask2
data['pic_weith']=num_mask3
last=0
current=0
count=0
count_list=[]
count_for_every_mask=[]
sumn=0
for i in range(num_mask):
maskname="mask"+str(i)+"counts"
print(i)
for j in range(num_mask2):
for k in range(num_mask3):
current= seg_label[i,j,k]
if current==last:
count=count+1
if j==(num_mask2-1) and k==(num_mask3-1):
count_list.append(count)
else:
count_list.append(count)
count=1
last=current
count=0
last=0
count_for_every_mask.append(list(count_list))
# print(count_for_every_mask)
data[maskname]=count_for_every_mask[i]
count_list.clear()
string=json.dumps(data)
with open(out_file+'.json','w') as jsonfile:
jsonfile.write(string)
结果
输出JSON文件:
(为了方便观看,我手动敲了回车,实际上是紧凑输出,没有回车)
{"image_id": "1311870427.199132.png",
"score": [0.7585232853889465, 0.6723414659500122, 0.5829629898071289, 0.55458003282547, 0.542271614074707, 0.5399870276451111, 0.3881881833076477, 0.32466474175453186, 0.29188352823257446, 0.29082342982292175, 0.28237277269363403],
"category _id": [62, 66, 58, 77, 58, 75, 2, 64, 39, 67, 73],
"mask_number": 11,
"pic_height": 480,
"pic_weith": 640,
"mask0counts": [100975, 1, 628, 14, 617, 23, 609, 32, 597, 43, 584, 56, 579, 61, 573, 67, 554, 86, 543, 97, 538, 102, 524, 116, 517, 123, 515, 125, 514, 126, 513, 127, 512, 128, 512, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 129, 511, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 511, 129, 511, 129, 511, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 131, 509, 131, 509, 131, 509, 131, 509, 131, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 510, 130, 511, 129, 511, 129, 511, 129, 511, 129, 511, 128, 512, 128, 512, 127, 514, 125, 515, 124, 516, 122, 518, 119, 521, 116, 524, 112, 529, 109, 531, 108, 532, 106, 534, 103, 537, 98, 543, 90, 550, 83, 557, 77, 563, 64, 576, 61, 579, 57, 583, 49, 591, 45, 595, 40, 601, 32, 609, 27, 614, 21, 624, 1, 120572],
"mask1counts": [195091, 9, 626, 16, 600, 5, 14, 22, 592, 50, 585, 56, 581, 60, 575, 66, 561, 80, 554, 87, 550, 91, 545, 96, 534, 108, 524, 117, 511, 129, 503, 139, 489, 152, 481, 160, 477, 164, 474, 167, 472, 169, 471, 169, 471, 170, 470, 170, 471, 170, 470, 171, 469, 171, 470, 170, 471, 169, 472, 168, 472, 166, 474, 162, 479, 157, 483, 155, 486, 145, 496, 138, 503, 135, 505, 133, 508, 123, 518, 116, 525, 113, 527, 111, 530, 102, 539, 97, 543, 95, 546, 91, 550, 82, 558, 79, 562, 75, 566, 69, 572, 58, 583, 53, 587, 49, 592, 41, 600, 33, 608, 30, 611, 25, 615, 15, 626, 9, 633, 4, 75097],
"mask2counts": [79529, 3, 636, 4, 635, 5, 634, 6, 633, 8, 632, 8, 632, 8, 632, 8, 631, 9, 631, 9, 631, 9, 630, 10, 579, 6, 44, 11, 578, 8, 43, 11, 578, 9, 41, 12, 578, 11, 39, 12, 578, 12, 38, 13, 577, 12, 37, 14, 577, 13, 36, 14, 577, 13, 35, 15, 577, 14, 33, 16, 577, 14, 33, 16, 577, 15, 32, 16, 577, 15, 31, 17, 578, 15, 30, 17, 578, 15, 30, 17, 578, 16, 28, 18, 578, 16, 28, 19, 577, 17, 26, 20, 577, 17, 25, 21, 577, 18, 22, 23, 577, 18, 20, 25, 577, 19, 16, 28, 577, 20, 14, 29, 577, 21, 12, 31, 576, 21, 11, 32, 576, 22, 9, 33, 576, 23, 7, 34, 576, 65, 575, 65, 575, 66, 17, 6, 551, 67, 12, 11, 550, 68, 3, 19, 550, 91, 549, 90, 550, 90, 550, 90, 550, 90, 550, 90, 550, 90, 550, 89, 550, 90, 550, 90, 548, 92, 539, 100, 540, 100, 540, 100, 540, 100, 540, 99, 541, 99, 542, 98, 542, 98, 542, 97, 543, 97, 496, 2, 46, 95, 496, 5, 44, 95, 496, 6, 14, 8, 21, 95, 496, 7, 10, 13, 19, 94, 497, 9, 4, 19, 17, 94, 496, 35, 15, 94, 496, 38, 11, 94, 497, 45, 3, 95, 496, 144, 496, 144, 487, 3, 3, 147, 485, 154, 485, 155, 485, 155, 485, 154, 486, 154, 486, 154, 486, 153, 487, 153, 488, 152, 488, 152, 487, 153, 487, 153, 486, 153, 487, 153, 487, 153, 487, 153, 487, 153, 488, 152, 488, 152, 489, 151, 489, 151, 489, 151, 489, 151, 489, 151, 489, 150, 490, 150, 489, 151, 488, 152, 488, 151, 489, 151, 489, 151, 490, 150, 490, 150, 491, 149, 491, 148, 492, 148, 493, 147, 492, 148, 492, 147, 493, 147, 493, 147, 493, 147, 492, 148, 492, 148, 491, 148, 490, 150, 488, 152, 485, 155, 471, 169, 469, 171, 469, 171, 469, 171, 468, 171, 469, 171, 469, 170, 469, 171, 468, 171, 467, 172, 468, 171, 469, 170, 470, 169, 472, 168, 472, 167, 474, 166, 474, 166, 474, 166, 474, 166, 474, 166, 474, 166, 474, 166, 474, 167, 474, 166, 474, 166, 474, 167, 473, 167, 472, 169, 470, 171, 468, 173, 466, 175, 464, 176, 463, 178, 462, 179, 462, 178, 463, 178, 463, 178, 464, 176, 466, 175, 469, 172, 470, 170, 472, 169, 473, 168, 474, 166, 476, 165, 477, 164, 479, 161, 482, 159, 482, 158, 484, 157, 485, 155, 486, 155, 486, 154, 487, 154, 488, 152, 489, 152, 488, 152, 488, 153, 487, 154, 486, 154, 485, 156, 483, 158, 481, 159, 480, 160, 478, 163, 475, 166, 471, 169, 467, 174, 455, 185, 454, 187, 454, 186, 458, 182, 460, 181, 461, 180, 463, 177, 465, 176, 465, 175, 465, 176, 464, 177, 463, 177, 462, 179, 461, 179, 460, 180, 460, 180, 460, 167, 3, 10, 459, 166, 8, 6, 460, 166, 474, 165, 475, 164, 475, 165, 474, 165, 475, 165, 474, 166, 472, 168, 470, 145, 10, 14, 469, 146, 15, 10, 468, 24, 2, 120, 20, 4, 470, 18, 18, 110, 492, 14, 27, 106, 490, 12, 36, 102, 489, 9, 42, 100, 540, 99, 542, 98, 542, 98, 542, 98, 542, 98, 541, 99, 541, 99, 540, 47, 6, 47, 540, 45, 8, 47, 539, 44, 10, 47, 539, 42, 13, 46, 539, 40, 15, 46, 538, 39, 17, 46, 538, 37, 19, 46, 538, 36, 20, 46, 538, 35, 20, 46, 539, 33, 22, 46, 538, 33, 23, 46, 538, 32, 24, 46, 538, 31, 25, 46, 537, 31, 25, 47, 536, 30, 27, 46, 537, 29, 28, 46, 536, 29, 28, 47, 536, 28, 29, 47, 535, 28, 30, 46, 536, 27, 31, 46, 536, 26, 31, 46, 536, 26, 32, 46, 536, 26, 32, 46, 535, 26, 33, 45, 536, 25, 34, 45, 536, 24, 35, 44, 538, 4, 7, 11, 36, 43, 552, 7, 37, 43, 597, 42, 598, 40, 600, 40, 600, 39, 601, 38, 602, 37, 602, 38, 602, 37, 603, 37, 603, 36, 604, 36, 603, 37, 603, 36, 604, 36, 603, 37, 603, 36, 603, 37, 601, 38, 599, 41, 596, 43, 595, 45, 592, 47, 590, 50, 588, 51, 587, 52, 586, 54, 585, 54, 585, 55, 583, 57, 582, 58, 580, 60, 579, 61, 578, 62, 577, 63, 577, 63, 576, 64, 575, 65, 574, 66, 574, 66, 573, 67, 573, 67, 572, 68, 572, 68, 572, 68, 571, 69, 571, 69, 570, 71, 569, 71, 569, 71, 569, 71, 569, 71, 569, 71, 569, 71, 569, 72, 568, 72, 569, 71, 570, 70, 570, 70, 570, 70, 571, 69, 571, 69, 572, 69, 571, 69, 571, 69, 571, 69, 572, 68, 572, 68, 572, 68, 572, 68, 573, 67, 573, 67, 573, 67, 573, 67, 574, 65, 575, 65, 575, 65, 576, 64, 576, 64, 577, 62, 578, 62, 578, 62, 579, 61, 579, 61, 579, 60, 580, 60, 580, 60, 580, 60, 580, 60, 581, 58, 582, 57, 584, 55, 586, 52, 589, 49, 593, 1, 7, 36, 611, 12, 1805],
"mask3counts": [88568, 9, 630, 17, 622, 25, 614, 29, 610, 35, 604, 38, 601, 40, 599, 42, 597, 45, 595, 47, 593, 48, 592, 50, 590, 52, 588, 53, 586, 55, 585, 56, 584, 57, 583, 58, 581, 59, 581, 60, 579, 61, 579, 62, 577, 63, 577, 63, 577, 62, 578, 62, 578, 62, 578, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 578, 62, 578, 62, 577, 63, 577, 63, 576, 64, 576, 64, 575, 65, 575, 65, 575, 65, 575, 65, 574, 66, 574, 67, 572, 69, 571, 69, 570, 70, 570, 70, 569, 71, 569, 72, 567, 72, 567, 26, 2, 45, 566, 27, 3, 44, 566, 27, 4, 7, 2, 33, 566, 27, 5, 7, 3, 31, 567, 27, 5, 6, 5, 27, 570, 27, 5, 6, 5, 16, 580, 28, 6, 4, 7, 13, 581, 28, 9, 1, 11, 4, 587, 27, 612, 26, 614, 24, 616, 23, 617, 23, 617, 22, 618, 21, 619, 21, 619, 21, 619, 20, 620, 19, 621, 18, 621, 17, 624, 14, 627, 11, 166167],
"mask4counts": [97625, 3, 634, 9, 630, 13, 626, 15, 624, 18, 619, 23, 615, 26, 613, 27, 611, 30, 607, 33, 605, 35, 605, 36, 602, 38, 599, 42, 597, 44, 595, 45, 595, 46, 594, 47, 593, 48, 593, 47, 596, 45, 599, 41, 601, 39, 601, 39, 601, 40, 601, 39, 601, 39, 601, 39, 601, 40, 600, 40, 600, 40, 600, 40, 600, 40, 600, 41, 598, 42, 597, 43, 596, 44, 595, 45, 595, 45, 595, 45, 594, 46, 594, 46, 595, 45, 595, 45, 596, 44, 596, 44, 596, 44, 596, 44, 596, 44, 596, 44, 597, 43, 597, 43, 597, 43, 597, 43, 597, 43, 597, 43, 597, 43, 598, 42, 601, 39, 602, 38, 603, 38, 602, 38, 602, 38, 602, 38, 602, 38, 603, 37, 603, 37, 603, 37, 604, 36, 604, 36, 605, 35, 605, 35, 605, 35, 606, 34, 606, 35, 606, 34, 607, 33, 608, 32, 609, 31, 610, 30, 611, 28, 613, 27, 613, 27, 615, 24, 618, 21, 155790],
"mask5counts": [253558, 2, 638, 3, 636, 5, 635, 5, 634, 6, 633, 7, 624, 16, 622, 19, 618, 23, 6, 4, 602, 39, 599, 42, 597, 44, 595, 45, 593, 47, 591, 50, 589, 51, 587, 53, 585, 56, 584, 56, 583, 57, 581, 59, 580, 60, 580, 60, 579, 62, 577, 63, 576, 64, 576, 64, 575, 65, 575, 66, 574, 66, 573, 67, 573, 67, 572, 69, 571, 69, 571, 69, 571, 69, 570, 70, 570, 70, 570, 70, 570, 71, 570, 70, 570, 70, 571, 69, 571, 69, 572, 68, 572, 68, 573, 67, 573, 67, 574, 66, 574, 67, 573, 67, 573, 67, 574, 66, 574, 66, 574, 66, 574, 66, 574, 66, 574, 66, 575, 65, 575, 65, 575, 64, 576, 64, 576, 64, 577, 63, 577, 63, 577, 63, 577, 63, 578, 62, 578, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 61, 579, 60, 581, 59, 581, 58, 582, 58, 582, 57, 584, 55, 586, 53, 589, 50, 590, 49, 591, 49, 505],
"mask6counts": [33215, 2, 636, 17, 622, 30, 609, 36, 603, 47, 592, 50, 589, 52, 587, 54, 586, 55, 584, 58, 582, 61, 578, 64, 576, 64, 576, 65, 575, 65, 575, 65, 576, 63, 577, 63, 579, 61, 580, 59, 582, 24, 14, 20, 582, 23, 17, 17, 585, 20, 20, 14, 590, 15, 24, 10, 593, 12, 629, 10, 632, 7, 257332],
"mask7counts": [207308, 11, 628, 14, 627, 16, 624, 25, 615, 29, 611, 30, 611, 31, 609, 35, 606, 35, 605, 36, 606, 34, 607, 34, 607, 34, 607, 34, 608, 33, 609, 31, 610, 31, 610, 31, 611, 30, 612, 28, 613, 27, 614, 26, 617, 23, 618, 22, 619, 21, 621, 18, 626, 12, 630, 10, 632, 7, 81931],
"mask8counts": [121856, 6, 631, 12, 627, 14, 625, 16, 623, 18, 621, 20, 619, 21, 619, 21, 619, 21, 619, 21, 619, 21, 619, 21, 619, 21, 619, 21, 619, 22, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 617, 23, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 618, 22, 619, 21, 619, 21, 619, 22, 619, 21, 619, 21, 619, 21, 619, 22, 618, 22, 618, 22, 618, 22, 619, 21, 619, 20, 620, 9, 632, 5, 635, 2, 145026],
"mask9counts": [207951, 1, 3, 6, 629, 14, 625, 27, 613, 31, 609, 32, 609, 33, 608, 34, 607, 34, 606, 34, 607, 34, 608, 33, 608, 33, 607, 34, 608, 32, 610, 31, 610, 31, 610, 31, 611, 29, 613, 27, 614, 26, 615, 25, 617, 23, 618, 22, 619, 21, 622, 17, 626, 12, 630, 10, 632, 7, 81931],
"mask10counts": [146139, 8, 609, 35, 602, 39, 596, 44, 593, 48, 591, 51, 589, 51, 589, 52, 587, 54, 586, 54, 585, 56, 584, 57, 582, 59, 581, 59, 580, 61, 580, 60, 580, 61, 579, 62, 578, 62, 579, 61, 580, 60, 580, 60, 580, 60, 580, 60, 581, 58, 582, 57, 583, 45, 596, 38, 602, 35, 606, 32, 609, 24, 616, 14, 627, 10, 631, 5, 139966]}