```python:inference/core/workflows/execution_engine/entities/base.py
from dataclasses import dataclass
from datetime import datetime
from typing import Optional

import numpy as np
from pydantic import BaseModel, Field


class VideoMetadata(BaseModel):
    video_identifier: str = Field(
        description="Identifier string for video. To be treated as opaque."
    )
    frame_number: int
    frame_timestamp: datetime = Field(
        description="The timestamp of video frame. When processing video it is suggested that "
        "blocks rely on `fps` and `frame_number`, as real-world time-elapse will not "
        "match time-elapse in video file",
    )
    fps: Optional[float] = Field(
        description="Field represents FPS value (if possible to be retrieved)",
        default=None,
    )
    measured_fps: Optional[float] = Field(
        description="Field represents measured FPS of live stream",
        default=None,
    )
    comes_from_video_file: Optional[bool] = Field(
        description="Field is a flag telling if frame comes from video file or stream - "
        "if not possible to be determined - pass None",
        default=None,
    )


@dataclass(frozen=True)
class OriginCoordinatesSystem:
    left_top_x: int
    left_top_y: int
    origin_width: int
    origin_height: int


@dataclass(frozen=True)
class ImageParentMetadata:
    parent_id: str
    origin_coordinates: Optional[OriginCoordinatesSystem] = None


class WorkflowImageData:

    # Use __slots__ to reduce memory footprint and speed attribute access
    __slots__ = (
        "_parent_metadata",
        "_workflow_root_ancestor_metadata",
        "_image_reference",
        "_base64_image",
        "_numpy_image",
        "_video_metadata",
        "_image_name",
    )

    def __init__(
        self,
        parent_metadata: ImageParentMetadata,
        workflow_root_ancestor_metadata: Optional[ImageParentMetadata] = None,
        image_reference: Optional[str] = None,
        base64_image: Optional[str] = None,
        numpy_image: Optional[np.ndarray] = None,
        video_metadata: Optional[VideoMetadata] = None,
        image_name: Optional[str] = None,
    ):
        if not base64_image and numpy_image is None and not image_reference:
            raise ValueError("Could not initialise empty `WorkflowImageData`.")
        self._parent_metadata = parent_metadata
        self._workflow_root_ancestor_metadata = (
            workflow_root_ancestor_metadata
            if workflow_root_ancestor_metadata
            else self._parent_metadata
        )
        self._image_reference = image_reference
        self._base64_image = base64_image
        self._numpy_image = numpy_image
        self._video_metadata = video_metadata
        self._image_name = image_name
```
```python:inference/core/workflows/core_steps/common/deserializers.py
import os
from typing import Any, Optional

import cv2
import numpy as np
from inference.core.utils.image_utils import (
    attempt_loading_image_from_string, load_image_from_url)
# The deserialize_parent_origin helper (preserved behavior)
from inference.core.workflows.core_steps.common.deserializers import \
    deserialize_parent_origin  # type: ignore
from inference.core.workflows.errors import RuntimeInputError
from inference.core.workflows.execution_engine.constants import (
    IMAGE_NAME_KEY, PARENT_ID_KEY, PARENT_ORIGIN_KEY, ROOT_PARENT_ID_KEY,
    ROOT_PARENT_ORIGIN_KEY)
from inference.core.workflows.execution_engine.entities.base import (
    ImageParentMetadata, VideoMetadata, WorkflowImageData)
from pydantic import ValidationError

# Simple cache for common case where parent_origin is falsy (avoid repeated ImageParentMetadata allocation)
_PARENT_METADATA_CACHE: dict[str, ImageParentMetadata] = {}


def deserialize_image_kind(
    parameter: str,
    image: Any,
    prevent_local_images_loading: bool = False,
) -> WorkflowImageData:
    if isinstance(image, WorkflowImageData):
        return image

    original_image = image
    is_image_dict = isinstance(original_image, dict)

    # Extract inexpensive fields early

    video_metadata = None
    image_name = None
    if is_image_dict:
        img_dict = original_image  # type: ignore[var-annotated]
        image_name = img_dict.get(IMAGE_NAME_KEY)
        if "video_metadata" in img_dict:
            video_metadata = deserialize_video_metadata_kind(
                parameter=parameter, video_metadata=img_dict["video_metadata"]
            )

        # Fast path: dict holding numpy array directly as "value"
        maybe_value = img_dict.get("value")
        if isinstance(maybe_value, np.ndarray):
            image = maybe_value
            parent_id = img_dict.get(PARENT_ID_KEY, parameter)
            parent_origin = img_dict.get(PARENT_ORIGIN_KEY)
            root_parent_id = img_dict.get(ROOT_PARENT_ID_KEY)
            root_parent_origin = img_dict.get(ROOT_PARENT_ORIGIN_KEY)

            parent_metadata = _parse_optional_parent_metadata(
                parameter=parameter,
                parent_id=parent_id,
                parent_origin=parent_origin,
            )
            workflow_root_ancestor_metadata = _parse_optional_parent_metadata(
                parameter=parameter,
                parent_id=root_parent_id,
                parent_origin=root_parent_origin,
            )
            return WorkflowImageData(
                parent_metadata=parent_metadata,
                workflow_root_ancestor_metadata=workflow_root_ancestor_metadata,
                numpy_image=image,
                video_metadata=video_metadata,
                image_name=image_name,
            )

    # Non-dict numpy array fast path
    if isinstance(original_image, np.ndarray) and not is_image_dict:
        parent_metadata = _parse_optional_parent_metadata(
            parameter=parameter,
            parent_id=parameter,
            parent_origin=None,
        )
        return WorkflowImageData(
            parent_metadata=parent_metadata,
            workflow_root_ancestor_metadata=parent_metadata,
            numpy_image=original_image,
            video_metadata=None,
            image_name=None,
        )
    try:
        if is_image_dict:
            img_dict = original_image  # type: ignore[var-annotated]
            value = img_dict.get("value")
        else:
            value = original_image

        if isinstance(value, str):
            base64_image = None
            image_reference = None
            if value.startswith("http://") or value.startswith("https://"):
                image_reference = value
                image = load_image_from_url(value=value)
            elif not prevent_local_images_loading and os.path.exists(value):
                # prevent_local_images_loading is introduced to eliminate
                # server vulnerability - namely it prevents local server
                # file system from being exploited.
                image_reference = value
                image = cv2.imread(value)
            else:
                base64_image = value
                image = attempt_loading_image_from_string(value)[0]

            # Now build parent metadata once we have the value and references
            parent_id = img_dict.get(PARENT_ID_KEY, parameter) if is_image_dict else parameter
            parent_origin = img_dict.get(PARENT_ORIGIN_KEY) if is_image_dict else None
            root_parent_id = img_dict.get(ROOT_PARENT_ID_KEY) if is_image_dict else None
            root_parent_origin = img_dict.get(ROOT_PARENT_ORIGIN_KEY) if is_image_dict else None

            parent_metadata = _parse_optional_parent_metadata(
                parameter=parameter,
                parent_id=parent_id,
                parent_origin=parent_origin,
            )
            workflow_root_ancestor_metadata = _parse_optional_parent_metadata(
                parameter=parameter,
                parent_id=root_parent_id,
                parent_origin=root_parent_origin,
            )

            return WorkflowImageData(
                parent_metadata=parent_metadata,
                workflow_root_ancestor_metadata=workflow_root_ancestor_metadata,
                numpy_image=image,
                base64_image=base64_image,
                image_reference=image_reference,
                video_metadata=video_metadata,
                image_name=image_name,
            )
    except Exception as error:
        raise RuntimeInputError(
            public_message=f"Detected runtime parameter `{parameter}` defined as `WorkflowImage` "
            f"that is invalid. Failed on input validation. Details: {error}",
            context="workflow_execution | runtime_input_validation",
        ) from error
    raise RuntimeInputError(
        public_message=f"Detected runtime parameter `{parameter}` defined as `WorkflowImage` "
        f"with type {type(original_image)} that is invalid. Workflows accept only np.arrays, `WorkflowImageData` "
        f"and dicts with keys `type` and `value` compatible with `inference` (or list of them).",
        context="workflow_execution | runtime_input_validation",
    )


def _parse_optional_parent_metadata(
    parameter: str,
    parent_id: Optional[str],
    parent_origin: Optional[Any],
) -> Optional[ImageParentMetadata]:
    if not parent_id:
        return None

    if not parent_origin:
        cached = _PARENT_METADATA_CACHE.get(parent_id)
        if cached is not None:
            return cached
        instance = ImageParentMetadata(parent_id=parent_id)
        _PARENT_METADATA_CACHE[parent_id] = instance
        return instance


    parsed_origin = deserialize_parent_origin(parameter, parent_origin)
    return ImageParentMetadata(
        parent_id=parent_id,
        origin_coordinates=parsed_origin.to_origin_coordinates_system(),
    )


def deserialize_video_metadata_kind(
    parameter: str,
    video_metadata: Any,
) -> VideoMetadata:
    if isinstance(video_metadata, VideoMetadata):
        return video_metadata
    if not isinstance(video_metadata, dict):
        raise RuntimeInputError(
            public_message=f"Detected runtime parameter `{parameter}` holding "
            f"`WorkflowVideoMetadata`, but provided value is not a dict.",
            context="workflow_execution | runtime_input_validation",
        )
    try:
        return VideoMetadata.model_validate(video_metadata)
    except ValidationError as error:
        raise RuntimeInputError(
            public_message=f"Detected runtime parameter `{parameter}` holding "
            f"`WorkflowVideoMetadata`, but provided value is malformed. "
            f"See details in inner error.",
            context="workflow_execution | runtime_input_validation",
            inner_error=error,
        )
```
```python:inference/core/utils/image_utils.py
import re
import urllib.parse
from typing import Tuple, Union

import cv2
import numpy as np
import requests
import tldextract
from _io import _IOBase
from inference.core.exceptions import (InputFormatInferenceFailed,
                                       InputImageLoadError,
                                       InvalidImageTypeDeclared,
                                       InvalidNumpyInput)
from inference.core.utils.requests import api_key_safe_raise_for_status
from requests import RequestException

BASE64_DATA_TYPE_PATTERN = re.compile(r"^data:image\/[a-z]+;base64,")



# Reuse a single TLDExtract instance to avoid repeated construction costs.
_TLD_EXTRACTOR = tldextract.TLDExtract(suffix_list_urls=())


def attempt_loading_image_from_string(
    value: Union[str, bytes, bytearray, _IOBase],
    cv_imread_flags: int = cv2.IMREAD_COLOR,
) -> Tuple[np.ndarray, bool]:
    """
    Attempt to load an image from a string.

    Args:
        value (Union[str, bytes, bytearray, _IOBase]): The image data in string format.
        cv_imread_flags (int): OpenCV flags used for image reading.

    Returns:
        Tuple[np.ndarray, bool]: A tuple of the loaded image in numpy array format and a boolean flag indicating if the image is in BGR format.
    """
    try:
        return load_image_base64(value=value, cv_imread_flags=cv_imread_flags), True
    except:
        pass
    try:
        return (
            load_image_from_encoded_bytes(value=value, cv_imread_flags=cv_imread_flags),
            True,
        )
    except:
        pass
    try:
        return (
            load_image_from_buffer(value=value, cv_imread_flags=cv_imread_flags),
            True,
        )
    except:
        pass
    try:
        return load_image_from_numpy_str(value=value), True
    except InvalidImageTypeDeclared as error:
        raise error
    except InvalidNumpyInput as error:
        raise InputFormatInferenceFailed(
            message="Input image format could not be inferred from string.",
            public_message="Input image format could not be inferred from string.",
        ) from error


def load_image_from_url(
    value: str, cv_imread_flags: int = cv2.IMREAD_COLOR
) -> np.ndarray:
    """Loads an image from a given URL.

    Args:
        value (str): URL of the image.

    Returns:
        Image.Image: The loaded PIL image.
    """
    _ensure_url_input_allowed()
    try:
        parsed_url = urllib.parse.urlparse(value)
    except ValueError as error:
        message = "Provided image URL is invalid"
        raise InputImageLoadError(
            message=message,
            public_message=message,
        ) from error
    _ensure_resource_schema_allowed(schema=parsed_url.scheme)
    domain_extraction_result = _TLD_EXTRACTOR(parsed_url.netloc)  # we get rid of potential ports and parse FQDNs
    _ensure_resource_fqdn_allowed(fqdn=domain_extraction_result.fqdn)
    address_parts_concatenated = _concatenate_chunks_of_network_location(
        extraction_result=domain_extraction_result
    )  # concatenation of chunks - even if there is no FQDN, but address
    # it allows white-/black-list verification
    _ensure_location_matches_destination_whitelist(
        destination=address_parts_concatenated
    )
    _ensure_location_matches_destination_blacklist(
        destination=address_parts_concatenated
    )
    try:
        response = requests.get(value, stream=True)
        api_key_safe_raise_for_status(response=response)
        return load_image_from_encoded_bytes(
            value=response.content, cv_imread_flags=cv_imread_flags
        )
    except (RequestException, ConnectionError) as error:
        raise InputImageLoadError(
            message=f"Could not load image from url: {value}. Details: {error}",
            public_message="Data pointed by URL could not be decoded into image.",
        )
```