coco MAP测试

1.根据TXT结果,json文件生成。

#!/usr/bin/env python
# coding=utf-8
# Copyright 2017 challenger.ai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Evaluation utility for human skeleton system keypoint task.

This python script is used for calculating the final score (mAP) of the test result,

based on your submited file and the reference file containing ground truth.

usage

python keypoint_eval.py --submit SUBMIT_FILEPATH --ref REF_FILEPATH

A test case is provided, submited file is submit.json, reference file is ref.json, test it by:

python keypoint_eval.py --submit ./keypoint_sample_predictions.json \
                        --ref ./keypoint_sample_annotations.json

The final score of the submited result, error message and warning message will be printed.
"""

import json
import time
import argparse
import pprint, sys
from glob2 import glob

import numpy as np
from pycocotools.coco import COCO
import pylab
import os
import csv

save_json = r'F:\f\f\prediction_4.json'
file_name = r'F:\f\f\pre_coco_test\*.txt'

if __name__ == "__main__":
    predictions = []
    for i in glob(file_name):
        file_object = open(i, 'rU')
        human_dict = {}
        temp = 1
        keypoint_ls = []
        img_id = 0
        #get img_id
        for line in file_object:
            person = 0
            #TODO
            if len(line) == 1:
                continue
            line = line.replace('\n','')
            line = line.replace(' ','')
            human_ls = line.split(',')
            keypoint_ls.append(float(human_ls[1]))
            keypoint_ls.append(float(human_ls[2]))
            if human_ls[1] == '0' and human_ls[2] == '0':
                keypoint_ls.append(2)
            else:
                keypoint_ls.append(2)
            img_id = int(i[-10: -4])
            temp +=1
        temp1 = 1
        point_ls = []
        # get keypoint_ls
        for kp in keypoint_ls:
            if temp1 == 54*(person+1)+1:
                point_ls = []
                person += 1
            point_ls.append(kp)
            temp1 += 1
            human_dict['human'+str(person)] = point_ls
        for per in human_dict:
            prediction = {}
            anno_keypoint = np.reshape(human_dict[per], (18, 3))
            anno_keypoint = np.delete(anno_keypoint, 1, 0)  # 删除A的第二行,剩17行()
            anno_keypoint_copy = anno_keypoint.copy()
            # 15-2
            anno_keypoint[1] = anno_keypoint_copy[14]
            # 14-3
            anno_keypoint[2] = anno_keypoint_copy[13]
            # 17-4
            anno_keypoint[3] = anno_keypoint_copy[16]
            # 16-5
            anno_keypoint[4] = anno_keypoint_copy[15]
            # 5-6
            anno_keypoint[5] = anno_keypoint_copy[4]
            # 2-7
            anno_keypoint[6] = anno_keypoint_copy[1]
            # 6-8
            anno_keypoint[7] = anno_keypoint_copy[5]
            # 3-9
            anno_keypoint[8] = anno_keypoint_copy[2]
            # 7-10
            anno_keypoint[9] = anno_keypoint_copy[6]
            # 4-11
            anno_keypoint[10] = anno_keypoint_copy[3]
            # 11-12
            anno_keypoint[11] = anno_keypoint_copy[10]
            # 8-13
            anno_keypoint[12] = anno_keypoint_copy[7]
            # 12-14
            anno_keypoint[13] = anno_keypoint_copy[11]
            # 9-15
            anno_keypoint[14] = anno_keypoint_copy[8]
            # 13-16
            anno_keypoint[15] = anno_keypoint_copy[12]
            # 10-17
            anno_keypoint[16] = anno_keypoint_copy[9]
            temp_ls = anno_keypoint.tolist()
            # temp_str = "".join(anno_keypoint.tolist())
            temp_str = str(temp_ls)
            temp_str = temp_str.replace("[",',')
            temp_str = temp_str.replace(']',',')
            temp_str = temp_str.replace(',, ,',',')
            temp_str = temp_str.replace(',,','')
            temp_ls = temp_str.split(',')
            temp_ls = map(float, temp_ls)
            prediction['keypoints'] = temp_ls
            prediction['image_id'] = img_id
            prediction['score'] = 1
            prediction['category_id'] = 1
            predictions.append(prediction)
        # print ls_human
    jsobj = json.dumps(predictions)
    fileObject = open(save_json, 'w')
    fileObject.write(jsobj)
    fileObject.close()
    # 原版========
    # # 15-2
    # anno_keypoint[1] = anno_keypoint_copy[14]
    # # 14-3
    # anno_keypoint[2] = anno_keypoint_copy[13]
    # # 17-4
    # anno_keypoint[3] = anno_keypoint_copy[16]
    # # 16-5
    # anno_keypoint[4] = anno_keypoint_copy[15]
    # # 5-6
    # anno_keypoint[5] = anno_keypoint_copy[4]
    # # 2-7
    # anno_keypoint[6] = anno_keypoint_copy[1]
    # # 6-8
    # anno_keypoint[7] = anno_keypoint_copy[5]
    # # 3-9
    # anno_keypoint[8] = anno_keypoint_copy[2]
    # # 7-10
    # anno_keypoint[9] = anno_keypoint_copy[6]
    # # 4-11
    # anno_keypoint[10] = anno_keypoint_copy[3]
    # # 11-12
    # anno_keypoint[11] = anno_keypoint_copy[10]
    # # 8-13
    # anno_keypoint[12] = anno_keypoint_copy[7]
    # # 12-14
    # anno_keypoint[13] = anno_keypoint_copy[11]
    # # 9-15
    # anno_keypoint[14] = anno_keypoint_copy[8]
    # # 13-16
    # anno_keypoint[15] = anno_keypoint_copy[12]
    # # 10-17
    # anno_keypoint[16] = anno_keypoint_copy[9]

2.测试

#-*- coding:utf-8 -*-
import matplotlib.pyplot as plt
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
import numpy as np
import skimage.io as io
import pylab,json
pylab.rcParams['figure.figsize'] = (10.0, 8.0)
def get_img_id(file_name):
    ls = []
    myset = []
    annos = json.load(open(file_name, 'r'))
    for anno in annos:
        ls.append(anno['image_id'])
    myset = {}.fromkeys(ls).keys()
    return myset
if __name__ == '__main__':
    annType = ['segm', 'bbox', 'keypoints']#set iouType to 'segm', 'bbox' or 'keypoints'
    annType = annType[2]  # specify type here
    cocoGt_file = r"F:\coco_data\person_keypoints_val2014.json"#标注集json文件
    cocoGt = COCO(cocoGt_file)#取得标注集中coco json对象
    cocoDt_file = r"F:\f\f\prediction_4.json"#结果json文件
    imgIds = get_img_id(cocoDt_file)
    print len(imgIds)
    cocoDt = cocoGt.loadRes(cocoDt_file)#取得结果集中image json对象
    imgIds = sorted(imgIds)#按顺序排列coco标注集image_id
    imgIds = imgIds[0:100]#标注集中的image数据一百个一组
    # imgId = imgIds[np.random.randint(100)]#numpy.random.randint(low,high=None,size=None,dtype)生成在半开半闭区间[low,high)上离散均匀分布的整数值;high=None,则取值区间变为[0,low)
    cocoEval = COCOeval(cocoGt, cocoDt, annType)
    cocoEval.params.imgIds = imgIds#参数设置
    cocoEval.evaluate()#评价
    cocoEval.accumulate()#积累
    cocoEval.summarize()#总结
    # =====================

猜你喜欢

转载自blog.csdn.net/qq_34568522/article/details/80564095