This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
hyko_toolkit/functions/utils/converters/image_converter/Dockerfile
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,7 @@ | ||
FROM hyko-sdk:latest | ||
|
||
RUN --mount=type=cache,target=/root/.cache/pip pip install opencv-python | ||
|
||
COPY . . | ||
|
||
CMD ["uvicorn", "--host", "0.0.0.0", "--port", "3000", "main:func"] |
34 changes: 34 additions & 0 deletions
34
hyko_toolkit/functions/utils/converters/image_converter/main.py
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,34 @@ | ||
import os | ||
|
||
import cv2 | ||
import numpy as np | ||
from metadata import Inputs, Outputs, Params, func | ||
|
||
from hyko_sdk.io import Image | ||
|
||
|
||
@func.on_execute | ||
async def main(inputs: Inputs, params: Params) -> Outputs: | ||
""" | ||
Convert an input image to a specified target image type. | ||
Parameters: | ||
- inputs (Inputs): An object containing the input image data. | ||
- params (Params): An object containing parameters including the target image type. | ||
Returns: | ||
- Outputs: An object containing the converted image data. | ||
""" | ||
_, ext = os.path.splitext(inputs.input_image.get_name()) | ||
with open(f"./image{ext}", "wb") as f: | ||
f.write(inputs.input_image.get_data()) | ||
image = cv2.imread(f"./image{ext}") | ||
|
||
result_img_path = f"./image{ext.split('.')[-1]}.{params.target_type}" | ||
|
||
success = cv2.imwrite( | ||
result_img_path, cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) | ||
) | ||
if success: | ||
result = cv2.imread(result_img_path) | ||
return Outputs(image=Image.from_ndarray(result)) |
47 changes: 47 additions & 0 deletions
47
hyko_toolkit/functions/utils/converters/image_converter/metadata.py
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,47 @@ | ||
from enum import Enum | ||
|
||
from pydantic import Field | ||
|
||
from hyko_sdk.function import SDKFunction | ||
from hyko_sdk.io import Image | ||
from hyko_sdk.metadata import CoreModel | ||
|
||
|
||
class SupportedTypes(str, Enum): | ||
png = "png" | ||
jpg = "jpg" | ||
jpeg = "jpeg" | ||
tiff = "tiff" | ||
tif = "tif" | ||
bmp = "bmp" | ||
webp = "webp" | ||
jp2 = "jp2" | ||
dib = "dib" | ||
pgm = "pgm" | ||
ppm = "ppm" | ||
pnm = "pnm" | ||
ras = "ras" | ||
hdr = "hdr" | ||
|
||
|
||
func = SDKFunction( | ||
description="Convert an input image to a specified target image type.", | ||
) | ||
|
||
|
||
@func.set_input | ||
class Inputs(CoreModel): | ||
input_image: Image = Field(..., description="Input image") | ||
|
||
|
||
@func.set_param | ||
class Params(CoreModel): | ||
target_type: SupportedTypes = Field( | ||
..., | ||
description="The Target Type.", | ||
) | ||
|
||
|
||
@func.set_output | ||
class Outputs(CoreModel): | ||
image: Image = Field(..., description="Converted image") |