-
Notifications
You must be signed in to change notification settings - Fork 1
/
gallery.php
308 lines (276 loc) · 15 KB
/
gallery.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php require_once('header.php'); ?>
<div>
<ul class="breadcrumb">
<li>
<a href="index.php">Home</a> <span class="divider">/</span>
</li>
<li>
<a href="gallery.php">Gallery</a>
</li>
</ul>
</div>
<div class="row-fluid sortable">
<div class="box span12">
<div class="box-header well" data-original-title>
<h2><i class="icon-picture"></i> Gallery</h2>
<div class="box-icon">
<a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a>
<a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a>
<a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a>
</div>
</div>
<div class="box-content">
<?php
if ( Tools::getIsset('topk') &&
Tools::getIsset('distanceFunction') &&
Tools::getIsset('colorSpace') &&
Tools::getIsset('histogram') &&
Validate::isUnsignedInt(Tools::getValue('topk'))
)
{
$sqlThres = "SELECT COUNT(*) AS total_images FROM ". DB_PREFIX ."image";
$imageThres = DB::getOne($sqlThres);
if (Tools::getValue('topk') <= 0 || Tools::getValue('topk') > $imageThres[0])
echo MESSAGE_INVALID_TOPK;
else
{
$_SESSION['threshold'] = Tools::getValue('topk');
$_SESSION['distanceFunction'] = Tools::getValue('distanceFunction');
$_SESSION['colorSpace'] = Tools::getValue('colorSpace');
unset($_SESSION['rgb_histogram']);
unset($_SESSION['hsv_histogram']);
session_write_close();
echo MESSAGE_SUCCESS_SETTINGS;
}
}
set_time_limit(0);
if (Tools::isSubmit('submit') && Tools::getIsset('submit'))
{
/* rocchio algorimthm start */
$sum_nr = 0;
$difference_nr = 0;
$sum_image_histogram_sum = array_fill(0, 64, '0');
$difference_image_histogram_sum = array_fill(0, 64, '0');
foreach ($_POST as $key => $id) :
if($key != "submit")
{
if (isset($_SESSION['rgb_histogram']) && !empty($_SESSION['rgb_histogram']))
{
$sql_image = "SELECT array_to_json(color_histogram) AS image_histogram
FROM ". DB_PREFIX ."image
WHERE id_image = ".$key;
}
if (isset($_SESSION['hsv_histogram']) && !empty($_SESSION['hsv_histogram']))
{
$sql_image = "SELECT array_to_json(hsv_histogram) AS image_histogram
FROM ". DB_PREFIX ."image
WHERE id_image = ".$key;
}
$qresult_image = DB::getAll($sql_image);
$image_histogram = json_decode($qresult_image[0]['image_histogram']);
if($id == 1)
{
$sum_image_histogram_sum = DistanceMetrics::sumVectors($image_histogram, $sum_image_histogram_sum);
$sum_nr++;
}
elseif($id == -1)
{
$difference_image_histogram_sum = DistanceMetrics::sumVectors($image_histogram, $difference_image_histogram_sum);
$difference_nr++;
}
}
endforeach;
if($sum_nr != 0)
{
for ($i = 0; $i < count($sum_image_histogram_sum); $i++)
{
$sum_image_histogram_sum[$i] = $sum_image_histogram_sum[$i] / $sum_nr * 0.70;
}
}
if($difference_nr != 0)
{
for ($i = 0; $i < count($difference_image_histogram_sum); $i++)
{
$difference_image_histogram_sum[$i] = $difference_image_histogram_sum[$i] / $difference_nr * 0.25;
}
}
if(isset($_SESSION['rgb_histogram']) || isset($_SESSION['hsv_histogram']))
{
if (!empty($_SESSION['rgb_histogram']))
$original_image_histogram = $_SESSION['rgb_histogram'];
elseif (!empty($_SESSION['hsv_histogram']))
$original_image_histogram = $_SESSION['hsv_histogram'];
$query_image_histogram_sum = array_fill(0, 64, 0);
//$query_image_histogram_sum = 0;
$query_image_histogram_sum = DistanceMetrics::sumVectors($original_image_histogram, $query_image_histogram_sum);
$query_image_histogram_sum = DistanceMetrics::sumVectors($sum_image_histogram_sum, $query_image_histogram_sum);
//$query_image_histogram_sum = DistanceMetrics::sumVectors($sum_image_histogram_sum, $original_image_histogram);
$query_image_histogram_sum = DistanceMetrics::diffVectors($query_image_histogram_sum, $difference_image_histogram_sum);
// uncoment this if you want to requery multiple times
if (!empty($_SESSION['rgb_histogram']))
$_SESSION['rgb_histogram'] = $query_image_histogram_sum;
elseif (!empty($_SESSION['hsv_histogram']))
$_SESSION['hsv_histogram'] = $query_image_histogram_sum;
session_write_close();
}
/* rocchio algorimthm end */
$targetFolder = '/cbires/img/gallery/thumbs/query_image.jpg';
$galleryFolder = '/cbires/img/gallery/query_image.jpg';
if ( !empty( $_FILES['fileInput']['name'] ) &&
!empty( $_FILES['fileInput']['type'] ) &&
!empty( $_FILES['fileInput']['tmp_name'] ) )
{
$tempFile = $_FILES['fileInput']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
$galleryPath = $_SERVER['DOCUMENT_ROOT'] . $galleryFolder;
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['fileInput']['name']);
if ( in_array( Tools::strtolower( $fileParts['extension'] ), $fileTypes ) )
{
$img = new Image($tempFile);
$img->save($galleryPath, false);
$img->resize(300, 0); // Lower quality image created using width ratio otherwise $img->resize(300, 0, false); for quality not speed
$img->save($targetPath);
$resized_img = $img->file_name;
//$filemime = 'image/'.$fileParts['extension'];
if ( isset($_SESSION['colorSpace']) && !empty($_SESSION['colorSpace']) &&
isset($_SESSION['distanceFunction']) && !empty($_SESSION['distanceFunction']) &&
isset($_SESSION['threshold']) && !empty($_SESSION['threshold'])
)
{
switch ($_SESSION['colorSpace'])
{
case 'RGB':
{
$objRGB = new Histogram($resized_img);
$histoRGB = $objRGB->generateHistogram();
$normHistRGB = DistanceMetrics::computeHistogram($histoRGB, 64, min($histoRGB), max($histoRGB));
$_SESSION['rgb_histogram'] = $normHistRGB;
unset($_SESSION['hsv_histogram']);
session_write_close();
$meanRGB = DistanceMetrics::mean($normHistRGB);
$stdRGB = DistanceMetrics::std($normHistRGB);
$combRGB = PopulateImages::computeRgbImages($normHistRGB);
// Retrieval Results
PopulateImages::retrievalResults($combRGB);
break;
}
case 'HSV':
{
$objHSV = new Histogram($resized_img);
$histoHSV = $objHSV->generateHistogram(true);
$binHistHSV = DistanceMetrics::computeHistogram($histoHSV, 64, min($histoHSV), max($histoHSV), false);
$normHistHSV = DistanceMetrics::normalize($binHistHSV);
$_SESSION['hsv_histogram'] = $normHistHSV;
unset($_SESSION['rgb_histogram']);
session_write_close();
$meanHSV = DistanceMetrics::mean($normHistHSV);
$stdHSV = DistanceMetrics::std($normHistHSV);
$combHSV = PopulateImages::computeHsvImages($normHistHSV);
// Retrieval Results
PopulateImages::retrievalResults($combHSV);
break;
}
}
}
else
{
$objRGB = new Histogram($resized_img);
$histoRGB = $objRGB->generateHistogram();
$normHistRGB = DistanceMetrics::computeHistogram($histoRGB, 64, min($histoRGB), max($histoRGB));
$_SESSION['rgb_histogram'] = $normHistRGB;
unset($_SESSION['hsv_histogram']);
session_write_close();
$meanRGB = DistanceMetrics::mean($normHistRGB);
$stdRGB = DistanceMetrics::std($normHistRGB);
$combRGB = PopulateImages::computeRgbImages($normHistRGB, true);
// Retrieval Results
PopulateImages::retrievalResults($combRGB);
}
unset($img);
}
else
{
echo MESSAGE_INVALID_FILE_TYPE;
}
}
else
{
//$combRGB = PopulateImages::computeRgbImages($query_image_histogram_sum);
if ( isset($_SESSION['colorSpace']) && !empty($_SESSION['colorSpace']) &&
isset($_SESSION['distanceFunction']) && !empty($_SESSION['distanceFunction']) &&
isset($_SESSION['threshold']) && !empty($_SESSION['threshold'])
)
{
switch ($_SESSION['colorSpace'])
{
case 'RGB':
{
if ( empty( $_FILES['fileInput']['name'] ) && empty( $_FILES['fileInput']['type'] ) &&
empty( $_FILES['fileInput']['tmp_name']) && Tools::getValue('submit') != "relevance_feedback" )
echo MESSAGE_EMPTY_FILE_UPLOAD;
else
{
$combRGB = PopulateImages::computeRgbImages($query_image_histogram_sum);
// Retrieval Results
PopulateImages::retrievalResults($combRGB);
}
break;
}
case 'HSV':
{
if ( empty( $_FILES['fileInput']['name'] ) && empty( $_FILES['fileInput']['type'] ) &&
empty( $_FILES['fileInput']['tmp_name']) && Tools::getValue('submit') != "relevance_feedback" )
echo MESSAGE_EMPTY_FILE_UPLOAD;
else
{
$normHistHSV = DistanceMetrics::normalize($query_image_histogram_sum);
$combHSV = PopulateImages::computeHsvImages($normHistHSV);
// Retrieval Results
PopulateImages::retrievalResults($combHSV);
}
break;
}
}
}
else
{
if ( empty( $_FILES['fileInput']['name'] ) && empty( $_FILES['fileInput']['type'] ) &&
empty( $_FILES['fileInput']['tmp_name'] ) && Tools::getValue('submit') != "relevance_feedback" )
echo MESSAGE_EMPTY_FILE_UPLOAD;
else
{
$combRGB = PopulateImages::computeRgbImages($query_image_histogram_sum, true);
// Retrieval Results
PopulateImages::retrievalResults($combRGB);
}
}
}
}
set_time_limit(30);
//session_unset();
?>
<legend class="center"></legend>
<!-- START OF: info message 3 -->
<div class="alert alert-info">
<p class="center" style="color: blue; font-size: x-large;">Upload - Use query image from your file system</p>
</div>
<div class="box-content">
<form class="form-horizontal" action="gallery.php" method="post" target="_self" enctype="multipart/form-data">
<fieldset>
<div class="control-group">
<p class="center">File input:
<!--<input type="hidden" name="submit-value" id="submit-value" value="submited" />-->
<input class="input-file uniform_on" type="file" id="fileInput" name="fileInput" />
<button class="btn btn-small btn-danger btn-round" type="submit" id="submit" name="submit">Query :-)</button>
</p>
</div>
</fieldset>
</form>
</div>
<!-- END OF: info message 3 -->
</div>
</div><!--/span-->
</div><!--/row-->
<?php require_once('footer.php'); ?>