forked from datatorch-actions/dextr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
50 lines (37 loc) · 1.29 KB
/
server.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
from dextr.model import DextrModel
from flask import Flask, request, jsonify
from PIL import Image
import numpy as np
import time
import torch
import os
from imantics import Mask
device = os.getenv("DEVICE", "cpu")
torch_device = torch.device(device)
app = Flask(__name__)
model = DextrModel.pascalvoc_resunet101()
model.eval()
@app.route("/", methods=["GET", "POST"])
def hello_world():
if request.method == "POST":
start = time.time()
content = request.json
path = content["path"]
points = np.array(content["points"])
print(f"Processing Image: {path}")
image = Image.open(path)
print(f"Image Size: {image.size}", flush=True)
# points come in [x,y] order; this must be flipped
points = points[:, ::-1]
mask = model.predict([image], [points])[0]
mask_bin = mask >= 0.5
polygons = Mask(mask_bin).polygons().points
polygons = [polygon.tolist() for polygon in polygons if len(polygon) > 2]
print(f"Result: {polygons}", flush=True)
end = time.time()
process_time = end - start
print(f"Process Time: {process_time:9.3} seconds", flush=True)
return jsonify({"polygons": polygons})
return "<h4>DEXTR Action is running.</h4>"
if __name__ == "__main__":
app.run()