File size: 1,954 Bytes
1ea0f2c
 
6326d5f
 
 
1ea0f2c
 
 
 
 
 
 
 
 
 
6326d5f
1ea0f2c
 
6326d5f
 
 
 
 
 
1ea0f2c
 
6326d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ea0f2c
 
 
6326d5f
 
 
 
 
 
 
 
 
 
 
1ea0f2c
 
 
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
from flask import Flask, request, jsonify
import torch
from PIL import Image
from io import BytesIO
import torchvision.transforms as transforms
from transformers import AutoModelForSequenceClassification, AutoTokenizer

# Load Meta Sapiens Pose model
sapiens_model = torch.jit.load('/models/sapiens_pose/model.pt')
sapiens_model.eval()

# Load MotionBERT model
motionbert_model = AutoModelForSequenceClassification.from_pretrained('/models/motionbert')
motionbert_tokenizer = AutoTokenizer.from_pretrained('/models/motionbert')

# Flask app
app = Flask(__name__)

# Define a transformation for input images
transform = transforms.Compose([
    transforms.Resize((256, 256)),  # Resize image to the required size
    transforms.ToTensor(),           # Convert image to PyTorch tensor
])

@app.route('/pose_estimation', methods=['POST'])
def pose_estimation():
    try:
        # Accept an image file as input for pose estimation
        image = request.files['image']
        img = Image.open(BytesIO(image.read()))
        
        # Preprocess the image
        img_tensor = transform(img).unsqueeze(0)  # Add batch dimension

        # Perform pose estimation
        with torch.no_grad():
            pose_result = sapiens_model(img_tensor)

        return jsonify({"pose_result": pose_result.tolist()})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

@app.route('/sequence_analysis', methods=['POST'])
def sequence_analysis():
    try:
        # Accept keypoint data as input for sequence analysis
        keypoints = request.json['keypoints']
        inputs = motionbert_tokenizer(keypoints, return_tensors="pt")

        with torch.no_grad():
            sequence_output = motionbert_model(**inputs)

        return jsonify({"sequence_analysis": sequence_output.logits.tolist()})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)