Skip to content

Commit

Permalink
Merge branch 'main' into find-contours
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeNaccarato authored Apr 5, 2023
2 parents 87e4268 + 98d4401 commit 0bd731e
Show file tree
Hide file tree
Showing 29 changed files with 138 additions and 171 deletions.
4 changes: 2 additions & 2 deletions data/examples.dvc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
outs:
- md5: 2fb7de8f828795665a28311095a67d20.dir
size: 2171082294
- md5: d0e378db3ed75907019882c2b76adf83.dir
size: 2169856488
nfiles: 5
path: examples
6 changes: 3 additions & 3 deletions dvc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ stages:
cmd: python -m boilercv.stages.schema
deps:
- path: src/boilercv/models/paths.py
md5: 4505c3e4ae283c3ee576c06dce722714
md5: 88b036c3649bb3b7fca150eb18a9f081
size: 3549
- path: src/boilercv/stages/schema.py
md5: 6eaac97ab24e94f410ccff1517f42d0e
Expand All @@ -26,8 +26,8 @@ stages:
size: 7424602696
nfiles: 308
- path: src/boilercv/stages/update_binarized_preview.py
md5: f97c70b2c05bc67f5093ee0a0b3f8f71
size: 1247
md5: 186799fb6a90ec27a63cb274e05eca54
size: 1153
outs:
- path: data/previews/binarized.nc
md5: 5598ee8b69f8591e2035194a9054f2c3
Expand Down
6 changes: 3 additions & 3 deletions params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ paths:
models: src/boilercv/models
paths_module: src/boilercv/models/paths.py
data: data
examples: data/examples
previews: data/previews
binarized_preview: data/previews/binarized.nc
contours: data/contours
examples: data/examples
rois: data/rois
samples: data/samples
sources: data/sources
previews: data/previews
binarized_preview: data/previews/binarized.nc
project_schema: data/schema
stage_contours: src/boilercv/stages/contours.py
stage_schema: src/boilercv/stages/schema.py
Expand Down
8 changes: 5 additions & 3 deletions src/boilercv/data/sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def get_dataset(name: str, num_frames: int = 0, frame: slice = ALL_FRAMES) -> DS
roi = PARAMS.paths.rois / f"{name}.nc"
with xr.open_dataset(source) as ds, xr.open_dataset(roi) as roi_ds:
if not unc_source.exists():
xr.Dataset({VIDEO: ds[VIDEO]}).to_netcdf(path=unc_source)
xr.Dataset({VIDEO: ds[VIDEO], HEADER: ds[HEADER]}).to_netcdf(
path=unc_source
)
return xr.Dataset(
{
VIDEO: unpack(ds[VIDEO].sel(frame=frame)),
Expand All @@ -47,7 +49,7 @@ def get_dataset(name: str, num_frames: int = 0, frame: slice = ALL_FRAMES) -> DS
)


def get_large_dataset(video: str) -> DS:
def get_large_dataset(name: str) -> DS:
"""Load a large video dataset."""
with xr.open_dataset(LOCAL_PATHS.large_sources / f"{video}.nc") as ds:
with xr.open_dataset(LOCAL_PATHS.large_sources / f"{name}.nc") as ds:
return ds
24 changes: 5 additions & 19 deletions src/boilercv/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,22 @@
"""Examples, experiments, and demonstrations."""

from collections.abc import Iterator
from pathlib import Path

import cv2 as cv
import xarray as xr

from boilercv.data import VIDEO
from boilercv.models.params import PARAMS
from boilercv.types import DA, Img
from boilercv.types import DA

EXAMPLE_NUM_FRAMES = 1000
EXAMPLE_VIDEO_NAME = "2022-11-30T13-41-00"
SOURCE = PARAMS.paths.examples / f"{EXAMPLE_VIDEO_NAME}.nc"
NUM_FRAMES = 300
EXAMPLE_CONTOURS = PARAMS.paths.examples / f"{EXAMPLE_VIDEO_NAME}.h5"
# TODO: Source the ROI from the dataset.
EXAMPLE_ROI = PARAMS.paths.examples / f"{EXAMPLE_VIDEO_NAME}_roi.yaml"


def get_images() -> DA:
with xr.open_dataset(SOURCE) as ds:
return ds[VIDEO].sel(frame=slice(None, NUM_FRAMES))
with xr.open_dataset(PARAMS.paths.examples / f"{EXAMPLE_VIDEO_NAME}.nc") as ds:
return ds[VIDEO].sel(frame=slice(None, EXAMPLE_NUM_FRAMES))


EXAMPLE_VIDEO = get_images()
EXAMPLE_FRAME_LIST = list(EXAMPLE_VIDEO.values)


def capture_images(path: Path) -> Iterator[Img]:
"""Images from a video file."""
video_capture = cv.VideoCapture(str(path))
while True:
read_is_successful, image = video_capture.read()
if not read_is_successful:
break
yield image
7 changes: 2 additions & 5 deletions src/boilercv/examples/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
from loguru import logger

from boilercv.examples.basic_test import main as main2
from boilercv.examples.blobs.bubbles import main as main4
from boilercv.examples.blobs.galaxy import main as main3
from boilercv.examples.contours.bubbles import main as main6
from boilercv.examples.contours.bubbles_mp4 import main as main5
from boilercv.examples.contours import main as main3
from boilercv.examples.starry import main as main1


Expand All @@ -17,5 +14,5 @@ def run_example(func: Callable[[], None]):
func()


for func in (main1, main2, main3, main4, main5, main6):
for func in (main1, main2, main3):
run_example(func)
5 changes: 2 additions & 3 deletions src/boilercv/examples/blobs/bubbles.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Find bubbles as blobs."""


import cv2 as cv

from boilercv.colors import RED
Expand All @@ -10,8 +9,8 @@
from boilercv.images.cv import apply_mask, build_mask_from_polygons, convert_image
from boilercv.types import ArrInt

NUM_FRAMES = 10
SHORTER_FRAME_LIST = EXAMPLE_FRAME_LIST[:NUM_FRAMES]
_NUM_FRAMES = 10
SHORTER_FRAME_LIST = EXAMPLE_FRAME_LIST[:_NUM_FRAMES]


def main():
Expand Down
35 changes: 35 additions & 0 deletions src/boilercv/examples/contours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Get bubble contours."""

import cv2 as cv

from boilercv.data import VIDEO
from boilercv.data.frames import idx
from boilercv.data.sets import get_dataset
from boilercv.examples import EXAMPLE_CONTOURS, EXAMPLE_NUM_FRAMES, EXAMPLE_VIDEO_NAME
from boilercv.gui import view_images
from boilercv.images import scale_bool
from boilercv.images.cv import draw_contours
from boilercv.stages.contours import get_all_contours
from boilercv.types import ArrInt, Img


def main():
ds = get_dataset(EXAMPLE_VIDEO_NAME, EXAMPLE_NUM_FRAMES)
video = ds[VIDEO]
df = get_all_contours(
cv.bitwise_not(scale_bool(video.values)), method=cv.CHAIN_APPROX_SIMPLE
)
df.to_hdf(EXAMPLE_CONTOURS, "contours", complib="zlib", complevel=9)
result: list[Img] = []
for frame_num, frame in enumerate(video):
contours: list[ArrInt] = list( # type: ignore
df.loc[idx[frame_num], :] # type: ignore
.groupby("contour")
.apply(lambda grp: grp.values) # type: ignore
)
result.append(draw_contours(scale_bool(frame.values), contours))
view_images(result)


if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion src/boilercv/examples/contours/__init__.py

This file was deleted.

10 changes: 3 additions & 7 deletions src/boilercv/examples/detect_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@
from boilercv.data import VIDEO, YX_PX, apply_to_img_da
from boilercv.data.frames import df_points
from boilercv.data.sets import get_dataset
from boilercv.examples import EXAMPLE_ROI, EXAMPLE_VIDEO_NAME
from boilercv.examples import EXAMPLE_NUM_FRAMES, EXAMPLE_ROI, EXAMPLE_VIDEO_NAME
from boilercv.gui import get_calling_scope_name, save_roi, view_images
from boilercv.images import scale_bool
from boilercv.images.cv import find_contours, get_wall
from boilercv.types import DA, ArrInt, Img

NUM_FRAMES = 1000


def main():
ds = get_dataset(EXAMPLE_VIDEO_NAME, NUM_FRAMES)
ds = get_dataset(EXAMPLE_VIDEO_NAME, EXAMPLE_NUM_FRAMES)
video = ds[VIDEO]
roi = ds["roi"]
wall: DA = apply_to_img_da(get_wall, scale_bool(roi), name="wall")
Expand All @@ -40,9 +38,7 @@ def main():
if len(contours) > 1:
warn("More than one contour found when searching for the ROI.", stacklevel=1)
save_roi(contours[0], EXAMPLE_ROI)
# df = get_all_contours(video.values, method=cv.CHAIN_APPROX_SIMPLE)
if DEBUG:
view_images(dict(boiling_surface=boiling_surface, roi=roi))
view_images(dict(boiling_surface=boiling_surface, roi=roi))


def find_boiling_surface(img: Img) -> tuple[Img, ArrInt]:
Expand Down
9 changes: 9 additions & 0 deletions src/boilercv/examples/fill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Fill bubble contours."""


def main():
...


if __name__ == "__main__":
main()
58 changes: 0 additions & 58 deletions src/boilercv/examples/get_contours.py

This file was deleted.

5 changes: 2 additions & 3 deletions src/boilercv/examples/large/convert.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""Convert CINE files to the NetCDF file format."""

from boilercv.data.video import prepare_dataset
from boilercv.examples import EXAMPLE_NUM_FRAMES
from boilercv.models.paths import LOCAL_PATHS

NUM_FRAMES = 100


def main():
destination = (
LOCAL_PATHS.large_examples / f"{LOCAL_PATHS.large_example_cine.stem}.nc"
)
ds = prepare_dataset(LOCAL_PATHS.large_example_cine, num_frames=NUM_FRAMES)
ds = prepare_dataset(LOCAL_PATHS.large_example_cine, num_frames=EXAMPLE_NUM_FRAMES)
ds.to_netcdf(path=destination)


Expand Down
4 changes: 2 additions & 2 deletions src/boilercv/images/cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ def find_contours(img: Img, method: int = cv.CHAIN_APPROX_NONE) -> list[ArrInt]:


def draw_contours(
img: Img, contours: ArrInt, contour_index: int = -1, thickness=2
img: Img, contours: Sequence[ArrInt], contour_index: int = -1, thickness=2
) -> Img:
"""Draw contours on an image."""
# OpenCV expects contours as shape (N, 1, 2) instead of (N, 2)
contours = np.fliplr(contours).reshape(-1, 1, 2)
contours = [np.fliplr(contour).reshape(-1, 1, 2) for contour in contours]
# Need three-channel image to paint colored contours
three_channel_gray = convert_image(img, cv.COLOR_GRAY2RGB)
return cv.drawContours(
Expand Down
43 changes: 21 additions & 22 deletions src/boilercv/manual/binarize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,29 @@


def main():
logger.info("start binarize")
for source in get_sorted_paths(LOCAL_PATHS.large_sources):
try:
loop(source)
except Exception:
logger.exception(source.stem)
destination = PARAMS.paths.sources / f"{source.stem}.nc"
if destination.exists():
continue


def loop(source):
with xr.open_dataset(source) as ds:
video = ds[VIDEO]
maximum = video.max(FRAME)
flooded: DA = apply_to_img_da(flood, maximum)
roi = apply_to_img_da(get_roi, scale_bool(flooded))
masked: DA = apply_to_img_da(apply_mask, video, scale_bool(roi), vectorize=True)
binarized: DA = apply_to_img_da(binarize, masked, vectorize=True)
ds[VIDEO] = pack(binarized)
ds.to_netcdf(
path=PARAMS.paths.sources / source.name,
encoding={VIDEO: {"zlib": True}},
)
ds[ROI] = roi
ds = ds.drop_vars(VIDEO)
ds.to_netcdf(path=PARAMS.paths.rois / source.name)
with xr.open_dataset(source) as ds:
video = ds[VIDEO]
maximum = video.max(FRAME)
flooded: DA = apply_to_img_da(flood, maximum)
roi: DA = apply_to_img_da(get_roi, scale_bool(flooded))
masked: DA = apply_to_img_da(
apply_mask, video, scale_bool(roi), vectorize=True
)
binarized: DA = apply_to_img_da(binarize, masked, vectorize=True)
ds[VIDEO] = pack(binarized)
ds.to_netcdf(
path=PARAMS.paths.sources / source.name,
encoding={VIDEO: {"zlib": True}},
)
ds[ROI] = roi
ds = ds.drop_vars(VIDEO)
ds.to_netcdf(path=PARAMS.paths.rois / source.name)
logger.info("finish binarize")


if __name__ == "__main__":
Expand Down
2 changes: 2 additions & 0 deletions src/boilercv/manual/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


def main():
logger.info("start convert")
for source in get_sorted_paths(LOCAL_PATHS.cines):
destination = LOCAL_PATHS.large_sources / f"{source.stem}.nc"
if destination.exists():
Expand All @@ -16,6 +17,7 @@ def main():
except Exception:
logger.exception(source.stem)
continue
logger.info("finish convert")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 0bd731e

Please sign in to comment.