1
00:00:00,330 --> 00:00:04,540
So welcome back to Lesson 40, which looks like the last lesson here.

2
00:00:04,560 --> 00:00:09,990
However, we have some lessons of looking at video, which I'll demonstrate to you after with this lesson.

3
00:00:10,470 --> 00:00:15,750
But for now, this is the last I python that book in the open TV chapter in this section.

4
00:00:16,260 --> 00:00:18,940
So go ahead and open this notebook.

5
00:00:18,970 --> 00:00:24,900
This is we're going to do facial recognition using one of the facial recognition libraries called face

6
00:00:24,900 --> 00:00:25,470
recognition.

7
00:00:25,890 --> 00:00:28,770
So to do this, just install this here.

8
00:00:28,800 --> 00:00:31,540
So this group pip install face recognition.

9
00:00:31,560 --> 00:00:37,920
So what this library allows us to do, it allows us to compare faces and then using that comparison

10
00:00:37,920 --> 00:00:44,050
scroll, we can actually use that metric now to find the face that it it's it is more similar to in

11
00:00:44,100 --> 00:00:45,210
a database of faces.

12
00:00:45,900 --> 00:00:51,010
So this should take maybe about about a minute to install.

13
00:00:51,030 --> 00:00:52,020
So let's wait for it.

14
00:00:55,800 --> 00:00:56,320
There we go.

15
00:00:56,340 --> 00:00:57,150
It's complete.

16
00:00:57,630 --> 00:01:01,110
So let's take a look at how we use this function, so let's stick.

17
00:01:01,110 --> 00:01:04,620
That's literally initial function that we've used previously.

18
00:01:04,950 --> 00:01:06,590
And let's download some images.

19
00:01:06,600 --> 00:01:12,360
We're going to download an image of Trump and two different images of President Joe Biden.

20
00:01:12,660 --> 00:01:15,960
So let's get those images here using the double get function.

21
00:01:16,470 --> 00:01:17,820
So we now have them safe.

22
00:01:18,450 --> 00:01:22,720
And let's display those images so we can take a look at the images we're looking with.

23
00:01:23,250 --> 00:01:29,250
So you can see Joe Biden is here two different pictures of him where he does look somewhat similar.

24
00:01:29,460 --> 00:01:32,880
And this is Donald Trump, his well, looking a little bit angry.

25
00:01:33,630 --> 00:01:35,610
So let's take a look at something.

26
00:01:36,060 --> 00:01:38,400
Let's import or face recognition library.

27
00:01:38,970 --> 00:01:40,860
Let's load each image here.

28
00:01:40,890 --> 00:01:42,840
So we use face recognition to load.

29
00:01:42,840 --> 00:01:50,250
Image file has an inbuilt loading function, which probably which not probably it does load it and it's

30
00:01:50,430 --> 00:01:52,770
in its own type of array here.

31
00:01:53,370 --> 00:02:00,300
And what we do, it is another function in it called face recognition, dot, facial water, face and

32
00:02:00,300 --> 00:02:00,840
coatings.

33
00:02:01,230 --> 00:02:06,540
So we take the image that's been loaded here, put it into here, and we just index the output to get

34
00:02:06,540 --> 00:02:08,580
this is to get the first image out of it.

35
00:02:08,730 --> 00:02:09,900
The first, including out of it.

36
00:02:10,290 --> 00:02:15,600
After multiple offices, you can use different index values to get them, but for now, where we are

37
00:02:15,630 --> 00:02:19,050
using images with just one face to make this simpler.

38
00:02:19,830 --> 00:02:21,630
So we have the Biden and including.

39
00:02:21,960 --> 00:02:23,910
And then we call the Trump image.

40
00:02:24,300 --> 00:02:25,380
Unknown image here.

41
00:02:25,830 --> 00:02:27,210
So we have the unknown, including here.

42
00:02:27,600 --> 00:02:32,810
So we're just going to just do that because we're going to use the unknown image to recognize it afterward.

43
00:02:32,820 --> 00:02:38,400
But for now, just this is just the Trump image, and this is the sort of Trump including in the Trump

44
00:02:38,400 --> 00:02:38,760
image.

45
00:02:39,270 --> 00:02:45,900
And we just use the don't compare faces function in the face recognition library to compare.

46
00:02:45,900 --> 00:02:49,440
Doesn't cuttings notice that it is in an array here?

47
00:02:50,370 --> 00:02:54,320
The Biden encoding sorry, it's an array, and then this one isn't.

48
00:02:54,390 --> 00:03:01,140
So just from the note that you this can probably take a bunch of different images here so you can get

49
00:03:01,140 --> 00:03:03,120
the output, the results of that as well.

50
00:03:03,570 --> 00:03:07,830
And then we index and the first result because we just want to compare to the first face of compared

51
00:03:07,830 --> 00:03:08,070
to.

52
00:03:08,820 --> 00:03:11,820
And we can see what it says.

53
00:03:12,750 --> 00:03:15,390
It's going to compare faces and tell you if it's a match or not.

54
00:03:16,380 --> 00:03:22,710
And it says the fierce match is false, which means that Biden and Trump images are this similar or

55
00:03:22,710 --> 00:03:23,490
not similar.

56
00:03:24,090 --> 00:03:25,770
So it doesn't it doesn't match.

57
00:03:26,100 --> 00:03:28,890
So no, that's tried with two different Joe Biden images.

58
00:03:28,890 --> 00:03:35,400
So let's load this and run the same functions here where we get the first face out of each one, get

59
00:03:35,400 --> 00:03:37,680
the encoding and then compare them here.

60
00:03:38,640 --> 00:03:39,750
And now it's as true.

61
00:03:39,870 --> 00:03:42,360
So this is working as it's intended.

62
00:03:42,840 --> 00:03:44,070
It just matched.

63
00:03:44,550 --> 00:03:49,200
It's just as true and matched the two different images of Joe Biden, these two images.

64
00:03:49,680 --> 00:03:52,620
It said it's the same person, so that's exactly what we want.

65
00:03:53,070 --> 00:03:59,130
So now let's use this same function here to recognize faces and images.

66
00:03:59,520 --> 00:04:03,750
So to do that, we're going to look at a sample picture here.

67
00:04:04,410 --> 00:04:06,120
This is the image of Trump.

68
00:04:06,600 --> 00:04:08,610
We're going to look at another sample here.

69
00:04:08,640 --> 00:04:09,600
This is one of Joe Biden.

70
00:04:10,050 --> 00:04:14,640
We're going to put those in cuttings here into this, this array here.

71
00:04:15,390 --> 00:04:17,460
And then we're going to do similarly with the first names here.

72
00:04:17,460 --> 00:04:20,040
So we store the name of Donald Trump and Joe Biden in it.

73
00:04:20,490 --> 00:04:23,010
This is creating our database of names.

74
00:04:23,730 --> 00:04:25,590
And then we initialize some variables here.

75
00:04:25,590 --> 00:04:31,520
These are all office locations, including surface names, and process this from being true to variable

76
00:04:31,530 --> 00:04:33,750
that we use inside of here.

77
00:04:34,350 --> 00:04:34,770
All right.

78
00:04:35,280 --> 00:04:35,910
You can see it here.

79
00:04:35,910 --> 00:04:37,890
So if it's a process frame, we just do that.

80
00:04:38,970 --> 00:04:42,180
This is a way we can process every other frame if we needed to in a video.

81
00:04:42,540 --> 00:04:47,730
However, I just love this line of code here, just in case you wanted to see it, how it would to a

82
00:04:47,730 --> 00:04:52,020
video, but it's not going to do anything here, just going to it's just going to process this year.

83
00:04:52,800 --> 00:04:55,110
So what we do loaded.

84
00:04:55,260 --> 00:04:56,730
So we load and undo an image.

85
00:04:57,210 --> 00:04:59,440
So this unknown image is not in the database.

86
00:04:59,460 --> 00:05:02,160
Remember that this is the picture of Biden.

87
00:05:02,310 --> 00:05:09,660
Joe Biden, too, and we passed a free every resize it to a quarter of its size so it can improve the

88
00:05:09,660 --> 00:05:14,820
facial recognition time because larger images do take longer with this algorithm.

89
00:05:15,480 --> 00:05:22,410
And then we convert the image to for BGR to RGV be in that way, you can do it without using open space

90
00:05:22,410 --> 00:05:22,890
functions.

91
00:05:23,280 --> 00:05:26,850
This is just some indexing of the array.

92
00:05:28,260 --> 00:05:33,450
And then what we do, we if this process is this frame, that's that's the one we have true.

93
00:05:34,170 --> 00:05:37,020
This is allows us to skip other frames if we wanted to in the video.

94
00:05:37,230 --> 00:05:40,850
But forget that for now, what we're doing here, we're processing this.

95
00:05:40,860 --> 00:05:43,430
So we take this more to be frame.

96
00:05:43,800 --> 00:05:45,570
We get to first locations here.

97
00:05:46,440 --> 00:05:47,580
That's for the bounding box.

98
00:05:47,580 --> 00:05:55,110
By the way, it has its own decode facial features detector in this and then we take that interface

99
00:05:55,110 --> 00:05:55,320
out of.

100
00:05:55,370 --> 00:06:01,160
That's we use face locations to extract a face from it, and we get to face and coatings here.

101
00:06:01,910 --> 00:06:03,200
So now what do we do?

102
00:06:03,710 --> 00:06:11,030
We get the facial, including stat, the facial recognition lighting, coatings captured and the locations

103
00:06:11,030 --> 00:06:11,390
as well.

104
00:06:11,810 --> 00:06:17,270
And we look through all of the faces that have been encoded from dysfunction here because remember,

105
00:06:17,270 --> 00:06:18,560
there can be multiple faces.

106
00:06:19,160 --> 00:06:23,770
So we know check to see if a face is a match for one of our known faces.

107
00:06:23,780 --> 00:06:30,410
Remember, we created this little index here of known faces, previously unknown coatings.

108
00:06:31,040 --> 00:06:34,190
So this towards the end coatings for our database.

109
00:06:34,820 --> 00:06:38,630
So you can have imagine if you had like a database of 100 images, you would.

110
00:06:38,630 --> 00:06:42,890
You can create this array just by looking through all of the images in the directory and appending the

111
00:06:42,890 --> 00:06:44,270
results to that early here.

112
00:06:44,840 --> 00:06:48,800
So no, it is hardcoded the results in this area for simplicity's sake.

113
00:06:49,520 --> 00:06:54,680
And then what we do, we compared with the face, including if we just captured in this unknown image

114
00:06:55,370 --> 00:06:57,560
and the name for no is unknown.

115
00:06:58,670 --> 00:06:59,630
And then what do we do?

116
00:07:00,290 --> 00:07:01,430
We take a look.

117
00:07:01,770 --> 00:07:03,220
Do we get the first distance?

118
00:07:03,230 --> 00:07:09,680
That's a way to measure the smallest, the basically the Euclidean of cosine type distance from the

119
00:07:09,680 --> 00:07:10,460
two faces.

120
00:07:11,060 --> 00:07:16,190
And we just use this augment function here to get the best minimum match.

121
00:07:16,190 --> 00:07:22,610
That means the match that is closest to the face in our database here and then we append the names to

122
00:07:22,610 --> 00:07:23,300
the end here.

123
00:07:24,230 --> 00:07:28,750
So and then what we do once we have that, we can now loop through the results.

124
00:07:28,760 --> 00:07:35,300
We use the first locations here to get these values top right, bottom left, use the open TV rectangle

125
00:07:35,300 --> 00:07:37,070
function to draw on the face here.

126
00:07:37,790 --> 00:07:43,760
Similarly, we draw a label boxed right below this rectangle so we can put the name in there and then

127
00:07:43,760 --> 00:07:48,080
we use pretexts to get the name that we got from databases onto the image.

128
00:07:48,260 --> 00:07:52,160
So it seems like a lot seems a little bit complicated if you're a beginner.

129
00:07:52,550 --> 00:07:55,670
I can understand why this could could seem a bit overwhelming.

130
00:07:56,030 --> 00:08:00,800
However, if you just take your time and go through the court line by line, it will all make sense

131
00:08:00,800 --> 00:08:01,160
to you.

132
00:08:01,730 --> 00:08:03,020
So let's see the results.

133
00:08:03,620 --> 00:08:04,850
Well, it worked.

134
00:08:05,210 --> 00:08:13,220
So you can see we are able to now recognize undoing images and apply them label to that face by this

135
00:08:13,220 --> 00:08:13,760
code here.

136
00:08:14,060 --> 00:08:16,220
And you can extend this for your own purposes.

137
00:08:16,220 --> 00:08:21,950
If you had pictures of your employees, you can put them here and could get destroyed, including here.

138
00:08:22,460 --> 00:08:28,070
And then every time there's a new face like, let's say, there's a video camera or Raspberry Pi that's

139
00:08:28,070 --> 00:08:30,380
just checking feces of someone approaching a door.

140
00:08:30,890 --> 00:08:37,190
You can use this to notarize shooting people to enter or subject certain people to not have access.

141
00:08:37,850 --> 00:08:43,190
So that's it for this facial recognition chapter, and I hope you enjoy this.

142
00:08:43,580 --> 00:08:46,550
This was a very good lesson and I've used this library.

143
00:08:47,030 --> 00:08:49,400
It's not state of the art, but it's pretty good.

144
00:08:50,240 --> 00:08:54,110
We will do facial recognition in more detail in the deep learning section of discourse.

145
00:08:54,740 --> 00:08:56,480
So stay tuned for that.

146
00:08:56,480 --> 00:09:02,440
And for now, that concludes these and see if your lessons will now move on to some open TV lessons.

147
00:09:02,450 --> 00:09:05,720
Where we started working with VIDEO can take a look at these lessons here.

148
00:09:06,230 --> 00:09:12,650
However, this is going to be run on my local machine just because it's easier to demonstrate these

149
00:09:12,650 --> 00:09:13,310
lessons here.

150
00:09:13,430 --> 00:09:20,020
So if you don't have open TV install locally, there's a video created in the beginning.

151
00:09:20,030 --> 00:09:26,210
Or actually, I'll put it in this section here so you can see how to install open TV locally and run

152
00:09:26,210 --> 00:09:27,530
these run these files.

153
00:09:27,710 --> 00:09:32,210
These no books on your system, so stay tuned for these lessons.

154
00:09:32,420 --> 00:09:32,810
Thank you.
