DEV Community

Cover image for GoCV + openCV: an approach to capture video
Oleg Sydorov
Oleg Sydorov

Posted on

GoCV + openCV: an approach to capture video

When considering the issue of capturing images from external devices, in general, such devices can be divided into 3 categories: web cameras, IP (network) cameras, video capture cards. The subject of this guide is web cameras.

As a result of analyzing the methods of interaction with cameras, we will highlight the open source project openCV. Using these libraries will allow us to work both natively on C\C++ and write wrapper programs using higher-level languages, Golang for example.

HELLO VIDEO

The code of the wrapper program can be surprisingly simple. Here is a didactic example (using gocv wrapper):

package main

import (
    "gocv.io/x/gocv"
)

func main() {
    webcam, _ := gocv.VideoCaptureDevice(0)
    window := gocv.NewWindow("Hello")
    img := gocv.NewMat()

    for {
        webcam.Read(&img)
        window.IMShow(img)
        window.WaitKey(1)
    }
}
Enter fullscreen mode Exit fullscreen mode

The price of ease of writing code is the assembly and linking of openCV library. The matter is complicated by the fact that the ATM runs on MS Windows. Let's go through this path in detail from start to finish.

The price of ease of writing code is the assembly and linking of openCV library. The matter is complicated by the fact that some equipment sometimes runs on MS Windows =) Let's go through this path in detail from start to finish.

  1. Download and install MSYS2:

https://www.msys2.org/

  1. Launch MSYS2 MSYS (via Start) pacman -Syu

Restart MSYS2 and run:

pacman -Su

pacman -S mingw-w64-x86_64-gcc

$env:PATH += ";C:\msys64\mingw64\bin"

gcc --version
Enter fullscreen mode Exit fullscreen mode

Here's a step-by-step guide on how to build OpenCV with "contrib" modules (including Aruco) under Windows:

  1. Download opencv sources

Let's assume that the directory C:\sources is created

cd C:\sources

git clone https://github.com/opencv/opencv.git

// create the opencv directory

git clone https://github.com/opencv/opencv_contrib.git

// create the opencv_contrib directory
Enter fullscreen mode Exit fullscreen mode
  1. Install CMake Download the installer from the official website:

https://cmake.org/download/

During installation, check “Add CMake to system PATH” — so you can call cmake from the command line.

NB! It is possible to simply install the “Desktop development with C++” workload from the MS Visual Studio 2022 package, which will provide CMake

  1. In MSYS2 (since you are already using C:\msys64\mingw64\bin) the mingw32-make utility is included in the mingw-w64-x86_64-make package. To install it:

Run the MSYS2 MinGW-64-bit (not the usual "MSYS2 MSYS"!) shell.

Update the package database and the system itself:
pacman -Syu

(if asked, restart the shell and repeat pacman -Syu again)

Install make:

pacman -S mingw-w64-x86_64-make

Check that it is now available in PATH:

where mingw32-make

  1. Run make Let's say we installed сmake as a component of Visual Studio. To run CMake, run x64 Native Tools Command Promt for VS 2022

Download and install Python >= 3.6 https://www.python.org/downloads/windows/

Install numPy

py -m pip install --upgrade pip
py -m pip install numpy
Enter fullscreen mode Exit fullscreen mode

Or optionally use make flag -D WITH_PYTHON=OFF

set "PATH=C:\msys64\mingw64\bin;%PATH%" //set mingw instead of cl.exe from MS VS

NB! MS VS compatible building is deprecated and needs other flags and components. MS VS and minGW libraries are mutually exclusive as well!

cmake -S C:/sources/opencv -B C:/build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Relecmake -S C:/sources/opencv -B C:/build -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -DOPENCV_EXTRA_MODULES_PATH=C:/sources/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX=C:/build/install -D BUILD_opencv_world=ON -DBUILD_SHARED_LIBS=ON -DBUILD_EXAMPLES=OFF 
Enter fullscreen mode Exit fullscreen mode

flag -D BUILD_opencv_world=ON enables assembly of libraries into 1 file. - optional, recommended!

  1. Make && make install Assume we build in the C:\buld directory
c:\build> mingw32-make -j8
c:\build> mingw32-make install
Enter fullscreen mode Exit fullscreen mode
  1. Build Go GoCV wrapper executable executable

Power Shell:

$env:CGO_ENABLED = "1"  
$env:CGO_CFLAGS = "-IC:/build/install/include -IC:/build/install/include/opencv2"
$env:CGO_CXXFLAGS = "-IC:/build/install/include -IC:/build/install/include/opencv2"
$env:CGO_LDFLAGS = "-LC:/build/install/x64/mingw/lib -lopencv_world4110"
Enter fullscreen mode Exit fullscreen mode

go build -x -tags customenv -ldflags="-H=windows" -o capture.exe main.go

The newest GoCV vewsion needs openCV 4.1.1, but you can build with a newer 4.1.2 version, just renaming library libopencv_world4120 => libopencv_world4110 (4.1.2 compiles better).

4.1.2 library builds better because some inaccuracies in the header declarations were fixed, that were causing some problems when building under Windows.

Enjoy!

Top comments (0)