suvadityamuk
commited on
Commit
•
520c0fb
1
Parent(s):
03cfcc0
new files added and lfs used
Browse filesSigned-off-by: Suvaditya Mukherjee <[email protected]>
- .gitattributes +5 -0
- app.py +149 -0
- deploy.prototxt.txt +3 -0
- openface.nn4.small2.v1.t7 +3 -0
- requirements.txt +1 -0
- res10_300x300_ssd_iter_140000.caffemodel +3 -0
- unknownEmbeddings.pkl +3 -0
- unknownNames.pkl +3 -0
.gitattributes
CHANGED
@@ -32,3 +32,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
deploy.prototxt.txt filter=lfs diff=lfs merge=lfs -text
|
36 |
+
openface.nn4.small2.v1.t7 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
res10_300x300_ssd_iter_140000.caffemodel filter=lfs diff=lfs merge=lfs -text
|
38 |
+
unknownEmbeddings.pkl filter=lfs diff=lfs merge=lfs -text
|
39 |
+
unknownNames.pkl filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import os
|
5 |
+
from imutils import resize
|
6 |
+
import pickle
|
7 |
+
from sklearn.preprocessing import LabelEncoder
|
8 |
+
from sklearn.svm import SVC
|
9 |
+
import numpy as np
|
10 |
+
import cv2
|
11 |
+
from imutils import resize
|
12 |
+
|
13 |
+
|
14 |
+
def calc_embeddings(all_files, names):
|
15 |
+
detector = cv2.dnn.readNetFromCaffe(
|
16 |
+
"deploy.prototxt.txt", "res10_300x300_ssd_iter_140000.caffemodel"
|
17 |
+
)
|
18 |
+
embedder = cv2.dnn.readNetFromTorch("openface.nn4.small2.v1.t7")
|
19 |
+
knownNames = []
|
20 |
+
knownEmbeddings = []
|
21 |
+
total = 0
|
22 |
+
for file in all_files:
|
23 |
+
name = names[total]
|
24 |
+
f = open(f"/content/Celebrity Faces Dataset/{name}/{file}", "rb")
|
25 |
+
file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8)
|
26 |
+
image = cv2.imdecode(file_bytes, 1)
|
27 |
+
image = resize(image, width=600)
|
28 |
+
(h, w) = image.shape[:2]
|
29 |
+
|
30 |
+
imageBlob = cv2.dnn.blobFromImage(
|
31 |
+
cv2.resize(image, (300, 300)),
|
32 |
+
1.0,
|
33 |
+
(300, 300),
|
34 |
+
(104.0, 177.0, 123.0),
|
35 |
+
swapRB=False,
|
36 |
+
crop=False,
|
37 |
+
)
|
38 |
+
detector.setInput(imageBlob)
|
39 |
+
detections = detector.forward()
|
40 |
+
|
41 |
+
if len(detections) > 0:
|
42 |
+
i = np.argmax(detections[0, 0, :, 2])
|
43 |
+
confidence = detections[0, 0, i, 2]
|
44 |
+
|
45 |
+
if confidence > 0.5:
|
46 |
+
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
47 |
+
(startX, startY, endX, endY) = box.astype("int")
|
48 |
+
face = image[startY:endY, startX:endX]
|
49 |
+
(fH, fW) = face.shape[:2]
|
50 |
+
if fW < 20 or fH < 20:
|
51 |
+
continue
|
52 |
+
|
53 |
+
faceBlob = cv2.dnn.blobFromImage(
|
54 |
+
face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False
|
55 |
+
)
|
56 |
+
embedder.setInput(faceBlob)
|
57 |
+
vec = embedder.forward()
|
58 |
+
knownNames.append(name)
|
59 |
+
knownEmbeddings.append(vec.flatten())
|
60 |
+
total += 1
|
61 |
+
with open("/content/unknownEmbeddings.pkl", "rb") as fp:
|
62 |
+
l = pickle.load(fp)
|
63 |
+
with open("/content/unknownNames.pkl", "rb") as fp:
|
64 |
+
n = pickle.load(fp)
|
65 |
+
for i in l:
|
66 |
+
knownEmbeddings.append(i)
|
67 |
+
knownNames = knownNames + n
|
68 |
+
return knownEmbeddings, knownNames
|
69 |
+
|
70 |
+
|
71 |
+
def recognize(embeddings, names):
|
72 |
+
|
73 |
+
le = LabelEncoder()
|
74 |
+
labels = le.fit_transform(names)
|
75 |
+
recognizer = SVC(C=1.0, kernel="linear", probability=True)
|
76 |
+
recognizer.fit(embeddings, names)
|
77 |
+
|
78 |
+
return le, recognizer
|
79 |
+
|
80 |
+
|
81 |
+
def run_inference(myImage):
|
82 |
+
|
83 |
+
os.chdir("/content/Celebrity Faces Dataset")
|
84 |
+
celebs = []
|
85 |
+
scores = dict()
|
86 |
+
|
87 |
+
for celeb in os.listdir():
|
88 |
+
files = []
|
89 |
+
names = []
|
90 |
+
if celeb in celebs:
|
91 |
+
continue
|
92 |
+
name = celeb
|
93 |
+
celebs.append(name)
|
94 |
+
for file in os.listdir(celeb):
|
95 |
+
files.append(file)
|
96 |
+
names.append(name)
|
97 |
+
embeddings, names = calc_embeddings(files, names)
|
98 |
+
le, model = recognize(embeddings, names)
|
99 |
+
|
100 |
+
detector = cv2.dnn.readNetFromCaffe(
|
101 |
+
"/content/deploy.prototxt.txt",
|
102 |
+
"/content/res10_300x300_ssd_iter_140000.caffemodel",
|
103 |
+
)
|
104 |
+
embedder = cv2.dnn.readNetFromTorch("/content/openface.nn4.small2.v1.t7")
|
105 |
+
(h, w) = myImage.shape[:2]
|
106 |
+
imageBlob = cv2.dnn.blobFromImage(
|
107 |
+
cv2.resize(myImage, (300, 300)),
|
108 |
+
1.0,
|
109 |
+
(300, 300),
|
110 |
+
(104.0, 177.0, 123.0),
|
111 |
+
swapRB=False,
|
112 |
+
crop=False,
|
113 |
+
)
|
114 |
+
detector.setInput(imageBlob)
|
115 |
+
detections = detector.forward()
|
116 |
+
for i in range(0, detections.shape[2]):
|
117 |
+
confidence = detections[0, 0, i, 2]
|
118 |
+
if confidence > 0.5:
|
119 |
+
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
|
120 |
+
(startX, startY, endX, endY) = box.astype("int")
|
121 |
+
face = myImage[startY:endY, startX:endX]
|
122 |
+
(fH, fW) = face.shape[:2]
|
123 |
+
if fW < 20 or fH < 20:
|
124 |
+
continue
|
125 |
+
|
126 |
+
faceBlob = cv2.dnn.blobFromImage(
|
127 |
+
face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False
|
128 |
+
)
|
129 |
+
embedder.setInput(faceBlob)
|
130 |
+
vec = embedder.forward()
|
131 |
+
preds = model.predict_proba(vec)[0]
|
132 |
+
j = np.argmax(preds)
|
133 |
+
proba = preds[j]
|
134 |
+
name = le.classes_[j]
|
135 |
+
# text = "{}: {:.2f}%".format(name, proba * 100)
|
136 |
+
scores[name] = proba * 100
|
137 |
+
result = sorted(scores.items(), key=lambda x: x[1], reverse=True)
|
138 |
+
return result
|
139 |
+
|
140 |
+
|
141 |
+
iface = gr.Interface(
|
142 |
+
fn=run_inference,
|
143 |
+
inputs="image",
|
144 |
+
outputs="label",
|
145 |
+
live=True,
|
146 |
+
interpretation="default",
|
147 |
+
title="Who do you look Like?!",
|
148 |
+
)
|
149 |
+
iface.launch()
|
deploy.prototxt.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f62621cac923d6f37bd669298c428bb7ee72233b5f8c3389bb893e35ebbcf795
|
3 |
+
size 28092
|
openface.nn4.small2.v1.t7
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9b72d54aeb24a64a8135dca8e792f7cc675c99a884a6940350a6cedcf7b7ba08
|
3 |
+
size 31510785
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
|
res10_300x300_ssd_iter_140000.caffemodel
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2a56a11a57a4a295956b0660b4a3d76bbdca2206c4961cea8efe7d95c7cb2f2d
|
3 |
+
size 10666211
|
unknownEmbeddings.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ef0614d4878acf5106126df50398cee6cf7e90c9e0328c9d44f355c45fbc4c54
|
3 |
+
size 5571
|
unknownNames.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:28448313ad59e72fd623d0d4b40cce6a74147f2660aa1f41ba316fc264a1170a
|
3 |
+
size 44
|