File size: 1,383 Bytes
f46aee3
 
 
 
 
5c22f66
 
f46aee3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3b9b817
f46aee3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
from PIL import Image
import io
import base64
import logging
import numpy as np

app = Flask(__name__)

# Dummy pose recognition function
def recognize_pose(image):
    # Replace with your model inference code
    pose = "warrior"  # Example dummy pose
    return pose

@app.route('/api/recognize_pose', methods=['POST'])
def api_recognize_pose():
    try:
        # Decode the image from the request
        image_data = request.json['image'].split(",")[1]
        image_bytes = base64.b64decode(image_data)
        image = Image.open(io.BytesIO(image_bytes))
        
        # Preprocess and recognize pose
        image = preprocess_image(image)
        pose = recognize_pose(image)
        return jsonify({'pose': pose})
    except KeyError as ke:
        logging.error(f"Key Error: {ke}")
        return jsonify({'error': 'Invalid input data format'}), 400
    except Exception as e:
        logging.error(f"Unexpected error: {e}")
        return jsonify({'error': str(e)}), 500

def preprocess_image(image):
    # Example preprocessing; adjust as needed for your model
    image = image.resize((224, 224))  # Resize image to expected input size
    image = np.array(image) / 255.0  # Normalize image
    image = np.expand_dims(image, axis=0)  # Add batch dimension
    return image

if __name__ == '__main__':
    app.run(debug=True)