File size: 3,881 Bytes
afd2199
577bb6b
e2522a6
afd2199
 
cd41c0c
a21de06
afd2199
e2522a6
 
08528b3
 
 
aef40ac
08528b3
 
 
 
 
 
 
 
 
 
8db8164
 
c76c2fc
8db8164
92499cd
fa05982
8db8164
abeb59d
4ccfac3
 
 
4d440ec
 
 
e2522a6
 
a21de06
e2522a6
 
 
 
 
a21de06
e2522a6
a21de06
e2522a6
 
 
 
332fb03
e2522a6
 
 
 
a21de06
e2522a6
 
0e5fb59
ceba3ce
 
aef40ac
b1ec559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcbfb53
92499cd
 
afd2199
 
 
 
 
 
 
 
 
92499cd
afd2199
31fdeeb
c76c2fc
92499cd
0e5fb59
 
 
 
 
 
 
92499cd
 
 
 
 
 
 
 
 
 
 
 
31fdeeb
 
 
 
 
 
6b7f67c
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
import mmpose
import os
import glob
from mmpose.apis import MMPoseInferencer
import gradio as gr
import numpy as np
import cv2

print("[INFO]: Imported modules!")

# inferencer = MMPoseInferencer('hand') # 'hand', 'human , device='cuda'
# inferencer = MMPoseInferencer('human')

inferencer = MMPoseInferencer(pose3d="human3d")
# https://github.com/open-mmlab/mmpose/tree/dev-1.x/configs/body_3d_keypoint/pose_lift
# motionbert_ft_h36m-d80af323_20230531.pth
# simple3Dbaseline_h36m-f0ad73a4_20210419.pth
# videopose_h36m_243frames_fullconv_supervised_cpn_ft-88f5abbb_20210527.pth
# videopose_h36m_81frames_fullconv_supervised-1f2d1104_20210527.pth
# videopose_h36m_27frames_fullconv_supervised-fe8fbba9_20210527.pth
# videopose_h36m_1frame_fullconv_supervised_cpn_ft-5c3afaed_20210527.pth

# https://github.com/open-mmlab/mmpose/blob/main/mmpose/apis/inferencers/pose3d_inferencer.py

print("[INFO]: Downloaded models!")


def poses(photo):
    print("[INFO]: Running inference!")
    result_generator = inferencer(photo, 
                                  vis_out_dir =".",
                                  return_vis=True,
                                  thickness=2,
                                  rebase_keypoint_height=True)    
    
    result = next(result_generator)
    #for result in result_generator:
    #     print("[INFO] Result: ", result)
    # # Prepare to save video
    # output_file = os.path.join("output.mp4")

    # fourcc = cv2.VideoWriter_fourcc(*"mp4v")  # Codec for MP4 video
    # fps = 32
    # height = 480
    # width = 640
    # size = (width,height)

    # out_writer = cv2.VideoWriter(output_file, fourcc, fps, size)

    # for result in result_generator:
    #     print("[INFO] Result: ", result)
    #     frame = result["visualization"]
    #     out_writer.write(cv2.cvtColor(frame[0], cv2.COLOR_BGR2RGB))

    # print(os.listdir())
    # print("[INFO]: Visualizing results!")
    # print(os.listdir())
    # print()

    # out_writer.release()
    # cv2.destroyAllWindows() # Closing window
    output_file = glob.glob("*.mp4")
    print(glob.glob("*.mp4"))
    print(glob.glob("*.avi"))

    cap = cv2.VideoCapture(os.path.join(output_file[0]))
    formatfile = "test.mp4"
    fourcc = cv2.VideoWriter_fourcc(*"mp4v")  # Codec for MP4 video
    fps = 32
    height = 480
    width = 640
    size = (width,height)

    out_writer = cv2.VideoWriter(formatfile, fourcc, fps, size)
    
    while cap.isOpened():
        ret, frame = cap.read()
        out_writer.write(frame)


    # Release everything if job is finished
    cap.release()
    out_writer.release()
    cv2.destroyAllWindows()


    return formatfile #output_file[0]



# # specify detection model by alias
# # the available aliases include 'human', 'hand', 'face', 'animal',
# # as well as any additional aliases defined in mmdet
# inferencer = MMPoseInferencer(
#     # suppose the pose estimator is trained on custom dataset
#     pose2d='custom_human_pose_estimator.py',
#     pose2d_weights='custom_human_pose_estimator.pth',
#     det_model='human'
# )

    
def run():
    #https://github.com/open-mmlab/mmpose/blob/main/docs/en/user_guides/inference.md
    webcam = gr.Interface(
        fn=poses,
        inputs= gr.Video(source="webcam"),
        outputs =  gr.PlayableVideo(format='mp4', interactive=True),
        title = 'Pose estimation', 
        description = 'Pose estimation on video',
        allow_flagging=False
        )

    file = gr.Interface(
        poses,
        inputs = gr.Video(source="upload"),
        outputs = gr.PlayableVideo(format='mp4', interactive=True),
        allow_flagging=False
    )
    demo = gr.TabbedInterface(
            interface_list=[file, webcam],
            tab_names=["From a File", "From your Webcam"]
        )



    demo.launch(server_name="0.0.0.0", server_port=7860)


if __name__ == "__main__":
    run()