-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_socketio.py
62 lines (48 loc) · 2.01 KB
/
server_socketio.py
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
import io
import os
import base64
import argparse
import cv2
import numpy as np
from flask import Flask, render_template
from flask_socketio import SocketIO, emit
from cv_client.selfie_segmentation import SelfieSegmentation
PORT = None
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
segmentor = SelfieSegmentation(tflite_model_path="cv_client/selfie_segmentation/weights/model_float16_quant.tflite")
@app.route('/', methods=['POST', 'GET'])
def index():
return render_template('index_socketio.html', port=int(PORT))
@socketio.on('image')
def image(data_image):
sbuf = io.StringIO()
sbuf.write(data_image)
# decode and convert into image in BGR space
img_stream = io.BytesIO(base64.b64decode(data_image))
frame = cv2.imdecode(np.frombuffer(img_stream.read(), np.uint8), 1)
# Process the image frame
disp_wh = (500, 375)
stringData = ''
if frame is not None:
frame = segmentor.segment_frame(frame, disp_wh)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
imgencode = cv2.imencode('.jpg', frame)[1]
# base64 encode
stringData = base64.b64encode(imgencode).decode('utf-8')
b64_src = 'data:image/jpg;base64,'
stringData = b64_src + stringData
# emit the frame back
emit('response_back', stringData)
if __name__ == '__main__':
parser = argparse.ArgumentParser("Flask webserver with socketIO")
parser.add_argument("-i", "--ip", type=str, default="0.0.0.0",
help="ip address of the device. Default: 0.0.0.0")
parser.add_argument("-o", "--port", type=int, default=8080,
help="ephemeral port number of the server (1024 to 65535). Default: 8080")
parser.add_argument("-bg", "--bgd_img_path", type=str,
help="background image path. If not provided, use a dark background")
args = parser.parse_args()
PORT = os.getenv('HTTP_PORT', args.port)
segmentor.load_new_bgd(args.bgd_img_path)
socketio.run(app=app, host=args.ip, port=args.port)