Does a computer see in color or black and white?
It's time to find out on episode 11 of Talk Python to Me
with our guest, Adrian Rosebrock,
recorded Thursday, May 20th, 2015.
Welcome to Talk Python to Me.
A weekly podcast on Python, the language, the libraries,
the ecosystem, and the personalities.
This is your host, Michael Kennedy.
Follow me on Twitter where I'm @mkennedy
and keep up with the show and listen to past episodes
at talkpythontome.com.
This episode, we'll be talking with Adrian Rosebrock
about computer vision, OpenCV, and PyImage Search.
Hello, everyone.
I have a bunch of cool news and announcements for you this week.
First, this show on PyImage Search
is a listener-suggested show.
Thank you to J.I. Lorenzetti
for reaching out to me and suggesting this topic.
You can find his contact details in the show notes.
As always, I'm also excited to be able to tell you
that this episode is brought to you by CodeShip.
CodeShip is a platform for continuous integration
and continuous delivery as a service.
Please take a moment and check them out at codeship.com
or follow them on Twitter where they're at codeship.
Did you know that most of our shows come with full transcripts
and a cool little search filter feature?
If you're looking for something you hear in an episode,
just click the full transcript button on the episode page
and search for it.
Also, I want to say thank you to everyone
who has been participating in the conversation on Twitter
where we're at Talk Python.
It's a great feeling to see all the feedback and thoughts
every week when we release a new show.
But if you have something more nuanced to say
that doesn't fit in 140 characters
or you want it to be more permanent than Twitter,
every episode page has a Discus comment section at the bottom.
I encourage you to post your thoughts there.
This week, I ran across a really awesome GitHub project
called Python-Patterns.
You can find it at github.com
slash f-a-i-f slash python dash patterns.
It's a collection of really crisp design patterns
implemented in a Pythonic manner.
For example, you'll find patterns such as
the adapter, builder, chain, decorator, facade,
and flyweight patterns, just to name a few.
It's really extensive and pretty cool.
I think you'll learn something if you check it out.
Finally, I put together a cool YouTube playlist.
This is a series of nine lectures from Dr. Philip Gao,
a professor at the University of Rochester, New York.
Find him on Twitter where he's at P-G-B-O-V-I-N-E, P-G-Bovine.
The video series is entitled C Internals,
a 10-hour code walk through the Python interpreter source code.
You can find it at bit.ly.com slash cpythonwalk,
all lowercase, no spaces.
Also, you'll find all these links in the show notes.
Now, let's get to the interview with Adrian.
Let me introduce Adrian.
Adrian Rosebrock is an author and blogger at pyimagesearch.com.
He has a PhD in computer science with a focus on computer vision and machine learning
and has been studying computer vision his entire adult life.
He has consulted for the National Cancer Institute to develop methods to predict breast cancer risks
using breast histology images and authored a book, Practical Python and OpenCV,
on utilizing Python and OpenCV to build real-world computer vision applications.
Adrian, welcome to the show.
Oh, thank you.
It's great to be here.
I'm very excited about computer vision and sort of merging the real world with computer science,
with robotics.
And I think there's just some really neat stuff going on.
And you're doing a very cool part in that.
Oh, thank you.
So we're going to talk about PyImageSearch.
We're going to talk about OpenCV and some of the challenges and even the future of these types of technologies.
But before we get there, you know, everyone's interested in how people got started in programming and Python.
What's your story?
I started programming when I was in high school.
I started out with the basics of HTML, JavaScript, CSS, did some basic programming.
And, you know, I'm probably getting a lot of hate mail about this.
But when I first started learning how to program, I did not like the Python programming language that much.
And this was around the early version 2 of Python.
I didn't like the syntax.
I didn't like the white space.
And for a long time, I was really, really put off by Python.
And that was a huge mistake on my part.
I don't know what was wrong with me back then.
I guess it was just high school ignorance or something.
But by the time I got to college, I started working in Python a lot more.
And that's especially true in the scientific area.
You see all these incredible packages in Python like NumPy and SciPy that just integrate with computer vision and machine learning.
And all other types of libraries.
And more and more people were transitioning over from languages like MATLAB to languages like Python.
And that's so cool.
And it really wasn't until college that I got into Python.
And I remember this one girl.
She was in my machine learning class.
And she had a sticker on the back of her laptop that said, Python will save the world.
I don't know how, but it will.
And that resonated with me.
I'm like, that sticker's true.
That is absolutely true.
It's such a great language.
So unfortunately, I did not have the best first experience with Python.
It took me four or five years later to actually come around.
But now that I'm here, I love it.
And I can't imagine programming in any other language.
It's almost a freeing feeling, a relaxing zen when you're coding in Python.
Yeah, that's a funny story.
It really is a wonderful language.
I also took a while to get there.
But looking back, I would have enjoyed being there sooner.
I went from MATLAB to C++ on Silicon Graphics machines.
So I had a bit of a torturous introduction.
But it was all good.
CodeShip is a hosted, continuous delivery service focused on speed, security, and customizability.
You can set up continuous integration in a matter of seconds and automatically deploy when your tests have passed.
CodeShip supports your GitHub and BitBucket projects.
You can get started with CodeShip's free plan today.
Should you decide to go with a premium plan,
Talk Python listeners can save 20% off any plan for the next three months by using the code TALKPYTHON.
All caps, no spaces.
Check them out at CodeShip.com.
And tell them thanks for sponsoring the show on Twitter where they're at CodeShip.
So you're focused on computer vision and image processing.
Where did that story begin?
That story also started in high school.
Originally, I had this idea that I wanted to go work for Adobe.
And I wanted to work on developing Photoshop and Illustrator.
I love the idea of being able to write code that could analyze an image.
And for whatever reason, that just like really captured my imagination.
I could see these algorithms running in Photoshop.
And I was like, you know, what's really going on behind the scenes?
Like, how are they manipulating these images?
What does this code look like?
So for the longest time, I really wanted to develop these graphic editing applications.
But I didn't have the math experience.
This may surprise some people, given that I have a PhD in computer science.
But up until late high school, I did not do well in mathematics courses.
I got C's in algebra and geometry.
And it really wasn't until I kind of really put my back against the wall.
And I said, you know what?
I got to learn calculus and statistics.
So I did a self-study in AP Calc.
And I took AP statistics.
And I did well with those.
I'm like, man, math is fun now.
Like, I understand this.
So I got to college.
And I only took one computer vision course at the, because the school I went to didn't really
have a computer vision focus.
They had a wonderful machine learning focus, but not really a computer vision focus.
And what I found out was that, you know, you don't need a mathematical background to get
started in computer vision.
And I think this is true in a lot of areas of computer science, whether or not people want
to admit it.
A lot of people talk themselves out of getting started and stuff, especially challenging things,
because they're just scared of it.
They don't want to fail.
And that's the cool thing about Python.
Like, you almost don't have to worry about the code.
You get to focus on learning a new skill.
And for me, that was computer vision.
And that was the OpenCV library, an open source library that makes working with computer vision
a lot easier.
So again, it really wasn't until college that I really started to get into it.
getting interested.
Or not necessarily getting interested.
More so being able to take action on what I wanted to do.
Right.
Maybe, you know, it felt a little unattainable.
Like, I'm going to go be an engineer, but I don't know math.
And so there's no way I can do this.
But once you kind of got over that hump, then it was no big deal, right?
Right.
Yeah.
That's very freeing.
So you mentioned OpenCV and your project is PyImageSearch.
What's the relationship there?
So OpenCV, again, it's a computer vision library that makes working with images a lot easier.
You know, it abstracts the code that loads an image off of disk or does edge detection or
thresholding or any other simple image processing function like that.
allows you to actually build complicated computer vision programs.
You can do things like tracking objects and images or video streams, for example.
Detecting faces.
Recognizing whose face it is.
And OpenCV really facilitates this process.
And OpenCV really is like the de facto library for computer vision and image processing.
And you have bindings for it in countless languages.
The library itself is written in C and C++, but you can get bindings and access it in Java
and any of the .NET frameworks in Python.
So again, while you can access it in a programming language, I have this love for Python now.
And when I took the course, the computer vision course in college, I realized, man, like, people
are spending a lot of time writing their class projects in C and C++.
Why are they doing that?
Like, you're fighting over these weird compile time errors.
And, you know, you're not really learning anything.
And that's kind of the tenet behind PyImageSearch.
It's a blog that I run dedicated to teaching computer vision, image processing, and OpenCV using
the Python programming language.
That's great.
And I think that, you know, using Python seems like the perfect choice.
You're sort of orchestrating these high-level functions that are calling down into C++, doing
high-performance stuff, and then giving you the answer.
And that seems like the right way to be using Python.
So what's the actual package I use?
If I were to say pip install something, what do I type to get started?
So unfortunately, OpenCV is not pip installable.
I wish it was, but it is not.
And it is not the easiest package to get installed on your system.
If you're using Ubuntu or any Debian-based operating system, you technically can do an app git install.
But that's going to pull down a previous version of OpenCV.
You're going to run into a lot of problems with the Python bindings, and it's not a very
good experience.
So what you actually have to do is compile it from source, download the code from their
GitHub or the SourceForge account, and manually compile it and install it.
And in fact, that's really the only way to do it if you're interested in using virtual environments,
which, as most Python developers are interested in, sequestering their packages.
Yeah, absolutely.
Okay.
So I go and I download that.
And then what packages are in there that I would work with?
Is that CV2?
Is that the one I would import?
Yep.
So if you were to open up your favorite editor, you would just type in import CV2, and I'll
give you access to all of your OpenCV bindings.
Okay, great.
And now I looked at some samples on your blog about how I might go and grab like an image
from a camera hooked to an Adreno.
Maybe we could talk a little bit about the type of hardware that you need and the spectrum
of devices you can interact with and that kind of stuff before we get into the more theoretical
bits.
Sure.
So that's kind of the cool thing about OpenCV is that it's meant to be run in real time.
So you can easily process video files, raw video streams without too much of a problem,
again, depending on the complexity of your algorithm.
And OpenCV is meant to run on a variety of different devices.
I personally develop applications on my MacBook, but I also own a Raspberry Pi and a camera module
for the Raspberry Pi.
And using OpenCV, I can access the Raspberry Pi video stream and then actually build like
a home surveillance system using nothing but OpenCV and a Raspberry Pi.
I built this one project where I had a Raspberry Pi camera mounted on my kitchen cabinets looking
over the front door of my apartment.
And it would detect motion, such as when you're opening the door and somebody's walking inside.
So once it detected motion, it would snap a photo of whoever was walking inside, try and identify
their face, and then it would take that screenshot or the screen capture and then upload it to my
personal Dropbox.
So I had like this real-time home surveillance system.
That was really, really cool to develop.
And again, like this is using simple hardware.
The Raspberry Pi is not a powerful machine, but you could still build some really cool computer
vision applications with it.
Yeah.
And it's cheap too, right?
Yeah.
The Pi itself is, I think, $35 and probably another $20 for the camera module.
Yeah.
That's really easy to get started.
So very cool.
Very cool.
How does computer vision work?
I mean, I have a little bit of a background in trying to identify things and images.
I worked at this place called eye tracking, E-Y-E, tracking, not I, the letter I, tracking.
And we did a lot of stuff with image recognition and detecting eyes.
And I know enough to know that it seems really hard, but how does it work?
So computer vision as a field is really just encompassing methods on acquiring, processing,
analyzing, and just understanding and interpreting the contents of an image.
For humans, this is really, really easy.
We see a picture of a cat, and we know, like, oh, that's a cat.
And we see a picture of a dog, and we obviously know that's a dog.
But a computer, it doesn't have a clue.
It just sees a bunch of pixels, just a big matrix of pixels.
And the challenging part, as you suggested, is writing code and creating these algorithms
that can understand the contents of an image.
You can't open up your Python source file and then write if statements that say,
if this pixel equals, you know, whatever RGB code, you know, then this is a cat, right?
If it equals this pixel value, then it's a dog.
Like, you can't do that.
So what happens is computer vision really leverages machine learning as well.
So we can take this data-driven approach and say, here's a ton of examples of a cat,
and here's a ton of examples of a dog.
Let's see how we can abstractly quantify and represent this huge image.
And just like a small, what they call feature vector.
It's a fancy academic way of saying a list of numbers.
I'm going to quantify this big 3,000 by 3,000 pixel image into a feature vector that's 128 numbers long.
And then I can compare them to each other.
I can rank them for similarity.
I can pass them to machine learning algorithms to actually classify them.
So the field of computer vision is very large.
And again, it spans so many different areas of processing and analyzing images.
But if we're talking strictly about classifying an image and detecting objects in an image,
then we're most likely leveraging some machine learning at some point.
Okay, and cool.
And when you say machine learning, is that like neural networks or what's going on back there?
The machine learning algorithm you would use really depends on your application.
Deep learning has gotten so much attention over the past few years.
And deep learning has its roots in neural networks.
So we see a lot of that.
You also see very simple machine learning methods like support vector machines, logistic aggression.
You see that a lot as well.
And these methods, while simple, they're actually – the bulk of the work is actually happening on describing the image itself, you know, quantifying it.
So if you have a really good quantification of an image, it's a lot easier for the machine learning algorithm to take that and perform the classification.
Right, sure.
And so how much of this exists in external libraries like Scikit-learn or OpenCV or something like this?
And how much of that is like I've got to create that system for myself when I'm getting started based on my application?
So OpenCV does include some machine learning components, but I really don't recommend that people use them just because they're a little finicky and they're not that fun to use.
And especially in the Python ecosystem, you have Scikit-learn.
So you should be defaulting to that.
And to give an example, I wrote my entire dissertation, gathered all the examples using OpenCV and Scikit-learn.
I took the results that OpenCV was giving me and I passed them on to the machine learning methods and Scikit-learn.
Right.
Oh, that sounds very useful.
I think a lot of the challenging aspects of getting started in something new like this, if you're not already involved in it, is just knowing what exists, what you can reuse, and what you have to write yourself.
So knowing that that's out there is really nice.
Yeah, for sure.
And some of these algorithms you definitely don't want to be implementing yourself.
No, I'm sure you don't.
Unless you're really, really into high-performance matrix multiplication and other types of processing that make your day, right?
Exactly.
I have some sort of mental models of how I might use computer vision.
And then you have the Hollywood models, right?
Like Minority Report and so on.
But what's the current state of the art?
Like where do you see computer vision really prominently being used in the world?
So computer vision is used in your everyday life, whether you realize it or not.
And it's kind of scary, but it's also kind of cool.
Back about a year ago, I was traveling back and forth between Maryland and Connecticut on the East Coast of the United States constantly for work-related activities.
And one day I was exhausted.
It was Friday.
I just really wanted to leave Maryland and get back up to Connecticut and sleep in my own bed and just pass out.
So I left to work a little early.
And I started tearing up 95.
It was a beautiful summer day.
Sunlight streaming down.
Had my windows open and the wind blowing in.
And if you've ever driven on 95, specifically on the East Coast of the United States, you know there's always just like a ton of traffic or just lots of construction to ruin your drive.
And for whatever reason this day, there was no traffic.
There was no construction.
And I was just flying down the road.
And I made excellent time getting home.
However, two weeks later, I get my mail.
And I noticed that there is a speeding citation addressed to me.
Apparently, I had passed one of the speeding cameras that was mounted along the side of the road.
It detected that my car was traveling above the posted speed limit.
It snapped a photo of my license plate.
And then it applied what's called automatic license plate recognition where it takes the image, automatically analyzes it, finds my license plate, and then looks it up in a database and mails me a ticket.
I was like, man, I've written code to do this.
I know exactly how this worked.
So, like, it was the only time where I had a smile on my face as I was writing out the $40 check or whatever it was.
I'm like, you guys got me.
And I know how you did it.
Yeah, exactly.
Like, your love for image recognition is slightly turned against you.
Just briefly there.
Just briefly.
You said you worked on this thing called ID My Pill.
What's that?
So, ID My Pill is an iPhone application and an API that allows you to identify your prescription pills in the snap of your phone.
So, the general idea is that we are a little too faithful in pharmacists and our doctors.
And not to say there's anything against that, but mistakes do happen.
And people do get hurt.
They get sick.
And some of them do die every year due to taking the wrong medication.
So, the idea behind ID My Pill is to validate your prescription pills.
And it's also a way for pharmacies, for healthcare providers to facilitate better care for their patients.
So, you just take your pills, snap a photo of them, and then computer vision algorithms are used to automatically analyze and recognize the pill.
That way, you can validate that, yes, this is the correct pill.
This is what it says on the pill bottle.
And I know what I'm taking is correct.
That sounds really useful.
What do you think the chances are, like, something along those lines could be automated?
So, as the pharmacists are, like, actually filling the prescription, you know, the computer knows what they're filling and it sees what they're putting into bottles.
Could it say, whoa, whoa, whoa, this does not look legit?
Yeah, I think it absolutely can be automated.
The current systems right now that pharmacies use, especially within hospitals, some of them are taking RFID chips.
So...
You know, when you take a pill bottle off the shelf, it's able to validate that you are taking the correct medication and filling it.
But again, that's not perfect.
And pills can get mixed up.
So, in a perfect world, what you end up doing is you would have that RFID mechanism in place.
And then, you know, you have a mounted camera looking down at their workstation, at their desk.
And then, you know, it validates the pills in real time.
And they get a nice little thumbs up on the screen or whatever heads up display that they have in front of them.
And they can continue filling the medication.
Yeah, that sounds really helpful.
So, I used a slightly less productive, contribute to society sort of computer vision last night.
I was out with some friends and my wife.
We were having some wine.
And there's this really cool iPhone app called Vivino.
I think I'm saying it right.
Yeah.
And you can just take a picture of a bottle, even if the background is all messy and there's people around and so on.
And it'll tell you what the ratings are, how much it should cost at, you know, sort of standard retail price.
And I was really impressed with that.
You know, there's a lot of interesting sort of consumer style uses as well, I think.
Oh, for sure.
Vivino is one of the major ones.
And another one that you can see computer vision used a lot in, and I don't think people really notice it, but they appreciate it, is within sports.
So, in America, if you're watching a football game, you'll notice that they have, like, a yellow line drawn across the field marking the, you know, the spot of the ball along with the first down marker.
And these lines are drawn using calibrated cameras.
So, computer vision is used to calibrate the cameras and then know where to actually draw that line on the broadcast of the game.
And then, similarly, you can use computer vision and machine learning in the back end and analyze games to determine, you know, what's the optimal strategy.
And this is actually done in Europe a lot in soccer games.
So, they'll detect how players are moving around, how the ball is passed back and forth.
And they can almost run these data mining algorithms to learn how you're going to beat the other team and try and learn their strategy.
It's pretty cool.
Yeah, that really is amazing.
I feel like these days watching sports on television is actually a better experience than going to them live a lot of times because of these types of things, right?
It's very clear.
Oh, look, they've got to go, like, a foot and a half forward and then they get the first down.
Otherwise, they're going to fail.
And, you know, you don't really quite get the same feel live, which is ironic.
Right.
It's almost that it used to be that you didn't get the full story unless you were there.
And now it's kind of flipped around.
Like, you get more than the full story if you're watching the game on TV.
You get all the detail that you could possibly want.
Yeah, that's right.
So, you know, that sort of leads into the whole story of augmented reality and stuff like that with, you know, the Microsoft HoloLens, Google Glass, and, you know, a bunch of iPhone apps and other mobile apps as well.
What kind of interesting stuff do you see out in that world?
I have not had a chance to play around with the HoloLens.
I have used the Google Glass and played around with that.
Most of the applications I see, again, this is just because of my work with ID mypyll, is medical related.
So, you'll see surgeons going into really, really long, you know, 10 plus hour surgeries that they perform these complex operations.
And they may need to look up some sort of reference material while they're doing this surgery.
And instead of having an assistant doing that for them, I mean, they could put on the Google Glass and have this information right there in front of them.
So, you see a lot of that.
And since the Glass has a camera, there's a lot of research focused on, especially within medicine, identifying various body parts as you're performing this surgery.
So, you can have this documented procedure pulled up in front of you as you're working.
There's no need to instruct the Google Glass to do it for you.
Yeah, that's pretty wild.
I can imagine calibrating some kind of augmented reality thing to the body that is there.
And you could almost see, like, the organs and stuff overlaid.
Yeah, absolutely.
Some really interesting new uses there.
Very cool.
What about things like the Google self-driving cars?
What role does image recognition and computer vision work there versus, say, GPS versus laser versus whatever else?
Do you know?
That remains to be seen.
I guess I am a little bit of a pessimist when it comes to computer vision being used for driving cars.
You maybe know too much about the little problems you're into, right?
Well, that's the thing is I don't believe computer vision itself will ever be adequate, again, by itself, for self-driving cars.
I think you need a lot more sensors.
I think you do need things like radar to help you out with that and help detect objects in front of you.
And that's a problem computer vision does face is when you take a photo of something, it is a 2D representation of a 3D world.
So, it makes it very hard to compute depth based off of that.
And now we have things like the Xbox 360 Kinect and stereo cameras where we can compute depth, which is making things like the self-driving cars more feasible.
But, again, for something like a self-driving car, it doesn't make sense to rely strictly on computer vision.
I think you want to incorporate as many sensors as you possibly can.
Yeah, I think that makes a lot of sense.
And it might make sense to actually have the road have things like RFID style stuff in it where the car can be sure it's between the lanes, at least on the major long part of the drives.
Oh, for sure.
And I think that's kind of the point I will say is just because you can use computer vision to solve something doesn't necessarily mean that you should.
There might be better solutions out there.
Yeah, sure.
You don't want to be the solution in search of a problem.
You want to be the solution to a problem.
Yeah, yeah.
There's a really good show I'd like to recommend to people, by the way, about this whole computers, driving cars, and that.
Nova did a show called The Great Robot Race.
And you can just Google The Great Robot Race or whatever.
And it's all about this DARPA competition that kind of preceded the whole Google self-driving cars and so on.
And it's like a two-hour really sort of technical documentary on, like, the problems these teams technically faced and stuff.
It was very cool.
So if you want to learn more, check that out.
Nice.
Yeah.
It's a really good sort of conceptual idea of what's going on.
But, you know, what do I do?
Like, what kind of code do I write as a Python developer to get started?
I mean, it's tough to talk about code on audio only.
So don't do too much.
But just, like, can you give me a sense of what kind of code I would write to maybe grab an image and ID something in it?
Yeah.
So let's take a fun example.
If you've ever used your smartphone to scan a document, you've basically taken, you've, like, set a piece of paper on your desk, held your phone above it, and snapped a photo.
And then the document's already scanned and stored as an image on your phone.
And you could email it to yourself or text it to someone.
What's really cool is that while those programs and those applications almost seem like magic, they're really, really simple to build.
So if I were to build a mobile document scanner, I would basically say, first up, capture the image.
So I'm going to read it from disk.
I'm going to read it from a camera sensor.
Nice.
I'm going to convert it to grayscale because color doesn't really matter.
I'm going to assume there's enough contrast between a document and a desk that I'll be able to detect it.
And I'm probably going to blur it, get rid of any type of high-frequency noise, just allowing me to focus more on the structural objects inside the image and less on the detail.
From there, I'll say, yeah, let's perform edge detection.
Let's find all the edges in the image.
And based on the outlines of the objects in the image, I'm going to take these, and I'm going to look for a rectangle.
And if you consider the geometry of a rectangle, it just has four points, four vertices.
So I'm going to loop over the largest regions in this image, and I'm going to find the largest one that has four vertices.
And I'm going to assume that's my document.
And then once I have the region of the image, I'm going to perform a perspective transform to give me this top-down, bird's-eye view of the document.
And from there, you've basically built yourself a document scanner.
You can apply OCR, optical character recognition to try and convert the text in the image to a string in Python.
Or you could just save the scanned document as a raw image.
That works, too.
It's really actually not a complicated system to build.
How do you deal with the slight imperfections of reality, like a crumpled receipt or document or something like that?
So to handle, as you suggested, like these slight imperfections, I would suggest for the document scanner example to do what's called contour approximation.
So, again, if a region of an image can be defined as a set of vertices, and due to the crumpling of the piece of paper, maybe I find a region of an image that has eight vertices or 12 vertices.
It's not a perfect rectangle.
It's kind of jagged in some places.
Well, what I can actually do is I can approximate that contour.
And I could basically do line splitting and try and reduce the number of points to form that contour.
And that's actually a very critical step in building a mobile document scanner because you're not going to find perfect rectangles in the real world just due to noise capturing the photo.
Even perspective, right?
Yeah, even perspective.
That can dramatically distort things.
Are there libraries out there that help you with that kind of stuff, or is that where you need to know some math?
You don't really need to know that much math at all for it.
It's all about gluing the right functions together at the right time.
And I think that's one of the harder points of learning computer vision and why I run the PyMid Search blogs.
I want to show people real-world solutions to problems using computer vision.
And when I do that, it's not really the actual code that matters.
It's learning why I'm applying certain functions at different times in the context of the end goal.
It's about taking these functions and gluing them together in a way that gives you a real solution.
Okay, that makes sense.
Sounds a little bit like the code variation of I open up Photoshop and there's a thousand things it does.
And individually, they're all simple, but I don't know how to go from a bad picture to a great picture with all of my little pieces, right?
It's kind of like that with the code libraries for image stuff, right?
For sure.
There's a couple of really cool apps that are coming to mind when you're talking about that.
One is called WordLens.
Do you know about WordLens?
Yeah, that's the one where you have your iPhone or your smartphone and you can hold it over a foreign language and it automatically translates it for you.
Is that right?
Yeah, that's right.
And it will basically replace a menu or a street sign with whatever language you want, English in my case, right?
That seems like a really cool use as well.
Another one that I liked is this thing called Peak.AR.
And if you're out hiking in the mountains, you can hold it up and it'll like highlight the various mountaintop names and how high they are and how far away they are using computer vision.
Very nice.
Yeah, that's pretty cool.
Let's talk about your blog and your book a little bit.
Like what kind of stuff do you have on there for people to go learn?
You have like a short course people can sign up for and I saw you also have like a video course that they could take as well.
Is that right?
So it's not a video course.
I have two small mini courses on the PyImageSearch blog.
One is like a 21-day crash course on building image search engines, which is my area of expertise within computer vision.
We're all familiar with going to Google and typing in our query and finding results.
And that's essentially what image search engines are.
Only instead of using text as our query, we're using images.
We're analyzing the image and then searching databases for images with similar visual content.
And again, this is without any knowledge of text associated with the image.
Right.
That's really cool.
You know, I think I first saw that with Google image search where I could take a picture of like the Sydney Bridge and it would say, hey, that's the Sydney Bridge.
But it seems like there might be more practical uses for that.
Oh, there definitely are.
I've used image search at sea in the National Cancer Institute, actually.
I worked as a consultant there for about six months and we developed methods to automatically analyze breast histology images for cancer risk factors.
So we would actually develop algorithms that would go in and automatically analyze these cellular structures.
And then based off of that, be able to kind of predict that person's cancer risk factor five years from now, 10 years from now, 15 years from now.
But you could also apply image search to that and say, you know, here's an example of these cellular structures.
Go and find the other cellular structures in this image data set that look like that.
And then you can compare them, compare their cancer risk factors and look for correlations.
So there's image search is a wonderful, wonderful field.
I personally love it.
Then again, I'm biased.
So there's an incredible number of uses for it.
And it's not all consumer facing.
It's also, you know, on the back end, on the business facing, on government and all other sectors.
Yeah, that's cool.
One company I saw, I can't remember where I saw them, but they were basically starting a company that would help you identify parts of like machines and cars and stuff.
And you're like, I have a part.
I know it's broken.
I have no idea what it is.
Take a picture of it.
And they'll say, oh, this is, you know, widget X and it costs $32.
Here's how you order it.
Oh, cool.
Yeah.
So, you know, it doesn't help people quite as much, but still very, very cool.
Tell us about your book.
What's the title?
So I have a book called Practical Python and OpenCV.
And it's just a really quick, really quick kickstart guide to like get you through learning the basics of computer vision and image processing.
It starts off with very rudimentary things like here's how you load an image off of a disk.
And here's how you detect edges in an image.
And then it goes on to show you how to detect faces and images in video streams and how to track objects in images and how to recognize handwritten characters.
So it really takes you from assuming you have very basic programming experience and no prior experience in computer vision all the way to, hey, look, I'm solving some real world problems using computer vision.
And again, it's a quick read and you can learn a ton about computer vision really quickly.
And I would also love to offer a discount to talk Python listeners if they want to go to pyimagesearch.com slash talk Python.
You can get 20% off of any of the books that I offer on that page.
And I'll keep that open for the next two weeks.
That's awesome.
Thanks for doing that.
Oh, thank you.
Thank you so much for having this opportunity.
This is great.
Yeah, so people should check out your book.
That's really cool.
Is it used for like any university classes or is it more not so academic, a little more like industry focused?
It is much more practical and hands on.
So it's not used that much in college level classrooms.
However, I have seen it used in Amsterdam and a few universities in the United States as well.
Yeah, that's really cool.
It's got to make you feel good.
It certainly does.
I'm happy that I can contribute and give back to a field that I love so much.
Yeah, that's awesome.
A lot of people build web apps and web apps don't have attached cameras.
Well, I guess you could get one from the browser, right?
You could do something with that if you could capture the people's browser.
But what are some like web scenarios rather than say iPhone or Raspberry Pi type scenarios for computer vision?
Let's say that we're going to build, you know, like a Twitter or Facebook style application
where a user has a profile picture.
And we're going to build the functionality to upload a profile picture and then set it.
Well, there's a naive way of doing it where you just kind of upload a profile picture
and maybe you ask the user to crop the image or maybe you just automatically assume that the user's face or their profile is directly in the center.
So you just automatically resize the image and crop it.
Those are viable solutions and there's nothing wrong with them.
But we could also use computer vision to create a much more fluid user experience.
So the user can upload a profile picture.
And in the background, we're running these computer vision algorithms.
So we can analyze the image and find the face and automatically crop that face from the image and set it as the user's profile picture.
And again, the goal here is making a much more seamless experience.
That's really cool.
I think, you know, people's expectations of consumer focused and even B2B type apps to some degree, you know, things like Slack and so on,
are there definitely have gone up in the last three or four years.
And, you know, I blame the iPhone, right?
We can no longer build just ugly, crummy apps that users are forced to use, right?
They expect a cool experience.
And something like that is just, you know, really nice touch.
Yeah, for sure.
And from a business perspective, I mean, making a good impression on your users is crucial.
So you want them to have this really, really great experience when using your app, even if it is something as simple as a profile picture.
I mean, things like those do matter from a user experience level.
Right.
If somebody goes, oh, my gosh, you wouldn't believe what happened.
I uploaded this and it made the best profile picture ever, right?
That could possibly be something, a little bit of word of mouth marketing.
That'd be cool.
Yeah.
And then getting back to the Facebook example and the Twitter example, if you've uploaded a picture to Facebook recently,
you've probably noticed that it can not only detect your face or other people's faces and images,
but it can recognize whose face that belongs to.
So these images are being automatically tagged for you.
And maybe that's creepy.
Maybe that's not, you know, kind of beside the point.
But again, it's these computer vision algorithms running behind the scenes.
Right.
That's really fantastic.
Have you played with Google Auto Awesome from Google Plus?
No, I haven't.
Oh, my gosh.
There's amazing stuff that happens.
I don't do much with Google Plus.
I mean, there's some people on it and I check it every now and then and it's okay and so on.
But, you know, Twitter is really my place.
But for images, Google Plus is crazy.
So it'll do things like take a picture.
If you have like three pictures of a group and in one picture, there's somebody frowning and there's another picture.
Someone else has their eyes closed.
But in the other picture, the closed eyes persons were open.
The other, you know, if you could combine them and they would all like look good, right?
You could take out the bad faces and put the good faces in from the other pictures.
And it'll do that automatically for you.
Wow.
So it'll like take a set of group pictures and make the best possible like unified group picture of all the different faces.
That's pretty crazy.
That is.
And other things like that as well, like focusing on faces and so on.
So I'm sure there's some really cool computer vision stuff going on there.
Yeah, I'll definitely have to check that out.
Yeah, you can just like turn, install Google Plus, turn on your iPhone and just tell it to auto backup.
And then like you'll get these notifications every now and then.
You have these crazy pictures.
Nice.
Yeah.
So suppose I'm new.
I'm pretty new to computer vision.
How do I get started?
So if you want to get started, the first thing I would suggest doing is installing OpenCV.
I mean, that's a pretty important prerequisite.
Another library that you might want to look into that is pip installable is Scikit Image.
And this library, I love OpenCV, but Scikit Image releases updates a lot, lot faster.
It's a smaller library, but it's very good at what it does.
And in my opinion, it stays a little bit closer to what the state of the art is in computer vision.
So if an academic paper is published and they implement some new algorithm, Scikit Image is much more likely to have that implementation than OpenCV will, simply because it's a smaller community.
But it's a faster moving community.
So they can release updates faster.
Okay.
Yeah, that sounds really cool.
And do I need hardware?
Do I need a Raspberry Pi and a camera kit or can I just use my webcam?
You know, you can use pretty much whatever you want.
I recommend just starting out with your laptop or your desktop system.
There's no reason that you need any special hardware.
And realistically, you don't even need a webcam unless you plan on processing video, real-time video.
And in the meantime, if you do, you could still go ahead and get started.
You could just work with pre-recorded videos.
Maybe you recorded from years ago from files you have lying around on your system.
There's no reason why you can't get started.
And I think that's what's really cool about computer vision is, you know, back in high school, I had this impression that computer vision and image processing was just magic going on behind the scenes.
But it's really not, and it is possible to learn these algorithms and understand them without having a degree in computer science or a focus in machine learning or, you know, tons of understanding of high-level mathematics.
Yeah, very cool.
Especially with things like OpenCV where you don't have to understand, like, the video encoding format and all that kind of stuff, right?
Yeah, that's taken care of for you behind the scenes.
All right, well, maybe you could tell everyone a few of your favorite packages or libraries out there that you like to use.
You know, everyone has their own flavor and things they've discovered and problems they're solving.
So, you got any favorites?
Yeah, so I mentioned two of my favorites already.
The first one is Scikit-learn for machine learning.
The other is Scikit-image for computer vision.
OpenCV is great, but again, not pip installable.
If you do decide to play around with computer vision, I would suggest that you check out the imutils package that I created.
It's basically a set of basic functions for image manipulations, such as resizing, rotation, translations, that can take a little bit of code to do in OpenCV, and then it just reduces it to a single function call inside the imutils package.
Yeah, that's great.
I'll put that in the show notes.
Yeah, it's definitely worth taking a look at.
The other fun one that I like to play around with doesn't have a ton of real-world application, but it's called Color Transfer.
And the idea is that, let's say you went to the beach at 1 p.m. in the afternoon, and you took this really great picture, but you wanted to make it look like it was taken at sunset.
So you would have this beautiful orange glow of the sun over top of the ocean.
Maybe you could create that effect in Photoshop, or you could just have an example image of a sunset and then algorithmically take the color space from the sunset image and then transfer it to the image you took.
So if you want to play around with that, that's in the color transfer package on PyPy.
Oh, that sounds really interesting.
Yeah, it's a lot of fun.
Not exactly the most real-world applicable package, but it's fun nonetheless.
Yeah, it sounds really fun.
Okay, any final shout-outs or things you want to tell the listeners?
Yeah, if you want to learn more about computer vision, definitely head over to pyimagesearch.com.
Check out the blog posts I have.
I have, at the time of this recording, at least 80 free blog posts that discuss computer vision and solving real-world problems.
I have two courses, mini courses that you can take and learn about computer vision.
And again, I have my book, Practical Python, an open CV that you can purchase and read through and learn computer vision and get up to speed really quickly.
And again, I'm offering that 20% off to all Talk Python podcast listeners at pyimagesearch.com slash Talk Python.
All right, that's really interesting.
I appreciate the offer.
And I think everyone out there should go and check out your blog.
You have some tutorials you can go look at, and they're really short.
And they both walk you through the code as well as maybe setting this up on a Raspberry Pi with a camera board and all those kinds of things.
So I found it really helpful.
And hopefully, if anyone wants to try it out, I encourage you to do so.
Cool.
Thank you so much for having me on the show.
This was a great experience.
Yeah, Adrian, it was great to talk to you.
And I'd love to bring a different perspective on the cool stuff you can do with Python.
So here's one more awesome thing that people can do if they want to try it, right?
Yep, for sure.
All right.
Thanks for being here.
Talk to you later.
See you.
Bye.
This has been another episode of Talk Python to Me.
Today's guest was Adrian Rosebrock.
And this episode has been sponsored by Codeship.
Please check them out at Codeship.com and thank them on Twitter via at Codeship.
Don't forget the discount code for listeners.
It's easy.
Talk Python.
All caps, no spaces.
Remember, you can find the links from the show at Talk Python To Me.com slash episodes slash show slash 11.
And if you want to support the show, please check out our Patreon campaign at Patreon.com slash mkennedy,
where you can contribute as little as $1 per episode.
Be sure to subscribe to the show.
Visit the website and choose subscribe in iTunes or grab the episode RSS feed and drop it into your favorite podcatcher.
You'll find both at the footer of every page.
This is your host, Michael Kennedy.
Thanks for listening.
Smix, take us out of here.
Stating with my voice.
There's no norm that I can feel within.
Haven't been sleeping.
I've been using lots of rest.
I'll pass the mic back to who rocked it best.