summaryrefslogtreecommitdiff
path: root/megapixels/app
diff options
context:
space:
mode:
Diffstat (limited to 'megapixels/app')
-rw-r--r--megapixels/app/server/tasks/demo.py169
1 files changed, 65 insertions, 104 deletions
diff --git a/megapixels/app/server/tasks/demo.py b/megapixels/app/server/tasks/demo.py
index acc5dbac..38a0a3c2 100644
--- a/megapixels/app/server/tasks/demo.py
+++ b/megapixels/app/server/tasks/demo.py
@@ -33,7 +33,9 @@ def demo_task(self, uuid_name, fn):
opt_run_2d_68 = True
opt_run_3d_68 = True
opt_run_3d_68 = True
- paths
+
+ opt_gif_size = (256, 256,)
+ opt_gif_frames = 15
meta = {
'step': 0,
@@ -50,6 +52,17 @@ def demo_task(self, uuid_name, fn):
log.debug('> {}'.format(msg))
self.update_state(state='PROCESSING', meta=meta)
+ def save_image(key, title, data):
+ fn = '{}_{}.jpg'.format(uuid_name, key)
+ fpath = os.path.join(cfg.DIR_SITE_USER_CONTENT, fn)
+ paths.append(fpath)
+ cv.imwrite(fpath, data)
+
+ meta['data'][key] = {
+ 'title': title,
+ 'url': os.path.join('/user_content/', fn),
+ }
+
step('Loading image')
self.update_state(state='PROCESSING', meta=meta)
@@ -100,81 +113,69 @@ def demo_task(self, uuid_name, fn):
save_image('landmarks_3d_68', '3D Landmarks', im_landmarks_3d_68)
- def save_image(key, title, data):
- fn = '{}_{}.jpg'.format(uuid_name, key)
- fpath = os.path.join(cfg.DIR_SITE_USER_CONTENT, fn)
- paths.append(fpath)
- cv.imwrite(fpath, im_landmarks_3d_68)
-
- meta['data']['landmarks_3d_68'] = {
- 'title': '3D Landmarks',
- 'url': os.path.join('/user_content/', fn),
- }
- step('Generated 3D Landmarks', step=0)
-
# ----------------------------------------------------------------------------
# generate 3D GIF animation
- # step('Generating GIF Animation')
- # log.info('generating 3D animation...')
- # if not opt_fp_out:
- # fpp_im = Path(opt_fp_in)
- # fp_out = join(fpp_im.parent, f'{fpp_im.stem}_anim.gif')
- # else:
- # fp_out = opt_fp_out
- # st = time.time()
- # plot_utils.generate_3d_landmark_anim(np.array(points_3d_68), fp_out,
- # size=opt_gif_size, num_frames=opt_gif_frames)
- # log.info(f'Generated animation in {(time.time() - st):.2f}s')
- # log.info(f'Saved to: {fp_out}')
- # log.info('')
+ step('Generating GIF Animation')
+ log.info('generating 3D animation...')
+ fn = '{}_{}.gif'.format(uuid_name, '3d')
+ fp_out = os.path.join(cfg.DIR_SITE_USER_CONTENT, fn)
+ paths.append(fp_out)
- # # ----------------------------------------------------------------------------
- # # generate face vectors, only to test if feature extraction works
-
- # step('Generating face vectors')
- # log.info('initialize face recognition model...')
- # from app.processors import face_recognition
- # face_rec = face_recognition.RecognitionDLIB()
- # st = time.time()
- # log.info('generating face vector...')
- # vec = face_rec.vec(im_resized, bbox_dim)
- # log.info(f'generated face vector in {(time.time() - st):.2f}s')
- # log.info('')
+ st = time.time()
+ plot_utils.generate_3d_landmark_anim(np.array(points_3d_68), fp_out,
+ size=opt_gif_size, num_frames=opt_gif_frames)
+ log.info(f'Generated animation in {(time.time() - st):.2f}s')
+ log.info(f'Saved to: {fp_out}')
+ log.info('')
+ meta['data']['points_3d_68'] = points_3d_68
+ meta['data']['points_3d_68'] = {
+ 'title': '3D Animated GIF',
+ 'url': os.path.join('/user_content/', fn),
+ }
- # # ----------------------------------------------------------------------------
- # # generate 68 point landmarks using dlib
+ # ----------------------------------------------------------------------------
+ # generate 68 point landmarks using dlib
- # step('Generating 2D 68PT landmarks')
- # log.info('initializing face landmarks 68 dlib...')
- # from app.processors import face_landmarks
- # landmark_detector_2d_68 = face_landmarks.Dlib2D_68()
- # log.info('generating 2D 68PT landmarks...')
- # st = time.time()
- # points_2d_68 = landmark_detector_2d_68.landmarks(im_resized, bbox_dim)
- # log.info(f'generated 2D 68PT face landmarks in {(time.time() - st):.2f}s')
- # log.info('')
+ step('Generating 2D 68PT landmarks')
+ log.info('initializing face landmarks 68 dlib...')
+ from app.processors import face_landmarks
+ landmark_detector_2d_68 = face_landmarks.Dlib2D_68()
+ log.info('generating 2D 68PT landmarks...')
+ st = time.time()
+ points_2d_68 = landmark_detector_2d_68.landmarks(im_resized, bbox_dim)
+ log.info(f'generated 2D 68PT face landmarks in {(time.time() - st):.2f}s')
+ log.info('')
+ # draw 2d landmarks
+ im_landmarks_2d_68 = im_resized.copy()
+ draw_utils.draw_landmarks2D(im_landmarks_2d_68, points_2d_68)
+ draw_utils.draw_bbox(im_landmarks_2d_68, bbox_dim)
+ save_image('landmarks_2d_68', '2D Landmarks', im_landmarks_2d_68)
- # # ----------------------------------------------------------------------------
- # # generate pose from 68 point 2D landmarks
+ # ----------------------------------------------------------------------------
+ # generate pose from 68 point 2D landmarks
- # if opt_run_pose:
- # step('Generating pose')
- # log.info('initialize pose...')
- # from app.processors import face_pose
- # pose_detector = face_pose.FacePoseDLIB()
- # log.info('generating pose...')
- # st = time.time()
- # pose_data = pose_detector.pose(points_2d_68, dim)
- # log.info(f'generated pose {(time.time() - st):.2f}s')
- # log.info('')
+ if opt_run_pose:
+ step('Generating pose')
+ log.info('initialize pose...')
+ from app.processors import face_pose
+ pose_detector = face_pose.FacePoseDLIB()
+ log.info('generating pose...')
+ st = time.time()
+ pose_data = pose_detector.pose(points_2d_68, dim)
+ log.info(f'generated pose {(time.time() - st):.2f}s')
+ log.info('')
+ im_pose = im_resized.copy()
+ draw_utils.draw_pose(im_pose, pose_data['point_nose'], pose_data['points'])
+ draw_utils.draw_degrees(im_pose, pose_data)
+ save_image('pose', 'Pose', im_pose)
- # # ----------------------------------------------------------------------------
- # # generate pose from 68 point 2D landmarks
+ # ----------------------------------------------------------------------------
+ # generate pose from 68 point 2D landmarks
step('Done')
@@ -183,7 +184,6 @@ def demo_task(self, uuid_name, fn):
# self.log.debug('Add age apparent')
# self.log.debug('Add gender')
-
# # 3DDFA
# self.log.debug('Add depth')
# self.log.debug('Add pncc')
@@ -193,48 +193,9 @@ def demo_task(self, uuid_name, fn):
# self.log.debug('Add face texture flat')
# self.log.debug('Add ethnicity')
- # display
- # draw bbox
-
- # # draw 2d landmarks
- # im_landmarks_2d_68 = im_resized.copy()
- # draw_utils.draw_landmarks2D(im_landmarks_2d_68, points_2d_68)
- # draw_utils.draw_bbox(im_landmarks_2d_68, bbox_dim)
-
- # # draw pose
- # if opt_run_pose:
- # im_pose = im_resized.copy()
- # draw_utils.draw_pose(im_pose, pose_data['point_nose'], pose_data['points'])
- # draw_utils.draw_degrees(im_pose, pose_data)
-
- # # draw animated GIF
- # im = Image.open(fp_out)
- # im_frames = []
- # duration = im.info['duration']
- # try:
- # while True:
- # im.seek(len(im_frames))
- # mypalette = im.getpalette()
- # im.putpalette(mypalette)
- # im_jpg = Image.new("RGB", im.size)
- # im_jpg.paste(im)
- # im_np = im_utils.pil2np(im_jpg.copy())
- # im_frames.append(im_np)
- # except EOFError:
- # pass # end of GIF sequence
-
- # n_frames = len(im_frames)
- # frame_number = 0
-
- # # show all images here
- # cv.imshow('Original', im_resized)
- # cv.imshow('2D 68PT Landmarks', im_landmarks_2d_68)
- # cv.imshow('3D 68PT Landmarks', im_landmarks_3d_68)
- # cv.imshow('Pose', im_pose)
- # cv.imshow('3D 68pt GIF', im_frames[frame_number])
-
log.debug('done!!')
+ time.sleep(3)
for path in paths:
if os.path.exists(path):
os.remove(path)