https://github.com/mpolinowski/simple-face-recognition
DLIB Face Recognition
https://github.com/mpolinowski/simple-face-recognition
dlib-face-detection dlib-face-recognition
Last synced: 28 days ago
JSON representation
DLIB Face Recognition
- Host: GitHub
- URL: https://github.com/mpolinowski/simple-face-recognition
- Owner: mpolinowski
- Created: 2023-10-01T15:10:06.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-01T15:11:13.000Z (over 2 years ago)
- Last Synced: 2025-03-23T13:14:22.363Z (about 1 year ago)
- Topics: dlib-face-detection, dlib-face-recognition
- Language: Jupyter Notebook
- Homepage: https://mpolinowski.github.io/docs/IoT-and-Machine-Learning/ML/2023-10-01--delib-face-detection/2023-10-01
- Size: 35.4 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DLIB Face Recognition
* [Face Recognition](https://github.com/ageitgey/face_recognition)
Recognize and manipulate faces from Python or from the command line with the world's simplest face recognition library. Built using [dlib](http://dlib.net/)'s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the [Labeled Faces in the Wild](http://vis-www.cs.umass.edu/lfw/) benchmark.
* [PyTorch Jupyter](https://github.com/mpolinowski/pytorch-jupyter)
```bash
docker run --ipc=host --gpus all -ti --rm \
-v $(pwd):/opt/app -p 8888:8888 \
--name pytorch-jupyter \
pytorch-jupyter:latest
```
```python
!pip install face_recognition
```
```python
import face_recognition
import numpy as np
```
```python
img_bobby ="faces/bobbie_w_draper.jpg"
img_jim ="faces/jim_holden.jpg"
img_amos ="faces/amos_burton.jpg"
img_camina ="faces/camina_drummer.jpg"
img_naomi ="faces/naomi_nagata.jpg"
img_chrisjen ="faces/chrisjen_avasarala.jpg"
image_path = img_bobby
```
## Detect Face Location (CPU)
Re-run the following steps for all training images above:
```python
image = face_recognition.load_image_file(image_path)
face_locations = face_recognition.face_locations(image)
```
### Crop Location
```python
import cv2 as cv
import matplotlib.pyplot as plt
```
```python
img = cv.imread(image_path)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
```
```python
plt.imshow(img)
plt.axis('off')
for face_location in face_locations:
plt.plot(face_location[3], face_location[0], 'ro')
plt.plot(face_location[1], face_location[0], 'r+')
plt.plot(face_location[3], face_location[2], 'bo')
plt.plot(face_location[1], face_location[2], 'b+')
plt.show()
```

```python
for face_location in face_locations:
x1, y1 = face_location[3], face_location[2]
x2, y2 = face_location[1], face_location[2]
x3, y3 = face_location[1], face_location[0]
x4, y4 = face_location[3], face_location[0]
top_left_x = min([x1,x2,x3,x4])
top_left_y = min([y1,y2,y3,y4])
bot_right_x = max([x1,x2,x3,x4])
bot_right_y = max([y1,y2,y3,y4])
cropped_image = img[top_left_y:bot_right_y, top_left_x:bot_right_x]
```
```python
plt.imshow(cropped_image)
plt.axis('off')
```

```python
cv.imwrite('faces/cut/bobbie_w_draper.jpg', cv.cvtColor(cropped_image, cv.COLOR_RGB2BGR))
```
### Get all the Training Images
```python
bobby_train = face_recognition.load_image_file(img_bobby)
jim_train = face_recognition.load_image_file(img_jim)
amos_train = face_recognition.load_image_file(img_amos)
camina_train = face_recognition.load_image_file(img_camina)
naomi_train = face_recognition.load_image_file(img_naomi)
chrisjen_train = face_recognition.load_image_file(img_chrisjen)
bobby_encoding = face_recognition.face_encodings(bobby_train)[0]
jim_encoding = face_recognition.face_encodings(jim_train)[0]
amos_encoding = face_recognition.face_encodings(amos_train)[0]
camina_encoding = face_recognition.face_encodings(camina_train)[0]
naomi_encoding = face_recognition.face_encodings(naomi_train)[0]
chrisjen_encoding = face_recognition.face_encodings(chrisjen_train)[0]
```
```python
from glob import glob
cropped_images = glob('./faces/cut/*.jpg')
```
```python
plt.figure(figsize=(12, 8))
plt.suptitle('Training Images')
ax = plt.subplot(2, 3, 1)
img_path = cropped_images[0]
img_title = 'face: ' + cropped_images[0][12:-4]
plt.title(img_title, fontsize='medium')
image = plt.imread(img_path)
plt.imshow(image, cmap=plt.cm.binary)
ax = plt.subplot(2, 3, 2)
img_path = cropped_images[1]
img_title = 'face: ' + cropped_images[1][12:-4]
plt.title(img_title, fontsize='medium')
image = plt.imread(img_path)
plt.imshow(image, cmap=plt.cm.binary)
ax = plt.subplot(2, 3, 3)
img_path = cropped_images[2]
img_title = 'face: ' + cropped_images[2][12:-4]
plt.title(img_title, fontsize='medium')
image = plt.imread(img_path)
plt.imshow(image, cmap=plt.cm.binary)
ax = plt.subplot(2, 3, 4)
img_path = cropped_images[3]
img_title = 'face: ' + cropped_images[3][12:-4]
plt.title(img_title, fontsize='medium')
image = plt.imread(img_path)
plt.imshow(image, cmap=plt.cm.binary)
ax = plt.subplot(2, 3, 5)
img_path = cropped_images[4]
img_title = 'face: ' + cropped_images[4][12:-4]
plt.title(img_title, fontsize='medium')
image = plt.imread(img_path)
plt.imshow(image, cmap=plt.cm.binary)
ax = plt.subplot(2, 3, 6)
img_path = cropped_images[5]
img_title = 'face: ' + cropped_images[5][12:-4]
plt.title(img_title, fontsize='medium')
image = plt.imread(img_path)
plt.imshow(image, cmap=plt.cm.binary)
```

## Face Recognition
Loading a bunch of test images with "unknown" faces:
```python
test_image1 = face_recognition.load_image_file("faces/test/unknown_01.jpg")
test_image2 = face_recognition.load_image_file("faces/test/unknown_02.jpg")
test_image3 = face_recognition.load_image_file("faces/test/unknown_03.jpg")
test_image4 = face_recognition.load_image_file("faces/test/unknown_04.jpg")
test_image5 = face_recognition.load_image_file("faces/test/unknown_05.jpg")
test_image6 = face_recognition.load_image_file("faces/test/unknown_06.jpg")
test1_encoding = face_recognition.face_encodings(test_image1)
test2_encoding = face_recognition.face_encodings(test_image2)
test3_encoding = face_recognition.face_encodings(test_image3)
test4_encoding = face_recognition.face_encodings(test_image4)
test5_encoding = face_recognition.face_encodings(test_image5)
test6_encoding = face_recognition.face_encodings(test_image6)
```
### Compare Faces
Compare all detected images in the test dataset to the training images:
```python
trained_images = [bobby_encoding, jim_encoding, amos_encoding, camina_encoding, naomi_encoding, chrisjen_encoding]
trained_faces = np.array(["bobbie_w_draper", "jim_holden", "amos_burton", "camina_drummer", "naomi_nagata", "chrisjen_avasarala"])
```
#### Test Image 1
```python
test1_results = []
for detection in test1_encoding:
result = face_recognition.compare_faces(trained_images, detection)
test1_results.append(trained_faces[result])
```
```python
test_img1 = plt.imread('faces/test/unknown_01.jpg')
plt.title('detected faces: \n' + str(test1_results), fontsize='small')
plt.axis('off')
plt.imshow(test_img1)
```

#### Test Image 2
```python
test2_results = []
for detection in test2_encoding:
result = face_recognition.compare_faces(trained_images, detection)
test1_results.append(trained_faces[result])
```
```python
test_img2 = plt.imread('faces/test/unknown_02.jpg')
plt.title('detected faces: \n' + str(test2_results), fontsize='small')
plt.axis('off')
plt.imshow(test_img2)
```

#### Test Image 3
```python
test3_results = []
for detection in test3_encoding:
result = face_recognition.compare_faces(trained_images, detection)
test3_results.append(trained_faces[result])
test3_results
```
[array(['naomi_nagata'], dtype=' see `./main.py`
```python
with open('features/face_encodings.npy', 'wb') as f:
np.save(f, face_encodings)
with open('features/image_labels.npy', 'wb') as f:
np.save(f, image_labels)
```
```python
with open('features/face_encodings.npy', 'rb') as f:
feature_vectors = np.load(f)
with open('features/image_labels.npy', 'rb') as f:
feature_labels = np.load(f)
```
```python
np.unique(feature_labels, return_counts=True)
```
(array(['amos_burton', 'bobbie_w_draper', 'camina_drummer',
'chrisjen_avasarala', 'jim_holden', 'naomi_nagata'], dtype='