RuntimeError:不支持的图像类型,当尝试使用face_recognition解析来自IP摄像机的帧时,必须为8位灰度或RGB图像

时间:2018-11-16 07:22:42

标签: python-3.x face-recognition

我正在尝试从摄像机获取视频流并对其进行解析。这是我的代码:

import datetime
import os

import cv2
import face_recognition
from PIL import Image

import config
from helpers import get_known_faces, add_to_json, add_captured_face

print('Running...')
cap_vid = cv2.VideoCapture(config.stream_url)
while True:
    ret, frame = cap_vid.read()
    curr_face_encs = face_recognition.face_encodings(frame)
    curr_face_locs = face_recognition.face_locations(frame)
    face_names_in_DB, face_encs_in_DB = get_known_faces()
    if curr_face_encs:
        for idx, face in enumerate(curr_face_encs):
            res = face_recognition.compare_faces(face_encs_in_DB, face)
            if True in res:
                frame_data = {
                    'full_name': face_names_in_DB[res.index(True)],
                    'time': datetime.datetime.now().isoformat()
                }
                add_to_json(config.members_json_data, frame_data)
            else:
                top, right, bottom, left = curr_face_locs[idx]
                img = Image.fromarray(frame[top:bottom, left:right])
                img_name = '%s-%d.jpg' % (
                    datetime.datetime.now().isoformat().replace(':', 'c'), idx)
                img_path = os.path.join(config.stranger_dir, img_name)
                img.resize((40, 40)).save(img_path)
                add_captured_face(img_path,
                                datetime.datetime.now().isoformat())

config.stream_url设置为0时,即当我从网络摄像头读取内容时,一切正常。但是,当config.stream_url = rtsp://10.10.111.200/profile2/media.smp(我拥有完全管理权限的IP摄像机并且可以通过VLC播放器观看实时视频时,我想说的是url运行正常,我可以访问它),则出现以下错误:

Traceback (most recent call last):
File "C:/Users/turalm01/Desktop/work/ITSFaceDetector/main.py", line 25, in <module>
    curr_face_encs = face_recognition.face_encodings(frame)
File "C:\Users\turalm01\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 209, in face_encodings
    raw_landmarks = _raw_face_landmarks(face_image, known_face_locations, model="small")
File "C:\Users\turalm01\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 153, in _raw_face_landmarks
    face_locations = _raw_face_locations(face_image)
File "C:\Users\turalm01\AppData\Local\Programs\Python\Python36\lib\site-packages\face_recognition\api.py", line 102, in _raw_face_locations
    return face_detector(img, number_of_times_to_upsample)
RuntimeError: Unsupported image type, must be 8bit gray or RGB image.

我想也许它想让我将每帧转换为RGB,所以我尝试将每帧转换为RGB(但是我不确定这是怎么做的),例如:

import datetime
import os

import cv2
import face_recognition
from PIL import Image

import config
from helpers import get_known_faces, add_to_json, add_captured_face

print('Running...')
cap_vid = cv2.VideoCapture(config.stream_url)
while True:
    ret, frame = cap_vid.read()
    rgb_frame = frame[:, :, ::-1]
    curr_face_encs = face_recognition.face_encodings(rgb_frame)
    curr_face_locs = face_recognition.face_locations(rgb_frame)
    face_names_in_DB, face_encs_in_DB = get_known_faces()
    if curr_face_encs:
        for idx, face in enumerate(curr_face_encs):
            res = face_recognition.compare_faces(face_encs_in_DB, face)
            if True in res:
                frame_data = {
                    'full_name': face_names_in_DB[res.index(True)],
                    'time': datetime.datetime.now().isoformat()
                }
                add_to_json(config.members_json_data, frame_data)
            else:
                top, right, bottom, left = curr_face_locs[idx]
                img = Image.fromarray(rgb_frame[top:bottom, left:right])
                img_name = '%s-%d.jpg' % (
                    datetime.datetime.now().isoformat().replace(':', 'c'), idx)
                img_path = os.path.join(config.stranger_dir, img_name)
                img.resize((40, 40)).save(img_path)
                add_captured_face(img_path,
                                datetime.datetime.now().isoformat())

这一次它给了我以下错误:

Traceback (most recent call last):
File "C:/Users/turalm01/Desktop/work/ITSFaceDetector/main.py", line 25, in <module>
    rgb_frame = frame[:, :, ::-1]
TypeError: 'NoneType' object is not subscriptable

注意:我想提一个也许你们想知道的细节。在另一个Python模块中,我成功地从同一IP摄像机获取了实时视频流,但是有一个小错误,那就是我接收的视频是整个帧的右上角。

0 个答案:

没有答案