joselobenitezg commited on
Commit
783db6b
·
verified ·
1 Parent(s): 46a60b0

add zero gpu

Browse files
Files changed (1) hide show
  1. app.py +62 -11
app.py CHANGED
@@ -2,6 +2,8 @@ import os
2
  import gradio as gr
3
  import numpy as np
4
  from PIL import Image
 
 
5
 
6
  from inference.seg import process_image_or_video
7
  from config import SAPIENS_LITE_MODELS_PATH
@@ -10,7 +12,8 @@ def update_model_choices(task):
10
  model_choices = list(SAPIENS_LITE_MODELS_PATH[task.lower()].keys())
11
  return gr.Dropdown(choices=model_choices, value=model_choices[0] if model_choices else None)
12
 
13
- def gradio_wrapper(input_image, task, version):
 
14
  if isinstance(input_image, np.ndarray):
15
  input_image = Image.fromarray(input_image)
16
 
@@ -18,38 +21,86 @@ def gradio_wrapper(input_image, task, version):
18
 
19
  return result
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  with gr.Blocks() as demo:
22
- gr.Markdown("# Sapiens Arena 🤸🏽‍♂️ - WIP devmode- Not yet available")
23
  with gr.Tabs():
24
  with gr.TabItem('Image'):
25
  with gr.Row():
26
  with gr.Column():
27
  input_image = gr.Image(label="Input Image", type="pil")
28
- select_task = gr.Radio(
29
  ["seg", "pose", "depth", "normal"],
30
  label="Task",
31
  info="Choose the task to perform",
32
  value="seg"
33
  )
34
- model_name = gr.Dropdown(
35
  label="Model Version",
36
  choices=list(SAPIENS_LITE_MODELS_PATH["seg"].keys()),
37
  value="sapiens_0.3b",
38
  )
39
  with gr.Column():
40
  result_image = gr.Image(label="Result")
41
- run_button = gr.Button("Run")
42
 
43
  with gr.TabItem('Video'):
44
- gr.Markdown("In construction")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- select_task.change(fn=update_model_choices, inputs=select_task, outputs=model_name)
 
47
 
48
- run_button.click(
49
- fn=gradio_wrapper,
50
- inputs=[input_image, select_task, model_name],
51
  outputs=[result_image],
52
  )
53
 
 
 
 
 
 
 
54
  if __name__ == "__main__":
55
- demo.launch(share=True)
 
2
  import gradio as gr
3
  import numpy as np
4
  from PIL import Image
5
+ import cv2
6
+ import spaces
7
 
8
  from inference.seg import process_image_or_video
9
  from config import SAPIENS_LITE_MODELS_PATH
 
12
  model_choices = list(SAPIENS_LITE_MODELS_PATH[task.lower()].keys())
13
  return gr.Dropdown(choices=model_choices, value=model_choices[0] if model_choices else None)
14
 
15
+ @spaces.GPU(duration=120)
16
+ def process_image(input_image, task, version):
17
  if isinstance(input_image, np.ndarray):
18
  input_image = Image.fromarray(input_image)
19
 
 
21
 
22
  return result
23
 
24
+ def process_video(input_video, task, version):
25
+ cap = cv2.VideoCapture(input_video)
26
+ fps = cap.get(cv2.CAP_PROP_FPS)
27
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
28
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
29
+
30
+ output_video = cv2.VideoWriter('output_video.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
31
+
32
+ while cap.isOpened():
33
+ ret, frame = cap.read()
34
+ if not ret:
35
+ break
36
+
37
+ frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
38
+ processed_frame = process_image_or_video(frame_rgb, task=task.lower(), version=version)
39
+
40
+ if processed_frame is not None:
41
+ processed_frame_bgr = cv2.cvtColor(np.array(processed_frame), cv2.COLOR_RGB2BGR)
42
+ output_video.write(processed_frame_bgr)
43
+
44
+ cap.release()
45
+ output_video.release()
46
+
47
+ return 'output_video.mp4'
48
+
49
  with gr.Blocks() as demo:
50
+ gr.Markdown("# Sapiens Arena 🤸🏽‍♂️ - WIP devmode")
51
  with gr.Tabs():
52
  with gr.TabItem('Image'):
53
  with gr.Row():
54
  with gr.Column():
55
  input_image = gr.Image(label="Input Image", type="pil")
56
+ select_task_image = gr.Radio(
57
  ["seg", "pose", "depth", "normal"],
58
  label="Task",
59
  info="Choose the task to perform",
60
  value="seg"
61
  )
62
+ model_name_image = gr.Dropdown(
63
  label="Model Version",
64
  choices=list(SAPIENS_LITE_MODELS_PATH["seg"].keys()),
65
  value="sapiens_0.3b",
66
  )
67
  with gr.Column():
68
  result_image = gr.Image(label="Result")
69
+ run_button_image = gr.Button("Run")
70
 
71
  with gr.TabItem('Video'):
72
+ with gr.Row():
73
+ with gr.Column():
74
+ input_video = gr.Video(label="Input Video")
75
+ select_task_video = gr.Radio(
76
+ ["seg", "pose", "depth", "normal"],
77
+ label="Task",
78
+ info="Choose the task to perform",
79
+ value="seg"
80
+ )
81
+ model_name_video = gr.Dropdown(
82
+ label="Model Version",
83
+ choices=list(SAPIENS_LITE_MODELS_PATH["seg"].keys()),
84
+ value="sapiens_0.3b",
85
+ )
86
+ with gr.Column():
87
+ result_video = gr.Video(label="Result")
88
+ run_button_video = gr.Button("Run")
89
 
90
+ select_task_image.change(fn=update_model_choices, inputs=select_task_image, outputs=model_name_image)
91
+ select_task_video.change(fn=update_model_choices, inputs=select_task_video, outputs=model_name_video)
92
 
93
+ run_button_image.click(
94
+ fn=process_image,
95
+ inputs=[input_image, select_task_image, model_name_image],
96
  outputs=[result_image],
97
  )
98
 
99
+ run_button_video.click(
100
+ fn=process_video,
101
+ inputs=[input_video, select_task_video, model_name_video],
102
+ outputs=[result_video],
103
+ )
104
+
105
  if __name__ == "__main__":
106
+ demo.launch(share=False)