Skip to content
Snippets Groups Projects
Commit f9bc7450 authored by Mathieu Beligon's avatar Mathieu Beligon
Browse files

Merge branch 'common/image-perturbators' into 'master'

Common/image perturbators

See merge request polystar/robomaster/computer-vision/code!51
parents 661e357d c6987dcd
No related branches found
No related tags found
No related merge requests found
Showing
with 158 additions and 10 deletions
......@@ -68,13 +68,13 @@ You have 2 options to download the videos:
1. First, go on the [google sheet](https://docs.google.com/spreadsheets/d/1kIrMOjcKJ8hslZoVMx1D0H7QYj9nQLFvzUAQ1U4Le-I/edit#gid=0), and choose a video that nobody already did, and put your name in the 2nd column
2. Download the video, in **720p**
3. Rename it using the video id on twitch, and place it in [../dataset/twitch/videos](../dataset/twitch/videos)
5. Launch the python script [./research_common/scripts/extract_robots_views_from_video.py](research_common/scripts/extract_robots_views_from_video.py), with the video id as parameter (In Pycharm, `Run` > `Edit Configurations...`, then in parameters enter the id). You can put multiple video ids by separating them with spaces.
5. Launch the python script [./research/common/scripts/extract_robots_views_from_video.py](research/common/scripts/extract_robots_views_from_video.py), with the video id as parameter (In Pycharm, `Run` > `Edit Configurations...`, then in parameters enter the id). You can put multiple video ids by separating them with spaces.
The frames will appear in the [../dataset/twitch/robots-views](../dataset/twitch/robots-views) folder.
#### Aerial dataset
Once you have the robots views in the [../dataset/twitch/robots-views](../dataset/twitch/robots-views) folder, run the python script [./research_common/scripts/move_aerial_views.py](research_common/scripts/move_aerial_views.py). It will put the aerial views in the [../dataset/twitch/aerial-views](../dataset/twitch/aerial-views) directory.
Once you have the robots views in the [../dataset/twitch/robots-views](../dataset/twitch/robots-views) folder, run the python script [./research/common/scripts/move_aerial_views.py](research/common/scripts/move_aerial_views.py). It will put the aerial views in the [../dataset/twitch/aerial-views](../dataset/twitch/aerial-views) directory.
......@@ -6,7 +6,7 @@ DSET_DIR: Path = PROJECT_DIR / "dataset"
TWITCH_DSET_DIR: Path = DSET_DIR / "twitch"
DJI_ROCO_DSET_DIR: Path = DSET_DIR / "dji_roco"
DJI_ROCO_ZOOMED_DSET_DIR: Path = DSET_DIR / "dji_roco_zoomed_v1"
DJI_ROCO_ZOOMED_DSET_DIR: Path = DSET_DIR / "dji_roco_zoomed_v2"
TENSORFLOW_RECORDS_DIR: Path = DSET_DIR / "tf_records"
TWITCH_ROBOTS_VIEWS_DIR: Path = TWITCH_DSET_DIR / "robots-views"
TWITCH_DSET_ROBOTS_VIEWS_DIR: Path = TWITCH_DSET_DIR / "final-robots-views"
......
from pathlib import Path
from typing import Iterable
from research_common.dataset.roco_dataset import ROCODataset
from research.common.dataset.roco_dataset import ROCODataset
class DirectoryROCODataset(ROCODataset):
......
from enum import Enum
from research_common.constants import DJI_ROCO_DSET_DIR
from research_common.dataset.directory_roco_dataset import DirectoryROCODataset
from research.common.constants import DJI_ROCO_DSET_DIR
from research.common.dataset.directory_roco_dataset import DirectoryROCODataset
class DJIROCODataset(DirectoryROCODataset, Enum):
......
from enum import Enum, auto
from polystar.common.utils.str_utils import camel2snake
from research_common.constants import DJI_ROCO_ZOOMED_DSET_DIR
from research_common.dataset.directory_roco_dataset import DirectoryROCODataset
from research.common.constants import DJI_ROCO_ZOOMED_DSET_DIR
from research.common.dataset.directory_roco_dataset import DirectoryROCODataset
class DJIROCOZoomedDataset(DirectoryROCODataset, Enum):
def __init__(self, _):
super().__init__(DJI_ROCO_ZOOMED_DSET_DIR / camel2snake(self.name), f"{self.name}ZoomedV1")
super().__init__(DJI_ROCO_ZOOMED_DSET_DIR / camel2snake(self.name), f"{self.name}ZoomedV2")
CentralChina = auto()
NorthChina = auto()
......
......@@ -7,7 +7,7 @@ from polystar.common.models.box import Box
from polystar.common.models.image_annotation import ImageAnnotation
from polystar.common.target_pipeline.objects_validators.in_box_validator import InBoxValidator
from polystar.common.view.plt_results_viewer import PltResultViewer
from research_common.dataset.dji.dji_roco_datasets import DJIROCODataset
from research.common.dataset.dji.dji_roco_datasets import DJIROCODataset
def crop_image_annotation(image_annotation: ImageAnnotation, box: Box, min_coverage: float) -> ImageAnnotation:
......
from dataclasses import dataclass
import numpy as np
from polystar.common.models.image import Image
from research.common.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC
@dataclass
class BrightnessModifier(ImageModifierABC):
max_offset: float = 10.0
def modify(self, image: Image, intensity: float) -> Image:
offset = self.max_offset * intensity
perturbed_image = np.clip((image.astype(np.uint16) + offset), 0, 255).astype(np.uint8)
return perturbed_image
if __name__ == "__main__":
from research.common.dataset.perturbations.utils import simple_modifier_demo
simple_modifier_demo(BrightnessModifier())
from dataclasses import dataclass
import numpy as np
from polystar.common.models.image import Image
from research.common.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC
@dataclass
class ContrastModifier(ImageModifierABC):
min_coef: float = 0.8
max_coef: float = 1.5
def modify(self, image: Image, intensity: float) -> Image:
coef = self.min_coef + (self.max_coef - self.min_coef) * intensity
perturbed_image = np.clip((image.astype(np.uint16) * coef), 0, 255).astype(np.uint8)
return perturbed_image
if __name__ == "__main__":
from research.common.dataset.perturbations.utils import simple_modifier_demo
simple_modifier_demo(ContrastModifier())
from dataclasses import dataclass
import cv2
from polystar.common.models.image import Image
from research.common.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC
@dataclass
class GaussianBlurrer(ImageModifierABC):
max_factor: float = 0.015
def modify(self, image: Image, intensity: float) -> Image:
blur_factor = intensity * self.max_factor
width, height, *_ = image.shape
x, y = _to_odd_number(width * blur_factor), _to_odd_number(height * blur_factor)
image = cv2.GaussianBlur(image, (x, y), cv2.BORDER_DEFAULT)
return image
def _to_odd_number(number):
return int(number // 2 * 2) - 1
if __name__ == "__main__":
from research.common.dataset.perturbations.utils import simple_modifier_demo
simple_modifier_demo(GaussianBlurrer())
from dataclasses import dataclass
import numpy as np
from polystar.common.models.image import Image
from research.common.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC
@dataclass
class GaussianNoiser(ImageModifierABC):
max_variance: float = 300.0
def modify(self, image: Image, intensity: float) -> Image:
variance = self.max_variance * intensity
sigma = variance ** 0.5
row, column, ch = image.shape
gaussian = np.random.normal(0, sigma, (row, column, ch))
perturbed_image = np.clip((image.astype(np.uint16) + gaussian), 0, 255).astype(np.uint8)
return perturbed_image
if __name__ == "__main__":
from research.common.dataset.perturbations.utils import simple_modifier_demo
simple_modifier_demo(GaussianNoiser())
from dataclasses import dataclass
import cv2
import numpy as np
from polystar.common.models.image import Image
from research.common.dataset.perturbations.image_modifiers.image_modifier_abc import ImageModifierABC
@dataclass
class HorizontalBlurrer(ImageModifierABC):
max_kernel_size: int = 11
def modify(self, image: Image, intensity: float) -> Image:
kernel = self._make_zero_kernel(intensity)
self._fill_middle_row_with_ones(kernel)
self._normalize_kernel(kernel)
return self._apply_kernel(image, kernel)
def _make_zero_kernel(self, intensity: float) -> np.ndarray:
kernel_size = int(self.max_kernel_size * intensity) + 1
return np.zeros((kernel_size, kernel_size))
@staticmethod
def _fill_middle_row_with_ones(kernel: np.ndarray):
kernel[len(kernel) // 2, :] = 1
@staticmethod
def _normalize_kernel(kernel: np.ndarray):
kernel /= len(kernel)
@staticmethod
def _apply_kernel(image: Image, kernel: np.ndarray) -> Image:
return cv2.filter2D(image, -1, kernel)
if __name__ == "__main__":
from research.common.dataset.perturbations.utils import simple_modifier_demo
simple_modifier_demo(HorizontalBlurrer())
from abc import ABC, abstractmethod
from polystar.common.models.image import Image
class ImageModifierABC(ABC):
@abstractmethod
def modify(self, image: Image, intensity: float) -> Image:
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment