-
-
Save sumon328/1a0259fe14c86800a373935d8cf58c3d to your computer and use it in GitHub Desktop.
extract_features
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def extract_features(iDirName, cnn_model): | |
# get face detector from dlib and initialize the face aligner | |
detector = dlib.get_frontal_face_detector() | |
# open the video file | |
iFramePaths = [os.path.join(iDirName, iFramePath) for iFramePath in os.listdir(iDirName)] | |
# an empty list to hold the results | |
features = [] | |
# process each frame | |
for iFramePath in iFramePaths: | |
# read a frame from the dir | |
frame = cv2.imread(iFramePath) | |
frame_gray = cv2.imread(iFramePath, flags=cv2.COLOR_BGRA2GRAY) | |
# convert to grayscale | |
# frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
# detect a valid face from the frame | |
faces = detector(frame_gray, 2) | |
# continue if more than one face is detected | |
if len(faces) != 1: | |
continue | |
# get the coordinates of the bounding box | |
(x, y, w, h) = rect_to_bb(faces[0]) | |
# crop the negative values | |
if x < 0: | |
w = w + x | |
x = 0 | |
if y < 0: | |
h = h + y | |
y = 0 | |
# resize the image to 224x224 | |
# face = imutils.resize(frame[x:x + w, y:y + h], width=224, height=224) | |
try: | |
face = cv2.resize(frame[x:x + w, y:y + h], (224, 224), interpolation=cv2.INTER_AREA) | |
except Exception as e: | |
print(e) | |
print(f'x={x},y={y},w={w},h={h}') | |
print(f'The shape of the face is {frame[x:x + w, y:y + h].shape}') | |
print(f'The shape of the frame is {frame_gray.shape}') | |
print(f'The file is {iFramePath}') | |
sys.exit(-1) | |
# expand the shape of the image by one dimension | |
face_in = np.expand_dims(face, axis=0) | |
# convert to dtype 'float64' | |
face_in = face_in.astype('float64') | |
# some preprocessing required by the model | |
face_in = preprocess_input(face_in, version=2) | |
# feature extraction using CNN | |
feature = cnn_model.predict(face_in).ravel() | |
# add to the list | |
features.append(feature) | |
# convert to ndarray and return the results | |
return np.array(features) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment