-
Notifications
You must be signed in to change notification settings - Fork 0
Write YOLO Detector and Drawer #1
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
Open
FelonEkonom
wants to merge
23
commits into
master
Choose a base branch
from
implementation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,125
−40
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
97c8cb6
Write YOLO Live and Offline filters
FelonEkonom d9f4b10
Add demo for YOLO on computer camera
FelonEkonom 620f546
Prepare demos WiP
FelonEkonom 5020dd4
Improve examples
FelonEkonom 9cacb8b
Improve readme
FelonEkonom ed4e72a
Fix mix install
FelonEkonom eec0fec
Adjust to changes in raw video
FelonEkonom 8398c33
Fix setting log level
FelonEkonom e1f7f5a
Fix typo
FelonEkonom 9c4b8fd
Implement CR suggestions WiP
FelonEkonom 07de53c
Implement CR suggestions WiP
FelonEkonom 4826aa3
Improve demos WiP
FelonEkonom d4a5c82
Update raw video dep in examples
FelonEkonom 5399eb6
Rewrite almost everything WiP
FelonEkonom ea18ce4
Update Mix.exs
FelonEkonom 741e5dd
Rewrite plugin
FelonEkonom 5e03306
Fix livebook examples
FelonEkonom db48b51
Merge remote-tracking branch 'origin/master' into implementation
FelonEkonom 2dae611
Update docs
FelonEkonom fcc6dc7
Improve detector docs
FelonEkonom 0ca52ee
Add lacking handle end of stream
FelonEkonom a6f72d0
Bump version to clear cache in CircleCI
FelonEkonom 253763e
Maybe fix Circle
FelonEkonom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| examples/models/*.onnx | ||
| examples/outputs/* | ||
|
|
||
| compile_commands.json | ||
| .gdb_history | ||
| bundlex.sh | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,37 @@ | ||
| # Membrane Template Plugin | ||
| # Membrane YOLO Plugin | ||
|
|
||
| [](https://hex.pm/packages/membrane_template_plugin) | ||
| [](https://hexdocs.pm/membrane_template_plugin) | ||
| [](https://circleci.com/gh/membraneframework/membrane_template_plugin) | ||
| [](https://hex.pm/packages/membrane_yolo_plugin) | ||
| [](https://hexdocs.pm/membrane_yolo_plugin) | ||
| [](https://circleci.com/gh/membraneframework/membrane_yolo§ §_plugin) | ||
|
|
||
| This repository contains a template for new plugins. | ||
| Contains 2 Membrane Filters: | ||
| - `Membrane.YOLO.Detector` - for running object detection on a video stream. | ||
| - `Membrane.YOLO.Drawer` - for drawing object detection results generated by `Membrane.YOLO.Detector`. | ||
|
|
||
| Check out different branches for other flavors of this template. | ||
| Uses under the hood [yolo_elixir](https://github.com/poeticoding/yolo_elixir). | ||
|
|
||
| It's a part of the [Membrane Framework](https://membrane.stream). | ||
|
|
||
| ## Installation | ||
|
|
||
| The package can be installed by adding `membrane_template_plugin` to your list of dependencies in `mix.exs`: | ||
| The package can be installed by adding `membrane_yolo_plugin` to your list of dependencies in `mix.exs`: | ||
|
|
||
| ```elixir | ||
| def deps do | ||
| [ | ||
| {:membrane_template_plugin, "~> 0.1.0"} | ||
| {:membrane_yolo_plugin, "~> 0.1.0"} | ||
| ] | ||
| end | ||
| ``` | ||
|
|
||
| ## Usage | ||
| ## Examples | ||
|
|
||
| TODO | ||
| Open a Livebook `examples/yolo.livemd` or run `$ elixir examples/live_camera_capture.exs`, `$ elixir examples/live_mp4_processing.exs` and `$ elixir examples/offline_mp4_processing.exs` | ||
|
|
||
| ## Copyright and License | ||
|
|
||
| Copyright 2020, [Software Mansion](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_template_plugin) | ||
| Copyright 2025, [Software Mansion](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_yolo_plugin) | ||
|
|
||
| [](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_template_plugin) | ||
| [](https://swmansion.com/?utm_source=git&utm_medium=readme&utm_campaign=membrane_yolo_plugin) | ||
|
|
||
| Licensed under the [Apache License, Version 2.0](LICENSE) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| hardware_acceleration = | ||
| case :os.type() do | ||
| {:unix, :darwin} -> :coreml | ||
| {:unix, :linux} -> :cuda | ||
| end | ||
|
|
||
| Mix.install( | ||
| [ | ||
| {:membrane_yolo_plugin, path: Path.join(__DIR__, "..")}, | ||
| {:membrane_core, "~> 1.0"}, | ||
| {:membrane_camera_capture_plugin, "~> 0.7.4"}, | ||
| {:membrane_ffmpeg_swscale_plugin, "~> 0.16.3"}, | ||
| {:boombox, "~> 0.2.8"}, | ||
| {:exla, "~> 0.10"} | ||
| ], | ||
| config: [ | ||
| ortex: [ | ||
| {Ortex.Native, [features: [hardware_acceleration]]} | ||
| ], | ||
| nx: [ | ||
| default_backend: EXLA.Backend | ||
| ] | ||
| ] | ||
| ) | ||
|
|
||
| Logger.configure(level: :info) | ||
|
|
||
| model_name = "yolox_l.onnx" | ||
| model_path = Path.join("examples/models", model_name) | ||
|
|
||
| if not File.exists?(model_path) do | ||
| model_url = | ||
| "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/#{model_name}" | ||
|
|
||
| %{body: data} = Req.get!(model_url) | ||
| File.write!(model_path, data) | ||
| end | ||
|
|
||
| defmodule YOLO.CameraCapture.Pipeline do | ||
| use Membrane.Pipeline | ||
|
|
||
| @impl true | ||
| def handle_init(_ctx, _opts) do | ||
| spec = | ||
| child(:camera_capture, Membrane.CameraCapture) | ||
| |> child(:swscale_converter, %Membrane.FFmpeg.SWScale.Converter{ | ||
| format: :RGB, | ||
| output_width: 640 | ||
| }) | ||
| |> child(:yolo_detector, %Membrane.YOLO.Detector{ | ||
| mode: :live_low_latency, | ||
| yolo_model: | ||
| YOLO.load( | ||
| model_impl: YOLO.Models.YOLOX, | ||
| model_path: "examples/models/yolox_l.onnx", | ||
| classes_path: "examples/models/coco_classes.json", | ||
| eps: [unquote(hardware_acceleration)] | ||
| ) | ||
| }) | ||
| |> child(:yolo_drawer, Membrane.YOLO.Drawer) | ||
| |> via_in(:input, options: [kind: :video]) | ||
| |> child(:boombox_sink, %Boombox.Bin{output: :player}) | ||
|
|
||
| {[spec: spec], %{}} | ||
| end | ||
| end | ||
|
|
||
| {:ok, _supervisor, pipeline} = Membrane.Pipeline.start_link(YOLO.CameraCapture.Pipeline, []) | ||
| Process.monitor(pipeline) | ||
|
|
||
| receive do | ||
| {:DOWN, _ref, :process, _pid, _reason} -> :ok | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| hardware_acceleration = | ||
| case :os.type() do | ||
| {:unix, :darwin} -> :coreml | ||
| {:unix, :linux} -> :cuda | ||
| end | ||
|
|
||
| Mix.install( | ||
| [ | ||
| {:membrane_yolo_plugin, path: Path.join(__DIR__, "..")}, | ||
| {:membrane_core, "~> 1.0"}, | ||
| {:membrane_camera_capture_plugin, "~> 0.7.4"}, | ||
| {:membrane_ffmpeg_swscale_plugin, "~> 0.16.3"}, | ||
| {:boombox, "~> 0.2.8"}, | ||
| {:exla, "~> 0.10"} | ||
| ], | ||
| config: [ | ||
| ortex: [ | ||
| {Ortex.Native, [features: [hardware_acceleration]]} | ||
| ], | ||
| nx: [ | ||
| default_backend: EXLA.Backend | ||
| ] | ||
| ] | ||
| ) | ||
|
|
||
| Logger.configure(level: :info) | ||
|
|
||
| model_name = "yolox_l.onnx" | ||
| model_path = Path.join("examples/models", model_name) | ||
|
|
||
| if not File.exists?(model_path) do | ||
| model_url = | ||
| "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/#{model_name}" | ||
|
|
||
| %{body: data} = Req.get!(model_url) | ||
| File.write!(model_path, data) | ||
| end | ||
|
|
||
| defmodule YOLO.MP4.LivePipeline do | ||
| use Membrane.Pipeline | ||
|
|
||
| @impl true | ||
| def handle_init(_ctx, _opts) do | ||
| spec = | ||
| child(:mp4_source, %Boombox.Bin{input: "examples/fixtures/street.mp4"}) | ||
| |> via_out(:output, options: [kind: :video]) | ||
| |> child(:transcoder, %Membrane.Transcoder{output_stream_format: Membrane.RawVideo}) | ||
| |> child(:realtimer, Membrane.Realtimer) | ||
| |> child(:swscale_converter, %Membrane.FFmpeg.SWScale.Converter{ | ||
| format: :RGB, | ||
| output_width: 640 | ||
| }) | ||
| |> child(:yolo_detector, %Membrane.YOLO.Detector{ | ||
| mode: :live, | ||
| yolo_model: | ||
| YOLO.load( | ||
| model_impl: YOLO.Models.YOLOX, | ||
| model_path: "examples/models/yolox_l.onnx", | ||
| classes_path: "examples/models/coco_classes.json", | ||
| eps: [unquote(hardware_acceleration)] | ||
| ), | ||
| additional_latency: Membrane.Time.milliseconds(500) | ||
| }) | ||
| |> child(:yolo_drawer, Membrane.YOLO.Drawer) | ||
| |> via_in(:input, options: [kind: :video]) | ||
| |> child(:boombox_sink, %Boombox.Bin{output: :player}) | ||
|
|
||
| {[spec: spec], %{}} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_child_notification(:processing_finished, :boombox_sink, _ctx, state) do | ||
| {[terminate: :normal], state} | ||
| end | ||
| end | ||
|
|
||
| {:ok, supervisor, _pipeline} = Membrane.Pipeline.start_link(YOLO.MP4.LivePipeline, []) | ||
| Process.monitor(supervisor) | ||
|
|
||
| receive do | ||
| {:DOWN, _ref, :process, _pid, _reason} -> :ok | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| hardware_acceleration = | ||
| case :os.type() do | ||
| {:unix, :darwin} -> :coreml | ||
| {:unix, :linux} -> :cuda | ||
| end | ||
|
|
||
| Mix.install( | ||
| [ | ||
| {:membrane_yolo_plugin, path: Path.join(__DIR__, "..")}, | ||
| {:membrane_core, "~> 1.0"}, | ||
| {:membrane_camera_capture_plugin, "~> 0.7.4"}, | ||
| {:membrane_ffmpeg_swscale_plugin, "~> 0.16.3"}, | ||
| {:boombox, "~> 0.2.8"}, | ||
| {:exla, "~> 0.10"} | ||
| ], | ||
| config: [ | ||
| ortex: [ | ||
| {Ortex.Native, [features: [hardware_acceleration]]} | ||
| ], | ||
| nx: [ | ||
| default_backend: EXLA.Backend | ||
| ] | ||
| ] | ||
| ) | ||
|
|
||
| Logger.configure(level: :info) | ||
|
|
||
| model_name = "yolox_l.onnx" | ||
| model_path = Path.join("examples/models", model_name) | ||
|
|
||
| if not File.exists?(model_path) do | ||
| model_url = | ||
| "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/#{model_name}" | ||
|
|
||
| %{body: data} = Req.get!(model_url) | ||
| File.write!(model_path, data) | ||
| end | ||
|
|
||
| result_file_path = "examples/outputs/street_bounding_boxes.mp4" | ||
|
|
||
| defmodule YOLO.MP4.OfflinePipeline do | ||
| use Membrane.Pipeline | ||
| require Membrane.Logger | ||
|
|
||
| @impl true | ||
| def handle_init(_ctx, _opts) do | ||
| spec = | ||
| child(:mp4_source, %Boombox.Bin{input: "examples/fixtures/street_short.mp4"}) | ||
| |> via_out(:output, options: [kind: :video]) | ||
| |> child(:transcoder, %Membrane.Transcoder{output_stream_format: Membrane.RawVideo}) | ||
| |> child(:rgb_converter, %Membrane.FFmpeg.SWScale.Converter{ | ||
| format: :RGB, | ||
| output_width: 640 | ||
| }) | ||
| |> child(:yolo_detector, %Membrane.YOLO.Detector{ | ||
| mode: :offline, | ||
| yolo_model: | ||
| YOLO.load( | ||
| model_impl: YOLO.Models.YOLOX, | ||
| model_path: "examples/models/yolox_l.onnx", | ||
| classes_path: "examples/models/coco_classes.json", | ||
| eps: [unquote(hardware_acceleration)] | ||
| ) | ||
| }) | ||
| |> child(:yolo_drawer, Membrane.YOLO.Drawer) | ||
| |> child(:debug_logger, %Membrane.Debug.Filter{ | ||
| handle_buffer: fn buffer -> | ||
| pts_ms = Membrane.Time.as_milliseconds(buffer.pts, :round) | ||
|
|
||
| Membrane.Logger.info(""" | ||
| Processed #{inspect(pts_ms)} ms of 10_000 ms of fixture video | ||
| """) | ||
| end | ||
| }) | ||
| |> child(:i420_converter, %Membrane.FFmpeg.SWScale.Converter{ | ||
| format: :I420 | ||
| }) | ||
| |> via_in(:input, options: [kind: :video]) | ||
| |> child(:boombox_sink, %Boombox.Bin{output: unquote(result_file_path)}) | ||
|
|
||
| {[spec: spec], %{}} | ||
| end | ||
|
|
||
| @impl true | ||
| def handle_child_notification(:processing_finished, :boombox_sink, _ctx, state) do | ||
| {[terminate: :normal], state} | ||
| end | ||
| end | ||
|
|
||
| {:ok, supervisor, _pipeline} = Membrane.Pipeline.start_link(YOLO.MP4.OfflinePipeline, []) | ||
| Process.monitor(supervisor) | ||
|
|
||
| receive do | ||
| {:DOWN, _ref, :process, _pid, :normal} -> :ok | ||
| end | ||
|
|
||
| Boombox.play(result_file_path) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should only keep examples in the livebook. It's basically duplicated in this and other .exs scripts, not great for maintenance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The advantage of
.exsexamples is that the video player is visible right away, at least on my computer