Giter Site home page Giter Site logo

mbeyeler / opencv-python-blueprints Goto Github PK

View Code? Open in Web Editor NEW
290.0 26.0 185.0 188.56 MB

M. Beyeler (2015). OpenCV with Python Blueprints: Design and develop advanced computer vision projects using OpenCV with Python, Packt Publishing Ltd., ISBN 978-178528269-0.

License: GNU General Public License v3.0

Python 100.00%
opencv computer-vision python machine-learning image-processing

opencv-python-blueprints's Introduction

OpenCV with Python Blueprints

DOI Google group License: GPL v3

This repository contains all up-to-date source code for the following book:

Michael Beyeler
OpenCV with Python Blueprints: Design and develop advanced computer vision projects using OpenCV with Python

Packt Publishing Ltd., London, England
Paperback: 230 pages
ISBN 978-178528269-0

This book demonstrates how to develop a series of intermediate to advanced projects using OpenCV and Python, rather than teaching the core concepts of OpenCV in theoretical lessons. Instead, the working projects developed in this book teach the reader how to apply their theoretical knowledge to topics such as image manipulation, augmented reality, object tracking, 3D scene reconstruction, statistical learning, and object categorization.

By the end of this book, readers will be OpenCV experts whose newly gained experience allows them to develop their own advanced computer vision applications.

If you use either book or code in a scholarly publication, please cite as:

M. Beyeler, (2015). OpenCV with Python Blueprints: Design and develop advanced computer vision projects using OpenCV with Python. Packt Publishing Ltd., London, England, 230 pages, ISBN 978- 178528269-0.

Or use the following bibtex:

@book{OpenCVWithPythonBlueprints,
	title = {{OpenCV with Python Blueprints}},
	subtitle = {Design and develop advanced computer vision projects using {OpenCV} with {Python}},
	author = {Michael Beyeler},
	year = {2015},
	pages = {230},
	publisher = {Packt Publishing Ltd.},
	isbn = {978-178528269-0}
}

Scholarly work referencing this book:

  • B Zhang et al. (2018). Automatic matching of construction onsite resources under camera views. Automation in Construction.
  • A Jakubović & J Velagić (2018). Image Feature Matching and Object Detection Using Brute-Force Matchers. International Symposium ELMAR.
  • B Zhang et al. (2018). Multi-View Matching for Onsite Construction Resources with Combinatorial Optimization. International Symposium on Automation and Robotics in Construction (ISARC) 35:1-7.
  • LA Marcomini (2018). Identificação automática do comportamento do tráfego a partir de imagens de vídeo. Escola de Engenharia de São Carlos, Master's Thesis.
  • G Laica et al. (2018). Diseño y construcción de un andador inteligente para el desplazamiento autónomo de los adultos mayores con visión reducida y problemas de movilidad del hogar de vida "Luis Maldonado Tamayo" mediante la investigación de técnicas de visión artificial. Departamento de Ciencias de la Energía y Mecánica, Universidad de las Fuerzas Armadas ESPE, Master's Thesis.
  • I Huitzil-Velasco et al. (2017). Test of a Myo Armband. Revista de Ciencias Ambientales y Recursos Naturales 3(10): 48-56.
  • Y Güçlütürk et al. (2016). Convolutional sketch inversion. European Conference on Computer Vision (ECCV) 810-824.

All code was tested with OpenCV 2.4.9 and Python 2.7 on Ubuntu 14.04 and Windows 8.1, and is available from: https://github.com/mbeyeler/opencv-python-blueprints.

For questions, discussions, and more detailed help please refer to the Google group: https://groups.google.com/d/forum/opencv-python-blueprints

Critical Reception

What readers on Amazon have to say:

The author does a great job explaining the concepts needed to understand what's happening in the application without the need of going into too many details.
Sebastian Montabone

Excellent book to build practical OpenCV projects! I'm still relatively new to OpenCV, but all examples are well laid out and easy to follow. The author does a good job explaining the concepts in detail and shows how they apply in real life. As a professional programmer, I especially love that you can just fork the code from GitHub and follow along. Strongly recommend to readers with basic knowledge of computer vision, machine learning, and Python! – Amazon Customer

Usually I'm not a big fan of technical books because they are too dull, but this one is written in an engaging manner with a few dry jokes here and there. Can only recommend!
– lakesouth

Who This Book Is for

As part of Packt's Blueprints series, this book is for intermediate users of OpenCV who aim to master their skills by developing advanced practical applications. You should already have some experience of building simple applications, and you are expected to be familiar with OpenCV's concepts and Python libraries. Basic knowledge of Python programming is expected and assumed.

By the end of this book, you will be an OpenCV expert, and your newly gained experience will allow you to develop your own advanced computer vision applications.

Software Requirements

All projects can run on Windows, Mac, or Linux, and require the following software packages:

In addition, some chapters require the following free Python modules:

Furthermore, the use of iPython (http://ipython.org/install.html) is highly recommended as it provides a flexible, interactive console interface.

License

The software is released under the GNU General Public License (GPL), which is the most commonly used free software license according to Wikipedia. GPL allows for commercial use, distribution, modification, patent use, and private use.

The GPL is a copyleft license, which means that derived works can only be distributed under the same license terms. For more information, please see the license file.

opencv-python-blueprints's People

Contributors

mbeyeler 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opencv-python-blueprints's Issues

Fix code for Raspberry Pi

There are at least two issues on Raspberry Pi that need fixing:

  • cv2.VideoCapture(0) won't work
  • wxPython needs adjustments as suggested by G10DRAS

Chapter 1, filters.py OpenCV error

I am trying to run the cartoonify code

import cv2
import numpy as np

numDownSamples = 2       # number of downscaling steps
numBilateralFilters = 7  # number of bilateral filtering steps

img_rgb = cv2.imread("testimg.jpg")

# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in range(numDownSamples):
    img_color = cv2.pyrDown(img_color)

# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in range(numBilateralFilters):
    img_color = cv2.bilateralFilter(img_color, 9, 9, 7)

# upsample image to original size
for _ in range(numDownSamples):
    img_color = cv2.pyrUp(img_color)

# -- STEPS 2 and 3 --
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)

# -- STEP 4 --
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,
                                    cv2.ADAPTIVE_THRESH_MEAN_C,
                                    cv2.THRESH_BINARY, 9, 2)

# -- STEP 5 --
# convert back to color so that it can be bit-ANDed with color image
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
toon = cv2.bitwise_and(img_color, img_edge)
cv2.imwrite('cartoon.jpg', toon)

When I run it however I get the following error

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') in cv::binary_op, file C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp, line 225
Traceback (most recent call last):
  File "cartoonify.py", line 38, in <module>
    toon = cv2.bitwise_and(img_color, img_edge)
cv2.error: C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:225: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function cv::binary_op

I am running python 3.6
numpy==1.14.2
opencv-python==3.4.0.12
Pillow==5.1.0

Any ideas on how I can get this working?

wxPython can't be installed on Mac OS X 10.11 El Capitan

This is not a problem with the book code itself, but with the wxPython package, which is required for most chapters.

Apparently it's impossible to install wxPython on Mac OS X 10.11 El Capitan via common practices, resulting in the following error:

“wxPython3.0-osx-cocoa-py2.7.pkg” is damaged and can’t be opened. You should eject the disk image.

The bug is documented here:
http://stackoverflow.com/questions/34402303/install-wxpython-in-osx-10-11

There seems to be a workaround here:
http://davixx.fr/blog/2016/01/25/wxpython-on-os-x-el-capitan/

Try to get confirmation that the workaround works. Has anyone tried this??

If it works, include instructions in the doc to resolve this nasty issue, so that users won't get stuck in the future.

Chapter 2

Hi, I am trying to understand your code but keep getting an error that I can't quite get around.

I keep getting this error and I
user:~/opencv-python-blueprints/chapter2$ python chapter2.py
Could not open audio: -1
Traceback (most recent call last):
File "chapter2.py", line 93, in
main()
File "chapter2.py", line 87, in main
layout = KinectLayout(None, -1, 'Kinect Hand Gesture Recognition', capture)
File "/home/lpcs/opencv-python-blueprints/chapter2/gui.py", line 71, in init
self.bmp = wx.BitmapFromBuffer(self.imgWidth, self.imgHeight, frame)
File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_gdi.py", line 856, in BitmapFromBuffer
return gdi._BitmapFromBuffer(width, height, dataBuffer)
ValueError: Invalid data buffer size.

Bug for mac

Hi-
Chapter7.py : In mac the UI/window does not show up. However, in Ubuntu 14.04 64 bit machine with all the similar package versions but it seems that there it is working fine. Maybe this is because of gui.py or wxpython package in mac (I installed all of them using brew). Chapter3 code as well I am getting same issue.

In details, there is a infinite loop at App:init: line 1870 self._BootstrapApp() in core.py
/usr/local/lib/python2.7/site-packages/wx/core.py
Which basically tells that upon completion of this function OnInit() function will be called which should be available in derived class of App.

capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480) ----> does not work in opencv3

Hi- it seems that " capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480)" does not work with opencv3. I see the following error:

File "chapter7.py", line 293, in
main()
File "chapter7.py", line 282, in main
capture.set(cv2.CV_CAP_PROP_FRAME_HEIGHT, 480)
AttributeError: 'module' object has no attribute 'CV_CAP_PROP_FRAME_HEIGHT'

I use python 2.7 and followed the following link for installation:
http://www.pyimagesearch.com/2016/12/19/install-opencv-3-on-macos-with-homebrew-the-easy-way/

Move common code out of chapters

For the sake of the book release all chapters are supposed to be self-contained. However, now that this code is on GitHub, it would make more sense to move common code (such as gui.py) out of chapter directories and into a common directory in order to avoid code duplication.

  • Create common module
  • Move gui.py
  • Move classifiers.py

Cartoonizer on face with transparent background

I want to apply the Cartoonizer on a image with a transparent background. Sadly the alpha channel will always be removed and therefore all transparent parts are black after the filter was applied. Is there a workaround to fix this?

EDIT:
When I read in the image with alpha channels like this:
img = cv2.imread("frame004.png", cv2.IMREAD_UNCHANGED)

I get the following error:
OpenCV Error: Assertion failed ((src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.type() == dst.type() && src.size() == dst.size() && src.data != dst.data) in bilateralFilter_8u, file /tmp/opencv-20161221-94455-1gogyaj/opencv-2.4.13.2/modules/imgproc/src/smooth.cpp, line 1925 Traceback (most recent call last): File "cartoonizer.py", line 17, in <module> frame_cartooned = cartoonizer.render(img) File "/Users/oliverweiss/dev/cartoonizer/filters.py", line 163, in render img_color = cv2.bilateralFilter(img_color, 9, 9, 7) cv2.error: /tmp/opencv-20161221-94455-1gogyaj/opencv-2.4.13.2/modules/imgproc/src/smooth.cpp:1925: error: (-215) (src.type() == CV_8UC1 || src.type() == CV_8UC3) && src.type() == dst.type() && src.size() == dst.size() && src.data != dst.data in function bilateralFilter_8u

Thank you!

Chapter 5: Error too many values to unpack

Using the sample video of Chapter 5, when it run, it produces error:
in main
sal.get_proto_objects_map(use_otsu=False)))

File "tracking.py", line 68, in advance_frame
box_all = self._append_boxes_from_saliency(proto_objects_map, box_all)

File "tracking.py", line 105, in _append_boxes_from_saliency
cnt_sal, _ = cv2.findContours(proto_objects_map, 1, 2)

ValueError: too many values to unpack

could not align head

i am using opencv2 and python version 3.5.2
i had made some changes and now got some error as follows:

Warning: Testing is disabled
Could not find data file datasets/faces_preprocessed.pkl
Could not align head (eye detection failed?)

Test/update code for OpenCV3

Make sure code runs with OpenCV3. Update where necessary. Make it so that code works for both OpenCV2&3.

  • Fix CAP_PROP_FRAME_WIDTH (see issue #9)
  • Switch to print()
  • Check all chapters

TypeError: only length-1 arrays can be converted to Python scalars

when I run the code chapter6.py

I got this error.

/usr/bin/python2.7 /home/redhwan/Downloads/opencv-python-blueprints-master/chapter6/chapter6.py
feature None
 - strategy one-vs-one
    - train
Traceback (most recent call last):
  File "/home/redhwan/Downloads/opencv-python-blueprints-master/chapter6/chapter6.py", line 77, in <module>
    main()
  File "/home/redhwan/Downloads/opencv-python-blueprints-master/chapter6/chapter6.py", line 44, in main
    MCS.fit(X_train, y_train)
  File "/home/redhwan/Downloads/opencv-python-blueprints-master/chapter6/classifiers.py", line 258, in fit
    params=self.params)
TypeError: only length-1 arrays can be converted to Python scalars

Process finished with exit code 1

as you see, python 2.7
opencv 3.3
please help me to fix this error.
Thank you in advance

Classification label not drawn above bounding box

Thanks to @G10DRAS:

I found another bug, which is related to putTextabove a detected face. x,y co-ordinate are missing in function.

chapter7.py
def _process_frame(self, frame):
........
                # draw label above bounding box
                cv2.putText(frame, str(label), (x, y - 20),
                            cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)

metaclass error for chapter7

When I ran code on Mac OS 10.12.3 with Python 2.7 I received following error for chapter7.py

bash-3.2$ python chapter7.py
Traceback (most recent call last):
  File "chapter7.py", line 33, in <module>
    from gui import BaseLayout
  File "/Users/jinay/workspace/upwork/opencv-python-blueprints/chapter7/gui.py", line 20, in <module>
    class BaseLayout(Meta1):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/abc.py", line 87, in new
    cls = super(ABCMeta, mcls).__new__(mcls, name, bases, namespace)
TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases

Possible work around could be #16

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.