Giter Site home page Giter Site logo

webcam's Introduction

Use webcam in linux guest / windows host in VScode

1. we suppose you have installed Virtual Box on Windows 10 and add ubuntu 18.04 machine :

2. You need to Install VirtualBox Extension Pack on Windows : Virtual Box Download
it's necessary to add extension pack for :

  • Support for USB 2.0 and USB 3.0 devices. USB devices such as network adapters, flash drives, hard disks, web cams etc., that are inserted into physical USB ports of a host machine can be attached to a virtual machine running on VirtualBox. As a result, you can use a physical USB device in a guest operating system (OS).
  • Host webcam pass-through. With this feature you can connect your host webcam to a guest machine. The advantage is that you can use a webcam which is not supported by the guest operating system of your VM.

install extension pack

3. On windows 10 open cmd windows and list the webcams : the command is VBoxManage list webcams

	c:\Program Files\Oracle\VirtualBox>VBoxManage list webcams
	Video Input Devices: 1
	.1 "Integrated Webcam"
	\\?\usb#vid_0bda&pid_5650&mi_00#6&1d5726d5&0&0000#{65e8773d-8f56-11d0-a3b9-00a0c9223196}\global

4. if you have one webcam on your host, add this webcam on your linux machine: if you have only one webcam you will attach .1 (your webcam) ubuntu name of your virtual machine

	VboxManage controlvm "ubuntu-18_04" webcam attach .1

5. launch your linux machine and test the webcam with cheese:

6. install code (VScode): VScode install

7. install plugin: Easy Cpp Projects: Easy C++ projects

8. Create directory, close and relaunch VScode in this directory :

	salvat@salvat-VirtualBox:~/Documents$ mkdir VScodeTest
	salvat@salvat-VirtualBox:~/Documents$ cd VScodeTest/
	salvat@salvat-VirtualBox:~/Documents/VScodeTest$ code .

9. Create new project with easy cpp : new C++ projects You can also use New Folder in VSCode.

10. Where are default include files in gcc compiler ?

	salvat@salvat-VirtualBox:~/Documents/VScodeTest$  gcc -xc -E -v -
	Using built-in specs.
	COLLECT_GCC=gcc
	OFFLOAD_TARGET_NAMES=nvptx-none
	OFFLOAD_TARGET_DEFAULT=1
	Target: x86_64-linux-gnu
	Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.4.0-1ubuntu1~18.04.1' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
	Thread model: posix
	gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1) 
	COLLECT_GCC_OPTIONS='-E' '-v' '-mtune=generic' '-march=x86-64'
	 /usr/lib/gcc/x86_64-linux-gnu/7/cc1 -E -quiet -v -imultiarch x86_64-linux-gnu - -mtune=generic -march=x86-64 -fstack-protector-strong -Wformat -Wformat-security
	ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
	ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/7/../../../../x86_64-linux-gnu/include"
	#include "..." search starts here:
	#include <...> search starts here:
	 /usr/lib/gcc/x86_64-linux-gnu/7/include
 	/usr/local/include
 	/usr/lib/gcc/x86_64-linux-gnu/7/include-fixed
 	/usr/include/x86_64-linux-gnu
 	/usr/include
	End of search list.

11. Configure intellisense for code completion : -> Copy directories includes in gcc (see precedent command in etape 10) this files are : /usr/lib/gcc/x86_64-linux-gnu/7/include /usr/local/include /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed /usr/include/x86_64-linux-gnu /usr/include

intellisense configuration

Code of file c_cpp_properties.json generated by easy Cpp

    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                " /usr/lib/gcc/x86_64-linux-gnu/7/include",
                " /usr/local/include",
                " /usr/lib/gcc/x86_64-linux-gnu/7/include-fixed",
                " /usr/include/x86_64-linux-gnu",
                " /usr/include",
                "/usr/include/c++/7",
                "/usr/include/x86_64-linux-gnu/c++/7"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cppStandard": "c++17"
        }
    ],
    "version": 4
  }

You can test completion for printf for example test intellisense completion

You are ready to test program in vscode directory (in this github) (file main.c)

12. install library v4l2 for dev : and verify what files have been installed (you should see /usr/include/libv4l2.h) file that we will use in code

	sudo apt-get install libv4l-dev
	dpkg -L libv4l-dev

13. Copy content of vscode folder to your folder in Vscode. You need at minimum to copy main.c in src folder and the Makefile. compiler is gcc and math et v4l2 lib has to be ad to your linker
Makefile

	CXX		  := gcc
	CXX_FLAGS := -Wall -Wextra -ggdb

	BIN		:= bin
	SRC		:= src
	INCLUDE	:= include
	LIB		:= lib

	LIBRARIES	:= -lm -lv4l2
	EXECUTABLE	:= main


	all: $(BIN)/$(EXECUTABLE)

	run: clean all
		clear
		./$(BIN)/$(EXECUTABLE)

	$(BIN)/$(EXECUTABLE): $(SRC)/*.c
		$(CXX) $(CXX_FLAGS) -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES)

	clean:
		-rm $(BIN)/*

A. test project 1 : vscode folder

1. Open folder vscode and test the code in file vscode/src/main.c we will open /dev/video0 file (webcam) and use functions in main.c

//we give en example of main : take picture of webcam input and write to file ppm.
int main(int argc, char **argv)
{
    int fd = -1;
    struct buffer image;
    const char *dev_name = "/dev/video0";
    fd = v4l2_open(dev_name, O_RDWR | O_NONBLOCK, 0);
    if (fd < 0)
    {
        perror("Cannot open device");
        exit(EXIT_FAILURE);
    }
    printCapabilities(fd, dev_name);
    image = initCapture(fd, 640, 480);
    captureImage(fd, image);
    debug("SAVING TO in.ppm");
    saveppm("in.ppm", image);
    traitement(&image);
    debug("SAVING TO out.ppm");
    saveppm("out.ppm", image);
    v4l2_munmap(image.start, image.length);
    v4l2_close(fd);
    return 0;
}

B. use webcam in linux guest / windows host with opencv

B.1. install Opencv: Opencv install

B.2. Edit /etc/ld.so.conf.d/opencv.conf and add /usr/local/lib to it:

sudo gedit /etc/ld.so.conf.d/opencv.conf
sudo ldconfig

B.3. Open folder vscode_opencv and test the code in file vscode_opencv/src/main.cpp you've got classifier test. But you can copy file saveImageGray.cpp from tmp/ to src and erase main.cpp. This code is quite identical of code from A.1 but with opencv framework. We can go to Opencv test for code explained.

#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(-1);
//check if the file was opened properly
if(!cap.isOpened())
{
cout << "Webcam could not be opened succesfully" << endl;
exit(-1);
}
else
{
cout << "p n" << endl;
}
int w = 640;
int h = 480;
cap.set(CAP_PROP_FRAME_WIDTH, w);
cap.set(CAP_PROP_FRAME_HEIGHT, h);
Mat frame;
cap >>frame;
// converts the image to grayscale
Mat frame_in_gray;
cvtColor(frame, frame_in_gray, COLOR_RGB2GRAY);
// process the Canny algorithm
cout << "processing image with Canny..." << endl;
int threshold1 = 0;
int threshold2 = 80;
//Canny(frame_in_gray, frame_in_gray, threshold2, threshold2);
// saving the images in the files system
cout << "Saving the images..." << endl;
imwrite("captured.jpg", frame);
imwrite("captured_with_edges.jpg", frame_in_gray);
imwrite("opencv.jpg", frame);
cap.release();
return 0;
}

Somme remarks :

  1. you will remark that file in .config\code\User\settings.json add includePath of opencv for intellisense. Copy configuration lines in this files open via Menu (File, Preferences, Settings, Extensions, Edit in settings.json) (Cf image). You must close and open VScode to active the changes : in alls projetcs intellisence will use /usr/local/incluse/opencv4 directory.
	{
	    "C_Cpp.default.systemIncludePath": [
	     "/usr/local/include/opencv4/"
	  ],
	  "files.autoSave": "afterDelay",
	}

opencv change  2. you will see how the Makefile has been changed for opencv compile

CXX		  := g++
CXX_FLAGS := -Wall -Wextra  -ggdb 

BIN		:= bin
SRC		:= src
INCLUDE	:= include
LIB		:= lib


LIBRARIES	:=-lv4l2 -lm
EXECUTABLE	:= main

OPENCV_LIBS = $(shell pkg-config --libs opencv4)
OPENCV_INCLUDE = $(shell pkg-config --cflags opencv4)

all: $(BIN)/$(EXECUTABLE)

run: clean all
	clear
	./$(BIN)/$(EXECUTABLE)

$(BIN)/$(EXECUTABLE): $(SRC)/*.cpp
	$(CXX) $(CXX_FLAGS) -DDEBUG -I$(INCLUDE) -L$(LIB) $^ -o $@ $(LIBRARIES) $(OPENCV_LIBS) $(OPENCV_INCLUDE)

clean:
	-rm $(BIN)/*

Use webcam in windows with visual studio

1. install visual studio comunity 2017 or 2019: Visual Studio Comunity 2019
2. install opencv in windows 10: opencv for windows
3. create console application in Visual Studio 4. Configure your project to build opencv application with right clic on console application project you need to choose all configuration and x64 project before configuring Include and libs!

Configure opencv project

webcam's People

Contributors

jlsalvat avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.