N3mes1s / findfaces

C++OpenCV demo app with a rest endpoint for use in containers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Introduction

This is a sample that shows using the restinio http library and OpenCV to create a service that recieves a PUT request with an image file containing faces and requested name to save it as, and a GET request to retrieve an the original or a new file with the faces circled. We use a multi-stage container build to build the source and prepare a runtime container that is much smaller. We also have another dockerfile showing how to enable usage with VS.

The base Dockerfile provided uses Alpine Linux. Vcpkg use with the x64-linux-musl triplet here is experimental. We have provided a Dockerfile.debian to show usage with a non experimental vcpkg triplet.

Using the multistage container

It can be as simple as

docker build -t findfaces/run .

This does result in a large intermdiate image that is unnamed. You may wish to reuse that image for additional development purposes, e.g. for preparing a container for use with VS. If so simply target the build stage and name it.

docker build --target build -t findfaces/build .

Then that image will be cached and used when you prepare the run image.

docker build -t findfaces/run .

To run the service:

docker run -d --rm -p 8080:8080 --name findfaces findfaces/run

This will start the container detached and will delete it when it is stopped.

Attach to the container while running to watch process output.

docker exec -it findfaces

Or attach with an interactive session.

docker exec -it findfaces /bin/bash

Stop the container

docker stop findfaces

Service usage

Invoke the service with:

curl -X PUT -T picture.jpg localhost:8080/files?submit=picture.jpg

Get the original image:

curl -X GET localhost:8080/files/picture.jpg > picture.jpg

Get the modified image with faces circled (if none found it is the original image again):

curl -X GET localhost:8080/files/facespicture.jpg > facespicture.jpg

The GET operations can also be performed by providing the url in a browser.

Using the VS container

These instructions are based on some base VS enabled Linux container definitions David Ducatel has provided in this GitHub repo.

There is an additional Dockerfile.vs for creating an image suitable for usage with a container that VS can acceess. Open that file and examine the FROM statement, we’re basing this definition on the local image we created earlier in our multi-stage build. That container already has all our basic development prerequisites, but for Visual Studio usage we need a few more things enumerated above. Notably we need SSH for communication with VS which is configured in the RUN command. As we are enabling root login here this container definition is not appropriate for anything other than local development. Note that this is very fragile, the sshd_config file may have different options in different distros and releases. If you can't login start the container in interactive mode and examine this file to see if your settings took. The entry point for this container is SSH specified in the CMD line.

Building this container is simple.

docker build -t findfaces/vs . -f Dockerfile.vs

We need to specify a bit more to run a container based on this image so VS can access it.

docker run -d -p 12345:22 -p 8080:8080 --security-opt seccomp:unconfined --name findfacesvs findfaces/vs

The notable option here we haven’t covered before is --security-opt. We’re running the container in unconfined mode as to debug we need to run privileged operations normally blocked. Note also that two ports are exposed, one for SSH and one for the service itself. This allows you to invoke the service and debug it from within VS.

Once your container is running in Visual Studio you can add it to your available connections by going to Tools > Options > Connection Manager. Click add and enter localhost, port 12345, user root, password toor. On connection Visual Studio will pull the headers out of the container for use with IntelliSense when you select this connection.

Note that you need to close VS if you need to rebuild the container (issue with the dockerignore file to sort out). You also need to regnerate the CMake cache and create a new build after recreating the container, VS may think the cache is already there for the connection.

To interact with the container while it is running, e.g. use the shell and poke around

docker exec -it findfacesvs /bin/bash

About

C++OpenCV demo app with a rest endpoint for use in containers

License:MIT License


Languages

Language:C++ 42.3%Language:JavaScript 22.9%Language:Dockerfile 15.4%Language:HTML 9.7%Language:CMake 8.3%Language:Shell 1.4%