77
88import math
99from functools import partial
10- from typing import TYPE_CHECKING
10+ from typing import TYPE_CHECKING , Any
1111
1212import cv2
1313import numpy as np
1414from openvino import Model , OVAny , Type , layout_helpers
15- from openvino .runtime import Output
15+ from openvino .runtime import Input , Node , Output
1616from openvino .runtime import opset10 as opset
1717from openvino .runtime .utils .decorators import custom_preprocess_function
1818
2121
2222
2323class Layout :
24- def __init__ (self , layout = "" ) -> None :
24+ def __init__ (self , layout : str = "" ) -> None :
2525 self .layout = layout
2626
2727 @staticmethod
28- def from_shape (shape ) :
28+ def from_shape (shape : list [ int ] | tuple [ int , ...]) -> str :
2929 """Create Layout from given shape"""
3030 if len (shape ) == 2 :
3131 return "NC"
@@ -40,7 +40,7 @@ def from_shape(shape):
4040 raise RuntimeError (msg )
4141
4242 @staticmethod
43- def from_openvino (input ):
43+ def from_openvino (input : Input ):
4444 """Create Layout from openvino input"""
4545 return layout_helpers .get_layout (input ).to_string ().strip ("[]" ).replace ("," , "" )
4646
@@ -75,7 +75,7 @@ def parse_layouts(layout_string: str) -> dict | None:
7575 return user_layouts
7676
7777
78- def resize_image_letterbox_graph (input : Output , size , interpolation , pad_value ) :
78+ def resize_image_letterbox_graph (input : Output , size : tuple [ int , int ], interpolation : str , pad_value : int ) -> Node :
7979 if not isinstance (pad_value , int ):
8080 msg = "pad_value must be int"
8181 raise RuntimeError (msg )
@@ -157,7 +157,7 @@ def resize_image_letterbox_graph(input: Output, size, interpolation, pad_value):
157157 )
158158
159159
160- def crop_resize_graph (input : Output , size ) :
160+ def crop_resize_graph (input : Output , size : tuple [ int , int ]) -> Node :
161161 h_axis = 1
162162 w_axis = 2
163163 desired_aspect_ratio = size [1 ] / size [0 ] # width / height
@@ -288,11 +288,11 @@ def crop_resize_graph(input: Output, size):
288288
289289def resize_image_graph (
290290 input : Output ,
291- size ,
292- keep_aspect_ratio ,
293- interpolation ,
294- pad_value ,
295- ):
291+ size : tuple [ int , int ] ,
292+ keep_aspect_ratio : bool ,
293+ interpolation : str ,
294+ pad_value : int ,
295+ ) -> Node :
296296 if not isinstance (pad_value , int ):
297297 msg = "pad_value must be int"
298298 raise RuntimeError (msg )
@@ -365,7 +365,7 @@ def resize_image_graph(
365365 )
366366
367367
368- def resize_image (size , interpolation , pad_value ) :
368+ def resize_image (size : tuple [ int , int ], interpolation : str , pad_value : int ) -> Callable :
369369 return custom_preprocess_function (
370370 partial (
371371 resize_image_graph ,
@@ -377,7 +377,7 @@ def resize_image(size, interpolation, pad_value):
377377 )
378378
379379
380- def resize_image_with_aspect (size , interpolation , pad_value ) :
380+ def resize_image_with_aspect (size : tuple [ int , int ], interpolation : str , pad_value : int ) -> Callable :
381381 return custom_preprocess_function (
382382 partial (
383383 resize_image_graph ,
@@ -389,11 +389,11 @@ def resize_image_with_aspect(size, interpolation, pad_value):
389389 )
390390
391391
392- def crop_resize (size , interpolation , pad_value ) :
392+ def crop_resize (size : tuple [ int , int ], interpolation : str , pad_value : int ) -> Callable :
393393 return custom_preprocess_function (partial (crop_resize_graph , size = size ))
394394
395395
396- def resize_image_letterbox (size , interpolation , pad_value ) :
396+ def resize_image_letterbox (size : tuple [ int , int ], interpolation : str , pad_value : int ) -> Callable :
397397 return custom_preprocess_function (
398398 partial (
399399 resize_image_letterbox_graph ,
@@ -404,8 +404,8 @@ def resize_image_letterbox(size, interpolation, pad_value):
404404 )
405405
406406
407- def load_parameters_from_onnx (onnx_model ) :
408- parameters = {}
407+ def load_parameters_from_onnx (onnx_model : Any ) -> dict [ str , Any ] :
408+ parameters : dict [ str , Any ] = {}
409409
410410 def insert_hierarchical (keys , val , root_dict ):
411411 if len (keys ) == 1 :
@@ -423,7 +423,7 @@ def insert_hierarchical(keys, val, root_dict):
423423 return parameters
424424
425425
426- def get_rt_info_from_dict (rt_info_dict , path ) :
426+ def get_rt_info_from_dict (rt_info_dict : dict [ str , Any ], path : list [ str ]) -> OVAny :
427427 value = rt_info_dict
428428 try :
429429 value = rt_info_dict
@@ -436,13 +436,13 @@ def get_rt_info_from_dict(rt_info_dict, path):
436436
437437
438438def resize_image_ocv (
439- image ,
440- size ,
441- keep_aspect_ratio = False ,
442- is_pad = False ,
443- pad_value = 0 ,
444- interpolation = cv2 .INTER_LINEAR ,
445- ):
439+ image : np . ndarray ,
440+ size : tuple [ int , int ] ,
441+ keep_aspect_ratio : bool = False ,
442+ is_pad : bool = False ,
443+ pad_value : int = 0 ,
444+ interpolation : int = cv2 .INTER_LINEAR ,
445+ ) -> np . ndarray :
446446 if keep_aspect_ratio :
447447 h , w = image .shape [:2 ]
448448 scale = min (size [1 ] / h , size [0 ] / w )
@@ -460,7 +460,11 @@ def resize_image_ocv(
460460 return cv2 .resize (image , size , interpolation = interpolation )
461461
462462
463- def resize_image_with_aspect_ocv (image , size , interpolation = cv2 .INTER_LINEAR ):
463+ def resize_image_with_aspect_ocv (
464+ image : np .ndarray ,
465+ size : tuple [int , int ],
466+ interpolation : int = cv2 .INTER_LINEAR ,
467+ ) -> np .ndarray :
464468 return resize_image_ocv (
465469 image ,
466470 size ,
@@ -472,11 +476,11 @@ def resize_image_with_aspect_ocv(image, size, interpolation=cv2.INTER_LINEAR):
472476
473477
474478def resize_image_letterbox_ocv (
475- image ,
476- size ,
477- interpolation = cv2 .INTER_LINEAR ,
478- pad_value = 0 ,
479- ):
479+ image : np . ndarray ,
480+ size : tuple [ int , int ] ,
481+ interpolation : int = cv2 .INTER_LINEAR ,
482+ pad_value : int = 0 ,
483+ ) -> np . ndarray :
480484 ih , iw = image .shape [0 :2 ]
481485 w , h = size
482486 scale = min (w / iw , h / ih )
@@ -493,7 +497,7 @@ def resize_image_letterbox_ocv(
493497 )
494498
495499
496- def crop_resize_ocv (image , size ) :
500+ def crop_resize_ocv (image : np . ndarray , size : tuple [ int , int ]) -> np . ndarray :
497501 desired_aspect_ratio = size [1 ] / size [0 ] # width / height
498502 if desired_aspect_ratio == 1 :
499503 if image .shape [0 ] > image .shape [1 ]:
@@ -522,7 +526,7 @@ def crop_resize_ocv(image, size):
522526}
523527
524528
525- INTERPOLATION_TYPES = {
529+ INTERPOLATION_TYPES : dict [ str , int ] = {
526530 "LINEAR" : cv2 .INTER_LINEAR ,
527531 "CUBIC" : cv2 .INTER_CUBIC ,
528532 "NEAREST" : cv2 .INTER_NEAREST ,
@@ -533,9 +537,9 @@ def crop_resize_ocv(image, size):
533537class InputTransform :
534538 def __init__ (
535539 self ,
536- reverse_input_channels = False ,
537- mean_values = None ,
538- scale_values = None ,
540+ reverse_input_channels : bool = False ,
541+ mean_values : list [ float ] | None = None ,
542+ scale_values : list [ float ] | None = None ,
539543 ):
540544 self .reverse_input_channels = reverse_input_channels
541545 self .is_trivial = not (reverse_input_channels or mean_values or scale_values )
0 commit comments