Skip to content

Commit 227bc0d

Browse files
committed
Added Data Preparation and Post Processing Codebase
1 parent 2118854 commit 227bc0d

File tree

8 files changed

+1392
-0
lines changed

8 files changed

+1392
-0
lines changed

new_files/data_prep.py

+307
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
import numpy as np
2+
import tensorflow as tf
3+
from skimage import io
4+
from skimage import color
5+
import sqlite3
6+
import cv2
7+
import matplotlib.pyplot as plt
8+
import os
9+
import random
10+
from tqdm import tqdm
11+
from pdb import set_trace as brk
12+
import sys
13+
# The following are the database properties available (last updated version 2012-11-28):
14+
#
15+
# databases: db_id, path, description
16+
# faceellipse: face_id, x, y, ra, rb, theta, annot_type_id, upsidedown
17+
# faceimages: image_id, db_id, file_id, filepath, bw, widht, height
18+
# facemetadata: face_id, sex, occluded, glasses, bw, annot_type_id
19+
# facepose: face_id, roll, pitch, yaw, annot_type_id
20+
# facerect: face_id, x, y, w, h, annot_type_id
21+
# faces: face_id, file_id, db_id
22+
# featurecoords: face_id, feature_id, x, y
23+
# featurecoordtype: feature_id, descr, code, x, y, z
24+
# AFLW 21 points landmark
25+
# 0|LeftBrowLeftCorner
26+
# 1|LeftBrowCenter
27+
# 2|LeftBrowRightCorner
28+
# 3|RightBrowLeftCorner
29+
# 4|RightBrowCenter
30+
# 5|RightBrowRightCorner
31+
# 6|LeftEyeLeftCorner
32+
# 7|LeftEyeCenter
33+
# 8|LeftEyeRightCorner
34+
# 9|RightEyeLeftCorner
35+
# 10|RightEyeCenter
36+
# 11|RightEyeRightCorner
37+
# 12|LeftEar
38+
# 13|NoseLeft
39+
# 14|NoseCenter
40+
# 15|NoseRight
41+
# 16|RightEar
42+
# 17|MouthLeftCorner
43+
# 18|MouthCenter
44+
# 19|MouthRightCorner
45+
# 20|ChinCenter
46+
47+
select_string = "faceimages.filepath, faces.face_id, facepose.roll, facepose.pitch, facepose.yaw, facerect.x, facerect.y, facerect.w, facerect.h,faceimages.image_id,facemetadata.sex"
48+
from_string = "faceimages, faces, facepose, facerect,facemetadata"
49+
where_string = "faces.face_id = facepose.face_id and faces.file_id = faceimages.file_id and faces.face_id = facerect.face_id and faces.face_id = facemetadata.face_id"
50+
query_string = "SELECT " + select_string + " FROM " + from_string + " WHERE " + where_string
51+
52+
53+
54+
55+
conn = sqlite3.connect('/home/shashank/Documents/CSE-252C/AFLW/aflw/data/aflw.sqlite')
56+
c = conn.cursor()
57+
58+
img_path = '/home/shashank/Documents/CSE-252C/AFLW/'
59+
loc_file_path = '/home/shashank/Documents/CSE-252C/hyperface/code/locations_test/'
60+
tfrecords_train_filename = 'test_check.tfrecords'
61+
tfrecords_test_filename = 'aflw_test_new.tfrecords'
62+
63+
writer_train = tf.python_io.TFRecordWriter(tfrecords_train_filename)
64+
writer_test = tf.python_io.TFRecordWriter(tfrecords_test_filename)
65+
66+
def _bytes_feature(value):
67+
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
68+
69+
def _float_feature(value):
70+
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
71+
72+
def _int64_feature(value):
73+
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
74+
75+
def test_names():
76+
l=[]
77+
names = os.listdir(img_path+'0')
78+
random.shuffle(names)
79+
l.append(['0/'+name for name in names[:300]])
80+
81+
names = os.listdir(img_path+'2')
82+
random.shuffle(names)
83+
l.append(['2/'+name for name in names[:300]])
84+
85+
names = os.listdir(img_path+'3')
86+
random.shuffle(names)
87+
l.append(['3/'+name for name in names[:400]])
88+
89+
return l[0]+l[1]+l[2]
90+
91+
def make_tfrecord(test_images):
92+
93+
it_test =0
94+
it_train = 0
95+
gender_dict={'m':1,'f':0}
96+
97+
for row in (c.execute(query_string)):
98+
'''
99+
row[0] = image path str
100+
row[1] = face id int
101+
row[2] = roll float
102+
row[3] = pitch float
103+
row[4] = yaw float
104+
row[5] = x int
105+
row[6] = y int
106+
row[7] = w int
107+
row[8] = h int
108+
'''
109+
110+
111+
center_x = float(row[5]) + float(row[7])/2
112+
center_y = float(row[6]) + float(row[8])/2
113+
114+
115+
if not os.path.exists(loc_file_path+str(row[1])):
116+
continue
117+
118+
select_str = "coords.feature_id, coords.x, coords.y"
119+
from_str = "featurecoords coords"
120+
where_str = "coords.face_id = {}".format(row[1])
121+
query_str = "SELECT " + select_str + " FROM " + from_str + " WHERE " + where_str
122+
landmark = np.zeros((21,2)).astype(np.float32)
123+
visibility = np.zeros((21,1)).astype(np.int32)
124+
125+
c2 = conn.cursor()
126+
127+
for xx in c2.execute(query_str):
128+
landmark[xx[0]-1][0] = xx[1]#(xx[1] - center_x)/float(row[7])
129+
landmark[xx[0]-1][1] = xx[2]#(xx[2] - center_y)/float(row[8])
130+
visibility[xx[0]-1] = 1
131+
landmark = landmark.reshape(-1,42)
132+
133+
c2.close()
134+
135+
try:
136+
137+
img_raw = (np.asarray(cv2.imread(img_path+row[0])).astype(np.float32))/255.0
138+
cv2.imwrite('save_im.jpg',img_raw*255)
139+
landmark_pos = None
140+
141+
if len(img_raw.shape) !=3:
142+
continue#img_raw = color.gray2rgb(img_raw)
143+
if len(img_raw.shape) !=3 or img_raw.shape[2] != 3:
144+
continue
145+
print row[1]
146+
147+
w = img_raw.shape[1]
148+
h = img_raw.shape[0]
149+
if os.path.isfile(loc_file_path+str(row[1])+'/positive.npy'):
150+
pos_locs = np.load(loc_file_path+str(row[1])+'/positive.npy')[:,:4]
151+
cof_locs = np.tile(np.load(loc_file_path+str(row[1])+'/positive.npy')[:,4:6],(1,21))
152+
dim_locs = np.tile(np.load(loc_file_path+str(row[1])+'/positive.npy')[:,6:8],(1,21))
153+
n_pos_locs = pos_locs.shape[0]
154+
155+
landmark_pos = (landmark - cof_locs)/dim_locs
156+
visibility_pos = np.ones((landmark_pos.shape[0],21))
157+
visibility_pos[(np.where(landmark_pos > 0.5)[0],np.where(landmark_pos > 0.5)[1]/2)] = 0
158+
visibility_pos[(np.where(landmark_pos < -0.5)[0],np.where(landmark_pos < -0.5)[1]/2)] = 0
159+
160+
# visibility_pos[np.where(landmark_pos)]
161+
pos_locs = pos_locs.astype(np.float32).tostring()
162+
163+
# if pos_locs.shape[0] > 0:
164+
# pos_locs = np.concatenate([pos_locs,np.asarray([row[6]/float(h),row[5]/float(w),
165+
# (row[6]+row[8])/float(h),(row[5]+row[7])/float(w)]).reshape(1,4)],axis=0)
166+
167+
# n_pos_locs = pos_locs.shape[0]
168+
169+
# pos_locs = pos_locs.astype(np.float32).tostring()
170+
# else:
171+
# pos_locs = np.asarray([[row[6]/float(h),row[5]/float(w),(row[6]+row[8])/float(h),(row[5]+row[7])/float(w)]]).reshape(1,4)
172+
# n_pos_locs = pos_locs.shape[0]
173+
# pos_locs = pos_locs.astype(np.float32).tostring()
174+
175+
# else:
176+
# pos_locs = np.asarray([[row[6]/float(h),row[5]/float(w),(row[6]+row[8])/float(h),(row[5]+row[7])/float(w)]]).reshape(1,4)
177+
# n_pos_locs = pos_locs.shape[0]
178+
# pos_locs = pos_locs.astype(np.float32).tostring()
179+
180+
181+
182+
if os.path.isfile(loc_file_path+str(row[1])+'/negative.npy'):
183+
neg_locs = np.load(loc_file_path+str(row[1])+'/negative.npy')[:,:4]
184+
n_neg_locs = neg_locs.shape[0]
185+
cof_locs = np.tile(np.load(loc_file_path+str(row[1])+'/negative.npy')[:,4:6],(1,21))
186+
dim_locs = np.tile(np.load(loc_file_path+str(row[1])+'/negative.npy')[:,6:8],(1,21))
187+
188+
landmark_neg = (landmark - cof_locs)/dim_locs
189+
visibility_neg = np.zeros((landmark_neg.shape[0],21))
190+
191+
# visibility_pos[np.where(landmark_pos)]
192+
neg_locs = neg_locs.astype(np.float32).tostring()
193+
194+
all_landmarks = np.concatenate([landmark_pos,landmark_neg],axis=0)
195+
all_visibilities = np.concatenate([visibility_pos,visibility_neg],axis=0)
196+
all_landmarks = all_landmarks.astype(np.float32).tostring()
197+
all_visibilities = all_visibilities.astype(np.int32).tostring()
198+
199+
img_raw = img_raw.tostring()
200+
201+
print "{},{}".format(n_pos_locs,n_neg_locs)
202+
203+
pose_array = np.asarray([row[2],row[3],row[4]]).astype(np.float32)
204+
205+
206+
pose_array = pose_array.tostring()
207+
# landmark = landmark.tostring()
208+
# visibility=visibility.tostring()
209+
210+
211+
example = tf.train.Example(features=tf.train.Features(feature={
212+
'image_raw':_bytes_feature(img_raw),
213+
'width': _int64_feature(w),
214+
'height': _int64_feature(h),
215+
'face_id': _int64_feature(row[1]),
216+
'pose': _bytes_feature(pose_array),
217+
'loc_x': _int64_feature(row[5]),
218+
'loc_y': _int64_feature(row[6]),
219+
'loc_w': _int64_feature(row[7]),
220+
'loc_h': _int64_feature(row[8]),
221+
'gender':_int64_feature(gender_dict[row[10]]),
222+
'landmarks':_bytes_feature(all_landmarks),
223+
'visibility':_bytes_feature(all_visibilities),
224+
'pos_locs':_bytes_feature(pos_locs),
225+
'neg_locs':_bytes_feature(neg_locs),
226+
'n_pos_locs':_int64_feature(n_pos_locs),
227+
'n_neg_locs':_int64_feature(n_neg_locs)
228+
}))
229+
230+
writer_train.write(example.SerializeToString())
231+
it_train += 1
232+
break
233+
# if it_train >= 1:
234+
# break
235+
# if row[0] in test_images:
236+
# writer_test.write(example.SerializeToString())
237+
# it_test += 1
238+
# else:
239+
# writer_train.write(example.SerializeToString())
240+
# it_train += 1
241+
242+
except Exception as e:
243+
exc_type, exc_obj, exc_tb = sys.exc_info()
244+
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
245+
print(exc_type, fname, exc_tb.tb_lineno)
246+
247+
248+
print it_test,it_train
249+
c.close()
250+
writer_train.close()
251+
writer_test.close()
252+
253+
def extract_tfrecord():
254+
record_iterator = tf.python_io.tf_record_iterator(path=tfrecords_train_filename)
255+
count =0
256+
for string_record in tqdm(record_iterator):
257+
258+
count += 1
259+
example = tf.train.Example()
260+
example.ParseFromString(string_record)
261+
262+
img_string = example.features.feature['image_raw'].bytes_list.value[0]
263+
landmark_string = example.features.feature['landmarks'].bytes_list.value[0]
264+
landmarks = np.fromstring(landmark_string, dtype=np.float32).reshape(21,2)
265+
img_width = int(example.features.feature['width'].int64_list.value[0])
266+
img_height = int(example.features.feature['height'].int64_list.value[0])
267+
268+
img_2 = np.fromstring(img_string, dtype=np.uint8).reshape(-1,1)
269+
270+
img_1d = np.fromstring(img_string, dtype=np.uint8).reshape(img_height,img_width,3)
271+
print img_1d.shape
272+
loc_x = int(example.features.feature['loc_x'].int64_list.value[0])
273+
loc_y = int(example.features.feature['loc_y'].int64_list.value[0])
274+
loc_w = int(example.features.feature['loc_w'].int64_list.value[0])
275+
loc_h = int(example.features.feature['loc_h'].int64_list.value[0])
276+
sex = int(example.features.feature['gender'].int64_list.value[0])
277+
278+
279+
# center_x = img_width/2.0
280+
# center_y = img_height/2.0
281+
282+
# centers = np.tile(np.array([center_x,center_y]).reshape(1,2),(21,1))
283+
# normalized = landmarks - centers
284+
# w_h = np.tile(np.array([img_width,img_height]).reshape(1,2),(21,1))
285+
286+
# normalized = normalized/w_h
287+
288+
# for i in range(normalized.shape[0]):
289+
# if i == 5 or i == 9 or i==15 or i==16:
290+
# continue
291+
# point_x = normalized[i][0]*img_width + img_width/2.0
292+
# point_y = normalized[i][1]*img_height + img_height/2.0
293+
294+
# cv2.circle(img_1d,(int(point_x),int(point_y)), 1, (0,0,255), 2)
295+
296+
# cv2.rectangle(img_1d,(loc_x,loc_y),(loc_x+loc_w,loc_y+loc_h),(0,255,0),3)
297+
# cv2.imshow('result',img_1d)
298+
# cv2.waitKey(0)
299+
300+
301+
302+
if __name__ == '__main__':
303+
test_images = test_names()
304+
print len(test_images)
305+
make_tfrecord(test_images)
306+
#extract_tfrecord()
307+

0 commit comments

Comments
 (0)