#include "StdAfx.h" #include "SchemaLoader.h" #include "..\EvaluationUtil\HttpClient.h" #include "..\ZLibWrapMemLib\UnZipFile.h" #include "..\Schema\schema_struct.h" #include "..\Identifier\schema_struct.h" #include #include #include "..\Schema\schema.h" #include "common.h" using namespace cv; using namespace schema; using namespace cv::xfeatures2d; CSchemaLoader::CSchemaLoader(void) { } CSchemaLoader::~CSchemaLoader(void) { } /* 初始化模板 */ void CSchemaLoader::SetPageDefault(identify::schema::ISCH_SCHEMA_PAGE& page) { //page.group_spacer =';'; page.hei_du_ling_min_du =4; page.height =0; page.index =0; page.option_spacer =','; //page.unselect_char='*'; page.width=0; } bool sort_string(ISCH_SCHEMA_CLIP x1, ISCH_SCHEMA_CLIP x2){ if (atoi(x1.area_name_by_questionNo.c_str()) != atoi(x2.area_name_by_questionNo.c_str())) return atoi(x1.area_name_by_questionNo.c_str()) < atoi(x2.area_name_by_questionNo.c_str()); else return x1.markUnitPart < x2.markUnitPart; } // 加载模板信息 int CSchemaLoader::Load(std::string fileName, std::string muban_img_dir, schema::SCHEMA& schema_schema, boost::shared_ptr & schema_identify, boost::shared_ptr& handleInfos) { boost::shared_ptr handleInfos_0(new HANDLE_INFO); HANDLE_INFO & handleInfos_ = *handleInfos_0; CUnZipFile uzip(CString(fileName.c_str())); std::map> map; while(uzip.HasMoreEntry()){ CString fileNam; uzip.GetNextEntry(fileNam); char buffer[1024*8]; int len; std::vector& data =map[fileNam]; data.reserve(1024*128); while((len = uzip.Read(buffer,1024*8))>0){ if(data.capacity()>::iterator schemabytes= map.find(_T("schema.bytes")); if(schemabytes == map.end()){ return SCH_LOAD_ERR_MISSINGFORMAT; } SCHEMA_VERSION version; std::vector& data =schemabytes->second; serializor::SchemJsonSerializor::deserialize(std::string((char *)data.data(),data.size()),schema_schema,version); if(version!=CURRENT_SCHEMA_VERSTION)return SCH_LOAD_ERR_VERSIONCHANGE; if(schema_schema.state == 0) return SCH_LOAD_ERR_ZHUANGTAIBUZHENGQUE; boost::shared_ptr schema_identify_t(new ISCH_Schema); std::vector& schema_identify_ = *schema_identify_t; std::vector &questions =schema_schema.questions; handleInfos_.resize(schema_schema.pages.size()); schema_identify_.resize(schema_schema.pages.size()); int j = 0; for (int idx_p=0;idx_pindex = idx_p; page->width =schema_page.width; page->height =schema_page.height; page->locatePoints.resize(schema_page.locatePoints.size()); for (int idx_i = 0; idx_i < schema_page.locatePoints.size();idx_i++) { identify::schema::ISCH_SCHEMA_LOCATE_POINT& dst = page->locatePoints[idx_i]; schema::SCHEMA_LOCATE_POINT& src = schema_page.locatePoints[idx_i]; dst.centerx = src.centerx; dst.centery = src.centery; dst.width = src.width; dst.height = src.height; dst.nID = src.nID; } page->locateCrosses.resize(schema_page.locateCrosses.size()); for (int idx_i = 0; idx_i < schema_page.locateCrosses.size(); idx_i++) { identify::schema::ISCH_SCHEMA_LOACTE_CROSS& dst = page->locateCrosses[idx_i]; schema::SCHEMA_LOACTE_CROSS& src = schema_page.locateCrosses[idx_i]; dst.centerx = src.centerx; dst.centery = src.centery; dst.width = src.width; dst.height = src.height; dst.nID = src.nID; dst.angle = src.angle; dst.sign = src.sign; } std::vector groupsv; vector itemv; int nextItemId=1; handleInfo.page_index = schema_page.index; nextItemId = caulateGroupAndItem(schema_page.quekaobiaoji, nextItemId, itemv, groupsv, handleInfo.quekaoHandleInfo, GROUP_TYPE::QUEKAO); nextItemId = caulateGroupAndItem(schema_page.abjuanbjiaoji, nextItemId, itemv, groupsv, handleInfo.abjuanHandleInfo, GROUP_TYPE::NONE); nextItemId = caulateGroupAndItem(schema_page.tiantukaohao, nextItemId, itemv, groupsv, handleInfo.tiantukaohaoHandleInfo, GROUP_TYPE::TIANTUKAOHAO); nextItemId = caulateGroupAndItem2(schema_page.xuanzuoti, nextItemId, itemv, groupsv, handleInfo.xuanzuotiHandleInfo, GROUP_TYPE::XUANZUOTI); nextItemId = caulateGroupAndItem(schema_page.keguantiMatrix, nextItemId, itemv, groupsv, handleInfo.keguantiHandleInfo, GROUP_TYPE::KEGUANTI); std::map index_id_question; std::map index_questionCode_question; for (int idx_i=0;idx_i questions1; split(ke.outputcharsnew, (string)";", &questions1); for (int index_group=0;index_group questions2; split(questions1[index1],(string)",",&questions2); khi.option_question_code[index_group][index1]=questions2; for (int index2=0;index2& markunit=khi.mark_unit[index_group]; schema::SCHEMA_QUESTION * question = index_questionCode_question[questions2[index2]]; int markunit_index =-1; char szmarkunit[32]; sprintf_s(szmarkunit,"%d",question->nMarkUnit); for (int index_markunit=0;index_markunitquestion_code_new); }else{ khi.mark_unit_question_code[index_group][markunit_index].append(","); khi.mark_unit_question_code[index_group][markunit_index].append(question->question_code_new); } } } } } } page->codes.resize(schema_page.codes.size()); for (int idx_i = 0; idx_i < schema_page.codes.size(); idx_i++) { identify::schema::ISCH_SCHEMA_CODE& dst = page->codes[idx_i]; schema::SCHEMA_CODE& src = schema_page.codes[idx_i]; dst.centerx = src.centerx; dst.centery = src.centery; dst.width = src.width; dst.height = src.height; dst.nID = src.nID; dst.bDirection = (identify::schema::ISCH_DIRECTION)(int)src.bDirection; } page->groups=groupsv; page->items=itemv; page->cutAreas.resize(schema_page.clips.size() + schema_page.clips2.size()); int nClips2 = 0; for (int idx_i = 0; idx_i < schema_page.clips2.size(); idx_i++) { identify::schema::ISCH_SCHEMA_CLIP& dst = page->cutAreas[idx_i]; schema::SCHEMA_CLIP& src = schema_page.clips2[idx_i]; dst.centerx = src.centerx; dst.centery = src.centery; dst.width = src.width; dst.height = src.height; dst.nID = src.nID; dst.area_name = src.area_name; dst.area_name_by_questionNo = src.area_name_by_questionNoNew; dst.markUnit = src.markUnit; dst.markUnitPart = src.markUnitPart; nClips2++; } for (int idx_i = 0; idx_i < schema_page.clips.size(); idx_i++) { identify::schema::ISCH_SCHEMA_CLIP& dst = page->cutAreas[idx_i + nClips2]; schema::SCHEMA_CLIP& src = schema_page.clips[idx_i]; dst.centerx = src.centerx; dst.centery = src.centery; dst.width = src.width; dst.height = src.height; dst.nID = src.nID; dst.area_name = src.area_name; dst.area_name_by_questionNo = src.area_name_by_questionNoNew; dst.markUnit = src.markUnit; dst.markUnitPart = src.markUnitPart; } std::sort(page->cutAreas.begin(), page->cutAreas.end(), sort_string); CString imgName ; imgName.Format(_T("%05d.jpg"),schema_page.nID); vector & fff =map[imgName]; Mat img = imdecode(fff,CV_LOAD_IMAGE_GRAYSCALE); double angle =schema_page.angle; double a = fmod(fmod(angle,360)+360,360); int nWidth,nHeight; if((a>45&&a<=135)||(a>45+180&&a<=135+180)){ nHeight = img.cols; nWidth = img.rows; }else{ nWidth = img.cols; nHeight = img.rows; } page->width = nWidth; page->height = nHeight; double sina = sin(CV_PI*angle/180); double cosa = cos(CV_PI*angle/180); CvPoint2D32f center0 = cvPoint2D32f(img.cols/2.0,img.rows/2.0); CvPoint2D32f center1 = cvPoint2D32f(nWidth/2.0,nHeight/2.0); float offsetx = center1.x - (cosa*center0.x+sina*center0.y); float offsety = center1.y - (-sina*center0.x+cosa*center0.y); float data[6]={cosa,sina,offsetx,-sina,cosa,offsety}; Mat m(2,3,CV_32F,data); Mat img_rotated; warpAffine(img,img_rotated,m,cvSize(nWidth,nHeight)); CString imgFileName; char imgfilename_a [1024]; imgFileName.Format(_T("%s\\%05d_original.jpg"),CString(muban_img_dir.c_str()),idx_p); WideCharToMultiByte(CP_ACP,0,imgFileName,-1,imgfilename_a,1024,NULL,NULL); imwrite(imgfilename_a,img); imgFileName.Format(_T("%s\\%05d.jpg"), CString(muban_img_dir.c_str()), idx_p); WideCharToMultiByte(CP_ACP,0,imgFileName,-1,imgfilename_a,1024,NULL,NULL); imwrite(imgfilename_a,img_rotated); img.release(); IplImage * img_smoothd = &IplImage(img_rotated); cvSmooth(img_smoothd, img_smoothd, CV_GAUSSIAN, 5); caculateLocateArea(img_rotated,schema_page.locateAreas,page->locateAreas); std::vector& assistLocateArea = page->assistLocateArea; caculateLocateArea(img_rotated,schema_page.quekaobiaoji,assistLocateArea); caculateLocateArea(img_rotated,schema_page.abjuanbjiaoji,assistLocateArea); caculateLocateArea(img_rotated,schema_page.tiantukaohao,assistLocateArea); caculateLocateArea(img_rotated,schema_page.keguantiMatrix,assistLocateArea); caculateLocateArea(img_rotated,schema_page.xuanzuoti,assistLocateArea); } schema_identify = schema_identify_t; handleInfos = handleInfos_0; return SCH_LOAD_SUCCESS; } //选做题 template int CSchemaLoader::caulateGroupAndItem2(std::vector &matrix, int nextItemId, vector &itemv, std::vector &groupsv, std::vector& handleInfos, GROUP_TYPE _type) { for (int i=0;i answers; split((std::string)k.outputcharsnew,(std::string)";",&answers); if (k.direction == schema::Horizontal){ for (int m=0;m1?(k.width-(k.optionWidth*k.nCols))/(k.nCols-1):0; double vGap = k.nRows>1?(k.height-(k.optionHeight*k.nRows))/(k.nRows-1):0; item.centerx = (k.centerx-k.width/2)+n*(k.width+hGap)/k.nCols+k.optionWidth/2.0; item.centery = (k.centery-k.height/2)+m*(k.height+vGap)/k.nRows+k.optionHeight/2.0; item.width = k.optionWidth; item.height = k.optionHeight; item.nID = nextItemId++; strcpy_s(item.OutChar,answers[n].c_str()); group.itemIndex.push_back(itemv.size()); itemv.push_back(item); } groupsv.push_back(group); } }else{ for (int n=0;n1?(k.width-(k.optionWidth*k.nCols))/(k.nCols-1):0; double vGap = k.nRows>1?(k.height-(k.optionHeight*k.nRows))/(k.nRows-1):0; item.centerx = (k.centerx-k.width/2)+n*(k.width+hGap)/k.nCols+k.optionWidth/2.0; item.centery = (k.centery-k.height/2)+m*(k.height+vGap)/k.nRows+k.optionHeight/2.0; item.width = k.optionWidth; item.height = k.optionHeight; item.nID = nextItemId++; strcpy_s(item.OutChar,answers[m].c_str()); group.itemIndex.push_back(itemv.size()); itemv.push_back(item); } groupsv.push_back(group); } } handleInfos.push_back(hi); } return nextItemId; } template int CSchemaLoader::caulateGroupAndItem(std::vector &matrix, int nextItemId, vector &itemv, std::vector &groupsv, std::vector& handleInfos, GROUP_TYPE _type) { for (int i=0;i answers; split((std::string)k.outputchars,(std::string)",",&answers); if(k.direction == schema::Horizontal){ for (int m=0;m1?(k.width-(k.optionWidth*k.nCols))/(k.nCols-1):0; double vGap = k.nRows>1?(k.height-(k.optionHeight*k.nRows))/(k.nRows-1):0; item.centerx = (k.centerx-k.width/2)+n*(k.width+hGap)/k.nCols+k.optionWidth/2.0; item.centery = (k.centery-k.height/2)+m*(k.height+vGap)/k.nRows+k.optionHeight/2.0; item.width = k.optionWidth; item.height = k.optionHeight; item.nID = nextItemId++; strcpy_s(item.OutChar,answers[n].c_str()); group.itemIndex.push_back(itemv.size()); itemv.push_back(item); } groupsv.push_back(group); } }else{ for (int n=0;n1?(k.width-(k.optionWidth*k.nCols))/(k.nCols-1):0; double vGap = k.nRows>1?(k.height-(k.optionHeight*k.nRows))/(k.nRows-1):0; item.centerx = (k.centerx-k.width/2)+n*(k.width+hGap)/k.nCols+k.optionWidth/2.0; item.centery = (k.centery-k.height/2)+m*(k.height+vGap)/k.nRows+k.optionHeight/2.0; item.width = k.optionWidth; item.height = k.optionHeight; item.nID = nextItemId++; strcpy_s(item.OutChar,answers[m].c_str()); group.itemIndex.push_back(itemv.size()); itemv.push_back(item); } groupsv.push_back(group); } } handleInfos.push_back(hi); } return nextItemId; }