COCO Model Eval
This commit is contained in:
parent
b23ee6d509
commit
99e11b8a20
|
@ -275,6 +275,9 @@ target_link_libraries(GimbalUdpDetectionInfoSender sv_world)
|
||||||
add_executable(EvalFpsOnVideo samples/test/eval_fps_on_video.cpp)
|
add_executable(EvalFpsOnVideo samples/test/eval_fps_on_video.cpp)
|
||||||
target_link_libraries(EvalFpsOnVideo sv_world)
|
target_link_libraries(EvalFpsOnVideo sv_world)
|
||||||
|
|
||||||
|
add_executable(EvalModelOnCocoVal samples/test/eval_mAP_on_coco_val/eval_mAP_on_coco_val.cpp)
|
||||||
|
target_link_libraries(EvalModelOnCocoVal sv_world)
|
||||||
|
|
||||||
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/samples/calib)
|
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/samples/calib)
|
||||||
add_executable(CameraCalibrarion samples/calib/calibrate_camera_charuco.cpp)
|
add_executable(CameraCalibrarion samples/calib/calibrate_camera_charuco.cpp)
|
||||||
target_link_libraries(CameraCalibrarion ${OpenCV_LIBS})
|
target_link_libraries(CameraCalibrarion ${OpenCV_LIBS})
|
||||||
|
|
|
@ -0,0 +1,37 @@
|
||||||
|
from pycocotools.coco import COCO
|
||||||
|
from pycocotools.cocoeval import COCOeval
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
path = os.path.abspath(os.path.join(os.getcwd(),"../../.."))
|
||||||
|
pred_json = 'pd_coco.json'
|
||||||
|
anno_json = path + '/val2017/instances_val2017.json'
|
||||||
|
|
||||||
|
# use COCO API to load forecast results and annotations
|
||||||
|
cocoGt = COCO(anno_json)
|
||||||
|
with open(pred_json,'r') as file:
|
||||||
|
data = json.load(file)
|
||||||
|
|
||||||
|
# align anno_json with pred_json category_id
|
||||||
|
gtCatDicts = {}
|
||||||
|
for anns in range(len(cocoGt.getCatIds())):
|
||||||
|
gtCatDicts[anns] = cocoGt.getCatIds()[anns]
|
||||||
|
|
||||||
|
pdCatIds=list(set([d['category_id'] for d in data]))
|
||||||
|
|
||||||
|
if not set(pdCatIds).issubset(set(cocoGt.getCatIds())):
|
||||||
|
for ins in data:
|
||||||
|
temp = int(gtCatDicts[ins['category_id']])
|
||||||
|
ins['category_id'] = temp
|
||||||
|
|
||||||
|
# load prediction results
|
||||||
|
cocoDt = cocoGt.loadRes(data)
|
||||||
|
|
||||||
|
# create COCO eval object
|
||||||
|
cocoEval = COCOeval(cocoGt, cocoDt,'bbox')
|
||||||
|
|
||||||
|
# assessment
|
||||||
|
cocoEval.evaluate()
|
||||||
|
cocoEval.accumulate()
|
||||||
|
cocoEval.summarize()
|
|
@ -0,0 +1,94 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
// 包含SpireCV SDK头文件
|
||||||
|
#include <sv_world.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace cv;
|
||||||
|
|
||||||
|
//extract name
|
||||||
|
std::string GetImageFileName(const std::string& imagePath) {
|
||||||
|
size_t lastSlash = imagePath.find_last_of("/\\");
|
||||||
|
if (lastSlash == std::string::npos) {
|
||||||
|
return imagePath;
|
||||||
|
} else {
|
||||||
|
std::string fileName = imagePath.substr(lastSlash + 1);
|
||||||
|
size_t lastDot = fileName.find_last_of(".");
|
||||||
|
if (lastDot != std::string::npos) {
|
||||||
|
return fileName.substr(0, lastDot);
|
||||||
|
}
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
// 实例化 通用目标 检测器类
|
||||||
|
sv::CommonObjectDetector cod;
|
||||||
|
// 手动导入相机参数,如果使用Amov的G1等吊舱或相机,则可以忽略该步骤,将自动下载相机参数文件
|
||||||
|
cod.loadCameraParams(sv::get_home() + "/SpireCV/calib_webcam_640x480.yaml");
|
||||||
|
|
||||||
|
//load data
|
||||||
|
string val_path = sv::get_home() + "/SpireCV/val2017/val2017";
|
||||||
|
vector<string> val_image;
|
||||||
|
glob(val_path, val_image, false);
|
||||||
|
if (val_image.size() == 0)
|
||||||
|
{
|
||||||
|
printf("val_image error!!!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//preds folder
|
||||||
|
std::string folder = sv::get_home() + "/SpireCV/val2017/preds";
|
||||||
|
int checkStatus = std::system(("if [ -d \"" + folder + "\" ]; then echo; fi").c_str());
|
||||||
|
if(checkStatus == 0)
|
||||||
|
{
|
||||||
|
int removeStatus = std::system(("rm -rf \"" + folder + "\"").c_str());
|
||||||
|
if(removeStatus != 0)
|
||||||
|
{
|
||||||
|
printf("remove older preds folder error!!!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int status = std::system(("mkdir \""+folder+"\"").c_str());
|
||||||
|
if(status != 0)
|
||||||
|
{
|
||||||
|
printf("create preds folder error!!!\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (int i = 0; i < val_image.size(); i++) {
|
||||||
|
|
||||||
|
//create pred file
|
||||||
|
std::string val_image_name = GetImageFileName(val_image[i]);
|
||||||
|
std::string filename = folder+"/"+ val_image_name + ".txt";
|
||||||
|
std::ofstream file(filename);
|
||||||
|
file.is_open();
|
||||||
|
file<<std::fixed<<std::setprecision(6);
|
||||||
|
|
||||||
|
// 实例化SpireCV的 单帧检测结果 接口类 TargetsInFrame
|
||||||
|
sv::TargetsInFrame tgts(i);
|
||||||
|
cv::Mat img = imread(val_image[i]);
|
||||||
|
|
||||||
|
int rows = img.rows;
|
||||||
|
int cols = img.cols;
|
||||||
|
|
||||||
|
// 执行通用目标检测
|
||||||
|
cod.detect(img, tgts);
|
||||||
|
// 可视化检测结果,叠加到img上
|
||||||
|
sv::drawTargetsInFrame(img, tgts);
|
||||||
|
|
||||||
|
// reslusts
|
||||||
|
for (int j = 0; j < tgts.targets.size(); j++)
|
||||||
|
{
|
||||||
|
sv::Box b;
|
||||||
|
tgts.targets[j].getBox(b);
|
||||||
|
file<<tgts.targets[j].category_id<<" "<<(float)(b.x1+b.x2)/(2*cols)<<" "<<(float)(b.y1+b.y2)/(2*rows)<<" "<<(float)(b.x2-b.x1)/cols<<" "<<(float)(b.y2-b.y1)/rows<<" "<<(float)tgts.targets[j].score<<"\n";
|
||||||
|
}
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
import datetime
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
# revert prediction results to coco_json
|
||||||
|
path = os.path.abspath(os.path.join(os.getcwd(),"../../.."))
|
||||||
|
|
||||||
|
# all files dir
|
||||||
|
images_path = path+'/val2017/val2017'
|
||||||
|
preds_path = path+'/val2017/preds'
|
||||||
|
coco_json_save ='pd_coco.json'
|
||||||
|
|
||||||
|
# config coco_json
|
||||||
|
coco_json = []
|
||||||
|
|
||||||
|
# load images dir
|
||||||
|
images = os.listdir(images_path)
|
||||||
|
for image in images:
|
||||||
|
# get image name
|
||||||
|
image_name, image_suffix = os.path.splitext(image)
|
||||||
|
# get image W and H
|
||||||
|
image_path = images_path + '/' + image
|
||||||
|
img = cv2.imread(image_path)
|
||||||
|
height, width, _ = img.shape
|
||||||
|
|
||||||
|
# read pred's txt
|
||||||
|
pred_path = preds_path + '/' + image_name + '.txt'
|
||||||
|
if not os.path.exists(pred_path):
|
||||||
|
continue
|
||||||
|
with open(pred_path, 'r') as f:
|
||||||
|
preds = f.readlines()
|
||||||
|
preds = [l.strip() for l in preds]
|
||||||
|
for j,pred in enumerate(preds):
|
||||||
|
pred = pred.split(' ')
|
||||||
|
category_id = int(pred[0])
|
||||||
|
x = float(pred[1]) * width
|
||||||
|
y = float(pred[2]) * height
|
||||||
|
w = float(pred[3]) * width
|
||||||
|
h = float(pred[4]) * height
|
||||||
|
xmin = x - w / 2
|
||||||
|
ymin = y - h / 2
|
||||||
|
|
||||||
|
coco_json.append({
|
||||||
|
'image_id': int(image_name),
|
||||||
|
'category_id': int(category_id),
|
||||||
|
'bbox': [xmin, ymin, w, h],
|
||||||
|
'score': float(pred[5])})
|
||||||
|
|
||||||
|
# save json
|
||||||
|
with open(os.path.join(coco_json_save), 'w') as f:
|
||||||
|
json.dump(coco_json, f, indent=2)
|
||||||
|
print(len(coco_json), 'Done!')
|
Loading…
Reference in New Issue