ageitgey / face_recognition

The world's simplest facial recognition api for Python and the command line
MIT License
52.5k stars 13.38k forks source link

live webcam how to save it #1026

Open r3x07 opened 4 years ago

r3x07 commented 4 years ago

ok can anyone here tell me how to make it save the names of the people it detects on a webcam, i mean if it sees me is there anyway to make it save it into a file?

jobergum commented 4 years ago

Just modify https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py slightly to save away frames where your face was found along with other metadata you want to save away.

r3x07 commented 4 years ago

Mind giving example?

On Fri, Feb 14, 2020, 7:09 PM Jo Kristian Bergum notifications@github.com wrote:

Just modify https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py slightly to save away frames where your face was found along with other metadata you want to save away.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ageitgey/face_recognition/issues/1026?email_source=notifications&email_token=AI7ME7X2IMSH4Y6ZNAS7EMTRC2QXZA5CNFSM4KGKVHEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELZEPJQ#issuecomment-586303398, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI7ME7W3AIBRT7VZKVSYUH3RC2QXZANCNFSM4KGKVHEA .

jobergum commented 4 years ago

@r3x07

Slightly modified from https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

Where you have an image of your face stored in my-image.png, the snippet will read your camera and dump frames where a face is found which has a similar (as measured by euclidian distance) encoding representation in the 128 dimensional encoding space as compared to your stored image of you.

import face_recognition
import cv2
import numpy as np
video_capture = cv2.VideoCapture(0)

my_image = face_recognition.load_image_file('my-image.png')
my_encoding = face_recognition.face_encodings(my_image)[0]

known_face_encodings = [
    my_encoding
]
face_locations = []
face_encodings = []
process_this_frame = True
count = 0
while True:
    ret, frame = video_capture.read()
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    rgb_small_frame = small_frame[:, :, ::-1]

    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
        for face_encoding in face_encodings:
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            distance_metric = np.min(face_distances)
            if distance_metric < 0.6:
              print("Found myself in stream with distance : %f" % distance_metric)
              cv2.imwrite("my-from-video-%d.jpg" % count, frame) 
              count += 1

    process_this_frame = not process_this_frame

video_capture.release()
cv2.destroyAllWindows()
r3x07 commented 4 years ago

Thanks but please mind commenting codes function so I know which line saves photos

On Tue, Feb 25, 2020, 7:11 PM Jo Kristian Bergum notifications@github.com wrote:

@r3x07 https://github.com/r3x07

Slightly modified from https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py

Where you have an image of your face stored in my-image.png, the snippet will read your camera and dump frames where a face is found which has a similar (as measured by euclidian distance) encoding representation in the 128 dimensional encoding space as compared to your stored image of you.

import face_recognition import cv2 import numpy as np video_capture = cv2.VideoCapture(0)

my_image = face_recognition.load_image_file('my-image.png') my_encoding = face_recognition.face_encodings(my_image)[0]

known_face_encodings = [ my_encoding ] face_locations = [] face_encodings = [] process_this_frame = True count = 0 while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1]

if process_this_frame:
    # Find all the faces and face encodings in the current frame of video
    face_locations = face_recognition.face_locations(rgb_small_frame)
    face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
    for face_encoding in face_encodings:
        face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
        distance_metric = np.min(face_distances)
        if distance_metric < 0.6:
          print("Found myself in stream with distance : %f" % distance_metric)
          cv2.imwrite("my-from-video-%d.jpg" % count, frame)
          count += 1

process_this_frame = not process_this_frame

video_capture.release() cv2.destroyAllWindows()

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ageitgey/face_recognition/issues/1026?email_source=notifications&email_token=AI7ME7STWUQLXS4LS544G23REURJJA5CNFSM4KGKVHEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM4DDRQ#issuecomment-590885318, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI7ME7VCYPDBBHXNZ4MYKILREURJJANCNFSM4KGKVHEA .

jobergum commented 4 years ago

This line saves the frame from the video where your face was found:

cv2.imwrite("my-from-video-%d.jpg" % count, frame) 
r3x07 commented 4 years ago

Thanks alot and can you tell me how to make the box bigger? Like the resolution of that shit where my webcam is displayed in

On Wed, Feb 26, 2020, 1:18 AM Jo Kristian Bergum notifications@github.com wrote:

This line saves the frame from the video where your face was found:

cv2.imwrite("my-from-video-%d.jpg" % count, frame)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ageitgey/face_recognition/issues/1026?email_source=notifications&email_token=AI7ME7W2XRCISU4L3FM5S5LREV4IFA5CNFSM4KGKVHEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM5K7XI#issuecomment-591048669, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI7ME7RLSEFSJAUFNJCFDB3REV4IFANCNFSM4KGKVHEA .

r3x07 commented 4 years ago

Please tell me how to make the window bigger like full screen

On Wed, Feb 26, 2020, 2:58 PM Turkish Fun firco40@gmail.com wrote:

Thanks alot and can you tell me how to make the box bigger? Like the resolution of that shit where my webcam is displayed in

On Wed, Feb 26, 2020, 1:18 AM Jo Kristian Bergum notifications@github.com wrote:

This line saves the frame from the video where your face was found:

cv2.imwrite("my-from-video-%d.jpg" % count, frame)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ageitgey/face_recognition/issues/1026?email_source=notifications&email_token=AI7ME7W2XRCISU4L3FM5S5LREV4IFA5CNFSM4KGKVHEKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEM5K7XI#issuecomment-591048669, or unsubscribe https://github.com/notifications/unsubscribe-auth/AI7ME7RLSEFSJAUFNJCFDB3REV4IFANCNFSM4KGKVHEA .