Skip to content

Commit

Permalink
test/example: alias Evision, as: Cv
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-xu committed Sep 1, 2022
1 parent c62c487 commit c65cc7a
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 261 deletions.
19 changes: 8 additions & 11 deletions examples/cifar10.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,18 @@ Helper.download!("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat0

## alias
```elixir
# alias Evision as OpenCV
# please see the Namespace section in the README.md for reasons/motivations
# (in the root directory of the source code)
alias Evision, as: OpenCV
alias Evision, as: Cv
```

## Get input from OpenCV

```elixir
# in real-life use cases, the input source might be a camera
# instead of downloading a file and reading it
{:ok, img} = OpenCV.imread("cat.jpg", flags: OpenCV.cv_IMREAD_ANYCOLOR())
{:ok, resized_img} = OpenCV.resize(img, [128, 128])
{:ok, img} = Cv.imread("cat.jpg", flags: Cv.cv_IMREAD_ANYCOLOR())
{:ok, resized_img} = Cv.resize(img, [128, 128])

OpenCV.imencode(".png", resized_img)
Cv.imencode(".png", resized_img)
|> elem(1)
|> IO.iodata_to_binary()
|> Kino.Image.new(:png)
Expand Down Expand Up @@ -347,9 +344,9 @@ params = Demo.run(backend: Torchx.Backend, epochs: 50)
## Test

```elixir
{:ok, resized_img} = OpenCV.resize(img, [128, 128])
{:ok, resized_img} = Cv.resize(img, [128, 128])

OpenCV.imencode(".png", resized_img)
Cv.imencode(".png", resized_img)
|> then(fn {:ok, val} -> val end)
|> IO.iodata_to_binary()
|> Kino.Image.new(:png)
Expand All @@ -360,9 +357,9 @@ classes = [:airplane, :automobile, :bird, :cat, :deer, :dog, :frog, :horse, :shi

input_tensor =
img
|> OpenCV.resize([32, 32])
|> Cv.resize([32, 32])
|> then(fn {:ok, val} -> val end)
|> OpenCV.Nx.to_nx()
|> Cv.Nx.to_nx()
|> Nx.backend_transfer(Torchx.Backend)
|> Nx.flatten()

Expand Down
47 changes: 22 additions & 25 deletions examples/dnn-detection-model.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,7 @@ end

## alias
```elixir
# alias Evision as OpenCV
# please see the Namespace section in the README.md for reasons/motivations
# (in the root directory of the source code)
alias Evision, as: OpenCV
alias Evision, as: Cv
```

## Detection Model Module
Expand All @@ -85,32 +82,32 @@ defmodule DetectionModel do
confidence = "#{Float.round(confidence, 2)}"
label = Enum.at(labels, class_id)
text = "#{label}: #{confidence}"
{:ok, mat} = OpenCV.rectangle(mat, [l, t], [r, b], [255, 0, 0])
{:ok, mat} = Cv.rectangle(mat, [l, t], [r, b], [255, 0, 0])

{:ok, {{label_weight, label_height}, baseline}} =
OpenCV.getTextSize(text, OpenCV.cv_FONT_HERSHEY_SIMPLEX(), 0.5, 1)
Cv.getTextSize(text, Cv.cv_FONT_HERSHEY_SIMPLEX(), 0.5, 1)

label_weight = trunc(label_weight)
label_height = trunc(label_height)
top = max(t, label_height)

{:ok, mat} =
OpenCV.rectangle(mat, [l, top - label_height], [l + label_weight, top + baseline], [
Cv.rectangle(mat, [l, top - label_height], [l + label_weight, top + baseline], [
255,
255,
255
])

{:ok, mat} =
OpenCV.putText(mat, text, [l, top], OpenCV.cv_FONT_HERSHEY_SIMPLEX(), 0.5, [0, 0, 255])
Cv.putText(mat, text, [l, top], Cv.cv_FONT_HERSHEY_SIMPLEX(), 0.5, [0, 0, 255])

_visualise_pred(mat, labels, outs)
end

def postprocess(mat, detections, net, confidence_threshold) do
{:ok, out_layers} = OpenCV.DNN.Net.getUnconnectedOutLayers(net)
{:ok, out_layer} = OpenCV.DNN.Net.getLayer(net, Enum.at(out_layers, 0))
out_layer_type = OpenCV.DNN.Layer.get_type(out_layer) |> IO.iodata_to_binary()
{:ok, out_layers} = Cv.DNN.Net.getUnconnectedOutLayers(net)
{:ok, out_layer} = Cv.DNN.Net.getLayer(net, Enum.at(out_layers, 0))
out_layer_type = Cv.DNN.Layer.get_type(out_layer) |> IO.iodata_to_binary()
_postprocess(mat, detections, net, confidence_threshold, out_layer_type, [])
end

Expand All @@ -125,8 +122,8 @@ defmodule DetectionModel do
<<"DetectionOutput">>,
acc
) do
{:ok, data} = OpenCV.Mat.to_binary(outs)
{:ok, {h, w, _}} = OpenCV.Mat.shape(mat)
{:ok, data} = Cv.Mat.to_binary(outs)
{:ok, {h, w, _}} = Cv.Mat.shape(mat)
{:ok, translated_outs} = _translate_outs(confidence_threshold, data, h, w, [])

_postprocess(mat, detections, net, confidence_threshold, "DetectionOutput", [
Expand Down Expand Up @@ -175,31 +172,31 @@ defmodule DetectionModel do
end

def predict(mat, model, out_names, opts \\ []) do
{:ok, blob} = OpenCV.DNN.blobFromImage(mat, opts)
{:ok, blob} = Cv.DNN.blobFromImage(mat, opts)

{:ok, model} =
OpenCV.DNN.Net.setInput(model, blob, name: "", scalefactor: 1.0, mean: [0, 0, 0])
Cv.DNN.Net.setInput(model, blob, name: "", scalefactor: 1.0, mean: [0, 0, 0])

start_time = :os.system_time(:millisecond)
{:ok, detections} = OpenCV.DNN.Net.forward(model, outBlobNames: out_names)
{:ok, detections} = Cv.DNN.Net.forward(model, outBlobNames: out_names)
end_time = :os.system_time(:millisecond)
IO.puts("Inference time=>#{end_time - start_time} ms")
{:ok, mat, detections}
end

def predict_file(image_file, model, out_names, opts \\ []) do
{:ok, mat} = OpenCV.imread(image_file)
{:ok, mat} = Cv.imread(image_file)
predict(mat, model, out_names, opts)
end

def get_model(params, config, framework \\ "") do
{:ok, net} =
OpenCV.DNN.readNet(params,
Cv.DNN.readNet(params,
config: config,
framework: framework
)

{:ok, out_names} = OpenCV.DNN.Net.getUnconnectedOutLayersNames(net)
{:ok, out_names} = Cv.DNN.Net.getUnconnectedOutLayersNames(net)
{:ok, net, out_names}
end
end
Expand Down Expand Up @@ -310,7 +307,7 @@ Helper.download!("https://raw.githubusercontent.com/cocoa-xu/evision/main/test/d
```

```elixir
OpenCV.imencode(".png", mat)
Cv.imencode(".png", mat)
|> then(fn {:ok, val} -> val end)
|> IO.iodata_to_binary()
|> Kino.Image.new(:png)
Expand All @@ -320,31 +317,31 @@ OpenCV.imencode(".png", mat)
```elixir
# if you have a camera available
# uncomment the line below
# {:ok, video} = OpenCV.VideoCapture.videoCapture(0)
# {:ok, video} = Cv.VideoCapture.videoCapture(0)

# or the OpenCV library you compiled can decode video files
# uncomment the line below
# {:ok, video} = OpenCV.VideoCapture.videoCapture("/path/to/your/video/file")
# {:ok, video} = Cv.VideoCapture.videoCapture("/path/to/your/video/file")
```

```elixir
defmodule VideoDetection do
def detect(video, widget, max_frames \\ 30*60)
when is_reference(video) and is_integer(max_frames) do
{net, out_names, labels} = SSDMobileNetV2.get_detection_model()
frame_read = OpenCV.VideoCapture.read(video)
frame_read = Cv.VideoCapture.read(video)
_detect(net, out_names, labels, frame_read, video, widget, max_frames)
end

defp _detect(_, _, _, _, _, _, 0), do: :ok
defp _detect(net, out_names, labels, {:ok, frame}, video, widget, left_frames) when left_frames > 0 or left_frames < 0 do
{:ok, mat} = SSDMobileNetV2.predict_and_show(net, out_names, labels, frame)
OpenCV.imencode(".png", mat)
Cv.imencode(".png", mat)
|> elem(1)
|> IO.iodata_to_binary()
|> Kino.Image.new(:png)
|> then(&Kino.Frame.render(widget, &1))
frame_read = OpenCV.VideoCapture.read(video)
frame_read = Cv.VideoCapture.read(video)
if left_frames > 0 do
_detect(net, out_names, labels, frame_read, video, widget, left_frames - 1)
else
Expand Down
23 changes: 10 additions & 13 deletions examples/dnn-googlenet.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Helper.download!(
"classification_classes_ILSVRC2012.txt"
)

Helper.download!("https://docs.opencv.org/4.5.4/space_shuttle.jpg", "space_shuttle.jpg")
Helper.download!("https://docs.Cv.org/4.5.4/space_shuttle.jpg", "space_shuttle.jpg")
```

```output
Expand All @@ -107,10 +107,7 @@ Helper.download!("https://docs.opencv.org/4.5.4/space_shuttle.jpg", "space_shutt

## alias
```elixir
# alias Evision as OpenCV
# please see the Namespace section in the README.md for reasons/motivations
# (in the root directory of the source code)
alias Evision, as: OpenCV
alias Evision, as: Cv
```

## Read class names
Expand Down Expand Up @@ -151,7 +148,7 @@ classes =

```elixir
{:ok, model} =
OpenCV.DNN.readNet("bvlc_googlenet.caffemodel",
Cv.DNN.readNet("bvlc_googlenet.caffemodel",
config: "bvlc_googlenet.prototxt",
framework: ""
)
Expand All @@ -170,7 +167,7 @@ classes =
# "3: OpenCV implementation, "
# "4: VKCOM, "
# "5: CUDA
{:ok, model} = OpenCV.DNN.Net.setPreferableBackend(model, 0)
{:ok, model} = Cv.DNN.Net.setPreferableBackend(model, 0)

# "0: CPU target (by default), "
# "1: OpenCL, "
Expand All @@ -179,7 +176,7 @@ classes =
# "4: Vulkan, "
# "6: CUDA, "
# "7: CUDA fp16 (half-float preprocess)
{:ok, model} = OpenCV.DNN.Net.setPreferableTarget(model, 0)
{:ok, model} = Cv.DNN.Net.setPreferableTarget(model, 0)
```

```output
Expand All @@ -189,17 +186,17 @@ classes =
## Read the test image and set as input

```elixir
{:ok, mat} = OpenCV.imread("space_shuttle.jpg")
{:ok, mat} = Cv.imread("space_shuttle.jpg")

{:ok, blob} =
OpenCV.DNN.blobFromImage(mat,
Cv.DNN.blobFromImage(mat,
scalefactor: 1,
swapRB: true,
mean: [-104, -117, -123],
size: [224, 224]
)

{:ok, model} = OpenCV.DNN.Net.setInput(model, blob, name: "", scalefactor: 1.0, mean: [0, 0, 0])
{:ok, model} = Cv.DNN.Net.setInput(model, blob, name: "", scalefactor: 1.0, mean: [0, 0, 0])
```

```output
Expand All @@ -210,7 +207,7 @@ classes =

```elixir
start_time = :os.system_time(:millisecond)
{:ok, pred} = OpenCV.DNN.Net.forward(model, outputName: "")
{:ok, pred} = Cv.DNN.Net.forward(model, outputName: "")
end_time = :os.system_time(:millisecond)
"Inference time=>#{end_time - start_time} ms"
```
Expand All @@ -220,7 +217,7 @@ end_time = :os.system_time(:millisecond)
```

```elixir
pred = pred |> OpenCV.Nx.to_nx()
pred = pred |> Cv.Nx.to_nx()
pred_class_id = pred |> Nx.argmax() |> Nx.to_flat_list() |> Enum.at(0)

confidence =
Expand Down
39 changes: 18 additions & 21 deletions examples/imread.livemd
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,10 @@ end

## alias
```elixir
# alias Evision as OpenCV
# please see the Namespace section in the README.md for reasons/motivations
# (in the root directory of the source code)
alias Evision, as: OpenCV
alias Evision, as: Cv
```

## OpenCV.imread
## Cv.imread

### Read an image from file

Expand All @@ -86,21 +83,21 @@ Helper.download!(
lenna_test_image_path
)

{:ok, mat} = OpenCV.imread(lenna_test_image_path)
{:ok, {_height = 512, _width = 512, _channels = 3}} = OpenCV.Mat.shape(mat)
{:ok, encoded} = OpenCV.imencode(".png", mat)
{:ok, mat} = Cv.imread(lenna_test_image_path)
{:ok, {_height = 512, _width = 512, _channels = 3}} = Cv.Mat.shape(mat)
{:ok, encoded} = Cv.imencode(".png", mat)

encoded
|> IO.iodata_to_binary()
|> Kino.Image.new(:png)
```

### Resize image `OpenCV.resize`
### Resize image `Cv.resize`

```elixir
{:ok, resized_mat} = OpenCV.resize(mat, [_width = 256, _height = 128])
{:ok, {128, 256, 3}} = OpenCV.Mat.shape(resized_mat)
{:ok, encoded} = OpenCV.imencode(".png", resized_mat)
{:ok, resized_mat} = Cv.resize(mat, [_width = 256, _height = 128])
{:ok, {128, 256, 3}} = Cv.Mat.shape(resized_mat)
{:ok, encoded} = Cv.imencode(".png", resized_mat)

encoded
|> IO.iodata_to_binary()
Expand All @@ -111,12 +108,12 @@ encoded

```elixir
# encode in memory
{:ok, encoded_in_memory} = OpenCV.imencode(".png", resized_mat)
{:ok, encoded_in_memory} = Cv.imencode(".png", resized_mat)
encoded_in_memory = encoded_in_memory |> IO.iodata_to_binary()

# decode in memory
{:ok, decoded_mat} = OpenCV.imdecode(encoded_in_memory, OpenCV.cv_IMREAD_ANYCOLOR())
{:ok, {128, 256, 3}} = OpenCV.Mat.shape(decoded_mat)
{:ok, decoded_mat} = Cv.imdecode(encoded_in_memory, Cv.cv_IMREAD_ANYCOLOR())
{:ok, {128, 256, 3}} = Cv.Mat.shape(decoded_mat)
```

### Read png file with/without alpha channel
Expand All @@ -130,27 +127,27 @@ Helper.download!("https://github.com/cocoa-xu/evision/raw/main/test/test.png", d
```elixir
{:ok, without_alpha} =
download_path
|> OpenCV.imread()
|> Cv.imread()

OpenCV.Mat.shape(without_alpha)
Cv.Mat.shape(without_alpha)
```

```elixir
{:ok, with_alpha} =
download_path
|> OpenCV.imread(flags: OpenCV.cv_IMREAD_UNCHANGED())
|> Cv.imread(flags: Cv.cv_IMREAD_UNCHANGED())

OpenCV.Mat.shape(with_alpha)
Cv.Mat.shape(with_alpha)
```

### Read an image as grayscale

```elixir
{:ok, gray_scale_mat} =
lenna_test_image_path
|> OpenCV.imread(flags: OpenCV.cv_IMREAD_GRAYSCALE())
|> Cv.imread(flags: Cv.cv_IMREAD_GRAYSCALE())

{:ok, gray_scale} = OpenCV.imencode(".png", gray_scale_mat)
{:ok, gray_scale} = Cv.imencode(".png", gray_scale_mat)

gray_scale
|> IO.iodata_to_binary()
Expand Down
Loading

0 comments on commit c65cc7a

Please sign in to comment.