Skip to content

Commit

Permalink
use only first image to calculate chunk size
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Zhavoronkov committed Mar 25, 2020
1 parent 5c58603 commit 09f9f1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
33 changes: 10 additions & 23 deletions cvat/apps/engine/media_extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def image_names(self):
pass

@abstractmethod
def get_avg_image_size(self, percent_to_analize=10):
def get_avg_image_size(self):
pass

#Note step, start, stop have no affect
Expand Down Expand Up @@ -97,15 +97,9 @@ def save_preview(self, preview_path):
def image_names(self):
return self._source_path

def get_avg_image_size(self, percent_to_analize=10):
widths = []
heights = []
step = 100 // percent_to_analize or 1
for img_path in itertools.islice(self._source_path, 0, None, step):
img = Image.open(img_path)
widths.append(img.width)
heights.append(img.height)
return (sum(widths) // len(widths), sum(heights) // len(heights))
def get_avg_image_size(self):
img = Image.open(self._source_path[0])
return img.width, img.height

#Note step, start, stop have no affect
class DirectoryReader(ImageListReader):
Expand Down Expand Up @@ -197,16 +191,9 @@ def save_preview(self, preview_path):
with open(preview_path, 'wb') as f:
f.write(self._zip_source.read(self._source_path[0]))

def get_avg_image_size(self, percent_to_analize=10):
widths = []
heights = []
step = 100 // percent_to_analize or 1
for img_path in itertools.islice(self._source_path, 0, None, step):
img = Image.open(BytesIO(self._zip_source.read(img_path)))
widths.append(img.width)
heights.append(img.height)
return (sum(widths) // len(widths), sum(heights) // len(heights))

def get_avg_image_size(self):
img = Image.open(BytesIO(self._zip_source.read(self._source_path[0])))
return img.width, img.height

@property
def image_names(self):
Expand Down Expand Up @@ -259,9 +246,9 @@ def save_preview(self, preview_path):
def image_names(self):
return self._source_path

def get_avg_image_size(self, percent_to_analize=10):
first_image = (next(iter(self)))[0]
return first_image.width, first_image.height
def get_avg_image_size(self):
image = (next(iter(self)))[0]
return image.width, image.height

class IChunkWriter(ABC):
def __init__(self, quality):
Expand Down
27 changes: 15 additions & 12 deletions cvat/apps/engine/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,22 @@ def update_progress(progress):
original_chunk_writer = original_chunk_writer_class(100)

# calculate chunk size if it isn't specified
if db_data.chunk_size is None and isinstance(compressed_chunk_writer, ZipCompressedChunkWriter):
avg_w, avg_h = extractor.get_avg_image_size()
avg_area = avg_h * avg_w
if avg_area <= 1920 * 1080:
db_data.chunk_size = 36
elif avg_area <= 2560 * 1440:
db_data.chunk_size = 18
elif avg_area <= 3840 * 2160:
db_data.chunk_size = 9
elif avg_area <= 5120 * 2880:
db_data.chunk_size = 4
if db_data.chunk_size is None:
if isinstance(compressed_chunk_writer, ZipCompressedChunkWriter):
avg_w, avg_h = extractor.get_avg_image_size()
avg_area = avg_h * avg_w
if avg_area <= 1920 * 1080:
db_data.chunk_size = 36
elif avg_area <= 2560 * 1440:
db_data.chunk_size = 18
elif avg_area <= 3840 * 2160:
db_data.chunk_size = 9
elif avg_area <= 5120 * 2880:
db_data.chunk_size = 4
else:
db_data.chunk_size = 2
else:
db_data.chunk_size = 2
db_data.chunk_size = 36

frame_counter = 0
total_len = len(extractor) or 100
Expand Down

0 comments on commit 09f9f1e

Please sign in to comment.