Face_recognition: rtmp live face recognition

Created on 22 Aug 2018  路  9Comments  路  Source: ageitgey/face_recognition

Hello Adam,

i hope you can help me again, and i have 2 questions for you. sorry dude i know i did ask you a lot of questions.

  1. how can i do face recognition with rtmp live stream?
  2. how can i do fater search in album ? how can i using gpu cause i use 20 core cpu and 1K photo is between process time 2 - 3 minute.

Most helpful comment

The idea is to read rtsp/rtmp stream frame by frame, transferring frames to np.ndarray using OpenCV, then processing ndarray with face_recognition.
Here's a demo that reads a single frame from a rtsp stream.

import cv2
import PIL.Image

cap = cv2.VideoCapture(rtsp_addr)
if cap.isOpened():
    try:
        ret, frame = cap.read()
        fr_frame = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        # fr_frame is what we want here.
    finally:
        cap.release()

All 9 comments

The idea is to read rtsp/rtmp stream frame by frame, transferring frames to np.ndarray using OpenCV, then processing ndarray with face_recognition.
Here's a demo that reads a single frame from a rtsp stream.

import cv2
import PIL.Image

cap = cv2.VideoCapture(rtsp_addr)
if cap.isOpened():
    try:
        ret, frame = cap.read()
        fr_frame = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
        # fr_frame is what we want here.
    finally:
        cap.release()

Not Working bro. if you for me example rtmp capture face recognition fetch into know photos.

@windforce7 did U get this to work. Also really want this awesome code to work with rtsp streams.
Could it be done thru ffmpeg?
If so, how?

Thanks in advance!

@htilly Our rtsp source was a Hikvision ipc camera and we managed to read output stream. The code above worked and was good enough for me, though I don't know what exactly was @cengizakcan 's problem. You may have to solve the following issues on your own:

  1. how to handle sequential rtsp stream frames by wrapping the process in a loop:
try:
  while cap.isOpen():
    ret, frame = cap.read()
    # blah    

I can't tell you whether this would process all frames without missing any one of them. If the inside part ran too slow, it might not catch up with coming frames. In my project, we only need to take a few shots. So I didn't do much further digging.

  1. Do remember the face_recognition project can only deal with PIL image, so far as I know. So the key is to convert ffmpeg frames into this format. In this case, you are not alone:
    https://stackoverflow.com/questions/41580034/how-to-pipe-live-video-frames-from-ffmpeg-to-pil
    https://mhaller.github.io/pyffmpeg/

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

please use the demo in face_recognition

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

please use the demo in face_recognition project, this is the link
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

please use the demo in face_recognition

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

please use the demo in face_recognition project, this is the link
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.

I made the change but the streaming keeps crashing.

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

please use the demo in face_recognition

this is a good demo of face_recognition, the link is as flow:
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.
below is my test code:
https://github.com/shi-hao/face_recognition_demo/blob/master/face_recog_rtsp.py

I'm trying to do exactly what you did in this code, I used yours as a base, however streaming closed and got an encoding error I made some changes, and streaming no longer closes yet it freezes, when you use it works normally?

please use the demo in face_recognition project, this is the link
https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_webcam_faster.py
edit one line of the code :
video_capture = cv2.VideoCapture(0)
to
video_capture = cv2.VideoCapture('rtsp_url')
then all is done.

I made the change but the streaming keeps crashing.

debug
(1)face_recognition is ok?
(2)rtsp stream is ok?
(3)combine them, then is ok?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ayyucedemirbas picture ayyucedemirbas  路  4Comments

Sparviero-Sughero picture Sparviero-Sughero  路  4Comments

carlhung picture carlhung  路  6Comments

mrhydra-np picture mrhydra-np  路  3Comments

damey2011 picture damey2011  路  3Comments