This commit is contained in:
jackyu1127
2020-07-30 17:42:13 +08:00
parent df5b1292d7
commit 24ecdd93ad
79 changed files with 900 additions and 16632 deletions
@@ -0,0 +1,34 @@
//
// Created by 庾金科 on 20/09/2017.
//
#include <../include/PlateDetection.h>
void drawRect(cv::Mat image,cv::Rect rect)
{
cv::Point p1(rect.x,rect.y);
cv::Point p2(rect.x+rect.width,rect.y+rect.height);
cv::rectangle(image,p1,p2,cv::Scalar(0,255,0),1);
}
int main()
{
cv::Mat image = cv::imread("res/test1.jpg");
pr::PlateDetection plateDetection("model/cascade.xml");
std::vector<pr::PlateInfo> plates;
plateDetection.plateDetectionRough(image,plates);
for(pr::PlateInfo platex:plates)
{
drawRect(image,platex.getPlateRect());
cv::imwrite("res/cache/test.png",platex.getPlateImage());
cv::imshow("image",platex.getPlateImage());
cv::waitKey(0);
}
cv::imshow("image",image);
cv::waitKey(0);
return 0 ;
}
@@ -0,0 +1,34 @@
//
// Created by Jack Yu on 02/10/2017.
//
#include <../include/FastDeskew.h>
void drawRect(cv::Mat image,cv::Rect rect)
{
cv::Point p1(rect.x,rect.y);
cv::Point p2(rect.x+rect.width,rect.y+rect.height);
cv::rectangle(image,p1,p2,cv::Scalar(0,255,0),1);
}
void TEST_DESKEW(){
cv::Mat image = cv::imread("res/3.png",cv::IMREAD_GRAYSCALE);
// cv::resize(image,image,cv::Size(136*2,36*2));
cv::Mat deskewed = pr::fastdeskew(image,12);
// cv::imwrite("./res/4.png",deskewed);
// cv::Mat deskewed2 = pr::fastdeskew(deskewed,12);
//
cv::imshow("image",deskewed);
cv::waitKey(0);
}
int main()
{
TEST_DESKEW();
return 0 ;
}
@@ -0,0 +1,25 @@
//
// Created by Jack Yu on 24/09/2017.
//
#include "FineMapping.h"
int main()
{
cv::Mat image = cv::imread("res/cache/test.png");
cv::Mat image_finemapping = pr::FineMapping::FineMappingVertical(image);
pr::FineMapping finemapper = pr::FineMapping("model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel");
image_finemapping = finemapper.FineMappingHorizon(image_finemapping,0,-3);
cv::imwrite("res/cache/finemappingres.png",image_finemapping);
cv::imshow("image",image_finemapping);
cv::waitKey(0);
return 0 ;
}
+199
View File
@@ -0,0 +1,199 @@
//
// Created by Jack Yu on 23/10/2017.
//
#include "../include/Pipeline.h"
using namespace std;
template<class T>
static unsigned int levenshtein_distance(const T &s1, const T &s2) {
const size_t len1 = s1.size(), len2 = s2.size();
std::vector<unsigned int> col(len2 + 1), prevCol(len2 + 1);
for (unsigned int i = 0; i < prevCol.size(); i++) prevCol[i] = i;
for (unsigned int i = 0; i < len1; i++) {
col[0] = i + 1;
for (unsigned int j = 0; j < len2; j++)
col[j + 1] = min(
min(prevCol[1 + j] + 1, col[j] + 1),
prevCol[j] + (s1[i] == s2[j] ? 0 : 1));
col.swap(prevCol);
}
return prevCol[len2];
}
void TEST_ACC(){
pr::PipelinePR prc("model/cascade.xml",
"model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel",
"model/Segmentation.prototxt","model/Segmentation.caffemodel",
"model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel",
"model/SegmenationFree-Inception.prototxt","model/SegmenationFree-Inception.caffemodel"
);
ifstream file;
string imagename;
int n = 0,correct = 0,j = 0,sum = 0;
char filename[] = "/Users/yujinke/Downloads/general_test/1.txt";
string pathh = "/Users/yujinke/Downloads/general_test/";
file.open(filename, ios::in);
while (!file.eof())
{
file >> imagename;
string imgpath = pathh + imagename;
std::cout << "------------------------------------------------" << endl;
cout << "图片名:" << imagename << endl;
cv::Mat image = cv::imread(imgpath);
// cv::imshow("image", image);
// cv::waitKey(0);
std::vector<pr::PlateInfo> res = prc.RunPiplineAsImage(image,pr::SEGMENTATION_FREE_METHOD);
float conf = 0;
vector<float> con ;
vector<string> name;
for (auto st : res) {
if (st.confidence > 0.1) {
//std::cout << st.getPlateName() << " " << st.confidence << std::endl;
con.push_back(st.confidence);
name.push_back(st.getPlateName());
//conf += st.confidence;
}
else
cout << "no string" << endl;
}
// std::cout << conf << std::endl;
int num = con.size();
float max = 0;
string platestr, chpr, ch;
int diff = 0,dif = 0;
for (int i = 0; i < num; i++) {
if (con.at(i) > max)
{
max = con.at(i);
platestr = name.at(i);
}
}
// cout << "max:"<<max << endl;
cout << "string:" << platestr << endl;
chpr = platestr.substr(0, 2);
ch = imagename.substr(0, 2);
diff = levenshtein_distance(imagename, platestr);
dif = diff - 4;
cout << "差距:" <<dif << endl;
sum += dif;
if (ch != chpr) n++;
if (diff == 0) correct++;
j++;
}
float cha = 1 - float(n) / float(j);
std::cout << "------------------------------------------------" << endl;
cout << "车牌总数:" << j << endl;
cout << "汉字识别准确率:"<<cha << endl;
float chaccuracy = 1 - float(sum - n * 2) /float(j * 8);
cout << "字符识别准确率:" << chaccuracy << endl;
}
void TEST_PIPELINE(){
pr::PipelinePR prc("model/cascade.xml",
"model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel",
"model/Segmentation.prototxt","model/Segmentation.caffemodel",
"model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel",
"model/SegmenationFree-Inception.prototxt","model/SegmenationFree-Inception.caffemodel"
);
cv::Mat image = cv::imread("/Users/yujinke/ClionProjects/cpp_ocr_demo/test.png");
std::vector<pr::PlateInfo> res = prc.RunPiplineAsImage(image,pr::SEGMENTATION_FREE_METHOD);
for(auto st:res) {
if(st.confidence>0.75) {
std::cout << st.getPlateName() << " " << st.confidence << std::endl;
cv::Rect region = st.getPlateRect();
cv::rectangle(image,cv::Point(region.x,region.y),cv::Point(region.x+region.width,region.y+region.height),cv::Scalar(255,255,0),2);
}
}
cv::imshow("image",image);
cv::waitKey(0);
}
void TEST_CAM()
{
cv::VideoCapture capture("test1.mp4");
cv::Mat frame;
pr::PipelinePR prc("model/cascade.xml",
"model/HorizonalFinemapping.prototxt","model/HorizonalFinemapping.caffemodel",
"model/Segmentation.prototxt","model/Segmentation.caffemodel",
"model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel",
"model/SegmentationFree.prototxt","model/SegmentationFree.caffemodel"
);
while(1) {
//读取下一帧
if (!capture.read(frame)) {
std::cout << "读取视频失败" << std::endl;
exit(1);
}
//
// cv::transpose(frame,frame);
// cv::flip(frame,frame,2);
// cv::resize(frame,frame,cv::Size(frame.cols/2,frame.rows/2));
std::vector<pr::PlateInfo> res = prc.RunPiplineAsImage(frame,pr::SEGMENTATION_FREE_METHOD);
for(auto st:res) {
if(st.confidence>0.75) {
std::cout << st.getPlateName() << " " << st.confidence << std::endl;
cv::Rect region = st.getPlateRect();
cv::rectangle(frame,cv::Point(region.x,region.y),cv::Point(region.x+region.width,region.y+region.height),cv::Scalar(255,255,0),2);
}
}
cv::imshow("image",frame);
cv::waitKey(1);
}
}
int main()
{
TEST_ACC();
// TEST_CAM();
// TEST_PIPELINE();
return 0 ;
}
@@ -0,0 +1,54 @@
//
// Created by Jack Yu on 23/10/2017.
//
#include "../include/CNNRecognizer.h"
std::vector<std::string> chars{"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};
#include <opencv2/dnn.hpp>
using namespace cv::dnn;
void getMaxClass(cv::Mat &probBlob, int *classId, double *classProb)
{
// cv::Mat probMat = probBlob.matRefConst().reshape(1, 1); //reshape the blob to 1x1000 matrix
cv::Point classNumber;
cv::minMaxLoc(probBlob, NULL, classProb, NULL, &classNumber);
*classId = classNumber.x;
}
void TEST_RECOGNIZATION(){
// pr::CNNRecognizer instance("model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel");
Net net = cv::dnn::readNetFromCaffe("model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel");
cv::Mat image = cv::imread("res/char1.png",cv::IMREAD_GRAYSCALE);
cv::resize(image,image,cv::Size(14,30));
cv::equalizeHist(image,image);
cv::Mat inputBlob = cv::dnn::blobFromImage(image, 1/255.0, cv::Size(14,30), false);
net.setInput(inputBlob,"data");
cv::Mat res = net.forward();
std::cout<<res<<std::endl;
float *p = (float*)res.data;
int maxid= 0;
double prob = 0;
getMaxClass(res,&maxid,&prob);
std::cout<<chars[maxid]<<std::endl;
};
int main()
{TEST_RECOGNIZATION();
}
@@ -0,0 +1,43 @@
//
// Created by Jack Yu on 16/10/2017.
//
#include "../include/PlateSegmentation.h"
#include "../include/CNNRecognizer.h"
#include "../include/Recognizer.h"
std::vector<std::string> chars{"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","J","K","L","M","N","P","Q","R","S","T","U","V","W","X","Y","Z"};
void TEST_SLIDINGWINDOWS_EVAL(){
cv::Mat demo = cv::imread("res/cache/finemappingres.png");
cv::resize(demo,demo,cv::Size(136,36));
cv::Mat respones;
pr::PlateSegmentation plateSegmentation("model/Segmentation.prototxt","model/Segmentation.caffemodel");
pr::PlateInfo plate;
plate.setPlateImage(demo);
std::vector<cv::Rect> rects;
plateSegmentation.segmentPlatePipline(plate,1,rects);
plateSegmentation.ExtractRegions(plate,rects);
pr::GeneralRecognizer *recognizer = new pr::CNNRecognizer("model/CharacterRecognization.prototxt","model/CharacterRecognization.caffemodel");
recognizer->SegmentBasedSequenceRecognition(plate);
std::cout<<plate.decodePlateNormal(chars)<<std::endl;
delete(recognizer);
}
int main(){
TEST_SLIDINGWINDOWS_EVAL();
return 0;
}
@@ -0,0 +1,54 @@
//
// Created by Jack Yu on 29/11/2017.
//
#include "../include/SegmentationFreeRecognizer.h"
#include "../include/Pipeline.h"
#include "../include/PlateInfo.h"
std::string decodeResults(cv::Mat code_table,std::vector<std::string> mapping_table)
{
cv::MatSize mtsize = code_table.size;
int sequencelength = mtsize[2];
int labellength = mtsize[1];
cv::transpose(code_table.reshape(1,1).reshape(1,labellength),code_table);
std::string name = "";
std::vector<int> seq(sequencelength);
for(int i = 0 ; i < sequencelength; i++) {
float *fstart = ((float *) (code_table.data) + i * labellength );
int id = std::max_element(fstart,fstart+labellength) - fstart;
seq[i] =id;
}
for(int i = 0 ; i< sequencelength ; i++)
{
if(seq[i]!=labellength-1 && (i==0 || seq[i]!=seq[i-1]))
name+=mapping_table[seq[i]];
}
std::cout<<name;
return name;
}
int main()
{
cv::Mat image = cv::imread("res/cache/chars_segment.jpg");
// cv::transpose(image,image);
// cv::resize(image,image,cv::Size(160,40));
cv::imshow("xxx",image);
cv::waitKey(0);
pr::SegmentationFreeRecognizer recognizr("model/SegmenationFree-Inception.prototxt","model/ISegmenationFree-Inception.caffemodel");
std::pair<std::string,float> res = recognizr.SegmentationFreeForSinglePlate(image,pr::CH_PLATE_CODE);
std::cout<<res.first<<" "
<<res.second<<std::endl;
// decodeResults(plate,pr::CH_PLATE_CODE);
cv::imshow("image",image);
cv::waitKey(0);
return 0;
}