Wednesday 18 September 2019

Building a Real-Time Object Recognition App with Tensorflow and OpenCV on Windows

The blog on https://towardsdatascience.com/building-a-real-time-object-recognition-app-with-tensorflow-and-opencv-b7a2b4ebdc32 very welll explains how to execute this code on Linux and Mac,

However there are some challenges with Windows machine, I have listed down what all I have faced,

First, I pulled the TensorFlow models repo https://github.com/tensorflow/models and
then had a looked at the notebook that they released as well. It basically walked through the all steps of using a pre-trained model.


Follow the installation steps as mentioned on
https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/installation.md

Make sure you had following package
pip install tensorflow-object-detection-api

Now make sure we have latest version of Visual Studio installed, we can make use of Community verion which is free,
Install Microsoft Visual Studio 2019 and cmake gets installed at following location:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\cmake

This makes vcpkg available for you for compiling files,
Vcpkg helps you manage C and C++ libraries on Windows, Linux and MacOS. This tool and ecosystem are constantly evolving; your involvement is vital to its success!

To get started:

> git clone https://github.com/Microsoft/vcpkg.git
> cd vcpkg

PS> .\bootstrap-vcpkg.bat
Linux:~/$ ./bootstrap-vcpkg.sh
Then, to hook up user-wide integration, run (note: requires admin on first use)

PS> .\vcpkg integrate install
Linux:~/$ ./vcpkg integrate install
The best way to use installed libraries with cmake is via the toolchain file
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake"

PS> .\vcpkg install sdl2 curl
Linux:~/$ ./vcpkg install sdl2 curl

Protobuf and its dependencies can be installed directly by using vcpkg:

PS>.\vcpkg search protobuf
PS>.\vcpkg install protobuf protobuf:x64-windows

If it gives you error that its unable to find cmake file then add it to
.\vcpkg -DCMAKE_TOOLCHAIN_FILE="C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin" install sdl2 curl




If it doesn't work, then download protoc-3.8.0-win64.zip(or any other latest version) from github-release
extract in a directory and copy address of that
open start
type environment variable (in search)
open Edit environment variable
click on Environment variables...
find "Path" in list
click Edit
click New and paste address of extracted folder in the bottem of list
OK all and restart PC

go to C:\protoc-3.9.1-win64\bin
protoc object_detection/protos/*.proto --python_out=.

If that doesn't work, try replacing below command in ./libs/datasets/pycocotools/setup.py
You can replace extra_compile_args=['-Wno-cpp', '-Wno-unused-function', '-std=c99'] with extra_compile_args={'gcc': ['/Qstd=c99']}

If this doesn't work then, I used following powershell command on windows:
Get-ChildItem object_detection/protos/*.proto | Resolve-Path -Relative | %{ .\protoc $_ --python_out=. }
or
Get-ChildItem object_detection/protos/*.proto | Resolve-Path -Relative | %{ protoc $_ --python_out=. }



Now if you try to run scripts on object_deduction and you get following error, or somthing simalr to it than,
ModuleNotFoundError: No module named 'pycocotools._mask'

git clone https://github.com/philferriere/cocoapi.git
cd cocoapi/PythonAPI
python setup.py install


If you look inside the model/research/ folder, you will see the pycocotools folder. Try to rename it to any other name like pycocotoolstest
The reason of this problem is that the code imported the pycocotools from that folder, not from the installed folder that you just installed the COCO API.




No comments:

Post a Comment

Videos based solutions

City / Traffic Surveillance Target Sectors Government Sectors Traffic Surveillance Highway and State Road Surveillance Defense Ai...