Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,60 +3,56 @@ import matplotlib.pyplot as plt
|
|
| 3 |
import copy
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
from src import model
|
| 7 |
from src import util
|
| 8 |
from src.body import Body
|
| 9 |
from src.hand import Hand
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def pose_estimation(test_image):
|
| 13 |
bgr_image_path = './test.png'
|
| 14 |
with open(bgr_image_path, 'wb') as bgr_file:
|
| 15 |
bgr_file.write(test_image)
|
| 16 |
-
#
|
| 17 |
body_estimation = Body('model/body_pose_model.pth')
|
| 18 |
hand_estimation = Hand('model/hand_pose_model.pth')
|
| 19 |
|
| 20 |
-
|
| 21 |
-
oriImg = cv2.imread(test_image) # B,G,R order
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
# 姿态估计
|
| 26 |
candidate, subset = body_estimation(oriImg)
|
| 27 |
canvas = copy.deepcopy(oriImg)
|
| 28 |
-
# 绘制身体姿态
|
| 29 |
canvas = util.draw_bodypose(canvas, candidate, subset)
|
| 30 |
-
# print(candidate)
|
| 31 |
-
# print(subset)
|
| 32 |
-
# detect hand
|
| 33 |
hands_list = util.handDetect(candidate, subset, oriImg)
|
| 34 |
|
| 35 |
all_hand_peaks = []
|
| 36 |
for x, y, w, is_left in hands_list:
|
| 37 |
-
# cv2.rectangle(canvas, (x, y), (x+w, y+w), (0, 255, 0), 2, lineType=cv2.LINE_AA)
|
| 38 |
-
# cv2.putText(canvas, 'left' if is_left else 'right', (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 39 |
-
|
| 40 |
-
# if is_left:
|
| 41 |
-
# plt.imshow(oriImg[y:y+w, x:x+w, :][:, :, [2, 1, 0]])
|
| 42 |
-
# plt.show()
|
| 43 |
peaks = hand_estimation(oriImg[y:y+w, x:x+w, :])
|
| 44 |
peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], peaks[:, 0]+x)
|
| 45 |
peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y)
|
| 46 |
-
# else:
|
| 47 |
-
# peaks = hand_estimation(cv2.flip(oriImg[y:y+w, x:x+w, :], 1))
|
| 48 |
-
# peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], w-peaks[:, 0]-1+x)
|
| 49 |
-
# peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y)
|
| 50 |
-
# print(peaks)
|
| 51 |
all_hand_peaks.append(peaks)
|
| 52 |
|
| 53 |
canvas = util.draw_handpose(canvas, all_hand_peaks)
|
| 54 |
|
| 55 |
plt.imshow(canvas[:, :, [2, 1, 0]])
|
| 56 |
plt.axis('off')
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Convert the image path to bytes for Gradio to display
|
| 62 |
def convert_image_to_bytes(image_path):
|
|
@@ -65,39 +61,30 @@ def convert_image_to_bytes(image_path):
|
|
| 65 |
|
| 66 |
# Gradio interface
|
| 67 |
with gr.Blocks() as demo:
|
| 68 |
-
gr.Markdown(
|
| 69 |
-
'''
|
| 70 |
-
This space displays how to perform Pose Estimation.
|
| 71 |
-
## How to use this Space?
|
| 72 |
-
- Upload an image, preferably with a whole view of body.
|
| 73 |
-
- You will receive the result of the Pose Estimation after 5-10 seconds.
|
| 74 |
-
- Click the 'clear' button to clear all the files.
|
| 75 |
-
## Examples
|
| 76 |
-
- You can get the test examples from our [OpenPose Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/openpose)
|
| 77 |
-
'''
|
| 78 |
-
)
|
| 79 |
with gr.Row():
|
| 80 |
image = gr.File(label="Upload Image", type="binary")
|
| 81 |
output_image = gr.Image(label="Estimation Result")
|
|
|
|
| 82 |
submit_button = gr.Button("Start Estimation")
|
| 83 |
-
|
| 84 |
# Run pose estimation and display results when the button is clicked
|
| 85 |
submit_button.click(
|
| 86 |
pose_estimation,
|
| 87 |
inputs=[image],
|
| 88 |
-
outputs=[output_image]
|
| 89 |
)
|
| 90 |
-
|
| 91 |
# Clear the results
|
| 92 |
clear_button = gr.Button("Clear")
|
| 93 |
def clear_outputs():
|
| 94 |
output_image.clear()
|
|
|
|
| 95 |
clear_button.click(
|
| 96 |
clear_outputs,
|
| 97 |
inputs=[],
|
| 98 |
-
outputs=[output_image]
|
| 99 |
)
|
| 100 |
|
| 101 |
-
# ?
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch(debug=True)
|
|
|
|
| 3 |
import copy
|
| 4 |
import numpy as np
|
| 5 |
import gradio as gr
|
| 6 |
+
import json # Import json module
|
| 7 |
from src import model
|
| 8 |
from src import util
|
| 9 |
from src.body import Body
|
| 10 |
from src.hand import Hand
|
| 11 |
|
| 12 |
+
# This function will generate and save the pose data as JSON
|
| 13 |
+
def save_json(candidate, subset, json_file_path='./pose_data.json'):
|
| 14 |
+
pose_data = {
|
| 15 |
+
'candidate': candidate.tolist(),
|
| 16 |
+
'subset': subset.tolist()
|
| 17 |
+
}
|
| 18 |
+
with open(json_file_path, 'w') as json_file:
|
| 19 |
+
json.dump(pose_data, json_file)
|
| 20 |
+
return json_file_path
|
| 21 |
|
| 22 |
def pose_estimation(test_image):
|
| 23 |
bgr_image_path = './test.png'
|
| 24 |
with open(bgr_image_path, 'wb') as bgr_file:
|
| 25 |
bgr_file.write(test_image)
|
| 26 |
+
# Load the estimation models
|
| 27 |
body_estimation = Body('model/body_pose_model.pth')
|
| 28 |
hand_estimation = Hand('model/hand_pose_model.pth')
|
| 29 |
|
| 30 |
+
oriImg = cv2.imread(bgr_image_path) # B,G,R order
|
|
|
|
| 31 |
|
| 32 |
+
# Perform pose estimation
|
|
|
|
|
|
|
| 33 |
candidate, subset = body_estimation(oriImg)
|
| 34 |
canvas = copy.deepcopy(oriImg)
|
|
|
|
| 35 |
canvas = util.draw_bodypose(canvas, candidate, subset)
|
|
|
|
|
|
|
|
|
|
| 36 |
hands_list = util.handDetect(candidate, subset, oriImg)
|
| 37 |
|
| 38 |
all_hand_peaks = []
|
| 39 |
for x, y, w, is_left in hands_list:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
peaks = hand_estimation(oriImg[y:y+w, x:x+w, :])
|
| 41 |
peaks[:, 0] = np.where(peaks[:, 0]==0, peaks[:, 0], peaks[:, 0]+x)
|
| 42 |
peaks[:, 1] = np.where(peaks[:, 1]==0, peaks[:, 1], peaks[:, 1]+y)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
all_hand_peaks.append(peaks)
|
| 44 |
|
| 45 |
canvas = util.draw_handpose(canvas, all_hand_peaks)
|
| 46 |
|
| 47 |
plt.imshow(canvas[:, :, [2, 1, 0]])
|
| 48 |
plt.axis('off')
|
| 49 |
+
out_image_path = './out.jpg'
|
| 50 |
+
plt.savefig(out_image_path)
|
| 51 |
+
|
| 52 |
+
# Save JSON data and return its path
|
| 53 |
+
json_file_path = save_json(candidate, subset)
|
| 54 |
+
|
| 55 |
+
return out_image_path, json_file_path
|
| 56 |
|
| 57 |
# Convert the image path to bytes for Gradio to display
|
| 58 |
def convert_image_to_bytes(image_path):
|
|
|
|
| 61 |
|
| 62 |
# Gradio interface
|
| 63 |
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# Pose Estimation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Row():
|
| 66 |
image = gr.File(label="Upload Image", type="binary")
|
| 67 |
output_image = gr.Image(label="Estimation Result")
|
| 68 |
+
output_json = gr.File(label="Download Pose Data as JSON", type="file") # Add JSON output
|
| 69 |
submit_button = gr.Button("Start Estimation")
|
| 70 |
+
|
| 71 |
# Run pose estimation and display results when the button is clicked
|
| 72 |
submit_button.click(
|
| 73 |
pose_estimation,
|
| 74 |
inputs=[image],
|
| 75 |
+
outputs=[output_image, output_json] # Update outputs
|
| 76 |
)
|
| 77 |
+
|
| 78 |
# Clear the results
|
| 79 |
clear_button = gr.Button("Clear")
|
| 80 |
def clear_outputs():
|
| 81 |
output_image.clear()
|
| 82 |
+
output_json.clear() # Clear JSON output as well
|
| 83 |
clear_button.click(
|
| 84 |
clear_outputs,
|
| 85 |
inputs=[],
|
| 86 |
+
outputs=[output_image, output_json] # Update outputs
|
| 87 |
)
|
| 88 |
|
|
|
|
| 89 |
if __name__ == "__main__":
|
| 90 |
demo.launch(debug=True)
|