-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmatch.cpp
166 lines (151 loc) · 3.92 KB
/
match.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "match.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <io.h>
#include <stdio.h>
Match::Match()
{
}
Match::~Match()
{
}
void Match::init()
{
}
void Match::writeCode(string dir, string file, std::vector<char> code)
{
string filename = dir + ".iris";
ofstream out(filename,ios::app|ios::binary);
if(out.is_open())
{
out << dir << " " << file << " ";
for(int i = 0;i < code.size();i++)
out << code[i];
out << '\n';
out.close();
}
else
cout << "write failed!"<<endl;
}
void Match::writeCodeAll(string dir, string file, vector<char> code)
{
string filename = "twins.iris";
ofstream out(filename,ios::app|ios::binary);
if(out.is_open())
{
out << dir << " " << file << " ";
for(int i = 0;i < code.size();i++)
out << code[i];
out << '\n';
out.close();
}
else
cout << "write failed!"<<endl;
}
string Match::matchCode(vector<char> code)
{
string result = "match_result.txt";
if(access(result.c_str(),F_OK) == 0)
remove(result.c_str());
ofstream out(result,ios::app|ios::binary);
double matchValue = 1.0;
if(out.is_open())
{
for(int i = 0;i < irisCodes.size();i++)
{
double count = 0;
for(int j = 0;j < code.size();j++)
{
if(code[j] != irisCodes[i][j])
count++;
}
double val = count / code.size();
out<<ids[i]<<" "<<pictures[i]<<" match value: "<<val<<'\n';
matchValues.push_back(val);
if(val < matchValue){
matchValue = val;
stringstream ssm;
ssm << val;
string valstr = ssm.str();
message = ids[i] + " " + pictures[i] + " " + valstr;
}
}
out<<"best match:"<<message<<'\n';
out.close();
return message;
}
else
cout<<"match result write failed!"<<endl;
}
bool Match::batchMatch(string path,string target,vector<char> sampleCode)
{
string isMatch = "false";
ofstream out(path,ios::app|ios::binary);
if(out.is_open())
{
double bestMatch = 1.0;
string matchstr;
for(int i = 0;i < irisCodes.size();i++)
{
double count = 1.0;
for(int j = 0;j < sampleCode.size();j++)
if(sampleCode[j] != irisCodes[i][j])
count++;
double val = count / sampleCode.size();
if(val < bestMatch)
{
bestMatch = val;
stringstream ssm;
ssm << val;
string valstr = ssm.str();
if(target == ids[i])
isMatch = "true";
matchstr = "target:" + target + " match:" + ids[i] + " matchValue:" + valstr + " isMatch:" + isMatch;
}
}
out << matchstr << '\n';
out.close();
}
else
cout<<"failed !!!"<<endl;
if(isMatch == "true")
return true;
else
return false;
}
void Match::loadCodes(string codePath)
{
ifstream in(codePath,ios::in|ios::binary);
string line,id,pic,tempcode;
int count = 0;
if(in.is_open())
{
while(getline(in,line))
{
vector<char> ch;
istringstream iss(line);
iss >> id;
iss >> pic;
iss >> tempcode;
for(int i = 0;i < tempcode.length();i++)
ch.push_back(tempcode[i]);
ids.push_back(id);
pictures.push_back(pic);
irisCodes.push_back(ch);
ch.clear();
count++;
}
in.close();
cout<<"total numbers:"<<count<<endl;
}
else
cout << "load failed!" <<endl;
}
void Match::clearMemorry()
{
ids.clear();
pictures.clear();
irisCodes.clear();
matchValues.clear();
}