Skip to content

Commit

Permalink
Merge pull request #189 from cocoa-xu/cx-dnn-bboxes
Browse files Browse the repository at this point in the history
Using `Nx.Tensor` or `Evision.Mat` as input argument for `bboxes`
  • Loading branch information
cocoa-xu authored Apr 17, 2023
2 parents 646613d + 47acf78 commit 862db7d
Show file tree
Hide file tree
Showing 6 changed files with 607 additions and 117 deletions.
42 changes: 33 additions & 9 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,42 @@
# Changelog

## v0.1.31 (2022-04-17)
[Browse the Repository](https://github.com/cocoa-xu/evision/tree/v0.1.31) | [Released Assets](https://github.com/cocoa-xu/evision/releases/tag/v0.1.31)

### Changed
- [Evision.DNN] Added support for passing `Nx.Tensor` or `Evision.Mat` as the input argument of `bboxes` in `Evision.DNN.nmsBoxes/{4,5}`.
- [Evision.DNN] Added support for passing `Nx.Tensor` or `Evision.Mat` as the input argument of `bboxes` in
- `Evision.DNN.nmsBoxes/{4,5}`

```elixir
iex> Evision.DNN.nmsBoxes([{0,1,2,3}], [1], 0.4, 0.3)
[0]
iex> Evision.DNN.nmsBoxes(Nx.tensor([[0,1,2,3]]), [1], 0.4, 0.3)
[0]
iex> Evision.DNN.nmsBoxes(Evision.Mat.literal([[0,1,2,3]], :f64), [1], 0.4, 0.3)
[0]
```
```elixir
iex> Evision.DNN.nmsBoxes([{0,1,2,3}], [1], 0.4, 0.3)
[0]
iex> Evision.DNN.nmsBoxes(Nx.tensor([[0,1,2,3]]), [1], 0.4, 0.3)
[0]
iex> Evision.DNN.nmsBoxes(Evision.Mat.literal([[0,1,2,3]], :f64), [1], 0.4, 0.3)
[0]
```

- `Evision.DNN.nmsBoxesBatched/{5,6}`

```elixir
iex> Evision.DNN.nmsBoxesBatched([{0,1,2,3}], [1], [1], 0.4, 0.3)
[0]
iex> Evision.DNN.nmsBoxesBatched(Nx.tensor([[0,1,2,3]]), [1], [1], 0.4, 0.3)
[0]
iex> Evision.DNN.nmsBoxesBatched(Evision.Mat.literal([[0,1,2,3]], :f64), [1], [1], 0.4, 0.3)
[0]
```

- `Evision.DNN.softNMSBoxes/{4,5}`

```elixir
iex> Evision.DNN.softNMSBoxes([{0,1,2,3}], [1], 0.4, 0.3)
{[1.0], [0]}
iex> Evision.DNN.softNMSBoxes(Nx.tensor([[0,1,2,3]]), [1], 0.4, 0.3)
{[1.0], [0]}
iex> Evision.DNN.softNMSBoxes(Evision.Mat.literal([[0,1,2,3]], :s32), [1], 0.4, 0.3)
{[1.0], [0]}
```

## v0.1.30 (2022-03-24)
[Browse the Repository](https://github.com/cocoa-xu/evision/tree/v0.1.30) | [Released Assets](https://github.com/cocoa-xu/evision/releases/tag/v0.1.30)
Expand Down
46 changes: 46 additions & 0 deletions c_src/evision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,52 @@ inline bool evision_to_generic_vec(ErlNifEnv *env, ERL_NIF_TERM obj, std::vector
return true;
}

template <>
inline bool evision_to_generic_vec(ErlNifEnv *env, ERL_NIF_TERM obj, std::vector<cv::Rect>& value, const ArgInfo& info)
{
if (evision::nif::check_nil(env, obj)) {
return true;
}

if (!enif_is_list(env, obj)) {
ErlNifBinary data;
if (enif_inspect_binary(env, obj, &data)) {
const size_t rect_size = sizeof(int) * 4;
if (data.size > 0 && data.size % rect_size == 0) {
size_t num_elements = data.size / rect_size;
value.resize(num_elements);
memcpy(value.data(), data.data, data.size);
return true;
}
}
return false;
}
unsigned n = 0;
if (!enif_get_list_length(env, obj, &n)) {
return false;
}
// printf("arg: %s, list length: %d\r\n", info.name, n);

value.resize(n);
ERL_NIF_TERM head, tail, arr = obj;
for (size_t i = 0; i < n; i++)
{
cv::Rect inner_val;
if (!enif_get_list_cell(env, arr, &head, &tail)) {
return false;
}

if (!evision_to(env, head, inner_val, info)) {
return false;
}

value[i] = inner_val;
arr = tail;
}

return true;
}

template <>
inline bool evision_to_generic_vec(ErlNifEnv *env, ERL_NIF_TERM obj, std::vector<std::vector<int>>& value, const ArgInfo& info)
{
Expand Down
Loading

0 comments on commit 862db7d

Please sign in to comment.