Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix async demo in exportable code #2154

Merged
merged 9 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ All notable changes to this project will be documented in this file.
### Enhancements

- Make semantic segmentation OpenVINO models compatible with ModelAPI (<https://github.com/openvinotoolkit/training_extensions/pull/2029>).
- Support label hierarchy through LabelTree in LabelSchema for classification task (<https://github.com/openvinotoolkit/training_extensions/pull/2149>, <https://github.com/openvinotoolkit/training_extensions/pull/2152>)
- Support label hierarchy through LabelTree in LabelSchema for classification task (<https://github.com/openvinotoolkit/training_extensions/pull/2149>, <https://github.com/openvinotoolkit/training_extensions/pull/2152>).
- Enhance exportable code file structure, video inference and default value for demo (<https://github.com/openvinotoolkit/training_extensions/pull/2051>).

### Bug fixes

-
- Fix async mode inference for demo in exportable code (<https://github.com/openvinotoolkit/training_extensions/pull/2154>)

### Known issues

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from otx.api.usecases.exportable_code.streamer import get_streamer
from otx.api.usecases.exportable_code.visualizers import Visualizer
from otx.api.usecases.exportable_code.prediction_to_annotation_converter import DetectionToAnnotationConverter
from otx.api.utils.vis_utils import dump_frames


Expand Down Expand Up @@ -45,13 +46,16 @@ def run(self, input_stream: Union[int, str], loop: bool = False) -> None:
for frame in streamer:
results = self.async_pipeline.get_result(next_frame_id_to_show)
while results:
start_time = time.perf_counter()
output = self.render_result(results)
next_frame_id_to_show += 1
self.visualizer.show(output)
if self.visualizer.output:
saved_frames.append(output)
if self.visualizer.is_quit():
stop_visualization = True
# visualize video not faster than the original FPS
self.visualizer.video_delay(time.perf_counter() - start_time, streamer)
results = self.async_pipeline.get_result(next_frame_id_to_show)
if stop_visualization:
break
Expand All @@ -63,13 +67,19 @@ def run(self, input_stream: Union[int, str], loop: bool = False) -> None:
results = self.async_pipeline.get_result(next_frame_id_to_show)
output = self.render_result(results)
self.visualizer.show(output)
if self.visualizer.output:
saved_frames.append(output)
# visualize video not faster than the original FPS
self.visualizer.video_delay(time.perf_counter() - start_time, streamer)
dump_frames(saved_frames, self.visualizer.output, input_stream, streamer)

def render_result(self, results: Tuple[Any, dict]) -> np.ndarray:
"""Render for results of inference."""
predictions, frame_meta = results
if isinstance(self.converter, DetectionToAnnotationConverter):
# Predictions for the detection task
predictions = np.array([[pred.id, pred.score, *pred.get_coords()] for pred in predictions])
predictions.shape = len(predictions), 6
annotation_scene = self.converter.convert_to_annotation(predictions, frame_meta)
current_frame = frame_meta["frame"]
output = self.visualizer.draw(current_frame, annotation_scene, frame_meta)
Expand Down
2 changes: 1 addition & 1 deletion otx/api/usecases/exportable_code/demo/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openvino==2022.3.0
openmodelzoo-modelapi==2022.3.0
otx @ git+https://github.com/openvinotoolkit/training_extensions/@fb357c9956cc3865797bfb9a52179625454da275#egg=otx
otx @ git+https://github.com/openvinotoolkit/training_extensions/@5519de55fc0e16cdfd018e2cfa681ad47e61ce52#egg=otx
numpy>=1.21.0,<=1.23.5 # np.bool was removed in 1.24.0 which was used in openvino runtime
4 changes: 2 additions & 2 deletions otx/api/utils/vis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def dump_frames(saved_frames: list, output: str, input_path: Union[str, int], ca
out.release()
print(f"Video was saved to {video_path}")
else:
if len(filenames) < len(saved_frames):
filenames = [f"output_{i}" for i, _ in enumerate(saved_frames)]
if len(filenames) != len(saved_frames):
filenames = [f"output_{i}.jpeg" for i, _ in enumerate(saved_frames)]
for filename, frame in zip(filenames, saved_frames):
image_path = str(output_path / filename)
cv2.imwrite(image_path, frame)
Expand Down
20 changes: 20 additions & 0 deletions tests/test_suite/run_test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,26 @@ def otx_deploy_openvino_testing(template, root, otx_dir, args):
"../model",
"-i",
os.path.join(otx_dir, args["--input"]),
"--inference_type",
"sync",
"--no_show",
"--output",
os.path.join(deployment_dir, "output"),
],
cwd=os.path.join(deployment_dir, "python"),
)
assert os.path.exists(os.path.join(deployment_dir, "output"))

check_run(
[
"python3",
"demo.py",
"-m",
"../model",
"-i",
os.path.join(otx_dir, args["--input"]),
"--inference_type",
"async",
"--no_show",
"--output",
os.path.join(deployment_dir, "output"),
Expand Down