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