【问题标题】:Receiving the error in Opencv: (-215:Assertion failed) !_src.empty() in function 'cvtColor'在 Opencv 中收到错误:(-215:Assertion failed) !_src.empty() in function 'cvtColor'
【发布时间】:2021-07-12 22:04:50
【问题描述】:

我目前正在使用 opencv 来识别屏幕上的个人,并且我已经创建了一个模型,但是我收到了这个错误:

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

我搜索了许多论坛试图找到解决方案,但没有一个真正有帮助。以下是卡片的一部分。

#Creates the video capture
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)
camera.set(3, 650)  # Sets the camera width
camera.set(4, 480)  # Sets the camera height

# Creates the window size of for the face
minW = 0.1 * camera.get(3)
minH = 0.1 * camera.get(4)

face_module = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
eye_module = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml")

recogniser = cv2.face.LBPHFaceRecognizer_create()  # This will rea the LBPH method
recogniser.read("/content/trainner/trainner.yml")  # This is where the YML file will be read


def getStudent(ID):
    connection = sqlite3.connect("FaceBase.db")
    cmd = "SELECT * FROM Class WHERE ID=" + str(ID)
    cursor = connection.execute(cmd)
    student=None
    for row in cursor:
        student=row
    connection.close()
    return student


df = pd.read_csv("attendance.csv")
columnNames = ["ID", "Full Name", "Major", "Time"]
attendance = pd.DataFrame(columns=columnNames)

# This is where the detection logic will occur
while (True):
    ret, frame = camera.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    face = face_module.detectMultiScale(gray, 1.5, 5)


【问题讨论】:

  • 能否检查ret是否为真,并打印frame.shape
  • @Burak 感谢您的回答!我已经更新了问题的答案

标签: python python-3.x opencv machine-learning


【解决方案1】:

上面显示的代码有两个问题,其中之一是:

recogniser.read("/content/trainner/trainner.yml")

这带来了关于 trainner 文件路径的错误,但要解决这个问题,您需要实现它的绝对路径。

recogniser.read(r"/Users/...../Documents/....../trainner/trainner.yml") 

要修复 cv2Colour 问题,您需要在摄像头读取下方的代码中添加

    if ret is True:
        #print("frame.shape")
    
    else:
         continue

希望这对那里的人有所帮助!

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 2019-06-26
    • 2021-02-08
    • 2021-04-19
    • 2019-08-30
    • 2021-07-30
    • 2023-04-01
    • 2019-05-24
    • 1970-01-01
    相关资源
    最近更新 更多