def face_img_func(key, entry, viewer): # Image conversion img = entry['img'][0] # Use only a first data in the batch assert(img.ndim == 3 and (img.shape[0] == 1 or img.shape[0] == 3)) img = np.transpose(img, (1, 2, 0)) img = img.copy() # for safety img += 0.5 # [-0.5:0.5] -> [0:1] # Draw try: detection_raw = entry['detection'][0] detection = (detection_raw > 0.5) if 0.0 <= detection_raw <= 1.0: drawing.draw_detection(img, detection) landmark = entry['landmark'][0] visibility = entry['visibility'][0] landmark_color = (0, 1, 0) if detection == 1 else (0, 0, 1) drawing.draw_landmark(img, landmark, visibility, landmark_color, 0.5) pose = entry['pose'][0] drawing.draw_pose(img, pose) gender = entry['gender'][0] if 0.0 <= gender <= 1.0: gender = (gender > 0.5) drawing.draw_gender(img, gender) except KeyError: pass img = (img * 255).astype(np.uint8) caption = '{:02d}'.format(viewer.img_cnts[key]) return {'img': img, 'cap': caption}
def runOnImage(img): img = img.astype(np.float32) / 255.0 landmarks, visibilities, poses, genders, rects = hyperface(img) # Draw results for i in six.moves.xrange(len(landmarks)): landmark = landmarks[i] visibility = visibilities[i] pose = poses[i] gender = genders[i] rect = rects[i] landmark_color = (0, 1, 0) # detected color drawing.draw_landmark(img, landmark, visibility, landmark_color, 0.5, denormalize_scale=False) drawing.draw_pose(img, pose, idx=i) gender = (gender > 0.5) drawing.draw_gender_rect(img, gender, rect) img *= 255 return img
img = imgs[0] detection = detections[0] landmark = landmarks[0] visibility = visibilitys[0] pose = poses[0] gender = genders[0] img = np.transpose(img, (1, 2, 0)) img = img.copy() img += 0.5 # [-0.5:0.5] -> [0:1] detection = (detection > 0.5) gender = (gender > 0.5) # Draw results drawing.draw_detection(img, detection) landmark_color = (0, 1, 0) if detection == 1 else (0, 0, 1) drawing.draw_landmark(img, landmark, visibility, landmark_color, 0.5) drawing.draw_pose(img, pose) drawing.draw_gender(img, gender) # Show image logger.info('Show the result image') # cv2.imshow('result', img) # cv2.waitKey(0) img = img * 255 cv2.imwrite('sample_images/lena_face1.png', img) print('finished at', time.time()) print('cost time', time.time() - init_time)
landmarks, visibilities, poses, genders, rects = hyperface(img) # Draw results for i in six.moves.xrange(len(landmarks)): landmark = landmarks[i] visibility = visibilities[i] pose = poses[i] gender = genders[i] rect = rects[i] landmark_color = (0, 1, 0) # detected color drawing.draw_landmark(img, landmark, visibility, landmark_color, 0.5, denormalize_scale=False) drawing.draw_pose(img, pose, idx=i) gender = (gender > 0.5) # drawing.draw_gender(img, gender, idx=i) drawing.draw_gender_rect(img, gender, rect) # Send to imgviewer img *= 255 # [0:1] -> [0:255] max_cnt = 10 viewer_que.put(('imgs', '{}'.format(cnt_view), {'img': img})) cnt_view = (cnt_view + 1) % max_cnt cnt_img = (cnt_img + 1) % len(test) time.sleep(1.0)