Code Monkey home page Code Monkey logo

opencv-gpu-py's Introduction

Python OpenCV GPU Face Detection

Python wrapper for GPU CascadeClassifier, should work with OpenCV 2 and 3. Will fall back to CPU CascadeClassifier if CUDA isn't installed, but if the CPU version enough, just use stock OpenCV Python.

Installation

First install CUDA.

On Debian/Ubuntu, after downloading the .deb provided by NVIDIA:

sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb # Filename will vary
sudo apt-get update && sudo apt-get install cuda
echo "export CUDA_HOME=/usr/local/cuda-7.5" >> ~/.bashrc
echo "export PATH=$PATH:$CUDA_HOME/bin" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$CUDA_HOME/lib64" >> ~/.bashrc

Build/install OpenCV 3 from source:

sudo apt-get update && sudo apt-get install -y \
    build-essential cmake git libgtk2.0-dev pkg-config \
    libavcodec-dev libavformat-dev libswscale-dev python-dev \
    python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev
git clone https://github.com/Itseez/opencv
mkdir opencv/build && cd opencv/build
# Build OpenCV 2.4 instead of 3:
# mkdir opencv/build && cmake opencv && git checkout 2.4 && cd build
cmake .. && make -j$(nproc) && sudo make install

Install opencv-gpu-py:

git clone https://github.com/alexanderkoumis/opencv-gpu-py
pip install --user -e opencv-gpu-py

Test:

cd opencv-gpu-py
python test/test.py

Usage

# Full example in test/test.py
import cv2gpu

if cv2gpu.is_cuda_compatible():
    cv2gpu.init_gpu_detector('cascade_frontalface_gpu.xml')
else:
    cv2gpu.init_cpu_detector('cascade_frontalface_cpu.xml')

for (x, y, w, h) in cv2gpu.find_faces('image.jpg'):
    # Do something with face rectangle

opencv-gpu-py's People

Contributors

alexanderkoumis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

opencv-gpu-py's Issues

tracked rectangle size (h,w) only 96 or 240 ?!

Hello Alexander,
Testing face reco with CUDA works very well, BUT produce strange values on w and h values (box size):

testing different sizes (320x240) (160x120) and same results :

(x, y, w, h) feedback :
good (x, y) values, realistic ones !
(w, h) values : (240, 240) or (96, 96) only !!

These are not realistic values and they should vary !
Here, the box is either 240x240, or 96x96

I don'f find where I could change values in your program, to reduce limit !

An idea ?

Thanks Alexander

ImportError

import cv2gpu
Traceback (most recent call last):
File "", line 1, in
ImportError: /home/tim/opencv-gpu-py/cv2gpu.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN2cv3gpu21CascadeClassifier_GPUD1Ev

installing error for arm64

Hi Alexander,
Thanks for providing this binding for Face Detection with CascadeClassifier for Python + GPU

I'm having issues at installation step. I'm trying to install it in Nvidia Jetson Nano, in a virtualenv, with this specs:

  • aarch64
  • Ubuntu 18.04.3
  • Python 2.7.15
  • OpenCV 3.4.0 compiled for GPU
    • cv2.getBuildInformation() returns: NVIDIA CUDA: YES (ver 10.0) NVIDIA GPU arch: 53
  • Virtualenv 15.1.0

Due to being installing in virtualenv '--user' argument of pip is not allowed, I've run this commands:

git clone https://github.com/alexanderkoumis/opencv-gpu-py
pip install -e opencv-gpu-py

And I've received this output:

Installing collected packages: cv2gpu
  Running setup.py develop for cv2gpu
    ERROR: Command errored out with exit status 1:
     command: /home/nvidia/environments/bioengine/bin/python2.7 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/nvidia/opencv-gpu-py/setup.py'"'"'; __file__='"'"'/home/nvidia/opencv-gpu-py/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' develop --no-deps
         cwd: /home/nvidia/opencv-gpu-py/
    Complete output (32 lines):
    running develop
    running egg_info
    writing cv2gpu.egg-info/PKG-INFO
    writing top-level names to cv2gpu.egg-info/top_level.txt
    writing dependency_links to cv2gpu.egg-info/dependency_links.txt
    reading manifest file 'cv2gpu.egg-info/SOURCES.txt'
    writing manifest file 'cv2gpu.egg-info/SOURCES.txt'
    running build_ext
    building 'cv2gpu' extension
    creating build
    creating build/temp.linux-aarch64-2.7
    creating build/temp.linux-aarch64-2.7/src
    aarch64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ByWuWM/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include -I/usr/local/include/opencv -I/usr/include/python2.7 -c src/face_detector.cpp -o build/temp.linux-aarch64-2.7/src/face_detector.o -std=c++11
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    aarch64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ByWuWM/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/usr/local/include -I/usr/local/include/opencv -I/usr/include/python2.7 -c src/cv2gpu.cpp -o build/temp.linux-aarch64-2.7/src/cv2gpu.o -std=c++11
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    creating build/lib.linux-aarch64-2.7
    aarch64-linux-gnu-g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ByWuWM/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security -Wl,-Bsymbolic-functions -Wl,-z,relro -Wdate-time -D_FORTIFY_SOURCE=2 -g -fdebug-prefix-map=/build/python2.7-ByWuWM/python2.7-2.7.15=. -fstack-protector-strong -Wformat -Werror=format-security build/temp.linux-aarch64-2.7/src/face_detector.o build/temp.linux-aarch64-2.7/src/cv2gpu.o -L/usr/local/lib -lopencv_videostab -lopencv_shape -lopencv_cudacodec -lopencv_cudalegacy -lopencv_core -lopencv_cudawarping -lopencv_objdetect -lopencv_cudafeatures2d -lopencv_videoio -lopencv_cudafilters -lopencv_flann -lopencv_cudaimgproc -lopencv_highgui -lopencv_cudaarithm -lopencv_stitching -lopencv_features2d -lopencv_cudev -lopencv_superres -lopencv_calib3d -lopencv_imgcodecs -lopencv_cudaoptflow -lopencv_cudaobjdetect -lopencv_ml -lopencv_imgproc -lopencv_cudastereo -lopencv_video -lopencv_dnn -lopencv_cudabgsegm -lopencv_photo -o build/lib.linux-aarch64-2.7/cv2gpu.so
    /usr/bin/ld: cannot find -lopencv_cudacodec
    /usr/bin/ld: cannot find -lopencv_cudalegacy
    /usr/bin/ld: cannot find -lopencv_cudawarping
    /usr/bin/ld: cannot find -lopencv_cudafeatures2d
    /usr/bin/ld: cannot find -lopencv_cudafilters
    /usr/bin/ld: cannot find -lopencv_cudaimgproc
    /usr/bin/ld: cannot find -lopencv_cudaarithm
    /usr/bin/ld: cannot find -lopencv_cudev
    /usr/bin/ld: cannot find -lopencv_cudaoptflow
    /usr/bin/ld: cannot find -lopencv_cudaobjdetect
    /usr/bin/ld: cannot find -lopencv_cudastereo
    /usr/bin/ld: cannot find -lopencv_cudabgsegm
    collect2: error: ld returned 1 exit status
    error: command 'aarch64-linux-gnu-g++' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/nvidia/environments/bioengine/bin/python2.7 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/home/nvidia/opencv-gpu-py/setup.py'"'"'; __file__='"'"'/home/nvidia/opencv-gpu-py/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' develop --no-deps Check the logs for full command output.

Did you know about this issue? Could you give me any guidance to achieve installing your package?

Thanks in advance.

usage in ros : TypeError: argument 1 must be string, not numpy.ndarray

Board : Nvidia TX2
Ubuntu 16.04
Ros kinetic

I use your lib as a standard python 2.7 lib, and install like this :

git clone https://github.com/alexanderkoumis/opencv-gpu-py
cd opencv-gpu-py
sudo python setup.py install

My progr :

import cv2gpu # for face recognition with cuda cascades
from threading import Thread
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

cascade_file_gpu = '/home/nvidia/catkin_ws/src/alfred_vision/src/haarcascade_frontalface_default_cuda.xml'
cv2gpu.init_gpu_detector(cascade_file_gpu)

class Image_processing:

	def __init__(self):
		rospy.init_node('generic_Image_processing', anonymous=True)

		self.bridge = CvBridge()
                camera_topic = '/camera/rgb/image_raw'
		self.image_sub = rospy.Subscriber(camera_topic,Image,self.callback)

	def callback(self,data):
	    try:
			self.cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
			self.frame = cv2.cvtColor(self.cv_image, cv2.COLOR_BGR2GRAY)
                        self.frame = cv2.resize(self.frame, (0,0), fx=0.5, fy=0.5)
                        cv2.imwrite('color_img.jpg', self.frame)
                        for (x, y, w, h) in cv2gpu.find_faces(self.frame):
                            print(x,y,w,h)
	    except CvBridgeError as e:
			print(e)

def main():
	
  imProcess = Image_processing()
  rospy.spin()

if __name__ == '__main__':
    main()

My error :

Error: Problem parsing image path
[ERROR] [1525543228.850955]: bad callback: <bound method Image_processing.callback of <__main__.Image_processing instance at 0x7f8eeae4d0>>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/nvidia/catkin_ws/src/alfred_vision/src/face_tracking_cuda.py", line 44, in callback
    for (x, y, w, h) in cv2gpu.find_faces(self.frame):
TypeError: argument 1 must be string, not numpy.ndarray
Error: Problem parsing image path
[ERROR] [1525543228.850955]: bad callback: <bound method Image_processing.callback of <__main__.Image_processing instance at 0x7f8eeae4d0>>
Traceback (most recent call last):
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/home/nvidia/catkin_ws/src/alfred_vision/src/face_tracking_cuda.py", line 44, in callback
    for (x, y, w, h) in cv2gpu.find_faces(self.frame):
TypeError: argument 1 must be string, not numpy.ndarray

This your last version install !
Perhaps I missed anything ?

Thanks a lot, Alexander
Vincent

Incompatible with python 3

Hi. When I try installing this project, I get an error like this:

pip install -e opencv-gpu-py
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "~/opencv-gpu-py/setup.py", line 36, in <module>
        opencv_deps = pkgconfig('opencv')
      File "~/opencv-gpu-py/setup.py", line 18, in pkgconfig
        for key, value in flag_map.iteritems():
    AttributeError: 'dict' object has no attribute 'iteritems'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1

Now, when I update the offending lines in setup.py to use .items() instead of .iteritems(), I can get it to be installed, but when I run the test.py, I get an error like this:

python test/test.py 
Traceback (most recent call last):
  File "test/test.py", line 5, in <module>
    import cv2gpu
ImportError: ~/opencv-gpu-py/cv2gpu.cpython-35m-x86_64-linux-gnu.so: undefined symbol:

Any idea why?

TypeError: argument 1 must be string, not numpy.ndarray

hi Alexander.
Thanks for your nice lib.

Trying to use it in ROS, and here is the problem :
your lib asks for an image file (like *.jpg)
ros produce a numpy array image !!

Could anyone help me replacing string to array ! (don't anderstand C++,just python)

initialize py2gpu with many xml files ?!

hello Alexander,
I'd like to make my tracker stronger, trying some haarcascades on the fly. Anything like this :

cascades 1 to 3 are different cascades files;

        if self.cascade_1:
            faces = self.cascade_1.detectMultiScale(input_image, **self.haar_params)
        # If that fails, check the profile template
        if len(faces) == 0 and self.cascade_3:
            faces = self.cascade_3.detectMultiScale(input_image, **self.haar_params)
        # If that also fails, check a the other frontal template
        if len(faces) == 0 and self.cascade_2:
faces = self.cascade_2.detectMultiScale(input_image, **self.haar_params)

Is it possible to do it with your lib ?
cv2gpu.init_gpu_detector(self.cascade_1)

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.