'Jupyter kernel dying again and again for Recognizing faces in a video file
import face_recognition
import cv2
input_movie = cv2.VideoCapture("video1.mp4")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('output3.avi', fourcc, 23.98, (1280, 720))
amir_image = face_recognition.load_image_file("Aamir-Khan.jpg")
amir_face_encoding = face_recognition.face_encodings(amir_image)[0]
rac_image = face_recognition.load_image_file("Rachel_Green.jpg")
rac_face_encoding = face_recognition.face_encodings(rac_image)[0]
known_faces = [
amir_face_encoding,
rac_face_encoding
]
known_face_names = [
"Aamir",
"Rachel"
]
face_locations = []
face_encodings = []
face_names = []
frame_number = 0
while True:
ret, frame = input_movie.read()
frame_number += 1
if not ret:
break
rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
match = face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50)
name = "unknown"
face_distances = face_recognition.face_distance(known_face, face_encoding)
best_match_index = np.argmin(face_distances)
if match[best_match_index]:
name = known_face_names[best_match_index]
face_names.append(name)
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 25), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 0.5, (255, 255, 255), 1)
print("Writing frame {} / {}".format(frame_number, length))
cv2.inshow("Video",frame)
input_movie.release()
cv2.destroyAllWindows()
Here my jupyter notebook keeps on dying . I think there is some issue with while loop. I am not understanding where this went wrong. I used the code available on face-recognition library github My idea was to detect known faces in the video . video is working fine and is of mp4 extension.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
