-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(image-list): add image server
Keeps original image off state Remove commented code
- Loading branch information
Showing
5 changed files
with
84 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from aiohttp import web | ||
from PIL import Image | ||
import io | ||
from trame.app import get_server | ||
from nrtk_explorer.library.dataset import get_image_path | ||
|
||
|
||
ORIGINAL_IMAGE_ENDPOINT = "original-image" | ||
|
||
server = get_server() | ||
|
||
|
||
def is_browser_compatible_image(file_path): | ||
# Check if the image format is compatible with web browsers | ||
compatible_formats = {"jpg", "jpeg", "png", "gif", "webp"} | ||
return file_path.split(".")[-1].lower() in compatible_formats | ||
|
||
|
||
def make_response(image, format): | ||
bytes_io = io.BytesIO() | ||
image.save(bytes_io, format=format) | ||
bytes_io.seek(0) | ||
return web.Response(body=bytes_io.read(), content_type=f"image/{format.lower()}") | ||
|
||
|
||
async def original_image_endpoint(request: web.Request): | ||
id = request.match_info["id"] | ||
image_path = get_image_path(id) | ||
|
||
if image_path in server.context.images_manager.images: | ||
image = server.context.images_manager.images[image_path] | ||
send_format = "PNG" | ||
if is_browser_compatible_image(image.format): | ||
send_format = image.format.upper() | ||
return make_response(image, send_format) | ||
|
||
if is_browser_compatible_image(image_path): | ||
return web.FileResponse(image_path) | ||
else: | ||
image = Image.open(image_path) | ||
return make_response(image, "PNG") | ||
|
||
|
||
image_routes = [ | ||
web.get(f"/{ORIGINAL_IMAGE_ENDPOINT}/{{id}}", original_image_endpoint), | ||
] | ||
|
||
|
||
def app_available(wslink_server): | ||
"""Add our custom REST endpoints to the trame server.""" | ||
wslink_server.app.add_routes(image_routes) | ||
|
||
|
||
# --hot-reload does not work if this is configured as decorator on the function | ||
server.controller.add("on_server_bind")(app_available) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,24 @@ | ||
import kwcoco | ||
from pathlib import Path | ||
|
||
|
||
def load_dataset(path: str): | ||
return kwcoco.CocoDataset(path) | ||
|
||
|
||
dataset_json: kwcoco.CocoDataset = kwcoco.CocoDataset() | ||
dataset: kwcoco.CocoDataset = kwcoco.CocoDataset() | ||
dataset_path: str = "" | ||
|
||
|
||
def get_dataset(path: str, force_reload=False): | ||
global dataset_json, dataset_path | ||
global dataset, dataset_path | ||
if dataset_path != path or force_reload: | ||
dataset_path = path | ||
dataset_json = load_dataset(dataset_path) | ||
return dataset_json | ||
dataset = load_dataset(dataset_path) | ||
return dataset | ||
|
||
|
||
def get_image_path(id: str): | ||
dataset_dir = Path(dataset_path).parent | ||
file_name = dataset.imgs[int(id)]["file_name"] | ||
return str(dataset_dir / file_name) |