mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
Small optimizations when working with images (#375)
* data optimization * lint
This commit is contained in:
1 parent
3ae6502116
commit
9292a02287
2 files changed
+15
-13
No files matched your search
@@ -18,11 +18,11 @@ Camera Models
|
||||
import base64
|
||||
import math
|
||||
from enum import Enum, auto
|
||||
from typing import Dict, Optional, Tuple, Union
|
||||
from typing import Dict, Optional, Union
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
import torch
|
||||
import torchvision
|
||||
from torch.nn.functional import normalize
|
||||
from torchtyping import TensorType
|
||||
|
||||
@@ -246,14 +246,14 @@ class Cameras:
|
||||
self,
|
||||
camera_idx: int,
|
||||
image: Optional[TensorType["height", "width", 2]] = None,
|
||||
resize_shape: Optional[Tuple[int, int]] = None,
|
||||
max_size: Optional[int] = None,
|
||||
) -> Dict:
|
||||
"""Convert a camera to a json dictionary.
|
||||
|
||||
Args:
|
||||
camera_idx (int): Index of the camera to convert.
|
||||
image: An image in range [0, 1] that is encoded to a base64 string. Defaults to None.
|
||||
resize_shape: Shape to resize the image to. Defaults to None.
|
||||
max_size: Max size to resize the image to. Defaults to None.
|
||||
|
||||
Returns:
|
||||
A JSON representation of the camera
|
||||
@@ -268,11 +268,14 @@ class Cameras:
|
||||
"camera_index": camera_idx,
|
||||
}
|
||||
if image is not None:
|
||||
image_uint8 = (image * 255).detach().cpu().numpy().astype(np.uint8)
|
||||
if resize_shape:
|
||||
image_uint8 = cv2.resize(image_uint8, resize_shape)
|
||||
data = cv2.imencode(".png", image_uint8)[1].tobytes()
|
||||
json_["image"] = str("data:image/png;base64," + base64.b64encode(data).decode("ascii"))
|
||||
image_uint8 = (image * 255).detach().type(torch.uint8)
|
||||
if max_size is not None:
|
||||
image_uint8 = image_uint8.permute(2, 0, 1)
|
||||
image_uint8 = torchvision.transforms.functional.resize(image_uint8, max_size) # type: ignore
|
||||
image_uint8 = image_uint8.permute(1, 2, 0)
|
||||
image_uint8 = image_uint8.cpu().numpy()
|
||||
data = cv2.imencode(".jpg", image_uint8)[1].tobytes()
|
||||
json_["image"] = str("data:image/jpeg;base64," + base64.b64encode(data).decode("ascii"))
|
||||
return json_
|
||||
|
||||
def get_intrinsics_matrices(self) -> TensorType["num_cameras", 3, 3]:
|
||||
|
||||
Reference in new issue
Block a user