如何修复下面的代码我得到了cv2.error

2024-04-18 09:09:00 发布

您现在位置:Python中文网/ 问答频道 /正文

import numpy as np
import face_recognition
import cv2
import os
path="C:/Users/HP/Desktop/face1/img/known/"
images=list()
classnames=[]
my_list=os.listdir(path)
for i in my_list:
    currentim=cv2.imread(f'{path}{i}')
    images.append(currentim)
    classnames.append(os.path.splitext(i)[0])
def findenc(images):
    encs=[]
    for img in images :
        img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
        encs.append(face_recognition.face_encodings(img)[0])
    return encs
encs=findenc(images)
print("encoding has ended")
cap=cv2.VideoCapture(0)
while True:
    success,img=cap.read()
    imgsize=cv2.resize(img,(0,0),None,0.25,0.25)
    imgsize=cv2.cvtColor(imgsize,cv2.COLOR_BGR2RGB)
    faces=face_recognition.face_locations(imgsize)[0]
    encodescurf=face_recognition.face_encodings(imgsize)[0]
    for encode,faceloc in zip(encodescurf,faces):
        matches=face_recognition.compare_faces(encs,encode)
        facedis=face_recognition.face_distance(encs,encode)

当ı运行代码时ıget cv2.error:OpenCV(4.5.3)C:\Users\runneramin\AppData\Local\Temp\pip-req-build-sn\u xpupm\OpenCV\modules\imgproc\src\color.cpp:182:错误:(-215:断言失败)_函数“cv::cvtColor”中的src.empty()如何修复该错误

注:cv2版本为“4.5.3” 我使用visualstudio代码 它给出了第16行的错误


Tags: pathinimportimgforoscv2list
1条回答
网友
1楼 · 发布于 2024-04-18 09:09:00

发生此错误的原因可能是图像加载不正确。检查cv2.imread或尝试:

encs = []
for i in my_list:
    img_path = os.path.join(path, i)
    currentim=cv2.imread(img_path) 
    classnames.append(os.path.splitext(i)[0])
    img=cv2.cvtColor(currentim,cv2.COLOR_BGR2RGB)
    encs.append(face_recognition.face_encodings(img)[0])

正确检查图像的路径

Python中的os.path.join()方法在每个非空部分(最后一个路径组件除外)后面使用一个目录分隔符('/')连接各种路径组件。如果要联接的最后一个路径组件为空,则在末尾放置一个目录分隔符(“/”)

相关问题 更多 >