Visual Studio Code Opencv Python



-->

The sample code is available to download, but as an exercise, we will create this app from scratch. Even if you download the sample, code, you'll need to follow the steps in Compile the OpenCV Libraries and Add the Libraries to your Project below. Make sure your device is running and set up and you have Visual Studio. Installation of OpenCV in Visual Studio Environment.Originally was created for Image Processing class, Robotics Department, Nazarbayev University, Kazakhstan.

In this sample, we will build the OpenCV library for Windows and add it to a UWP C++ app, which will run facial and body recognition on a photo.

Create a new UWP C++ project

The sample code is available to download, but as an exercise, we will create this app from scratch.

Cv2

Install Python OpenCV library for Visual StudioInstall NumPy, SciPy, Matplotlib for PythonPython download: Numpy,Scipy.

Even if you download the sample, code, you'll need to follow the steps in Compile the OpenCV Libraries and Add the Libraries to your Project below.

  • Make sure your device is running and set up and you have Visual Studio installed.

  • You will need your device's IP address when connecting to it remotely.

  • Make sure you have the C++ components for Visual Studio. To check, open the Start Menu, type in Visual Studio Installer and hit enter. Once the Visual Studio Installer opens, click Modify under your installed version of Visual Studio. Check to make sure you have the below components selected. If not, select them and update your installation.

  1. Start Visual Studio 2017.

  2. Create a new project with (File | New Project..).

    In the New Project dialog, navigate to Universal as shown below (in the left pane in the dialog: Templates | Visual C++ | Windows | Universal).

  3. Select the template Blank App (Windows Universal).

    Note that we call the app OpenCVExample. You can name it something different, but you will have to adjust sample code that references OpenCVExample as well.

  • Python in Visual Studio Code Working with Python in Visual Studio Code, using the Microsoft Python extension, is simple, fun, and productive. The extension makes VS Code an excellent Python editor, and works on any operating system with a variety of Python interpreters.
  • .We will learn to setup OpenCV-Python in your Windows system. Below steps are tested in a Windows 7-64 bit machine with Visual Studio 2010 and Visual Studio 2012. The screenshots shows VS2012. Installing OpenCV from prebuilt binaries 1.Below Python packages are to be downloaded and installed to their default locations.

Note: If this is the first project you create, Visual Studio will likely prompt you to enable developer mode for Windows 10.

Compile the OpenCV Libraries

  1. Download the Microsoft fork of OpenCV from GitHub

    Download Microsoft's fork of the OpenCV library from GitHub. Unzip it to a place you can remember it, and copy the folder's path (i.e. C:/path/to/opencv/) for the next step.

  2. Create the OpenCV environment variable

    Open the Start Menu and enter Edit the system environment variables and hit Enter. On the next screen, press Environment Variables, then New. Create a new variable called OCV2015_ROOT with a value of the path you copied, i.e. C:/path/to/opencv/

  3. Build the solution

    This step depends on your device's architecture. Follow one of the options below:

    • x86 (i.e. Your machine or Minnowboard Max)

      Open the file explorer and navigate to the following path: '%OCV2015_ROOT%vs2015WS10.0x86', then open OpenCV.sln in Visual Studio.

      On the top of the screen, next to the green Run button, select Release instead of Debug and Win32 instead of ARM or x64.

    • ARM (i.e. Raspberry Pi)

      Open the file explorer and navigate to the following path: '%OCV2015_ROOT%vs2015WS10.0ARM', then open OpenCV.sln Download adobe lightroom cc for mac. in Visual Studio.

      On the top of the screen, next to the green Run button, select Release instead of Debug and ARM instead of x86 or x64.

    • x64

      Open the file explorer and navigate to the following path: '%OCV2015_ROOT%vs2015WS10.0x64', then open OpenCV.sln in Visual Studio.

      On the top of the screen, next to the green Run button, select Release instead of Debug and x64 instead of x86 or ARM.

    Press Ctrl+Shift+B to Build the solution. Alternatively, press Build | Build Solution. These results should compile without errors. You will have to re-build this project for each platform (x86, x64, or ARM) and release type (Debug or Release) that you want to use.

Add the Libraries to your Project

  1. Add the DLLs directly

    Return to your project's Visual Studio window. Right click on the project in the window and navigate to Add | Existing Items

    • x86: In the new window, navigate to %OCV2015_ROOT%vs2015WS10.0x86binRelease

    • ARM: In the new window, navigate to %OCV2015_ROOT%vs2015WS10.0ARMbinRelease

    • x64: In the new window, navigate to %OCV2015_ROOT%vs2015WS10.0x64binRelease

    Add the following files: opencv_core300.dll, open_imgcodecs300.dll, opencv_imgproc300.dll, opencv_ml300.dll, opencv_objdetect300.dll

    After adding the files, click on each one in the Solution Explorer and make Content = 'True'

  2. Modify you General Properties

    Right click on the OpenCVExample (Universal Windows) line of the Solution Explorer and click on Properties.

    In the dialog that pops up, navigate to Configuration Properties | C / C++ | General. From there, add the following to the Additional Include Directories field before the already-present contents:

    %OCV2015_ROOT%modulescoreinclude;%OCV2015_ROOT%modulesimgprocinclude;%OCV2015_ROOT%modulesimgcodecsinclude;%OCV2015_ROOT%modulesobjdetectinclude;%OCV2015_ROOT%moduleshalinclude;%OCV2015_ROOT%moduleshighguiinclude;%OCV2015_ROOT%modulesmlinclude;

    For each new DLL you want to add, you need to add the appropriate 'include' directory to this field.

  3. Modify your Linker Properties

    In the Properties window still, navigate to Linker | General, then add the following to the Additional Library Directories field:

    • x86: %OCV2015_ROOT%vs2015WS10.0x86librelease;%(AdditionalLibraryDirectories)

    • ARM: %OCV2015_ROOT%vs2015WS10.0ARMlibrelease;%(AdditionalLibraryDirectories)

    • x64: %OCV2015_ROOT%vs2015WS10.0x64librelease;%(AdditionalLibraryDirectories)

    Switch the menu to Input (still within Linker) and add the following to the Additional Dependencies field:

    opencv_core300.lib;opencv_imgproc300.lib;opencv_imgcodecs300.lib;opencv_objdetect300.lib;opencv_ml300.lib;

    For each new DLL you want to add, add the appropriate LIB in this field as well.

Set up the User Interface

Open MainPage.xaml and replace the existing code with the following code to create the window UI:

To view the entire UI, change the dropdown in the top left corner from '5' Phone' to '12' Tablet'.

Vs code python no intellisense

Modify the actual C++ Files

Modify the Header File

Open MainPage.xaml.h. Replace the contents with the following code:

Vs code not recognizing python

Vscode Cv2

The header file stablishes the functions we're going to declar in the main .cpp file, as well as a private variable (_stored_image) which stores the content of the storedImage XAML Image element once we upload it.

Add the Includes and Namespaces to the .cpp file

Download thai font for mac. Add the following header files to the top of your code, right after the #include 'MainPage.xaml.h' line:

These lines allow us to use OpenCV library functions, along with some necessary default classes as well. We also define the locations of the features classifiers we'll use later.

Add the UpdateImage function

Add the following function right after the MainPage Constructor

This function changes the image contained in the 'storedImage' XAML Image element to the contents of the 'image' argument.

Add the Upload Button (loadImageButton) handler

Add the following function right after the UpdateImage function

This function opens an image and uploads a default image to the StoredImage UI element.

Add the Canny Edge Button (cannyEdgesButton) handler

Add the following function right after the last handler:

This function applies Canny Edge detection to the image and updates the image container with the results.

Add the face and body classifiers

Add the following lines and function right after the last handler:

This function uses cascade classification to classify and detect bodies and faces in a video stream (or image) using two Haar classifiers, face_cascade and body_cascade, stored in the xml files we provided for you. It's a method of classification involving machine learning, as explained on OpenCV's website.

Visual Studio Code Opencv Python

Add the 'Detect Faces and Bodies' button (detectFeaturesButton) event handler

Opencv Vs Code

Add the following lines and function right after the last helper function:

This function loads the classifiers, re-reads the image (the classification doesn't work on a Canny image in case the user clicked that button first), finds the faces and bodies using the helper function from the last step, and draws rectangles around the results: red for the faces, black for the bodies. It then pushes the updated image to the container.

Add in the Resources

Download the picture, face classifier, and body classifier and add them to your Assets folder within your project.

Optional: Build and test your app locally

  1. If you've built the x86 version of OpenCV, you can test the program on your local machine. Make sure the app builds correctly by invoking the Build | Build Solution menu command.

  2. Press F5 to run the program on your machine.

    Press the 'Test Image' button to see the test image.

    Press the 'Canny' button to see the Canny edges on the test image.

    Press the 'Detect' button to see the detected faces and bodies in the image indicated by rectangles.

    Close the app once you're done validating its correct operation.

Deploy the app to your Windows 10 IoT Core Device

  1. Click on the dropdown next to the Local Machine label and click on Remote Machine. Make sure the dropdown just to the left says your device's architecture, either x86, x64, or ARM. The Remote Machine click should open a dialog. Enter your device's IP address (or unique name) into the field labeled Address, then press Select.

  2. If the dialog does not appear, right click on your project and go to the Properties menu. From there, click on Debugging. Clicking on Machine Name and then Locate should provide the same dialog to enter the device's IP address.

    Make sure the device is powered on, then click the 'Run' button. The app should run on your device.

  3. Congratulations! Your app should now be working!

Notes:

  • Make sure if you have Latest or Required Windows 10 SDK mentioned in the Project. Right click on Project -> Properties -> General ( under Configuration Properties ) -> Target Platform Version
  • Make sure to following the Instructions provided in above link
  • Compile OpenCV.sln as per link Instructions
  • Then Try to Compile this current solution as there are some dependency libraries thats get used in here.

Troubleshoot:

  • error LNK1104: cannot open file 'opencv_core300d.lib'Can be 2 reasons
    • a. You might not have followed OpenCV documentation and did not configure Environment Variable
    • b. You might have not compiled OpenCV.sln for respective platform for which you are trying to compile OpenCVExample.sln