1 00:00:00,000 --> 00:00:03,065 [No audio] 2 00:00:03,090 --> 00:00:07,140 Consider this gray scale image. Now this 3 00:00:07,170 --> 00:00:09,810 image is made of pixels, and this particular 4 00:00:09,810 --> 00:00:13,080 one has 3 by 5, so it has 15 5 00:00:13,080 --> 00:00:16,620 pixels. So it's quite a small image. Now 6 00:00:16,650 --> 00:00:19,650 each pixel has a value, and that's what 7 00:00:19,650 --> 00:00:22,440 defines the intensity of the gray color 8 00:00:22,620 --> 00:00:26,130 for each pixel. So in reality, these are 9 00:00:26,130 --> 00:00:29,190 numbers, but our computer display 10 00:00:29,220 --> 00:00:32,370 shows them in a color format, which is 11 00:00:32,370 --> 00:00:35,340 easy, readable by us humans. What I'm 12 00:00:35,340 --> 00:00:38,070 trying to get is that programs use 13 00:00:38,070 --> 00:00:41,460 numbers to store images, that the computer 14 00:00:41,460 --> 00:00:44,850 display, or so all the screen converts these 15 00:00:44,850 --> 00:00:48,030 numbers to colors. Python can also do 16 00:00:48,065 --> 00:00:51,485 image processing, just like photoshop 17 00:00:51,510 --> 00:00:54,360 does. Probably it cannot do all the cool 18 00:00:54,360 --> 00:00:56,250 stuff that you can do with Photoshop. 19 00:00:56,640 --> 00:00:58,860 But you can make use of Python to 20 00:00:58,860 --> 00:01:01,770 automate things. For instance, we will be 21 00:01:01,770 --> 00:01:03,600 using the image processing capabilities 22 00:01:03,600 --> 00:01:07,350 of Python to detect faces from photos, 23 00:01:07,980 --> 00:01:10,950 from images, and also detect moving 24 00:01:10,950 --> 00:01:13,620 objects in videos as well. So videos are 25 00:01:13,620 --> 00:01:15,900 made of images. So it's the same thing 26 00:01:15,925 --> 00:01:19,885 basically, and Python stores and erase images 27 00:01:20,370 --> 00:01:23,250 using arrays of numbers. For instance, 28 00:01:23,280 --> 00:01:27,840 this image could be represented as you 29 00:01:27,840 --> 00:01:31,721 know, a list of three other lists. 30 00:01:31,745 --> 00:01:33,975 [No audio] 31 00:01:34,000 --> 00:01:37,230 So three lists because we have three rows 32 00:01:37,230 --> 00:01:40,110 there with pixels, and then in each of 33 00:01:40,110 --> 00:01:43,320 the lists, you'd have like five numbers, 34 00:01:44,345 --> 00:01:48,095 and so on, and so on, and the fifth, and 35 00:01:48,120 --> 00:01:51,120 the same for the other two lists, we've 36 00:01:51,120 --> 00:01:53,220 got five numbers, because we have five 37 00:01:53,760 --> 00:01:56,940 columns, so five pixels for each row. 38 00:01:57,270 --> 00:02:00,090 And that's an image for Python, and here 39 00:02:00,090 --> 00:02:04,890 is where NumPy comes in handy. So while 40 00:02:04,890 --> 00:02:07,500 you can represent images with the lists, as 41 00:02:07,500 --> 00:02:11,280 we did here, this is not very efficient, 42 00:02:11,280 --> 00:02:14,970 because for big images, all these occupy 43 00:02:14,970 --> 00:02:17,850 lots of memory, and therefore, they slow 44 00:02:17,850 --> 00:02:20,910 down operations on them. So this is sold 45 00:02:20,940 --> 00:02:23,730 by NumPy, which is a library of Python 46 00:02:23,730 --> 00:02:25,950 library that provides a multi 47 00:02:25,950 --> 00:02:29,010 dimensional array object. So let me go 48 00:02:29,010 --> 00:02:31,410 ahead and create this array object. 49 00:02:31,434 --> 00:02:34,115 [No audio] 50 00:02:34,140 --> 00:02:36,000 First of all, what you need to do is 51 00:02:36,000 --> 00:02:38,820 you need to import numpy, and if you 52 00:02:38,820 --> 00:02:41,910 haven installed Pandas, NumPy should have 53 00:02:41,910 --> 00:02:43,530 been installed with Pandas, because 54 00:02:43,530 --> 00:02:47,160 Pandas is based on NumPy. If you haven't 55 00:02:47,160 --> 00:02:49,680 installed NumPy, just yet, just go ahead 56 00:02:49,680 --> 00:02:52,290 and pip install numpy. And if for some 57 00:02:52,290 --> 00:02:53,850 reason you have some problems on 58 00:02:53,850 --> 00:02:56,460 Windows, then just go ahead, and as I've 59 00:02:56,460 --> 00:02:58,950 showed you and find the pre-compiled Python 60 00:02:58,950 --> 00:03:01,050 libraries, go to this side and then 61 00:03:01,050 --> 00:03:05,040 search for NumPy, and figure out if 62 00:03:05,040 --> 00:03:07,590 you're on a 3.5 version of Python that 63 00:03:07,590 --> 00:03:09,720 just get that version, and then you 64 00:03:09,720 --> 00:03:12,480 point to this file with pip install and 65 00:03:12,480 --> 00:03:16,830 the name of the file. Great. Now I have 66 00:03:16,855 --> 00:03:20,460 NumPy installed, so let's create this multi 67 00:03:20,460 --> 00:03:24,300 dimensional object and store it in n 68 00:03:24,300 --> 00:03:28,621 variable. That would be numpy.arange, 69 00:03:29,612 --> 00:03:34,295 and let's say 27, execute that, and 70 00:03:34,320 --> 00:03:39,210 print out, and so this is a numpy array. 71 00:03:39,300 --> 00:03:41,730 That's how it is called, and this 72 00:03:41,730 --> 00:03:44,640 particular one is not exactly a multi 73 00:03:44,640 --> 00:03:47,460 dimensional array, because it only has 74 00:03:47,460 --> 00:03:50,430 one dimension. So it's a plain, it's 75 00:03:50,430 --> 00:03:52,770 like a plain list, a python list. But 76 00:03:52,795 --> 00:03:57,096 still it's not exactly a list, 77 00:03:58,740 --> 00:04:03,150 type, sorry. So it's a numpy n 78 00:04:03,210 --> 00:04:06,630 dimensional array. It can have 1 79 00:04:06,869 --> 00:04:10,739 dimensional, 2, or 3. So we have 1, 2, 3 80 00:04:10,908 --> 00:04:14,255 and I'll show whole these scenarios. So that was 81 00:04:14,280 --> 00:04:16,710 the 1 dimensional array, and if you 82 00:04:16,710 --> 00:04:21,480 want to print it in a nice form, and that'd 83 00:04:21,505 --> 00:04:23,732 be the array. Now 84 00:04:23,756 --> 00:04:25,756 [No audio] 85 00:04:25,781 --> 00:04:27,330 I'm just creating a 86 00:04:27,330 --> 00:04:30,540 NumPy array using numbers on the fly 87 00:04:30,540 --> 00:04:34,110 here. But normally you'd have to create 88 00:04:34,110 --> 00:04:36,630 arrays from images, and we'll do that in 89 00:04:36,630 --> 00:04:38,700 just a bit. So for now, let's create 90 00:04:38,700 --> 00:04:41,790 some arrays manually. Now let's see what 91 00:04:41,991 --> 00:04:46,341 2 dimensional arrays, reshape 3 92 00:04:46,380 --> 00:04:48,930 by 9. So we already have this 1 93 00:04:48,930 --> 00:04:51,030 dimensional array and we want to convert 94 00:04:51,030 --> 00:04:54,060 it to a 2 dimensional array. If you 95 00:04:54,060 --> 00:04:56,190 execute that, you get a 2 dimensional 96 00:04:56,190 --> 00:04:58,410 array. So that's a 2 dimensional array 97 00:04:58,410 --> 00:05:00,600 because it has 2 dimensions. So think 98 00:05:00,600 --> 00:05:05,160 of it as this image file, we have 2 99 00:05:05,160 --> 00:05:07,830 dimensions, vertical and horizontal. 100 00:05:08,753 --> 00:05:11,033 Now, what's a 3 dimensional array? 101 00:05:11,760 --> 00:05:14,070 Even though 3 dimensional arrays are 102 00:05:14,070 --> 00:05:16,380 less frequently used, it's still good to 103 00:05:16,380 --> 00:05:19,260 know about them. Anyhow, let me create a 104 00:05:19,260 --> 00:05:21,090 3 dimensional array. I could say a 105 00:05:21,090 --> 00:05:24,030 3 by 3 by 3, because you 106 00:05:24,030 --> 00:05:26,790 know, you have 27 elements, so 3 by 107 00:05:26,790 --> 00:05:31,710 3 by 3 gives you 27, and yep, that's a 108 00:05:31,710 --> 00:05:33,840 3 dimensional array. Think of that 109 00:05:33,840 --> 00:05:36,270 as a cube that has 3 dimensions. So 110 00:05:36,270 --> 00:05:38,100 3 by 3 by 3, and 111 00:05:38,100 --> 00:05:39,750 practically, you'll see that in just a 112 00:05:39,750 --> 00:05:42,660 bit in this lecture, where do we deal 113 00:05:42,690 --> 00:05:45,690 with 3 dimensional array. So bear 114 00:05:45,690 --> 00:05:48,780 with me, and yeah, you are able to see 115 00:05:48,780 --> 00:05:51,930 the similarities between a NumPy array 116 00:05:51,960 --> 00:05:55,800 and plain python list of lists. So this 117 00:05:55,800 --> 00:05:58,710 array here would be like a 2 dimensional 118 00:05:58,740 --> 00:06:00,720 array, or if you like to call it like 119 00:06:00,720 --> 00:06:04,530 that. Either the structure in the middle level 120 00:06:04,530 --> 00:06:07,020 is different between Python lists and 121 00:06:07,020 --> 00:06:09,330 NumPy arrays, and also NumPy arrays 122 00:06:09,330 --> 00:06:12,120 allow you to make some more efficient 123 00:06:12,630 --> 00:06:14,885 operations such as iteration between the 124 00:06:14,910 --> 00:06:18,120 array items, and so on, and you can also 125 00:06:18,120 --> 00:06:20,520 create NumPy array out of Python lists. 126 00:06:20,601 --> 00:06:23,220 For instance, I'll get this list here, 127 00:06:23,676 --> 00:06:29,045 and I'll create a new object and then points to 128 00:06:29,070 --> 00:06:32,340 NumPy, and to convert all atleast to a 129 00:06:32,340 --> 00:06:36,690 NumPy array, you'd want to use asarray 130 00:06:37,355 --> 00:06:40,685 methods and then between the brackets goes 131 00:06:40,710 --> 00:06:42,420 the object that you want to convert. 132 00:06:42,444 --> 00:06:44,444 [No audio] 133 00:06:44,471 --> 00:06:49,470 n and that's an array, which is almost exactly 134 00:06:49,470 --> 00:06:53,610 like this one in here, and if you print 135 00:06:53,610 --> 00:06:56,188 that you would be able to see the difference. 136 00:06:56,431 --> 00:06:59,010 You know, they look exactly 137 00:06:59,010 --> 00:07:01,770 the same. But they are not because this 138 00:07:01,770 --> 00:07:04,800 is a list and this is a NumPy. 139 00:07:05,250 --> 00:07:09,210 Alright, great. Let's move on. So that's 140 00:07:09,210 --> 00:07:11,700 NumPy, and as I mentioned earlier, a NumPy 141 00:07:11,730 --> 00:07:14,370 is a base library for all the libraries, 142 00:07:14,880 --> 00:07:18,330 such as Pandas, and also OpenCV, which 143 00:07:18,330 --> 00:07:20,633 is an image processing library. So 144 00:07:20,658 --> 00:07:23,460 Pandas DataFrames are based on NumPy 145 00:07:23,460 --> 00:07:26,040 arrays, and OpenCV objects are based on 146 00:07:26,040 --> 00:07:28,230 NumPy arrays. So Pandas, what Pandas 147 00:07:28,230 --> 00:07:31,920 does is, it just add some cool features in 148 00:07:31,920 --> 00:07:35,100 there, and such as it adds, it gives 149 00:07:35,100 --> 00:07:37,350 capabilities for having table headers, 150 00:07:37,650 --> 00:07:40,860 and indexes, which you can have in 151 00:07:40,860 --> 00:07:43,620 NumPy, because NumPy is meant to be a 152 00:07:43,620 --> 00:07:47,105 more simple and in a more low level of 153 00:07:47,130 --> 00:07:51,330 storing objects and doing operations. So 154 00:07:51,330 --> 00:07:53,250 NumPy is a requirement for many 155 00:07:53,250 --> 00:07:55,410 libraries, and yeah, let's stop this 156 00:07:55,410 --> 00:07:57,030 lecture in here, and in the next 157 00:07:57,030 --> 00:07:59,040 lecture, we will go straight ahead and 158 00:07:59,070 --> 00:08:02,940 create a NumPy array out of our image in 159 00:08:02,940 --> 00:08:07,146 here. So this image, we're going to see you there.