1
00:00:00,390 --> 00:00:04,800
In this lecture, you will learn how to detect human faces.

2
00:00:05,820 --> 00:00:14,910
Specifically, we will write a script which will draw a rectangle to each of these faces in this picture.

3
00:00:15,150 --> 00:00:23,340
So the output will be an image of this image, but with those rectangles overlaid on top to do this,

4
00:00:23,340 --> 00:00:24,720
you need two things.

5
00:00:24,900 --> 00:00:32,460
One is an image file, which has some humans inside with faces which the scripts could detect.

6
00:00:32,790 --> 00:00:40,890
And also, you need this higher cascade file, which is an example format, so you can download these

7
00:00:40,890 --> 00:00:42,750
two files from the luxury sources.

8
00:00:42,960 --> 00:00:52,950
If you don't have them, a whole Cascades file is basically a set of instructions of how faces human

9
00:00:52,950 --> 00:00:54,270
faces to look like.

10
00:00:54,510 --> 00:01:05,129
So Python will use this blueprint of this phase blueprint, and it will use it to search for faces in

11
00:01:05,340 --> 00:01:06,150
an image.

12
00:01:06,480 --> 00:01:08,370
So let's write this script.

13
00:01:08,670 --> 00:01:11,070
We need open service, so see two.

14
00:01:11,580 --> 00:01:17,750
So the first thing we might want to do is loads an image with CV to dots.

15
00:01:17,760 --> 00:01:25,620
I am read humans that JPEG, and so let's load it in color.

16
00:01:25,980 --> 00:01:29,670
Then next, what we want to load is this XML file.

17
00:01:30,060 --> 00:01:35,550
So to do that, you could say something like face cascades.

18
00:01:36,030 --> 00:01:43,020
So it's called the Cascades hard cascade that is equal to see two dots cascades.

19
00:01:45,900 --> 00:01:56,280
Classifier, which gets as input the path to the XML file, so we have an image, we have a face cascades,

20
00:01:56,550 --> 00:02:01,170
what can we do with these two is detect faces.

21
00:02:02,890 --> 00:02:14,950
Using face cascades that detect multi skill, which means that you want to detect faces in different

22
00:02:14,950 --> 00:02:17,710
scales so you could have a small face.

23
00:02:17,890 --> 00:02:20,020
Here you have a big face here.

24
00:02:21,400 --> 00:02:30,640
You know, depending on the depth of the photo and you want to detect everything from that image object,

25
00:02:30,880 --> 00:02:33,820
which we have in here, so image image?

26
00:02:35,710 --> 00:02:38,440
No, it's good to have two other arguments here.

27
00:02:39,570 --> 00:02:48,870
The first one after the image is the scale factor, which usually it's good to have something like one

28
00:02:48,870 --> 00:02:56,170
zero five or one, one of the next factor is the minimum neighbors.

29
00:02:56,490 --> 00:03:00,390
So what is the skill factor and what is minimum neighbors?

30
00:03:00,780 --> 00:03:07,050
Well, what happens when Python detects faces is that its image has pixels.

31
00:03:07,050 --> 00:03:11,760
So the first pixel, the second, the third and so on.

32
00:03:12,000 --> 00:03:15,780
And basically, the program will go through each pixel.

33
00:03:16,110 --> 00:03:21,950
But some phases could be bigger, as I said, and some phases could be smaller.

34
00:03:21,960 --> 00:03:27,740
And so the detection depends on the scale of the image.

35
00:03:27,760 --> 00:03:36,750
So if the algorithm is looking at certain scales at certain zoom, so to see and it has in mind how

36
00:03:36,840 --> 00:03:44,280
a face looks like, if that's Zoom, it could not find one because maybe the face in that level of zooming

37
00:03:44,280 --> 00:03:45,540
is bigger or smaller.

38
00:03:46,110 --> 00:03:52,890
Therefore, the algorithm has to check and check for image multiple times of different scales.

39
00:03:53,760 --> 00:04:01,470
So with this number here, basically you are seeing how much to increase the scale between different

40
00:04:01,470 --> 00:04:03,690
skill checks or the zoom.

41
00:04:04,050 --> 00:04:10,590
Let's say you want to start with a zoom of X, and the algorithm will check for faces there, and then

42
00:04:10,590 --> 00:04:17,130
it will zoom in with a factor of 1.1, so x times 1.1 and so one.

43
00:04:17,160 --> 00:04:23,270
So basically, we have a pyramid of the same images, but in different scales, and then we have four.

44
00:04:23,340 --> 00:04:29,970
Now four is a minimum neighbors, which means there are interpolation between pixels.

45
00:04:30,330 --> 00:04:38,310
So if Python is checking this pixel, for example, a two also consider the neighboring pixels around

46
00:04:38,310 --> 00:04:38,520
it.

47
00:04:38,670 --> 00:04:45,210
So with four, we are saying we're telling Python to consider four pixels around and to note, for example,

48
00:04:45,210 --> 00:04:53,310
more pixels, which should be like here and maybe here and so on relative to that pixel.

49
00:04:53,670 --> 00:04:59,880
So that's more or less that these are the popular numbers to use that have been tested to work well.

50
00:05:00,300 --> 00:05:03,630
But you can try with different numbers and see the results.

51
00:05:04,050 --> 00:05:05,820
Now what is phases?

52
00:05:05,850 --> 00:05:09,660
Let's bring it out and see what we have this far.

53
00:05:14,630 --> 00:05:20,900
So that is the face is variable, it's a list of.

54
00:05:21,910 --> 00:05:25,380
Lists, so one list here, another year.

55
00:05:25,600 --> 00:05:26,920
And so one.

56
00:05:27,880 --> 00:05:31,360
So basically a Nampai array, a 2D Ray.

57
00:05:31,820 --> 00:05:38,230
This contains the coordinates over rectangles, which has faces inside.

58
00:05:38,470 --> 00:05:47,320
So basically, we have one two three four two faces in total, which makes sense because there's a lot

59
00:05:47,320 --> 00:05:49,280
of people in this photo.

60
00:05:49,300 --> 00:05:50,920
So twenty two faces.

61
00:05:51,490 --> 00:06:00,670
And for example, these are the coordinates the pixel coordinates in the image of the rectangle to be

62
00:06:00,700 --> 00:06:07,240
thrown around a face around the first phase and then another face and so on.

63
00:06:08,380 --> 00:06:15,340
What we could do now is to iterate through that number early using a table.

64
00:06:15,730 --> 00:06:19,510
So x y width and height.

65
00:06:20,110 --> 00:06:25,900
What are these in phases, right?

66
00:06:26,440 --> 00:06:28,150
So the typical here.

67
00:06:29,160 --> 00:06:33,330
This table will get each of these lists.

68
00:06:33,990 --> 00:06:41,490
So basically, X will be replaced with nine o one in the first iteration, Y will be replaced two one

69
00:06:41,490 --> 00:06:46,920
eight five W with 60 and H with 60 and so on.

70
00:06:47,670 --> 00:06:57,300
So if we had an image like this and then we would have nine oh, one one eight five, that'd mean something

71
00:06:57,300 --> 00:07:02,790
like that nine hundred to one column, which would be somewhere, he writes.

72
00:07:03,830 --> 00:07:09,230
And the 185 rule, which is somewhere here.

73
00:07:09,260 --> 00:07:19,340
So this one here, this points here would be the start of the rectangle and then we would go 60 pixels

74
00:07:19,340 --> 00:07:21,860
to the right and 60 down.

75
00:07:22,250 --> 00:07:29,180
So we would have something like a square right, 60 here, 60 there.

76
00:07:29,300 --> 00:07:30,710
That would be a rectangle.

77
00:07:30,710 --> 00:07:35,510
And inside here it's supposed to be a face.

78
00:07:37,390 --> 00:07:44,170
So that's the idea we are capturing those numbers from each of these lists, and for each number, we

79
00:07:44,170 --> 00:07:53,260
will draw a rectangle using the Siri 2.0 rectangle methods, which gets the image object as first inputs.

80
00:07:53,650 --> 00:07:55,510
So image this one in here.

81
00:07:55,660 --> 00:08:00,460
We will draw those rectangles in that image and then a second argument.

82
00:08:00,820 --> 00:08:05,530
It will give the coordinates of the start of the rectangle, which is X or Y.

83
00:08:05,920 --> 00:08:15,790
As I explained to the corner, then you have X plus w and Y plus height.

84
00:08:16,060 --> 00:08:16,900
So H.

85
00:08:17,020 --> 00:08:18,430
So what are these?

86
00:08:18,640 --> 00:08:22,840
Well, that is the coordinates of the first corner of the rectangle.

87
00:08:25,210 --> 00:08:25,880
That one.

88
00:08:26,920 --> 00:08:29,410
And then we say X Plus, which.

89
00:08:30,340 --> 00:08:32,380
Which takes us somewhere here.

90
00:08:33,130 --> 00:08:38,919
And then why plus age, so it takes us at this point here.

91
00:08:39,370 --> 00:08:46,990
So basically, we are writing, we are drawing a rectangle using these two points as points to define

92
00:08:46,990 --> 00:08:47,830
that rectangle.

93
00:08:48,400 --> 00:08:50,620
That's what this rectangle methods expects.

94
00:08:51,460 --> 00:08:54,340
The two corners, the coordinates of those two corners.

95
00:08:54,580 --> 00:08:54,940
Right.

96
00:08:55,570 --> 00:08:57,970
It also gets two more arguments.

97
00:08:58,600 --> 00:09:02,080
One is the color if you want to have a rectangle.

98
00:09:02,080 --> 00:09:13,890
So why do you want to give 255 red and to 55 green at 255 blue and then the width, perhaps for some

99
00:09:13,900 --> 00:09:16,450
of the width of the rectangle sides?

100
00:09:17,770 --> 00:09:25,690
Then once you write all the rectangles, so all these rectangles in the image, then you want to say

101
00:09:26,050 --> 00:09:31,420
to the spine or sides and the right that image actually.

102
00:09:31,420 --> 00:09:36,910
First, you need to specify the name of the file of the new image.

103
00:09:38,050 --> 00:09:51,160
So let's say human faces, that's g jpeg and image or image file object, which now has the rectangles

104
00:09:51,610 --> 00:09:51,850
in.

105
00:09:52,480 --> 00:09:55,090
So it's one single image and the run.

106
00:09:57,240 --> 00:10:01,380
And here is the generated image, let's open it.

107
00:10:01,950 --> 00:10:03,150
And this is the ultimate.

108
00:10:03,600 --> 00:10:07,110
So as far as I can see, it has done quite a good job.

109
00:10:07,110 --> 00:10:12,330
Even though this face here is blurred, the script still detected.

110
00:10:12,330 --> 00:10:14,940
It's untrue and drew a rectangle around it.

111
00:10:15,210 --> 00:10:21,720
This one, too, even though it's also sort of a profile, it's not front face.

112
00:10:22,140 --> 00:10:30,630
This one was successful also, even though the person has glasses, and it can also detect people with

113
00:10:30,630 --> 00:10:31,260
hats.

114
00:10:31,920 --> 00:10:38,340
And sometimes the script also thinks that a fist is a face, as you see in here.

115
00:10:38,820 --> 00:10:44,450
So, of course, it's not 100 percent accurate, but it's still doing a great job.

116
00:10:44,460 --> 00:10:49,080
I think this face here is also a half face.

117
00:10:49,350 --> 00:10:53,130
So the script couldn't detect it as a face.

118
00:10:53,640 --> 00:10:59,040
I mean, that's could be good, but it could also be a bad thing for certain scenarios where you don't

119
00:10:59,040 --> 00:11:01,320
want to detect health faces.

120
00:11:01,710 --> 00:11:02,880
So that's the reality.

121
00:11:02,910 --> 00:11:04,860
I hope you enjoy this, and I'll talk to you later.

