1
00:00:00,060 --> 00:00:06,510
In this lecture, you will learn how to find images that contain human faces.

2
00:00:07,020 --> 00:00:08,620
So I have this ripple here.

3
00:00:08,670 --> 00:00:18,750
The code is from the previous lecture, so this code detects and throws a rectangle around faces in

4
00:00:18,750 --> 00:00:20,010
one single image.

5
00:00:20,670 --> 00:00:28,200
What we will do now is we will write a script which will go through this input folder, which contains

6
00:00:28,620 --> 00:00:30,210
four images.

7
00:00:30,990 --> 00:00:33,000
This one which don't have faces.

8
00:00:33,840 --> 00:00:38,100
This one also this image, which contains a face.

9
00:00:38,520 --> 00:00:41,400
And this one also which contains two faces.

10
00:00:42,690 --> 00:00:47,400
So two of them contains faces, and our scripts will check this folder.

11
00:00:47,400 --> 00:00:56,610
And then if it finds images with faces in them, it will draw a rectangle in those images around the

12
00:00:56,610 --> 00:01:03,210
face, and it will move those images to the output folder, which is empty.

13
00:01:03,630 --> 00:01:10,760
For now, let's do this since our script is getting bigger now.

14
00:01:11,100 --> 00:01:18,270
I would like to do some quick changes, such as defining cost and values in our script, which is,

15
00:01:18,270 --> 00:01:21,480
for example, the Cascades file path.

16
00:01:22,110 --> 00:01:25,410
And that is faces that example, right?

17
00:01:25,860 --> 00:01:30,510
That is the hard category that we use, and it's located here in my folder now.

18
00:01:30,840 --> 00:01:38,880
The path sort of the script will use that as instructions, as a blueprint to find faces and then what

19
00:01:38,880 --> 00:01:43,470
constants we're going to have else inputs folder, perhaps.

20
00:01:46,720 --> 00:01:50,120
Eagles to input.

21
00:01:50,770 --> 00:01:55,390
So this folder and also what puts.

22
00:01:56,920 --> 00:02:04,570
Folder equals soup output, why I'm writing this in capital letters, you ask.

23
00:02:04,780 --> 00:02:06,760
Well, that's a good practice.

24
00:02:07,480 --> 00:02:08,539
That's a convention.

25
00:02:08,560 --> 00:02:14,680
You don't have to use capital letters, but when you write variables that are constants which are going

26
00:02:14,680 --> 00:02:16,690
to be used in the scripts.

27
00:02:16,930 --> 00:02:20,400
So we don't plan to change these views.

28
00:02:20,410 --> 00:02:25,480
For example, input folder may be used by several instances in our scripts here.

29
00:02:26,020 --> 00:02:28,600
So that's convention the naming.

30
00:02:28,990 --> 00:02:37,210
So in face cascades, now we can just see Cascade and let's see what we can change from this code.

31
00:02:37,540 --> 00:02:40,330
This is finding faces in one single image.

32
00:02:40,630 --> 00:02:47,350
So we want to find faces, so we want to check if a phase is in an image.

33
00:02:47,650 --> 00:02:54,670
Therefore, I think we could start with a function such as has phase right, has phase could be a function

34
00:02:54,670 --> 00:03:07,180
which would receive one single image path, image, file name and return the same image with rectangles

35
00:03:07,180 --> 00:03:10,000
thrown around the faces in that image.

36
00:03:10,390 --> 00:03:17,080
So this function will return of the ends and image rights, and it will the return of the image if the

37
00:03:17,080 --> 00:03:19,720
image has faces inside.

38
00:03:20,200 --> 00:03:24,330
Otherwise, it will return none.

39
00:03:25,120 --> 00:03:29,320
So then down here, then ignore these for now.

40
00:03:29,620 --> 00:03:37,340
Down here, we would do something like images, eagles to always dots list their right.

41
00:03:37,900 --> 00:03:44,650
So 11 imports imports OS and the folder is Inputs folder.

42
00:03:45,550 --> 00:03:55,540
So we create a list of images and then we iterate over those image file names, such as for image in

43
00:03:55,540 --> 00:03:56,290
images.

44
00:03:57,400 --> 00:04:07,720
And then we create a face image object, which is equal to has face to face and image as the file name

45
00:04:08,380 --> 00:04:08,890
correct.

46
00:04:09,730 --> 00:04:12,100
So has face is called in here.

47
00:04:12,310 --> 00:04:16,029
And as I told you, as face, we will write some codes here, of course.

48
00:04:16,269 --> 00:04:19,000
But basically this will return the image object.

49
00:04:19,690 --> 00:04:23,890
And so we grab that image objects in here.

50
00:04:24,760 --> 00:04:29,230
But sometimes that image object will be just a value, right?

51
00:04:29,440 --> 00:04:33,220
So if there's no faces, we will return none.

52
00:04:33,370 --> 00:04:39,480
Therefore, we can check something like if face image is not none.

53
00:04:40,270 --> 00:04:49,300
The non special object, then we write that image objects into a file.

54
00:04:52,140 --> 00:04:55,560
Inside output folder.

55
00:04:56,910 --> 00:04:57,870
Slash.

56
00:05:00,350 --> 00:05:01,160
Image.

57
00:05:02,240 --> 00:05:11,270
So images, the file name that therefore we're going to see, right, that image in output slash the

58
00:05:11,270 --> 00:05:18,830
image name, such as, for example, air air not be written there because it doesn't have faces, but

59
00:05:18,830 --> 00:05:20,660
garden the jet pack.

60
00:05:21,470 --> 00:05:23,330
That's the structure more or less.

61
00:05:23,650 --> 00:05:28,400
Now what's left to do is just implement this function algorithm.

62
00:05:28,970 --> 00:05:35,510
So now we could just take this code here that we had from the previous video and just paste it inside

63
00:05:35,510 --> 00:05:36,230
the function.

64
00:05:36,560 --> 00:05:40,520
Be careful with an indentation from there to there.

65
00:05:41,100 --> 00:05:45,770
You want to press tab to indent everything, right?

66
00:05:46,190 --> 00:05:47,240
So this is what we do.

67
00:05:47,780 --> 00:05:52,940
Has face expects an image file name.

68
00:05:52,940 --> 00:05:56,090
So let's say image path.

69
00:05:56,690 --> 00:06:01,130
So air, the jetpack is going to go here.

70
00:06:01,310 --> 00:06:04,340
Also, galaxy in that jetpack is going to go here.

71
00:06:04,370 --> 00:06:11,360
So we get that Galaxy J pack and we provide that to, I'm reads.

72
00:06:12,140 --> 00:06:15,860
So image path, right?

73
00:06:16,880 --> 00:06:18,680
A color image, correct?

74
00:06:19,040 --> 00:06:21,410
Face cascades is that.

75
00:06:22,370 --> 00:06:23,180
That's correct.

76
00:06:23,720 --> 00:06:28,760
Face is the take to the face is from image from that image object.

77
00:06:29,120 --> 00:06:32,270
So don't confuse image with this image path.

78
00:06:32,510 --> 00:06:36,790
That's just the string, which represents the file name.

79
00:06:36,800 --> 00:06:40,310
This image here is the actual key to open.

80
00:06:40,310 --> 00:06:41,960
See the object right.

81
00:06:42,470 --> 00:06:44,870
We get the faces to the mattresses.

82
00:06:45,200 --> 00:06:48,380
We can print them out just to see them on the terminal.

83
00:06:48,800 --> 00:06:58,970
And then we iterate in each of those many faces, one face or multiple faces or nothing in that image.

84
00:06:59,750 --> 00:07:02,410
And so see two rectangle draw a rectangle.

85
00:07:02,420 --> 00:07:03,170
That's OK.

86
00:07:03,830 --> 00:07:11,800
You could also get this to just copy it from there and create a variable out of that, such as color.

87
00:07:11,810 --> 00:07:12,920
So that's a color, right?

88
00:07:14,870 --> 00:07:19,130
Equal to a vegetable and then in here you see color.

89
00:07:20,300 --> 00:07:21,560
Four is the width.

90
00:07:22,460 --> 00:07:30,200
So with equals two for the full replace that with width, color width.

91
00:07:30,830 --> 00:07:34,730
We could also do the same with that's one point one suit.

92
00:07:36,340 --> 00:07:43,090
Scale Eagles to 1.1 scale here and four wars minimum neighbors.

93
00:07:47,280 --> 00:07:48,450
Just neighbors is enough.

94
00:07:50,200 --> 00:07:53,440
That was for all right now.

95
00:07:54,730 --> 00:07:59,140
We need to add more codes here to change some details.

96
00:07:59,830 --> 00:08:01,910
But I do want to just keep adding code.

97
00:08:01,930 --> 00:08:07,600
I want to run the script now so that you see what is going on and we take things step by step.

98
00:08:07,780 --> 00:08:10,690
We fix the errors if there are any and so on.

99
00:08:11,920 --> 00:08:13,480
So I just ran the script.

100
00:08:15,290 --> 00:08:15,570
Right.

101
00:08:15,780 --> 00:08:26,700
That's the first error image, empty in function, I am right in line 23, line 23 is this one in here?

102
00:08:27,480 --> 00:08:37,020
So I'm right, this has no place here because we are writing that in this function.

103
00:08:37,530 --> 00:08:46,020
Therefore, we want to just delete that and actually, we want to return the image here, right?

104
00:08:46,260 --> 00:08:48,090
So image?

105
00:08:48,870 --> 00:08:49,800
This function returns.

106
00:08:49,800 --> 00:08:57,060
The image object of this gets that image object that is variable, and we check it if it's not none

107
00:08:57,060 --> 00:08:58,830
and writes it.

108
00:08:59,220 --> 00:09:00,180
So let's run again.

109
00:09:01,950 --> 00:09:09,180
OK, we have four empty tables, so the faces here are completely empty.

110
00:09:09,660 --> 00:09:11,310
Now this is not an error.

111
00:09:11,460 --> 00:09:19,440
It's just some output and it's sort of an empty output that makes it harder to troubleshoot the issue.

112
00:09:19,740 --> 00:09:22,140
And therefore you we want to use your logic now.

113
00:09:22,440 --> 00:09:24,390
There isn't a problem with open civvy.

114
00:09:24,390 --> 00:09:30,850
That's when you read an image which doesn't exist.

115
00:09:31,530 --> 00:09:40,260
The item reads Function will just give you an empty array so you can see you can print out image here

116
00:09:40,890 --> 00:09:44,310
and to run and you're going to see that you get none.

117
00:09:45,000 --> 00:09:48,540
So it's not a match, but it's not an error, either.

118
00:09:49,320 --> 00:09:55,470
So that means we're not reading any images here because you see that face image.

119
00:09:55,470 --> 00:10:02,820
His face is just getting an image file name and this image file name is just.

120
00:10:07,100 --> 00:10:15,680
That's galaxy the jet pack, but Galaxy the jet pack is inside this folder, so we can fix that by providing

121
00:10:15,680 --> 00:10:19,580
the full path through an f string.

122
00:10:21,780 --> 00:10:22,830
So like, that's.

123
00:10:24,480 --> 00:10:34,500
And another place holder here, a slash between inputs folder slash image.

124
00:10:35,560 --> 00:10:38,500
That should give us some different altitude now.

125
00:10:38,540 --> 00:10:42,700
Yeah, yeah, so we got the images printed out as you see the mattresses.

126
00:10:43,450 --> 00:10:45,520
And then we got stuck somewhere else.

127
00:10:45,940 --> 00:10:46,960
So I'm right.

128
00:10:47,020 --> 00:10:48,250
Missing record argument.

129
00:10:48,640 --> 00:10:51,410
I am sure it's missing the image.

130
00:10:51,430 --> 00:10:52,540
I'm right here.

131
00:10:52,540 --> 00:10:52,750
Yeah.

132
00:10:52,750 --> 00:10:55,450
So we only provided that path.

133
00:10:55,780 --> 00:11:02,320
Therefore, let's verify the image, which is not image, but the face image.

134
00:11:02,800 --> 00:11:06,580
So this image here could be a bit more clear.

135
00:11:06,610 --> 00:11:10,550
We could say image path four image path images.

136
00:11:10,810 --> 00:11:17,320
So it's explicitly declare that this is an image we know now that this is an image path, not an image

137
00:11:17,320 --> 00:11:22,540
object image path there and image path here as well.

138
00:11:23,080 --> 00:11:24,940
So famous image is the object.

139
00:11:25,720 --> 00:11:27,250
Image path is just a stream.

140
00:11:27,490 --> 00:11:28,270
Let's run again.

141
00:11:29,540 --> 00:11:33,140
OK, so we're getting something printed out.

142
00:11:34,880 --> 00:11:44,300
You see, no God and the JPEG is printed in here and then faces of God and the JPEG are printed afterwards.

143
00:11:46,060 --> 00:11:56,860
Then if we look up working, the pack also has these faces and also air the jet pack so outs, remove

144
00:11:56,860 --> 00:12:02,530
this print image here and just print the face is to see faces are printed out.

145
00:12:06,230 --> 00:12:08,720
So that we don't get all these mattresses.

146
00:12:10,100 --> 00:12:15,320
So I just run again, and, you know, now it's more compact of the output.

147
00:12:15,680 --> 00:12:23,360
You see, Galaxy J Pack has these faces, so an empty table working, the jetpack has two faces.

148
00:12:23,510 --> 00:12:26,090
You remember this two faces.

149
00:12:26,720 --> 00:12:28,520
And if you see here?

150
00:12:28,730 --> 00:12:28,960
Yup.

151
00:12:31,010 --> 00:12:36,950
The face here is detected a swirl of false positive swirl here and here as well.

152
00:12:37,670 --> 00:12:40,370
But we cannot be hundred percent accurate.

153
00:12:41,600 --> 00:12:51,650
Galaxy has no detected faces, air neither, so we got this rectangle drone around faces inside these

154
00:12:51,650 --> 00:12:52,610
two images.

155
00:12:53,910 --> 00:13:00,240
But our script somehow moved all the files in to output.

156
00:13:00,590 --> 00:13:03,890
We want only the files with faces to be moved there.

157
00:13:03,950 --> 00:13:05,270
Why did that happen?

158
00:13:06,140 --> 00:13:09,380
Well, that's happened because this is what we're doing, right?

159
00:13:09,800 --> 00:13:14,900
We iterate and we write a rectangle, but then we return the image for everything.

160
00:13:15,230 --> 00:13:24,800
So we need to do something here to suggest a conditional to distinguish between different phases.

161
00:13:24,800 --> 00:13:27,260
So an empty table here, an empty table here.

162
00:13:27,770 --> 00:13:28,720
And this is a list.

163
00:13:29,060 --> 00:13:39,860
What we could do is we could say something like if Len of faces is different from zero, then we draw

164
00:13:39,860 --> 00:13:40,280
faces.

165
00:13:40,280 --> 00:13:43,100
So I indented this so that this happens.

166
00:13:43,460 --> 00:13:45,200
We draw the rectangles.

167
00:13:46,350 --> 00:13:49,410
And we intend to return image if that's the case.

168
00:13:49,860 --> 00:13:56,720
Otherwise, you could see something else return none, but that is not necessarily because by default,

169
00:13:56,730 --> 00:14:00,090
Python will return none if this is not executed.

170
00:14:00,360 --> 00:14:09,840
So this block here is only executed when this is evaluated to true, which means when this is different

171
00:14:09,840 --> 00:14:14,190
from zero different from zero east in the case of this and that.

172
00:14:14,970 --> 00:14:18,420
So let's give it another try now I'm going to delete.

173
00:14:20,960 --> 00:14:21,950
These images.

174
00:14:29,320 --> 00:14:30,370
From output.

175
00:14:31,830 --> 00:14:32,520
And run.

176
00:14:36,740 --> 00:14:37,490
All right.

177
00:14:38,030 --> 00:14:43,790
My terminal is not displaying very well the ultimate yet, no, it's it's here.

178
00:14:44,450 --> 00:14:46,610
And both my outward folder.

179
00:14:47,730 --> 00:14:49,320
Has now of these two images.

180
00:14:49,860 --> 00:14:55,590
So we filter out only the files which have faces in them.

181
00:14:57,070 --> 00:14:59,110
That's our final code.

182
00:14:59,440 --> 00:15:01,870
Thanks for following and go to the next video.

183
00:15:01,900 --> 00:15:03,790
And we do some more coding.

