Skip to content

Commit

Permalink
update the ml-logger server
Browse files Browse the repository at this point in the history
  • Loading branch information
geyang committed Aug 11, 2022
1 parent e1bbe20 commit ca69d85
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions ml_logger/log_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def make_video(self, files, key, wd, order, **options):
return self.local_server.make_video(files=files, key=key, wd=wd, order=order, options=options)
else:
# todo: make the json serialization more robust. Not priority b/c this' client-side.
json = MakeVideoEntry(files=files, key=key, wd=wd, order=order, **options)._asdict()
json = MakeVideoEntry(files=files, key=key, wd=wd, order=order, options=options)._asdict()
res = self.session.post(self.make_video_url, json=json).result()
return res.json()
return res.json()['result']

3 changes: 1 addition & 2 deletions ml_logger/ml_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,8 +1588,7 @@ def save_video(self, frame_stack, key, format=None, fps=20, **imageio_kwargs):
def make_video(self, files, key, wd=None, order='ascending', **imageio_kwargs):
work_directory = pJoin(self.prefix, wd)
filename = pJoin(self.prefix, key)
self.client.make_video(files, key=filename, wd=work_directory, order=order, **imageio_kwargs)
return key
return self.client.make_video(files, key=filename, wd=work_directory, order=order, **imageio_kwargs)

# todo: incremental save pyplot to video.
# def VideoContext(self, fig = None)
Expand Down
30 changes: 18 additions & 12 deletions ml_logger/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def serve(self, host, port, workers):
self.app.add_route(self.remove_handler, '/', methods=['DELETE'])
self.app.add_route(self.move_handler, '/move', methods=['POST'])
self.app.add_route(self.copy_handler, '/copy', methods=['POST'])
self.app.add_route(self.make_video_handler, '/make_video', methods=['POST'])
# todo: need a file serving url
self.app.run(host=host, port=port, workers=workers, debug=Params.debug)

Expand Down Expand Up @@ -172,16 +173,17 @@ async def log_handler(self, req):
self.log(log_entry.key, data, log_entry.type, options)
return sanic.response.text('ok')

async def save_video_handler(self, req):
async def make_video_handler(self, req):
import sanic
if not req.json:
msg = f'request json is empty: {req.text}'
cprint(msg, 'red')
return sanic.response.text(msg)
return sanic.response.json({"message": msg})
entry = MakeVideoEntry(**req.json)
print(f"making video to {entry.key}")
self.make_video(file_list=entry.file_list, query=entry.glob, wd=entry.wd, key=entry.key, options=entry.options)
return sanic.response.text(entry.key)
print(f"making video to {entry.key}, {entry}")
result = self.make_video(files=entry.files, key=entry.key, wd=entry.wd, order=entry.order,
options=entry.options)
return sanic.response.json({"result": result})

def glob(self, query, wd, recursive: bool, start, stop):
"""
Expand Down Expand Up @@ -434,24 +436,28 @@ def make_video(self, files: Union[str, Sequence], key, wd, order: str, options=N
abs_key = self.abs_path(key)
abs_wd = self.abs_path(wd)

try:
writer = imageio.get_writer(abs_key, **options)
except FileNotFoundError as e:
os.makedirs(os.path.dirname(abs_key), exist_ok=True)
writer = imageio.get_writer(abs_key, **options)

if isinstance(files, str):
files = self.glob(files, wd=abs_wd, recursive=True, start=None, stop=None)
files = self.glob(files, wd=wd, recursive=True, start=None, stop=None)
if order is "ascending":
files = sorted(files)
elif order is "descending":
files = sorted(files)[::-1]

if not files:
return

try:
writer = imageio.get_writer(abs_key, **options)
except FileNotFoundError as e:
os.makedirs(os.path.dirname(abs_key), exist_ok=True)
writer = imageio.get_writer(abs_key, **options)

for fname in files:
img = imageio.imread(os.path.join(abs_wd, fname))
writer.append_data(img)

writer.close()
return key

# def exec(self, command, options: ShellOptions = None):
# import system
Expand Down
1 change: 0 additions & 1 deletion ml_logger/struts.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class PingData(NamedTuple):
class MakeVideoEntry(NamedTuple):
files: str
key: str
glob: str = None
wd: str = None
order: str = None
options: Any = None
Expand Down
3 changes: 3 additions & 0 deletions ml_logger_tests/test_ml_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ def test_make_video(setup):
# This one makes the video according to the random order given by glob
logger.make_video(file_list, key="videos/face_move_list.mp4")

# this should not reaise an error
path = logger.make_video("no_exist_*.png", key="videos/not_exist.mp4")
assert path is None

def test_pyplot(setup):
import scipy.misc
Expand Down

0 comments on commit ca69d85

Please sign in to comment.