Skip to content

Commit

Permalink
Make some more processors compatible with video-frames
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-uva committed Jan 9, 2024
1 parent b3981c3 commit 588290a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
4 changes: 2 additions & 2 deletions processors/metrics/clarifai_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def is_compatible_with(cls, module=None, user=None):
:param module: Module to determine compatibility with
"""
return module.type.startswith("image-downloader")
return module.type.startswith("image-downloader") or module.type == "video-frames"

options = {
"amount": {
Expand Down Expand Up @@ -132,7 +132,7 @@ def process(self):
send_batch = True

if image:
if image.name.startswith("."):
if image.name.startswith(".") or image.suffix in (".json", ".log"):
# .metadata.json
continue

Expand Down
4 changes: 2 additions & 2 deletions processors/metrics/google_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def is_compatible_with(cls, module=None, user=None):
:param module: Module to determine compatibility with
"""
return module.type.startswith("image-downloader")
return module.type.startswith("image-downloader") or module.type == "video-frames"

options = {
"amount": {
Expand Down Expand Up @@ -113,7 +113,7 @@ def process(self):
self.dataset.update_status("Annotating image %i/%i" % (done, total))
self.dataset.update_progress(done / total)

if image_file.name.startswith("."):
if image_file.name.startswith(".") or image_file.suffix in (".json", ".log"):
self.dataset.log(f"Skipping file {image_file.name}, probably not an image.")
continue

Expand Down
20 changes: 11 additions & 9 deletions processors/visualisation/image_wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from PIL import Image, ImageFile, ImageOps, ImageDraw, UnidentifiedImageError
from sklearn.cluster import KMeans
from pathlib import Path

from common.lib.helpers import UserInput, convert_to_int
from backend.lib.processor import BasicProcessor
Expand Down Expand Up @@ -53,7 +54,7 @@ def is_compatible_with(cls, module=None, user=None):
:param module: Dataset or processor to determine compatibility with
"""
return module.type.startswith("image-downloader")
return module.type.startswith("image-downloader") or module.type == "video-frames"

@classmethod
def get_options(cls, parent_dataset=None, user=None):
Expand Down Expand Up @@ -152,15 +153,15 @@ def numpy_to_rgb(numpy_array):
try:
picture = Image.open(str(path))
except UnidentifiedImageError:
self.dataset.update_status("Image %s could not be parsed. Skipping." % path)
self.dataset.update_status(f"File {path.name} could not be parsed. Skipping.")
continue

self.dataset.update_status("Analysing %s (%i/%i)" % (path.name, len(dimensions), self.source_dataset.num_rows))
self.dataset.update_status(f"Analysing {path.name} ({len(dimensions):,}/{self.source_dataset.num_rows:,})")
self.dataset.update_progress(len(dimensions) / self.source_dataset.num_rows / 2)

# these calculations can take ages for huge images, so resize if it is
# larger than the threshold
dimensions[path.name] = (picture.width, picture.height)
dimensions[str(path)] = (picture.width, picture.height)
value = 0

if sort_mode not in ("", "random") and (picture.height > sample_max or picture.width > sample_max):
Expand Down Expand Up @@ -238,9 +239,9 @@ def numpy_to_rgb(numpy_array):

# converted to HSV, because RGB does not sort nicely
if type(value) is int:
image_colours[path.name] = value
image_colours[str(path)] = value
else:
image_colours[path.name] = colorsys.rgb_to_hsv(*value)
image_colours[str(path)] = colorsys.rgb_to_hsv(*value)

index += 1

Expand Down Expand Up @@ -331,9 +332,9 @@ def numpy_to_rgb(numpy_array):
size_y = math.ceil(fitted_pixels / size_x / tile_y) * tile_y

else:
raise NotImplementedError("Sizing mode '%s' not implemented" % sizing_mode)
raise NotImplementedError(f"Sizing mode '{sizing_mode}' not implemented")

self.dataset.log("Canvas size is %ix%i" % (size_x, size_y))
self.dataset.log(f"Canvas size is {size_x}x{size_y}")
wall = Image.new("RGBA", (int(size_x), int(size_y)))
ImageDraw.floodfill(wall, (0, 0), (255, 255, 255, 0)) # transparent background
counter = 0
Expand All @@ -345,8 +346,9 @@ def numpy_to_rgb(numpy_array):

# now actually putting the images on a wall is relatively trivial
for path in sorted_image_files:
path = Path(path)
counter += 1
self.dataset.update_status("Rendering %s (%i/%i) to image wall" % (path, counter, len(sorted_image_files)))
self.dataset.update_status(f"Rendering {path.name} ({counter:,}/{len(sorted_image_files):,}) to image wall")
self.dataset.update_progress(0.5 + (counter / len(sorted_image_files) / 2))
picture = Image.open(str(staging_area.joinpath(path)))

Expand Down

0 comments on commit 588290a

Please sign in to comment.