From the course: Introduction to Deep Learning with OpenCV
Classification for an image: Getting the classes - OpenCV Tutorial
From the course: Introduction to Deep Learning with OpenCV
Classification for an image: Getting the classes
- So let's get started with using opencv's dnn module as an inference engine. Firstly, we need to read in the 1000 classes of the imagenet model. Let's use the file that we created to open an image in opencv as our starting point. So we've imported the numpy in cv2 module, this time, we're going to read an image called typewriter.jpg from our image folder. So I'm going to replace devon with typewriter. The imagenet image database is organized according to the wordnet hierarchy. Each meaningful concept in wordnet, which could be multiple words is called a synonym set or a synset. This 1000 classes are stored in this synset file. So if we open this synset file, we can see the 1000 different categories, and here, each row corresponds to a category and this starts with an id, and then one or more words describing the category. So we have 1000 rows in this file, corresponding to the 1000 classes. So if we head back to the editor, the next thing we want to do is to read in the synset words file and get rid of any character turns. Now this is stored in the models folder, so I'm just going to head to the models folder and I'm going to copy the name of that file. I head back to the text editor, let's start in the folder called model so model/synset_words.txt we want to read that file, we want to strip off any character turns and we want to split it, so let's store that in all_rows. Now if I just want to grab the different descriptions and not the id, we can use a list comprehension. So let's say r, r.find, and we're looking for a space, and we don't want to include the id for r in all_rows. And let's store that in classes. Now, let's make sure that this is working, so let's check the first couple of entries. So for I comma c in enumerate and that's in classes, we want to print just the first couple of entries so if I is four, I want to break, I forgot a colon there. Otherwise, I want to print I comma c. So let's save that and let's run this. So I'm going to head off to the correct directory so cd 04, 01 then python and image.python. So we could see that we've got the image file, but let's just make sure that we can actually see the first couple of classes from the synset file, and we can see that these are the first four classes of the synset file, so everything seems to be working and we're now in a position to actually pass this image through a pre-trained model. So I'm going to just press any key to close this image and next we look at passing the image through a pre-trained model.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.