Reputation: 11
I have a face recognition project using a camera without any problems.
I now want to do this simultaneously from two cameras.
This is my code for one camera, I haven't any idea on how to employ two cameras for this purpose.
import face_recognition
import cv2
import numpy as np
video_capture = cv2.VideoCapture('rtsp://admin:[email protected]:554/mode=real&idc=1&ids=2')
farid_image = face_recognition.load_image_file("farid.jpg")
farid_face_encoding = face_recognition.face_encodings(farid_image)[0]
# Load a second sample picture and learn how to recognize it.
roice_image = face_recognition.load_image_file("roice.jpg")
roice_face_encoding = face_recognition.face_encodings(roice_image)[0]
known_face_encodings = [
farid_face_encoding,
roice_face_encoding
]
known_face_names = [
"farid",
"roice"
]
while True:
ret, frame = video_capture.read()
rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
# Loop through each face in this frame of video
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# Calculate face distance
face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
# If a match was found in known_face_encodings, just use the first one.
if True in matches:
# first_match_index = matches.index(True)
# Sort nearest distance
name = known_face_names[np.argsort(face_distance)[0]]
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
I can Simply add more cameras using cv2.VideoCapture()
module, but how can I make changes to face_recognition
in order to work with two cameras?
Upvotes: 0
Views: 890
Reputation: 1
using another thread will still make cpu overwhelmed by frames , you can either use multiprocess module or make the script take argument when executing it , and enter rtsp_url for each camera and run the script with differente urls like this
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--rtsp-url", type=str, default='0')
parser.add_argument("--data-dir", type=str, default='Report')
parser.add_argument("--cam-id", type=str, default='0')
args = parser.parse_args()
Rtsp_url = args.rtsp_url
you can use "terminal keeper" vscode extension to automate execution for multiple script with different argument or you can follow this repo Git Repo:/M-M-Akash/Face_Recognition_System you can use "multiprocess" instead of thread for true parallelism
Upvotes: 0
Reputation: 105
You could try making this multithreaded. One thread for each camera that does it's own facial recognition on the images that it sees.
They would be acting independently on their respective streams, but you can then get results from both threads to combine the information for improved detection and/or recognition.
Upvotes: 0