我有face_recognition python脚本,如果执行可以正常运行,但是如果执行则随机显示错误:
Traceback (most recent call last):
File "faces_test.py", line 38, in <module>
rgb_frame = frame[:, :, ::-1]
TypeError: 'NoneType' object is not subscriptable
该错误表明有时运行有时会出现该错误。我不知道会发生什么,但是我之前将api.py的face_recognition容忍度从0.6更改为0.4。我不确定chage会对opencv造成随机错误
我想运行我的脚本,并且不会得到这样的随机错误,任何解决方案?
环境版本:
我的python脚本:
import face_recognition
import cv2
import numpy as np
# This is a super simple (but slow) example of running face recognition on live video from your webcam.
# There's a second example that's a little more complicated but runs faster.
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("./training/obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]
# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("./training/biden.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]
# Create arrays of known face encodings and their names
known_face_encodings = [
obama_face_encoding,
biden_face_encoding
]
known_face_names = [
"obama",
"biden"
]
while True:
# Grab a single frame of video
ret, frame = video_capture.read()
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
# Find all the faces and face enqcodings in the frame of video
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"
# If a match was found in known_face_encodings, just use the first one.
# if True in matches:
# first_match_index = matches.index(True)
# name = known_face_names[first_match_index]
# Or instead, use the known face with the smallest distance to the new face
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
#print(face_distances)
#print(best_match_index)
if matches[best_match_index]:
name = known_face_names[best_match_index]
print(name)
# 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()
最佳答案
很抱歉收到您的回复,但是如果您仍在使用OpenCV进行编码,那么可能会有所帮助。
第一评论
您可能没有通过网络摄像头源获得任何视频。
例如,看这段代码
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0 + cv2.CAP_DSHOW)
有时,摄像机将在插槽“1”而不是“0”中进行初始化。重要的是要考虑到这些初始化可能会反弹。如果要确保可以正确识别摄像机,则可以始终在变量中显式定义摄像机位置,还可以进行一些错误处理。问题一的示例:
def setup_camera():
"""
Initialize camera by calling cv2.VideoCapture
Produces a variable that you can read from using self.capture.read
If camera doesn't connect prints error and Camera not conntected
"""
device = '/dev/v4l/by-id/usb-HD_Camera_Manufacturer_USB_2.0_Camera-video-index0'
try:
capture = cv2.VideoCapture(device)
print("Camera Connection Successful")
self.capture.release()
except IOError as e:
print(e)
print("Camera Not Connected")
这至少可以帮助您入门,并且会告诉您是否未在录制摄像头。如果您希望能够在系统上轻松找到相机(并且您正在使用linux-我是一个Ubuntu专家,如果您使用的是Windows,抱歉。),您可以运行v4l-utils这样的简单程序包来查找相机。类似于以下代码:
sudo apt-get install v4l-utils
v4l2-ctl --list-devices
应该给您以下答复:USB 2.0 Camera: HD USB Camera (usb-0000:00:1a.0-1.5):
/dev/video0
然后,您可以使用udevadm运行命令以获取有关 / dev / video0 的更多信息:udevadm info --query=all --name=/dev/video0
这将返回如下内容:P: /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.5/1-1.5:1.0/video4linux/video0
N: video0
S: v4l/by-id/usb-HD_Camera_Manufacturer_USB_2.0_Camera-video-index0
S: v4l/by-path/pci-0000:00:1a.0-usb-0:1.5:1.0-video-index0
E: COLORD_DEVICE=1
E: COLORD_KIND=camera
E: DEVLINKS=/dev/v4l/by-path/pci-0000:00:1a.0-usb-0:1.5:1.0-video-index0 /dev/v4l/by-id/usb-HD_Camera_Manufacturer_USB_2.0_Camera-video-index0
E: DEVNAME=/dev/video0
E: DEVPATH=/devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.5/1-1.5:1.0/video4linux/video0
E: ID_BUS=usb
E: ID_FOR_SEAT=video4linux-pci-0000_00_1a_0-usb-0_1_5_1_0
E: ID_MODEL=USB_2.0_Camera
E: ID_MODEL_ENC=USB\x202.0\x20Camera
E: ID_MODEL_ID=9230
E: ID_PATH=pci-0000:00:1a.0-usb-0:1.5:1.0
E: ID_PATH_TAG=pci-0000_00_1a_0-usb-0_1_5_1_0
E: ID_REVISION=0100
E: ID_SERIAL=HD_Camera_Manufacturer_USB_2.0_Camera
E: ID_TYPE=video
E: ID_USB_DRIVER=uvcvideo
E: ID_USB_INTERFACES=:0e0100:0e0200:
E: ID_USB_INTERFACE_NUM=00
E: ID_V4L_CAPABILITIES=:capture:
E: ID_V4L_PRODUCT=USB 2.0 Camera: HD USB Camera
E: ID_V4L_VERSION=2
E: ID_VENDOR=HD_Camera_Manufacturer
E: ID_VENDOR_ENC=HD\x20Camera\x20Manufacturer
E: ID_VENDOR_ID=05a3
E: MAJOR=81
E: MINOR=0
E: SUBSYSTEM=video4linux
E: TAGS=:uaccess:seat:
E: USEC_INITIALIZED=23692957
如您所见, DEVLINKS 中的第二个值提供了可用于调用相机的硬件位置。第二条评论
OpenCV具有内置函数,可以对帧进行转换。您的方法和我将使用的示例都在this post中。为了更深入地研究事物,让我们尝试一下。
例如 -
您使用的代码方法很好:
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
如果问题二不起作用,请尝试使用此示例:rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
结论我希望这一答复能有所帮助。我不认为这是转换问题,但很可能是相机初始化问题。尝试获取更准确的相机进纸位置,这将为您提供更强大的相机调用方法。祝你今天愉快!
关于python - TypeError: 'NoneType'对象无法下标opencv-python/python face_recognition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60701743/