Skip to content

Commit

Permalink
Merge pull request #33 from pozitronik/issue_11
Browse files Browse the repository at this point in the history
Issue 11
  • Loading branch information
pozitronik authored Jul 25, 2023
2 parents 23429b3 + d05d55d commit 95e8a26
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
2 changes: 2 additions & 0 deletions sinner/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ def run(self, set_progress: Callable[[int], None] | None = None) -> None:
current_processor = BaseFrameProcessor.create(processor_name, self.parameters)
current_processor.process(frames_handler=current_handler, state=state, desc=processor_name, extract_frames=self.extract_frames, set_progress=set_progress)
current_processor.release_resources()
if not state.final_check():
raise Exception("Something went wrong on processed frames check")
current_target_path = state.out_dir
temp_resources.append(state.out_dir)
if self.extract_frames:
Expand Down
36 changes: 33 additions & 3 deletions sinner/State.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pathlib import Path
from typing import Any, Dict, List

from sinner.Status import Status
from sinner.Status import Status, Mood
from sinner.typing import Frame
from sinner.utilities import write_image
from sinner.validators.AttributeLoader import AttributeLoader, Rules
Expand Down Expand Up @@ -126,15 +126,23 @@ def is_started(self) -> bool:
def is_finished(self) -> bool:
return self.frames_count <= self.processed_frames_count != 0

@property
def processed_frames(self) -> List[str]:
return [os.path.join(self.out_dir, file) for file in os.listdir(self.out_dir) if file.endswith(".png")]

# Returns count of already processed frame for this target (0, if none).
@property
def processed_frames_count(self) -> int:
return len([os.path.join(self.out_dir, file) for file in os.listdir(self.out_dir) if file.endswith(".png")])
return len(self.processed_frames)

@property
def extracted_frames(self) -> List[str]:
return [os.path.join(self.in_dir, file) for file in os.listdir(self.in_dir) if file.endswith(".png")]

# Returns count of already extracted frame for this target (0, if none).
@property
def extracted_frames_count(self) -> int:
return len([os.path.join(self.in_dir, file) for file in os.listdir(self.in_dir) if file.endswith(".png")])
return len(self.extracted_frames)

# Returns count of still unprocessed frame for this target (0, if none).
@property
Expand All @@ -151,3 +159,25 @@ def zfill_length(self) -> int:
if self._zfill_length is None:
self._zfill_length = len(str(self.frames_count))
return self._zfill_length

def final_check(self) -> bool:
result = True
processed_frames_count = self.processed_frames_count
if not self.is_finished:
self.update_status(message=f"The final processing check failed: processing is done, but state is not finished. Check in {self.out_dir}, may be some frames lost?", mood=Mood.BAD)
result = False

# check if the last file name in the processed sequence is right
last_file_name = int(max(os.scandir(self.out_dir), key=lambda entry: int(os.path.splitext(entry.name)[0])).name.split('.')[0])
if self.frames_count != last_file_name:
self.update_status(message=f"Last processed frame is {last_file_name}, but expected {self.frames_count}. Check in {self.out_dir} for it.", mood=Mood.BAD)
result = False
# check if all frames are non zero-sized
zero_sized_files_count = 0
for file_path in self.processed_frames:
if os.path.getsize(file_path) == 0:
zero_sized_files_count += 1
if zero_sized_files_count > 0:
self.update_status(message=f"There is zero-sized files in {self.out_dir} temp directory ({zero_sized_files_count} of {processed_frames_count}). Check for free disk space and access rights.", mood=Mood.BAD)
result = False
return result
2 changes: 1 addition & 1 deletion sinner/processors/frame/BaseFrameProcessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def process(self, frames_handler: BaseFrameHandler, state: State, extract_frames
self.progress_callback = set_progress
frames_handler.current_frame_index = state.processed_frames_count
# todo: do not create on intermediate directory handler
frames_list: FramesDataType = frames_handler.get_frames_paths(state.in_dir)[state.processed_frames_count + 1:] if extract_frames and isinstance(frames_handler, Iterable) else frames_handler
frames_list: FramesDataType = frames_handler.get_frames_paths(state.in_dir)[state.processed_frames_count:] if extract_frames and isinstance(frames_handler, Iterable) else frames_handler
with tqdm(
total=state.frames_count,
desc=desc, unit='frame',
Expand Down
28 changes: 28 additions & 0 deletions tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,31 @@ def test_states() -> None:
assert state.is_finished is True
assert state.processed_frames_count == 10
assert state.unprocessed_frames_count == 0


def test_final_check_ok():
state = State(parameters=Namespace(), target_path=target_mp4, temp_dir=tmp_dir, frames_count=TARGET_FC, processor_name='DummyProcessor')
shutil.copytree(state_frames_dir, state.out_dir, dirs_exist_ok=True)
assert state.final_check() is True


def test_final_check_fail_state():
state = State(parameters=Namespace(), target_path=target_mp4, temp_dir=tmp_dir, frames_count=TARGET_FC, processor_name='DummyProcessor')
shutil.copytree(state_frames_dir, state.out_dir, dirs_exist_ok=True)
os.remove(os.path.join(state.out_dir, '05.png'))
assert state.final_check() is False


def test_final_check_fail_last_file():
state = State(parameters=Namespace(), target_path=target_mp4, temp_dir=tmp_dir, frames_count=TARGET_FC, processor_name='DummyProcessor')
shutil.copytree(state_frames_dir, state.out_dir, dirs_exist_ok=True)
os.rename(src=os.path.join(state.out_dir, '10.png'), dst=os.path.join(state.out_dir, '11.png'))
assert state.final_check() is False


def test_final_check_fail_zero_files():
state = State(parameters=Namespace(), target_path=target_mp4, temp_dir=tmp_dir, frames_count=TARGET_FC, processor_name='DummyProcessor')
shutil.copytree(state_frames_dir, state.out_dir, dirs_exist_ok=True)
with open(os.path.join(state.out_dir, '04.png'), 'r+') as file:
file.truncate(0)
assert state.final_check() is False

0 comments on commit 95e8a26

Please sign in to comment.