|
import gdown |
|
import gradio as gr |
|
|
|
import logging |
|
import os |
|
|
|
import cv2 |
|
import numpy as np |
|
import tensorflow as tf |
|
|
|
from ai.detection import detect |
|
from laeo_per_frame.interaction_per_frame_uncertainty import LAEO_computation |
|
from utils.hpe import hpe, project_ypr_in2d |
|
from utils.img_util import resize_preserving_ar, draw_detections, percentage_to_pixel, draw_key_points_pose, \ |
|
visualize_vector |
|
|
|
|
|
def load_image(camera, ): |
|
|
|
try: |
|
ret, frame = camera.read() |
|
return True, frame |
|
except: |
|
logging.Logger('Error reading frame') |
|
return False, None |
|
|
|
|
|
def demo_play(img, laeo=True, rgb=False): |
|
|
|
|
|
|
|
|
|
|
|
if not rgb: |
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) |
|
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) |
|
|
|
img_resized, new_old_shape = resize_preserving_ar(img, input_shape_od_model) |
|
|
|
print('inference centernet') |
|
detections, elapsed_time = detect(model, img_resized, min_score_thresh, |
|
new_old_shape) |
|
|
|
img_with_detections = draw_detections(img_resized, detections, max_boxes_to_draw, None, None, None) |
|
|
|
|
|
det, kpt = percentage_to_pixel(img.shape, detections['detection_boxes'], detections['detection_scores'], |
|
detections['detection_keypoints'], detections['detection_keypoint_scores']) |
|
|
|
|
|
|
|
|
|
people_list = [] |
|
|
|
print('inferece hpe') |
|
|
|
for j, kpt_person in enumerate(kpt): |
|
yaw, pitch, roll, tdx, tdy = hpe(gaze_model, kpt_person, detector='centernet') |
|
|
|
|
|
|
|
|
|
people_list.append({'yaw' : yaw[0].numpy()[0], |
|
'yaw_u' : 0, |
|
'pitch' : pitch[0].numpy()[0], |
|
'pitch_u' : 0, |
|
'roll' : roll[0].numpy()[0], |
|
'roll_u' : 0, |
|
'center_xy': [tdx, tdy] |
|
}) |
|
|
|
for i in range(len(det)): |
|
img = draw_key_points_pose(img, kpt[i]) |
|
|
|
|
|
clip_uncertainty = 0.5 |
|
binarize_uncertainty = False |
|
if laeo: |
|
interaction_matrix = LAEO_computation(people_list, clipping_value=clip_uncertainty, |
|
clip=binarize_uncertainty) |
|
else: |
|
interaction_matrix = np.zeros((len(people_list), len(people_list))) |
|
|
|
|
|
for index, person in enumerate(people_list): |
|
green = round((max(interaction_matrix[index, :])) * 255) |
|
colour = (0, green, 0) |
|
if green < 40: |
|
colour = (0, 0, 255) |
|
vector = project_ypr_in2d(person['yaw'], person['pitch'], person['roll']) |
|
img = visualize_vector(img, person['center_xy'], vector, title="", |
|
color=colour) |
|
return img |
|
|
|
|
|
demo = gr.Interface( |
|
fn=demo_play, |
|
inputs=[gr.Image(source="webcam", streaming=True), |
|
gr.Checkbox(value=True, label="LAEO", info="Compute and display LAEO"), |
|
gr.Checkbox(value=True, label="rgb", info="Display output on W/B image"), |
|
], |
|
outputs="image", |
|
live=True |
|
) |
|
|
|
if __name__=='__main__': |
|
if not os.path.exists("data"): |
|
gdown.download_folder("https://drive.google.com/drive/folders/1nQ1Cb_tBEhWxy183t-mIcVH7AhAfa6NO?usp=drive_link", |
|
use_cookies=False) |
|
gaze_model_path = 'data/head_pose_estimation' |
|
gaze_model = tf.keras.models.load_model(gaze_model_path, custom_objects={"tf": tf}) |
|
path_to_model = 'data/keypoint_detector/centernet_hg104_512x512_kpts_coco17_tpu-32' |
|
model = tf.saved_model.load(os.path.join(path_to_model, 'saved_model')) |
|
|
|
input_shape_od_model = (512, 512) |
|
|
|
min_score_thresh, max_boxes_to_draw, min_distance = .45, 50, 1.5 |
|
|
|
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU'))) |
|
|
|
demo.launch() |