-
Notifications
You must be signed in to change notification settings - Fork 5
/
database.py
709 lines (603 loc) · 22.2 KB
/
database.py
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
import numpy
import pylab
try:
import scipy.misc.pilutil as pil
except:
import scipy.misc as pil
import string
import pickle
import os.path
import glob
from util import myimread, loadmat
def getbboxVOC06(filename, cl="person", usetr=False, usedf=False):
"""
get the ground truth bbox from the PASCAL VOC 2006 database at filename
"""
fd = open(filename, "r")
lines = fd.readlines()
rect = []
cl = "PAS" + cl
for idx, item in enumerate(lines):
p = item.find("Bounding box") # look for the bounding box
if p != -1:
p = item.find(cl) # check if it is a person
if p != -1:
p = item.find("Difficult") # check that it is not truncated
if p == -1 or usedf:
p = item.find("Trunc") # check that it is not truncated
if p == -1 or usetr:
p = item.find(":")
item = item[p:]
# print item[p:]
p = item.find("(")
pXmin = int(item[p + 1:].split(" ")[0][:-1])
pYmin = int(item[p + 1:].split(" ")[1][:-1])
p = item[p:].find("-")
item = item[p:]
p = item.find("(")
pXmax = int(item[p + 1:].split(" ")[0][:-1])
pYmax = int(item[p + 1:].split(" ")[1][:-3])
rect.append((pYmin, pXmin, pYmax, pXmax, 0, 0))
return rect
import xml.dom.minidom
from xml.dom.minidom import Node
def getbboxVOC07(filename, cl="person", usetr=False, usedf=False):
"""
get the ground truth bbox from the PASCAL VOC 2007 database at filename
"""
rect = []
doc = xml.dom.minidom.parse(filename)
for node in doc.getElementsByTagName("object"):
# print node
tr = 0
df = 0
if node.getElementsByTagName("name")[0].childNodes[0].data == cl:
pose = node.getElementsByTagName(
"pose")[0].childNodes[0].data # last
if node.getElementsByTagName("difficult")[0].childNodes[0].data == "0" or usedf:
if node.getElementsByTagName("truncated")[0].childNodes[0].data == "0" or usetr:
if node.getElementsByTagName("difficult")[0].childNodes[0].data == "1":
df = 1
if node.getElementsByTagName("truncated")[0].childNodes[0].data == "1":
tr = 1
l = node.getElementsByTagName("bndbox")
for el in l:
if el.parentNode.nodeName == "object":
xmin = int(
el.getElementsByTagName("xmin")[0].childNodes[0].data)
ymin = int(
el.getElementsByTagName("ymin")[0].childNodes[0].data)
xmax = int(
el.getElementsByTagName("xmax")[0].childNodes[0].data)
ymax = int(
el.getElementsByTagName("ymax")[0].childNodes[0].data)
rect.append(
(ymin, xmin, ymax, xmax, tr, df, pose)) # last
return rect
class imageData:
"""
interface call to handle a database
"""
def __init__():
print "Not implemented"
def getDBname():
return "Not implemented"
def getImage(i):
"""
gives the ith image from the database
"""
print "Not implemented"
def getImageName(i):
"""
gives the ith image name from the database
"""
print "Not implemented"
def getBBox(self, i):
"""
retrun a list of ground truth bboxs from the ith image
"""
# print "Not implemented"
return []
def getTotal():
"""
return the total number of images in the db
"""
print "Not implemented"
def getRecord(data, total=-1, pos=True, pose=False, facial=False):
"""return all the gt data in a record"""
if total == -1:
total = data.getTotal()
else:
total = min(data.getTotal(), total)
arrPos = numpy.zeros(
total, dtype=[("id", numpy.int32), ("name", object), ("bbox", list)])
if facial:
arrPos = numpy.zeros(total, dtype=[
("id", numpy.int32), ("name", object), ("bbox", list), ("facial", object)])
if pose:
arrPos = numpy.zeros(total, dtype=[
("id", numpy.int32), ("name", object), ("bbox", list), ("facial", object), ("pose", object)])
for i in range(total):
arrPos[i]["id"] = i
arrPos[i]["name"] = data.getImageName(i)
arrPos[i]["bbox"] = data.getBBox(i)
if pose:
arrPos[i]["pose"] = data.getPose(i)
if facial:
arrPos[i]["facial"] = data.getFacial(i)
return arrPos
class VOC06Data(imageData):
"""
VOC06 instance (you can choose positive or negative images with the option select)
"""
def __init__(self, select="all", cl="person_train.txt",
basepath="meadi/DADES-2/",
trainfile="VOC2006/VOCdevkit/VOC2006/ImageSets/",
imagepath="VOC2006/VOCdevkit/VOC2006/PNGImages/",
annpath="VOC2006/VOCdevkit/VOC2006/Annotations/",
local="VOC2006/VOCdevkit/local/VOC2006/",
usetr=False, usedf=False, precompute=True):
self.cl = cl
self.usetr = usetr
self.usedf = usedf
self.local = basepath + local
self.trainfile = basepath + trainfile + cl
self.imagepath = basepath + imagepath
self.annpath = basepath + annpath
self.prec = precompute
fd = open(self.trainfile, "r")
self.trlines = fd.readlines()
fd.close()
if select == "all": # All images
self.str = ""
if select == "pos": # Positives images
self.str = "1\n"
if select == "neg": # Negatives images
self.str = "-1\n"
self.selines = self.__selected()
if self.prec:
self.selbbox = self.__precompute()
def __selected(self):
lst = []
for id, it in enumerate(self.trlines):
if self.str == "" or it.split(" ")[-1] == self.str:
lst.append(it)
return lst
def __precompute(self):
lst = []
tot = len(self.selines)
cl = self.cl.split("_")[0]
for id, it in enumerate(self.selines):
print id, "/", tot
filename = self.annpath + it.split(" ")[0] + ".txt"
lst.append(getbboxVOC06(filename, cl, self.usetr, self.usedf))
return lst
def getDBname(self):
return "VOC06"
def getImage(self, i):
item = self.selines[i]
return myimread((self.imagepath + item.split(" ")[0]) + ".png")
def getImageByName2(self, name):
return myimread(self.imagepath + name + ".png")
def getImageByName(self, name):
return myimread(name)
def getImageName(self, i):
item = self.selines[i]
return (self.imagepath + item.split(" ")[0] + ".png")
def getImageRaw(self, i):
item = self.selines[i]
return im.open((self.imagepath + item.split(" ")[0]) + ".png")
def getStorageDir(self):
return self.local
def getBBox(self, i, cl=None, usetr=None, usedf=None):
if usetr == None:
usetr = self.usetr
if usedf == None:
usedf = self.usedf
if cl == None:
cl = self.cl.split("_")[0]
bb = []
if self.prec:
bb = self.selbbox[i][:]
else:
item = self.selines[i]
filename = self.annpath + item.split(" ")[0] + ".txt"
bb = getbboxVOC06(filename, cl, usetr, usedf)
return bb
def getBBoxByName(self, name, cl=None, usetr=None, usedf=None):
if usetr == None:
usetr = self.usetr
if usedf == None:
usedf = self.usedf
if cl == None:
cl = self.cl.split("_")[0]
filename = self.annpath + name + ".txt"
return getbboxVOC06(filename, cl, usetr, usedf)
def getTotal(self):
return len(self.selines)
class VOC07Data(VOC06Data):
"""
VOC07 instance (you can choose positive or negative images with the option select)
"""
def __init__(self, select="all", cl="person_train.txt",
basepath="media/DADES-2/",
trainfile="VOC2007/VOCdevkit/VOC2007/ImageSets/Main/",
imagepath="VOC2007/VOCdevkit/VOC2007/JPEGImages/",
annpath="VOC2007/VOCdevkit/VOC2007/Annotations/",
local="VOC2007/VOCdevkit/local/VOC2007/",
usetr=False, usedf=False, mina=0):
self.cl = cl
self.usetr = usetr
self.usedf = usedf
self.local = basepath + local
self.trainfile = basepath + trainfile + cl
self.imagepath = basepath + imagepath
self.annpath = basepath + annpath
fd = open(self.trainfile, "r")
self.trlines = fd.readlines()
fd.close()
if select == "all": # All images
self.str = ""
if select == "pos": # Positives images
self.str = "1\n"
if select == "neg": # Negatives images
self.str = "-1\n"
self.selines = self.__selected()
self.mina = mina
def __selected(self):
lst = []
for id, it in enumerate(self.trlines):
if self.str == "" or it.split(" ")[-1] == self.str:
lst.append(it)
return lst
def getDBname(self):
return "VOC07"
def getStorageDir(self):
return self.local
def getImage(self, i):
item = self.selines[i]
return myimread((self.imagepath + item.split(" ")[0]) + ".jpg")
def getImageRaw(self, i):
item = self.selines[i]
return im.open((self.imagepath + item.split(" ")[0]) + ".jpg")
def getImageByName(self, name):
return myimread(name)
def getImageByName2(self, name):
return myimread(self.imagepath + name + ".jpg")
def getImageName(self, i):
item = self.selines[i]
return (self.imagepath + item.split(" ")[0] + ".jpg")
def getTotal(self):
return len(self.selines)
def getBBox(self, i, cl=None, usetr=None, usedf=None):
if usetr == None:
usetr = self.usetr
if usedf == None:
usedf = self.usedf
if cl == None: # use the right class
cl = self.cl.split("_")[0]
item = self.selines[i]
filename = self.annpath + item.split(" ")[0] + ".xml"
bb = getbboxVOC07(filename, cl, usetr, usedf)
auxb = []
for b in bb:
a = abs(b[0] - b[2]) * abs(b[1] - b[3])
# print a
if a > self.mina:
# print "OK!"
auxb.append(b)
return auxb
class LFW(VOC06Data):
"""
LFW
"""
def __init__(self, select="all", cl="face_train.txt",
basepath="media/DADES-2/",
trainfile="lfw/lfw_ffd_ann.txt",
imagepath="lfw/",
annpath="lfw/",
local="lfw/",
usetr=False, usedf=False, mina=0, fold=0, totalfold=10, fake=False):
self.fold = fold
self.totalfold = totalfold
self.usetr = usetr
self.usedf = usedf
self.local = basepath + local
self.trainfile = basepath + trainfile
self.imagepath = basepath + imagepath
self.annpath = basepath + annpath
fd = open(self.trainfile, "r")
self.trlines = fd.readlines()
fd.close()
self.selines = self.trlines[6:]
self.total = len(self.selines) # intial 5 lines of comments
self.mina = mina
self.fake = fake
def __selected(self):
lst = []
for id, it in enumerate(self.trlines):
if self.str == "" or it.split(" ")[-1] == self.str:
lst.append(it)
return lst
def getDBname(self):
return "VOC07"
def getStorageDir(self):
return self.local
def getImage(self, i):
i = i + int(self.total / self.totalfold) * self.fold
item = self.selines[i]
return myimread((self.imagepath + item.split(" ")[0]))
def getImageRaw(self, i):
i = i + int(self.total / self.totalfold) * self.fold
item = self.selines[i]
return im.open((self.imagepath + item.split(" ")[0]))
def getImageByName(self, name):
return myimread(name)
def getImageByName2(self, name):
return myimread(self.imagepath + name + ".jpg")
def getImageName(self, i):
i = i + int(self.total / self.totalfold) * self.fold
item = self.selines[i]
return (self.imagepath + item.split(" ")[0])
def getTotal(self):
return int(self.total / self.totalfold)
def getBBox(self, i, cl=None, usetr=None, usedf=None):
if self.fake:
im = LFW.getImage(self, i)
dd = 50
bb = [[dd, dd, im.shape[0] - dd, im.shape[1] - dd, 0, 0]]
return bb
i = i + int(self.total / self.totalfold) * self.fold
item = self.selines[i]
aux = item.split()
cx = float(aux[1])
cy = float(aux[2])
w = float(aux[3])
h = float(aux[4])
bb = [[cy, cx, cy + h, cx + w, 0, 0]]
auxb = []
for b in bb:
a = abs(float(b[0]) - float(b[2])) * abs(float(b[1]) - float(b[3]))
if a > self.mina:
auxb.append(b)
return auxb
def getPose(self, i):
i = i + int(self.total / self.totalfold) * self.fold
item = self.selines[i]
aux = item.split()
return int(aux[5])
def getFacial(self, i):
i = i + int(self.total / self.totalfold) * self.fold
item = self.selines[i]
aux = item.split()
return (numpy.array(aux[7:7 + int(aux[6]) * 2])).astype(numpy.float32)
class AFW(VOC06Data):
"""
AFW
"""
def __init__(self, select="all", cl="face_train.txt",
basepath="media/DADES-2/",
trainfile="",
imagepath="afw/testimages/",
annpath="afw/testimages/",
local="afw/",
usetr=False, usedf=False, minh=0, minw=0, useOldAnn=False):
self.usetr = usetr
self.usedf = usedf
self.local = basepath + local
self.imagepath = basepath + imagepath
self.annpath = basepath + annpath
self.useOldAnn = useOldAnn
if useOldAnn:
self.trainfile = "annotations/anno2.mat"
self.ann = loadmat(self.trainfile)["anno"]
else:
self.trainfile = "annotations/new_annotations_AFW.mat"
self.ann = loadmat(self.trainfile)["Annotations"]
self.total = len(self.ann)
self.minh = minh
self.minw = minw
def getDBname(self):
return "AFW"
def getStorageDir(self):
return self.local
def getImage(self, i):
item = self.ann[i][0][0]
return myimread((self.imagepath + item))
def getImageRaw(self, i):
item = self.ann[i][0][0]
return im.open((self.imagepath + item))
def getImageByName(self, name):
return myimread(name)
def getImageByName2(self, name):
return myimread(self.imagepath + name + ".jpg")
def getImageName(self, i):
if self.useOldAnn:
item = self.ann[i][0][0]
else:
item = self.ann[i]["imgname"][0][0]
return (self.imagepath + item)
def getTotal(self):
return self.total
def getBBox(self, i, cl=None, usetr=None, usedf=None):
if self.useOldAnn:
item = self.ann[i][1]
bb = []
for l in range(item.shape[1]):
it = item[0, l].flatten()
bb.append(
[float(it[1]), float(it[0]), float(it[3]), float(it[2]), 0, 0])
else:
item = self.ann[i]["objects"][0]
bb = []
for l in range(len(item)):
it = item[l] # .flatten()
bb.append([it[1], it[0], it[3], it[2], 0, it[5]])
auxb = []
for b in bb:
h = abs(float(b[0]) - float(b[2]))
w = abs(float(b[1]) - float(b[3]))
if w < self.minw or h < self.minh:
b[5] = 1
auxb.append(b)
return auxb
def getPose(self, i):
return self.ann[i][2][0][0][0] # int(aux[5])
def getFacial(self, i):
return self.ann[i][3][0].flatten()
class PASCALfaces(VOC06Data):
"""
PASCAL faces
"""
def __init__(self, select="all", cl="face_train.txt",
basepath="media/DADES-2/",
trainfile="annotations/Annotations_Face_PASCALLayout_large_fixed.mat",
imagepath="/xxx/",
annpath="afw/testimages/",
local="afw/",
usetr=False, usedf=False, minh=0, minw=0, useOldAnn=False):
self.usetr = usetr
self.usedf = usedf
self.local = basepath + local
if useOldAnn:
pp = trainfile.find("_fixed")
self.trainfile = trainfile[:pp] + ".mat"
else:
self.trainfile = trainfile
self.imagepath = basepath + imagepath
self.annpath = basepath + annpath
self.minh = minh
self.minw = minw
self.ann = loadmat(self.trainfile)["Annotations"]
self.total = len(self.ann)
def getDBname(self):
return "AFW"
def getStorageDir(self):
return self.local
def getImage(self, i):
item = self.ann[i][0][0]
return myimread((self.imagepath + item))
def getImageRaw(self, i):
item = self.ann[i][0][0]
return im.open((self.imagepath + item))
def getImageByName(self, name):
return myimread(name)
def getImageByName2(self, name):
return myimread(self.imagepath + name + ".jpg")
def getImageName(self, i):
item = self.ann[i]["imgname"][0][0]
return (self.imagepath + item)
def getTotal(self):
return self.total
def getBBox(self, i, cl=None, usetr=None, usedf=None):
item = self.ann[i]["objects"][0]
bb = []
for l in range(len(item)):
it = item[l]
if len(it) < 6:
it = [
float(it[0]), float(it[1]), float(it[2]), float(it[3]), 0, 0]
bb.append([it[1], it[0], it[3], it[2], 0, it[5]])
auxb = []
for b in bb:
h = abs(float(b[0]) - float(b[2]))
w = abs(float(b[1]) - float(b[3]))
if w < self.minw or h < self.minh:
b[5] = 1
auxb.append(b)
return auxb
def getPose(self, i):
return []
def getFacial(self, i):
return []
class AFLW(VOC06Data):
"""
AFLW
"""
def __init__(self, select="all", cl="face_train.txt",
basepath="media/DADES-2/",
trainfile="aflw/data/aflw.sqlite",
imagepath="aflw/data/flickr/",
annpath="aflw/",
local="aflw/",
usetr=False, usedf=False, mina=0, fold=0):
self.usetr = usetr
self.usedf = usedf
self.local = basepath + local
self.trainfile = basepath + trainfile
self.imagepath = basepath + imagepath
self.annpath = basepath + annpath
import sqlite3 as lite
self.mina = mina
con = lite.connect(self.trainfile)
self.cur = con.cursor()
self.cur.execute("SELECT file_id FROM Faces")
aux = self.cur.fetchall()
# remove bad image 9437
del aux[8160]
for idx, x in enumerate(aux):
# print idx,x
if x[0] == "image09437.jpg":
print "Removing file ", idx
del aux[idx]
# raw_input()
self.items = numpy.unique(aux)
self.total = len(self.items)
def getDBname(self):
return "AFLW"
def getStorageDir(self):
return self.local
def getImage(self, i):
self.cur.execute(
"SELECT filepath FROM FaceImages WHERE file_id = '%s'" % self.items[i][0])
impath = self.cur.fetchall()
return myimread((impath))
def getImageRaw(self, i):
item = self.ann[i][0][0]
return im.open((self.imagepath + item))
def getImageByName(self, name):
return myimread(name)
def getImageByName2(self, name):
return myimread(self.imagepath + name + ".jpg")
def getImageName(self, i):
self.cur.execute(
"SELECT filepath FROM FaceImages WHERE file_id = '%s'" % self.items[i][0])
impath = self.imagepath + self.cur.fetchall()[0][0]
return (impath)
def getTotal(self):
return self.total
def getBBox(self, i, cl=None, usetr=None, usedf=None):
self.cur.execute(
"SELECT face_id FROM Faces WHERE file_id = '%s'" % self.items[i][0])
faceid = self.cur.fetchall()
bb = []
for l in faceid:
self.cur.execute(
"SELECT x,y,w,h,annot_type_id FROM FaceRect WHERE face_id = %d" % l)
bb += self.cur.fetchall()
auxb = []
for b in bb:
a = abs(float(b[0]) - float(b[2])) * abs(float(b[1]) - float(b[3]))
if a > self.mina:
auxb.append([b[1], b[0], b[1] + b[3], b[0] + b[2], 0, 0])
return auxb
def getPose(self, i):
self.cur.execute(
"SELECT face_id FROM Faces WHERE file_id = '%s'" % self.items[i][0])
faceid = self.cur.fetchall()
poses = []
for l in faceid:
self.cur.execute(
"SELECT roll,pitch,yaw FROM FacePose WHERE face_id = '%s'" % l)
poses += self.cur.fetchall()
return poses
def getFacial(self, i):
self.cur.execute(
"SELECT face_id FROM Faces WHERE file_id = '%s'" % self.items[i][0])
faceid = self.cur.fetchall()
facial = []
for l in faceid:
self.cur.execute(
"SELECT descr,FeatureCoords.x,FeatureCoords.y FROM FeatureCoords,FeatureCoordTypes WHERE face_id = '%s'" % l)
facial += self.cur.fetchall()
return facial