File size: 1,052 Bytes
84a2273
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
import cv2
import numpy as np
import base64

app = Flask(__name__)

def decode_image(data_url):
    header, encoded = data_url.split(",", 1)
    image_data = base64.b64decode(encoded)
    image_np = np.fromstring(image_data, dtype=np.uint8)
    image = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
    return image

@app.route('/process_frame', methods=['POST'])
def process_frame():
    data = request.json
    frame_data = data['frame']
    frame = decode_image(frame_data)

    # Process the frame (use your existing run function)
    result_image = run(frame, model_complexity=1, enable_segmentation=True, 
                       min_detection_confidence=0.5, background_color='white')

    # Encode the image back to base64
    _, buffer = cv2.imencode('.jpg', result_image)
    encoded_image = base64.b64encode(buffer).decode('utf-8')
    response = {
        'processed_frame': f"data:image/jpeg;base64,{encoded_image}"
    }
    return jsonify(response)

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