Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import mediapipe as mp
|
4 |
+
import gradio as gr
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
mp_drawing = mp.solutions.drawing_utils
|
9 |
+
mp_pose = mp.solutions.pose
|
10 |
+
|
11 |
+
# 棒人間を描画する関数
|
12 |
+
def draw_pose_landmarks(image, results):
|
13 |
+
annotated_image = image.copy()
|
14 |
+
if results.pose_landmarks:
|
15 |
+
mp_drawing.draw_landmarks(
|
16 |
+
annotated_image,
|
17 |
+
results.pose_landmarks,
|
18 |
+
mp_pose.POSE_CONNECTIONS,
|
19 |
+
landmark_drawing_spec=mp_drawing.DrawingSpec(color=(0,255,0), thickness=2, circle_radius=2),
|
20 |
+
connection_drawing_spec=mp_drawing.DrawingSpec(color=(255,0,0), thickness=2)
|
21 |
+
)
|
22 |
+
return annotated_image
|
23 |
+
|
24 |
+
# メイン処理:動画→ポーズ認識→棒人間→新動画出力
|
25 |
+
def process_video(video_file):
|
26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as temp_output:
|
27 |
+
output_path = temp_output.name
|
28 |
+
|
29 |
+
cap = cv2.VideoCapture(video_file)
|
30 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
31 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
32 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
33 |
+
|
34 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
35 |
+
|
36 |
+
with mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5, min_tracking_confidence=0.5) as pose:
|
37 |
+
while cap.isOpened():
|
38 |
+
ret, frame = cap.read()
|
39 |
+
if not ret:
|
40 |
+
break
|
41 |
+
|
42 |
+
# RGBに変換
|
43 |
+
image_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
44 |
+
results = pose.process(image_rgb)
|
45 |
+
|
46 |
+
# 棒人間描画
|
47 |
+
annotated_frame = draw_pose_landmarks(frame, results)
|
48 |
+
out.write(annotated_frame)
|
49 |
+
|
50 |
+
cap.release()
|
51 |
+
out.release()
|
52 |
+
|
53 |
+
return output_path
|
54 |
+
|
55 |
+
# Gradioインターフェース
|
56 |
+
interface = gr.Interface(
|
57 |
+
fn=process_video,
|
58 |
+
inputs=gr.Video(label="動画をアップロードしてください"),
|
59 |
+
outputs=gr.Video(label="ポーズ認識後の動画"),
|
60 |
+
title="MediaPipeによる棒人間ポーズ認識",
|
61 |
+
description="アップロードした動画に対してMediaPipeでポーズ検出し、棒人間を描画します。"
|
62 |
+
)
|
63 |
+
|
64 |
+
if __name__ == "__main__":
|
65 |
+
interface.launch()
|