Unverified Commit 88ae2a4e authored by tripleMu's avatar tripleMu Committed by GitHub
Browse files

Add type hints in several files (#1999)



* Add typehint in such files

* Update mmcv/video/processing.py
Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* Fix np.ndarray str

* Fix

* Update mmcv/video/processing.py
Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* Fix

* Fix

* Update mmcv/video/processing.py
Co-authored-by: default avatarJiazhen Wang <47851024+teamwong111@users.noreply.github.com>
Co-authored-by: default avatarZaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: default avatarJiazhen Wang <47851024+teamwong111@users.noreply.github.com>
parent 85c0f85b
# Copyright (c) OpenMMLab. All rights reserved.
from typing import List, Union
import torch
def scatter(input, devices):
def scatter(input: Union[List, torch.Tensor], devices: List) -> List:
"""scatter copies tensor to MLU directly."""
if isinstance(input, list):
outputs = [scatter(_input, devices) for _input in input]
......
# Copyright (c) OpenMMLab. All rights reserved.
from typing import Callable, Union
import cv2
import numpy as np
def imconvert(img, src, dst):
def imconvert(img: np.ndarray, src: str, dst: str) -> np.ndarray:
"""Convert an image from the src colorspace to dst colorspace.
Args:
......@@ -19,7 +21,7 @@ def imconvert(img, src, dst):
return out_img
def bgr2gray(img, keepdim=False):
def bgr2gray(img: np.ndarray, keepdim: bool = False) -> np.ndarray:
"""Convert a BGR image to grayscale image.
Args:
......@@ -36,7 +38,7 @@ def bgr2gray(img, keepdim=False):
return out_img
def rgb2gray(img, keepdim=False):
def rgb2gray(img: np.ndarray, keepdim: bool = False) -> np.ndarray:
"""Convert a RGB image to grayscale image.
Args:
......@@ -53,7 +55,7 @@ def rgb2gray(img, keepdim=False):
return out_img
def gray2bgr(img):
def gray2bgr(img: np.ndarray) -> np.ndarray:
"""Convert a grayscale image to BGR image.
Args:
......@@ -67,7 +69,7 @@ def gray2bgr(img):
return out_img
def gray2rgb(img):
def gray2rgb(img: np.ndarray) -> np.ndarray:
"""Convert a grayscale image to RGB image.
Args:
......@@ -81,7 +83,7 @@ def gray2rgb(img):
return out_img
def _convert_input_type_range(img):
def _convert_input_type_range(img: np.ndarray) -> np.ndarray:
"""Convert the type and range of the input image.
It converts the input image to np.float32 type and range of [0, 1].
......@@ -109,7 +111,8 @@ def _convert_input_type_range(img):
return img
def _convert_output_type_range(img, dst_type):
def _convert_output_type_range(
img: np.ndarray, dst_type: Union[np.uint8, np.float32]) -> np.ndarray:
"""Convert the type and range of the image according to dst_type.
It converts the image to desired type and range. If `dst_type` is np.uint8,
......@@ -140,7 +143,7 @@ def _convert_output_type_range(img, dst_type):
return img.astype(dst_type)
def rgb2ycbcr(img, y_only=False):
def rgb2ycbcr(img: np.ndarray, y_only: bool = False) -> np.ndarray:
"""Convert a RGB image to YCbCr image.
This function produces the same results as Matlab's `rgb2ycbcr` function.
......@@ -174,7 +177,7 @@ def rgb2ycbcr(img, y_only=False):
return out_img
def bgr2ycbcr(img, y_only=False):
def bgr2ycbcr(img: np.ndarray, y_only: bool = False) -> np.ndarray:
"""Convert a BGR image to YCbCr image.
The bgr version of rgb2ycbcr.
......@@ -208,7 +211,7 @@ def bgr2ycbcr(img, y_only=False):
return out_img
def ycbcr2rgb(img):
def ycbcr2rgb(img: np.ndarray) -> np.ndarray:
"""Convert a YCbCr image to RGB image.
This function produces the same results as Matlab's ycbcr2rgb function.
......@@ -240,7 +243,7 @@ def ycbcr2rgb(img):
return out_img
def ycbcr2bgr(img):
def ycbcr2bgr(img: np.ndarray) -> np.ndarray:
"""Convert a YCbCr image to BGR image.
The bgr version of ycbcr2rgb.
......@@ -272,11 +275,11 @@ def ycbcr2bgr(img):
return out_img
def convert_color_factory(src, dst):
def convert_color_factory(src: str, dst: str) -> Callable:
code = getattr(cv2, f'COLOR_{src.upper()}2{dst.upper()}')
def convert_color(img):
def convert_color(img: np.ndarray) -> np.ndarray:
out_img = cv2.cvtColor(img, code)
return out_img
......
......@@ -5,7 +5,7 @@ import warnings
import torch
def is_custom_op_loaded():
def is_custom_op_loaded() -> bool:
# Following strings of text style are from colorama package
bright_style, reset_style = '\x1b[1m', '\x1b[0m'
......
......@@ -3,16 +3,17 @@ import os
import os.path as osp
import subprocess
import tempfile
from typing import List, Optional, Union
from mmcv.utils import requires_executable
@requires_executable('ffmpeg')
def convert_video(in_file,
out_file,
print_cmd=False,
pre_options='',
**kwargs):
def convert_video(in_file: str,
out_file: str,
print_cmd: bool = False,
pre_options: str = '',
**kwargs) -> None:
"""Convert a video with ffmpeg.
This provides a general api to ffmpeg, the executed command is::
......@@ -52,13 +53,13 @@ def convert_video(in_file,
@requires_executable('ffmpeg')
def resize_video(in_file,
out_file,
size=None,
ratio=None,
keep_ar=False,
log_level='info',
print_cmd=False):
def resize_video(in_file: str,
out_file: str,
size: Optional[tuple] = None,
ratio: Union[tuple, float, None] = None,
keep_ar: bool = False,
log_level: str = 'info',
print_cmd: bool = False) -> None:
"""Resize a video.
Args:
......@@ -90,14 +91,14 @@ def resize_video(in_file,
@requires_executable('ffmpeg')
def cut_video(in_file,
out_file,
start=None,
end=None,
vcodec=None,
acodec=None,
log_level='info',
print_cmd=False):
def cut_video(in_file: str,
out_file: str,
start: Optional[float] = None,
end: Optional[float] = None,
vcodec: Optional[str] = None,
acodec: Optional[str] = None,
log_level: str = 'info',
print_cmd: bool = False) -> None:
"""Cut a clip from a video.
Args:
......@@ -116,21 +117,21 @@ def cut_video(in_file,
if acodec is None:
options['acodec'] = 'copy'
if start:
options['ss'] = start
options['ss'] = start # type: ignore
else:
start = 0
if end:
options['t'] = end - start
options['t'] = end - start # type: ignore
convert_video(in_file, out_file, print_cmd, **options)
@requires_executable('ffmpeg')
def concat_video(video_list,
out_file,
vcodec=None,
acodec=None,
log_level='info',
print_cmd=False):
def concat_video(video_list: List,
out_file: str,
vcodec: Optional[str] = None,
acodec: Optional[str] = None,
log_level: str = 'info',
print_cmd: bool = False) -> None:
"""Concatenate multiple videos into a single one.
Args:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment