Title: visual-logging, my new favorite tool for debugging OpenCV and Python apps - PyImageSearch

URL Source: https://pyimagesearch.com/2014/12/22/visual-logging-new-favorite-tool-debugging-opencv-python-apps/

Published Time: 2014-12-22T15:00:39+00:00

Markdown Content:
[![Image 1](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20397'%3E%3C/svg%3E)](https://github.com/dchaplinsky/visual-logging)

A couple months ago, my friend [Dmitry Chaplinsky](https://github.com/dchaplinsky) sent me an email telling me about a new library that I just had to checkout. Always curious about new computer vision and image processing packages, I became intrigued.

You see, Dmitry’s work involves applying Python and OpenCV to tag and categorize shredded documents in hope of uncovering [corruption in the in Ukraine](http://www.bbc.com/news/magazine-26835441). His library, [unshred](https://github.com/dchaplinsky/unshred), does much of the dirty work.

But in order to build unshred he needed an easy way to debug OpenCV applications.

Just like we use `print` statements to debug simple Python apps, we use the OpenCV `cv2.imshow` and `cv2.waitKey` functions to visually display our images to our screen so that we can debug them.

In all honestly it’s a real pain in the ass and before you know it, your pipeline is completely filled up with `cv2.imshow` calls.

To fix this `cv2.imshow` hell, Dmitry created [visual-logging](https://github.com/dchaplinsky/visual-logging), a library that lets you debug and log your OpenCV pipeline directly to file. And it formats it all using a pretty HTML structure.

Before a few months ago, I had no idea that a tool like this even existed — and now I am incorporating it into my everyday computer vision workflow.

So if you’re interested in how you can (better) debug and log your OpenCV + Python apps, then read on. You won’t want to miss this awesome library.

_**Thanks again Dmitry!**_

![Image 2](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20128%20128'%3E%3C/svg%3E)

#### Looking for the source code to this post?

[Jump Right To The Downloads Section](#download-the-code)

**OpenCV and Python versions:**  
This example will run on **Python 2.7** and **OpenCV 2.4.X/OpenCV 3.0+**.

At the time of this writing, the [visual-logging](https://github.com/dchaplinsky/visual-logging) documentation claims that the package is `pip` -installable:

$ pip install visual-logging

However, when I executed the above command I got the dreaded “No distributions at all found for visual-logging”. I’m assuming that the package is not yet registered with PyPi.

Regardless, that’s not a big deal. We can simply clone from the GitHub repo and install the “old-fashioned” way:

(cv)annalee:VisualLogging adrianrosebrock$ git clone https://github.com/dchaplinsky/visual-logging.git
Cloning into 'visual-logging'...
remote: Counting objects: 137, done.
remote: Total 137 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (137/137), 361.23 KiB | 0 bytes/s, done.
Resolving deltas: 100% (70/70), done.
Checking connectivity... done.
(cv)annalee:VisualLogging adrianrosebrock$ cd visual-logging/
(cv)annalee:visual-logging adrianrosebrock$ python setup.py install

Now that we have `visual-logging` installed, let’s create a simple script that demonstrates how `visual-logging` can be used to help us visualize our pipeline:

\# import the necessary packages
from logging import FileHandler
from vlogging import VisualRecord
import logging
import cv2

# open the logging file
logger = logging.getLogger("visual\_logging\_example")
fh = FileHandler("demo.html", mode = "w")

# set the logger attributes
logger.setLevel(logging.DEBUG)
logger.addHandler(fh)

# load our example image and convert it to grayscale
image = cv2.imread("lex.jpg")
image = cv2.cvtColor(image, cv2.COLOR\_BGR2GRAY)

# loop over some varying sigma sizes
for s in xrange(3, 11, 2):
	# blur the image and detect edges
	blurred = cv2.GaussianBlur(image, (s, s), 0)
	edged = cv2.Canny(blurred, 75, 200)
	logger.debug(VisualRecord(("Detected edges using sigma = %d" % (s)),
		\[blurred, edged\], fmt = "png"))

The code here is fairly simple. On **Lines 2-5** we import the packages we’ll need.

We then setup our logging handlers and attributes on **Lines 8-13**.

We load an example image of [Lex Murphy](https://pyimagesearch.com/wp-content/uploads/2014/12/lex.jpg) (from _Jurassic Park_, duh) on **Line 16** and convert the image to grayscale on **Line 17**.

**Lines 20-25** test out our visual logger. We are going to progressively blur the image using a larger and larger _sigma_ size. We then detect edges in the blurred image.

Normally, to view the output of our blurring and edge detection we would have to make a call to `cv2.imshow` and `cv2.waitKey` .

But those days are done.

Instead, we are going to create a `VisualRecord` and log both the Gaussian blurred and edged map to file. This will allow us to debug our pipeline and easily view our results.

Speaking of results, execute the following command to generate your visual log file:

$ python visual\_logging\_example.py

Assuming the script executed without error, you should have a new file named `demo.html`  in your current working directory.

Here is a screenshot of what my `demo.html`  file looks like:

[![Image 3: Figure 1: Example of using visual-logger to log and debug OpenCV + Python computer vision pipelines.](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20600%20397'%3E%3C/svg%3E)](https://pyimagesearch.com/wp-content/uploads/2014/12/visual_logging_example.jpg)

**Figure 1:** Example of using visual-logger to log and debug OpenCV + Python computer vision pipelines.

As we can see from this example, as the size of the Gaussian _sigma_ increases, the image becomes progressively more blurred. And as the image becomes more blurred, less edges are detected.

Of course, this is a fairly trivial example of debugging and logging an OpenCV + Python application. But I think you get the point — the days of clogging your code up with `cv2.imshow`  and `cv2.waitKey`  statements are gone!

Instead, just use [visual-logging](https://github.com/dchaplinsky/visual-logging) — your life will be much simpler.

### What's next? We recommend [PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend).

**Course information:**  
84 total classes • 114+ hours of on-demand code walkthrough videos • Last updated: February 2024  
★★★★★ 4.84 (128 Ratings) • 16,000+ Students Enrolled

**I strongly believe that if you had the right teacher you could _master_ computer vision and deep learning.**

Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?

That’s _not_ the case.

All you need to master computer vision and deep learning is for someone to explain things to you in _simple, intuitive_ terms. _And that’s exactly what I do_. My mission is to change education and how complex Artificial Intelligence topics are taught.

If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here you’ll learn how to _successfully_ and _confidently_ apply computer vision to your work, research, and projects. Join me in computer vision mastery.

**Inside PyImageSearch University you'll find:**

*   ✓ **84 courses** on essential computer vision, deep learning, and OpenCV topics
*   ✓ **84 Certificates** of Completion
*   ✓ **114+ hours** of on-demand video
*   ✓ **Brand new courses released _regularly_**, ensuring you can keep up with state-of-the-art techniques
*   ✓ **Pre-configured Jupyter Notebooks in Google Colab**
*   ✓ Run all code examples in your web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
*   ✓ Access to **centralized code repos for _all_ 536+ tutorials** on PyImageSearch
*   ✓ **Easy one-click downloads** for code, datasets, pre-trained models, etc.
*   ✓ **Access** on mobile, laptop, desktop, etc.

[Click here to join PyImageSearch University](https://pyimagesearch.com/pyimagesearch-university/?utm_source=blogPost&utm_medium=bottomBanner&utm_campaign=What%27s%20next%3F%20I%20recommend)

Summary
-------

In this blog post we explored [visual-logging](https://github.com/dchaplinsky/visual-logging), my new favorite tool for debugging Python + OpenCV applications.

Up until now, debugging OpenCV apps has been a mess of `cv2.imshow`  and `cv2.waitKey`  calls. All of these functions calls are a pain in the ass to manage, and even worse for debugging. You’ll be taking screenshot after screenshot of each iteration of your computer vision pipeline.

But now we have a better way. We can utilize Dmitry Chaplinsky’s [visual-logging](https://github.com/dchaplinsky/visual-logging) package to help us easily debug and log our OpenCV apps.

Thanks Dmitry for such a great library!

![Image 4](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20410%20567'%3E%3C/svg%3E)

#### Download the Source Code and FREE 17-page Resource Guide

Enter your email address below to get a .zip of the code and a **FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning.** Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL!

![Image 5](data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20240%20240'%3E%3C/svg%3E)

#### **About the Author**

Hi there, I’m Adrian Rosebrock, PhD. All too often I see developers, students, and researchers wasting their time, studying the wrong things, and generally struggling to get started with Computer Vision, Deep Learning, and OpenCV. I created this website to show you what I believe is the best possible way to get your start.