-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
141 lines (118 loc) · 4.07 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
import ntpath
import tarfile
import os
import subprocess
import requests
import io
import shutil
import time
import pyglass
DRACO_DEC_PATH = "C:\\Users\\smithc\\AppData\\Local\\Continuum\\miniconda3\\Library\\bin\\draco_decoder.exe"
def from_tarfile(path_or_bytes, bodyID, name):
tf = tarfile.TarFile(fileobj=io.BytesIO(path_or_bytes))
members = sorted(tf.getmembers(), key=lambda m: m.name)
meshpathlist = []
new_path = 'C:\\VR_meshes\\'+ str(bodyID)
if not os.path.exists(new_path):
os.makedirs(new_path)
for member in members:
ext = member.name[-4:]
# Skip non-mesh files and empty files
if ext in ('.drc', '.obj', '.ngmesh') and member.size > 0:
buf = tf.extractfile(member).read()
#if len(buf) == 0:
# continue
with open(new_path + "\\" + member.name, "wb") as ff:
ff.write(buf)
#add subprocess to convert draco to obj cmd: draco_decoder
#if len(buf)
s = "C:\\Users\\smithc\\AppData\\Local\\Continuum\\miniconda3\\Library\\bin\\draco_decoder.exe -i " + new_path + "\\" + member.name + " -o " + new_path + "\\" + member.name[:-3] + "obj"
#print(s)
FNULL = open(os.devnull, 'w')
subprocess.call(s, stdout=FNULL, stderr=subprocess.STDOUT)
meshpathlist.append(new_path + "\\" + member.name[:-3] + "obj")
return meshpathlist
def copyEmpty(name):
emptyProject = "emptyProject\\emptyProject.syg"
emptyProjectMeta = "emptyProject\\emptyProject.sym"
if not os.path.exists("output\\" + name):
os.mkdir("output\\" + name)
shutil.copy(emptyProject, "output/" + name + "/" + name + ".syg")
shutil.copy(emptyProjectMeta, "output/" + name + "/" + name + ".sym")
def addMeshes(path, name, bodyList):
copyEmpty(name)
print(path)
projectPath = os.path.join(os.getcwd(), "output\\" + name + "\\" + name + ".syg")
print(projectPath)
project = pyglass.OpenProject(pyglass.path(projectPath))
l = path
project.ImportMeshOBJs("default", "\n".join(l))
for each in bodyList:
project.CreateTag("default", str(each))
while project.GetMeshIOPercentage() != 100:
print("Progress: " + str(project.GetMeshIOPercentage()) + "%")
print("Current mesh: " + project.GetMeshIOName() + "\n")
time.sleep(2)
for ii, id in enumerate(bodyList):
nameMesh = ntpath.basename(path[ii])
project.AddTagToMesh("default", str(id), nameMesh)
vl = pyglass.VolumeLibrary()
vl.ReloadLibrary()
entry = vl.CreateEntryFromPath(projectPath, name)
vl.PutEntry(entry)
project.RandomizeMeshColors()
print("Project Ready!")
# ------------------------------------------------------ #
app = FastAPI()
class meshRequest(BaseModel):
requestID: str
user: str
time : int
timestamp : int
body_list : List[int] = []
class meshAndDVIDRequest(BaseModel):
user: str
time : str
epoch : int
body_list : List[int] = []
dvid : str
port : int
uuid : str
segmentation : str
@app.get("/")
def read_root():
return {"Welcome to the syGlass Remote Server! Let's see what we can do!"}
@app.post("/request/")
def update_item(item: meshRequest):
print(item)
#bodyIDsToMakeIntoMeshes = item.body_list
#print(bodyIDsToMakeIntoMeshes);
#make into meshes
return item
@app.post("/dvidRequest/")
def update_dvid_item(item: meshAndDVIDRequest):
print(item)
server = item.dvid
uuid = item.uuid
port = item.port
body_id = item.body_list
segmentation = item.segmentation
body_length = len(item.body_list)
#name = f"Bodies{body_length}_{item.user.strip()}_{item.time}"
name = "Bodies" + str(len(item.body_list)).strip() + "_" + item.user.strip() + "_" + item.time
meshPathList = []
bodyList = []
sv_map_base = f"http://{server}:{port}/api/node/{uuid}/segmentation_sv_meshes/tarfile/"
for bodyID in body_id:
sv_map = "http://"+str(server)+":"+str(port)+"/api/node/"+str(uuid)+"/segmentation_sv_meshes/tarfile/"+str(bodyID)
print(sv_map)
map_response = requests.get(sv_map)
newMeshPathList = from_tarfile(map_response.content, bodyID, name)
tempBodyList = [bodyID] * len(newMeshPathList)
meshPathList = meshPathList + newMeshPathList
bodyList = bodyList + tempBodyList
addMeshes(meshPathList, name, bodyList)
return item