File size: 4,740 Bytes
613f91b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import gradio as gr
import math

demo = gr.Blocks()

def estimate(input_img):
  threshold = 0.9
  img  = Image.fromarray(input_img.astype('uint8'),'RGB')

  (w, h) = img.size
  image_intrinsics = np.array([[w + h, 0, w // 2], [0, w + h, h // 2], [0, 0, 1]])
      
  res = img2pose_model.predict([transform(img)])[0]

  all_bboxes = res["boxes"].cpu().numpy().astype('float')

  poses = []
  bboxes = []
  for i in range(len(all_bboxes)):
      if res["scores"][i] > threshold:
          bbox = all_bboxes[i]
          pose_pred = res["dofs"].cpu().numpy()[i].astype('float')
          pose_pred = pose_pred.squeeze()

          poses.append(pose_pred)  
          bboxes.append(bbox)

  trans_vertices = renderer.transform_vertices(img, poses)
  img = renderer.render(img, trans_vertices, alpha=1)    

  plt.figure(figsize=(8, 8))     
    
  for bbox in bboxes:
      plt.gca().add_patch(patches.Rectangle((bbox[0], bbox[1]), bbox[2] - bbox[0], bbox[3] - bbox[1],linewidth=3,edgecolor='b',facecolor='none'))            
    
  plt.imshow(img)        
  plt.show()
  pose_pred[0] = math.degrees(pose_pred[0])
  pose_pred[1] = math.degrees(pose_pred[1])
  pose_pred[2] = math.degrees(pose_pred[2])
  listToStr = ' '.join([str(elem) for elem in pose_pred])
  return img,pose_pred[0],pose_pred[1],pose_pred[2],pose_pred[3],pose_pred[4],pose_pred[5]


def estimate_crowd(input_img):
  threshold = 0.5
  img  = Image.fromarray(input_img.astype('uint8'),'RGB')

  (w, h) = img.size
  image_intrinsics = np.array([[w + h, 0, w // 2], [0, w + h, h // 2], [0, 0, 1]])
  
  res = img2pose_model.predict([transform(img)])[0]

  all_bboxes = res["boxes"].cpu().numpy().astype('float')

  poses = []
  bboxes = []
  for i in range(len(all_bboxes)):
      if res["scores"][i] > threshold:
          bbox = all_bboxes[i]
          pose_pred = res["dofs"].cpu().numpy()[i].astype('float')
          pose_pred = pose_pred.squeeze()

          poses.append(pose_pred)  
          bboxes.append(bbox)


  #render_plot(img.copy(), poses, bboxes)
  
  n = str((len(bboxes)))
  (w, h) = img.size
  image_intrinsics = np.array([[w + h, 0, w // 2], [0, w + h, h // 2], [0, 0, 1]])
    
  trans_vertices = renderer.transform_vertices(img, poses)
  img = renderer.render(img, trans_vertices, alpha=1) 
  return img, n


with demo:

    gr.Markdown("Head Pose Estimation in Crowd")

    with gr.Tabs():
        with gr.TabItem("What is Head Pose Estimation?"):
           gr.Markdown(
        """
            ### Head pose estimation in crowd (HPECS) is a task of estimating the head pose of people in the crowd scenes.

            Six degrees of freedom of head pose is the freedom of movement of the head pose in three-dimensional space. 6DoF is the 
            abbreviation of six degrees of freedoms. 6DoF of head pose records the rotational motion such as
            pitch, yaw and roll , and translational motions of the head pose including up/down, left/right and front/back. 
            6DoF of 3D head pose is being regressed as it can harvest more versatile 
            spatial information which able to track the movement of object even in a still image. 

    """
          )
           gr.Image("/content/gdrive/MyDrive/project_folder/img2pose/6DoF.gif",size=20)

 
        with gr.TabItem("Head Pose Estimation"):
            gr.Markdown(
        """
            ### Please insert image with single head

    """
          )
            image = gr.inputs.Image()
            text_button = gr.Button("Estimate")
            with gr.Row():
              outputs=[gr.outputs.Image(),gr.outputs.Textbox(type="auto", label="Yaw"),
                                              gr.outputs.Textbox(type="auto", label="Pitch"),
                                              gr.outputs.Textbox(type="auto", label="Roll"),
                                              gr.outputs.Textbox(type="auto", label="tx"),
                                              gr.outputs.Textbox(type="auto", label="ty"),
                                              gr.outputs.Textbox(type="auto", label="tz")]
            
        with gr.TabItem("Estimate Head Pose in Crowd"):
            gr.Markdown(
          """
              ### Please insert crowd scene image

      """
            )
            image_input = gr.Image()
            with gr.Row():
                
                image_output = [gr.outputs.Image(),gr.outputs.Textbox(type="auto", label="Number of head pose estimated"),
                                            ]
            image_button = gr.Button("Estimate")

    text_button.click(estimate, inputs=image, outputs=outputs)
    image_button.click(estimate_crowd, inputs=image_input, outputs=image_output)

demo.launch()