-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColumbiaUimgSearchDev.php
238 lines (204 loc) · 6.52 KB
/
ColumbiaUimgSearchDev.php
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
error_reporting(E_ALL | E_STRICT);
function downloadFile ($url, $path) {
$newfname = $path;
$file = fopen ($url, "rb");
if ($file) {
$newf = fopen ($newfname, "wb");
if ($newf)
while(!feof($file)) {
fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
}
}
if ($file) {
fclose($file);
}
if ($newf) {
fclose($newf);
}
}
function time_elapsed($secs){
$bit = array(
'y' => $secs / 31556926 % 12,
'w' => $secs / 604800 % 52,
'd' => $secs / 86400 % 7,
'h' => $secs / 3600 % 24,
'm' => $secs / 60 % 60,
's' => $secs % 60
);
foreach($bit as $k => $v)
if($v > 0) $ret[] = $v . $k;
if(count($ret) > 0) {
$out = join(' ', $ret);
}
else {
$out = 'Less than 1 second.';
}
return $out;
}
$mainpath = '/home/ubuntu/memex/';
$savepath = $mainpath . 'img/';
if (PHP_SAPI === 'cli') {
$image_url = $argv[1];
$query_num = $argv[2];
$vis = $argv[3];
$fast = $argv[4];
$nodup = $argv[5];
$neardup = $argv[6];
$neardup_type = $argv[7];
$neardup_th = $argv[8];
$noblur = $argv[10];
$nocache = $argv[9];
}
else {
$image_url = $_GET["url"];
$query_num = $_GET['num'];
$vis = $_GET['visualize'];
$fast = $_GET['fast'];
$nodup = $_GET['nodup'];
$neardup = $_GET['neardup'];
$neardup_th = $_GET['neardup_th'];
$neardup_type = $_GET['neardup_type'];
$noblur = $_GET['noblur'];
$nocache = $_GET['nocache'];
}
if (empty($image_url)) {
echo "<h1>Please provide an image url!</h1>How to use this API:<br/><ul><li>Minimal requirement: an image URL: https://isi.memexproxy.com/ColumbiaUimgSearch.php?url=https://hostingservice.com/image.jpg</li></ul><ul>Other parameters:<li>visualize: 1, 0 [JSON or visualization: default: 0]</li><li>nodup: 1, 0 [remove or display exact duplicate, default: 0]</li><li>num: maximum number of returned images [default: 30, 1000 if neardup activated]</li><li>neardup: 1, 0 [activate near duplicate search, default: 0]</li><li>neardup_type: strict, loose, balanced [default: balanced]</li></ul>Issues, questions or suggestions? Contact us:<ul><li>Svebor Karaman: [email protected]</li><li>Tao Chen: [email protected]</li></ul>";
}
if (empty($query_num)) {
$query_num=30;
}
if (empty($vis)) {
$vis = 0;
}
if (empty($fast)) {
$fast = 0;
}
if (empty($nodup)) {
$nodup = 0;
}
if (empty($neardup)) {
$neardup = 0;
}
if (empty($neardup_type)) {
$neardup_type = 'balanced';
} else {
$neardup = 1;
}
if (empty($nocache)) {
$nocache = 0;
}
if (empty($noblur)) {
$noblur = 0;
}
$dup = 1;
$dupstr = '_dup';
if ($nodup>0){
$dup =0;
$dupstr = '';
}
if ($query_num<1){
$query_num = 30;
}
if ($vis<1){
$vis = 0;
}
else{
$vis = 1;
}
if ($fast<1){
$fast = 0;
}
else{
$fast = 1;
}
$fgval = fopen ("global_var_new.json", "rb");
$gread=fread($fgval,filesize("global_var_new.json"));
$global_var = json_decode($gread);
if ($fast){
$ratio = $global_var->{'fast_ratio'};
}
else {
$ratio = $global_var->{'ratio'};
}
$neardupstr = '';
if ($neardup>0){
if (empty($neardup_th)) { // If specified manually, do not override
switch ($neardup_type) {
case "strict":
$neardup_th = $global_var->{'neardup_th_strict'};
break;
case "loose":
$neardup_th = $global_var->{'neardup_th_loose'};
break;
default:
$neardup_th = $global_var->{'neardup_th_balanced'};
break;
}
}
$neardupstr = '_neardup'.$neardup_th;
$query_num=1000;
}
//echo $query_num . ' ' . $vis;
$name = basename($image_url);
$fullname = $savepath . $name;
$pos = strrpos($fullname, ".");
if ($pos === false) { // note: three equal signs
// not found...
$fullname=$fullname.'.jpg';
$pos = strrpos($fullname, ".");
}
$fullnamet = substr_replace($fullname, "_" . Rand(), $pos, 0);
downloadFile($image_url,$fullnamet);
//$output = shell_exec("md5sum " . $fullnamet );
$output = shell_exec("sha1sum " . $fullnamet );
//list($md5, $tmp) = split(" ", $output);
list($sha1, $tmp) = split(" ", $output);
//$fullname = substr_replace($fullname, "_" . $md5, $pos, 0);
$fullname = substr_replace($fullname, "_" . $sha1, $pos, 0);
if (file_exists($fullname)) {
//echo "The file $filename exists";
unlink($fullnamet);
} else {
//echo "The file $filename does not exist";
rename($fullnamet, $fullname);
}
$start_time = time();
//$outname = substr_replace($fullname, "-sim_".$query_num."_".$ratio.$dupstr."_".date('Y-m-d_H').".json", -4, 4); // Date is for one hour caching.
$outname = substr_replace($fullname, "-sim_".$query_num."_".$ratio.$dupstr.$neardupstr.".json", -4, 4); // Date is for one hour caching.
if ($nocache==1) {
unlink($outname);
}
//echo "cd " . $mainpath . " && export LD_LIBRARY_PATH=/usr/local/cuda/lib64 && python getSimilarNew.py " . $fullname . " " . $query_num . " " . $ratio . " " . $dup . " " . $neardup . " " . $neardup_th;
shell_exec("cd " . $mainpath . " && export LD_LIBRARY_PATH=/usr/local/cuda/lib64 && python getSimilarDev.py " . $fullname . " " . $query_num . " " . $ratio . " " . $dup . " " . $neardup . " " . $neardup_th);
$fout = fopen ($outname, "rb");
if ($fout) {
$json = fread($fout,filesize($outname));
if ($vis==0){
echo $json;
}
else {
//echo '<div id="debug" value="'.time_elapsed(time()-$start_time).'" outfile="'.$outname.'" params="image_url:'.$image_url.';query_num:'.$query_num.';vis:'.$vis.';fast:'.$fast.';nodup:'.$nodup.';neardup:'.$neardup.';neardup_th:'.$neardup_th.';nocache:'.$nocache.';ratio:'.$ratio.';"></div>';
$obj = json_decode($json);
echo '<link rel="stylesheet" type="text/css" href="style.css" />';
if ($noblur) {
$img_style="img_vis";
$dup_style="dup_vis";
}
else {
$img_style="img_blur";
$dup_style="dup_blur";
}
echo '<font size="6"><b>Query Image</b></font><br><a href="'.$image_url.'"><img src="'.$image_url.'" class="'.$img_style.'" title="Query Image"></a><br><br><font size="6"><b>Query Results:</b><br>';
$imglist = $obj->{'images'}[0]->{'similar_images'}->{'cached_image_urls'};
$orilist = $obj->{'images'}[0]->{'similar_images'}->{'page_urls'};
$uidlist = $obj->{'images'}[0]->{'similar_images'}->{'ht_images_id'};
$sha1list = $obj->{'images'}[0]->{'similar_images'}->{'sha1'};
$distlist = $obj->{'images'}[0]->{'similar_images'}->{'distance'};
for ($i=0; $i<sizeof($imglist); $i++) {
$dupurl = 'getDuplicate.php?htid='.$uidlist[$i].'&visualize=1&style='.$dup_style.'';
echo '<a href="'.$dupurl.'"><img src="'.$imglist[$i].'" class="'.$img_style.'" origin="'.$orilist[$i].'" title="'.$distlist[$i].'"></a>';
}
}
}
?>