Why do I get the following error after and hour of operation cv2.error:(-215:Assertion failed) !src.empty() in function 'cvtColor'

Why do I get the following error after and hour of operation cv2.error:(-215:Assertion failed) !src.empty() in function 'cvtColor'

在 30 分钟到一个小时内一切正常,然后我收到以下错误:

active
inactive
active
active
active
inactive
active
Traceback (most recent call last):
  File "interface/larry5.py", line 93, in <module>
    vidCapture(c)
  File "interface/larry5.py", line 33, in vidCapture
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.4-dev) /tmp/pip-req-build-6qnmwb6g/opencv/modules/imgproc/src/color.cpp:182: 
error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

我的相机 运行 在两部三星手机上使用 IPcam,分辨率为 176x144,分辨率为 10fps。我下面的代码使用数据库来使用 OpenCV 切换相机。

    import cv2
    import sqlite3
    from datetime import datetime
    connection = sqlite3.connect("/home/harry/interface/wildlife.db")
    cursor = connection.cursor()
    x = 1
    c = 1
    streamTarget  = ''
    def activityScan(status,c):
        sql=cursor.execute("SELECT * FROM heavenStream WHERE id = ?",(c,))
        dbstatus = sql.fetchone()
        if(dbstatus[8]!=status):
            putMoe=(status,c)
            cursor.execute("UPDATE heavenStream SET streamTime =? where id=?", putMoe)
            connection.commit()
    
    def xmlFile(c):
        row=cursor.execute("SELECT * FROM heavenStream WHERE id = ?",(c,))
        streamTarget = row.fetchone()
        return streamTarget[2]
    
    def ipAddress(c):
        row=cursor.execute("SELECT * FROM heavenStream WHERE id = ?",(c,))
        streamTarget = row.fetchone()
    def vidCapture(c):
        h=0
        face_cascade = cv2.CascadeClassifier(xmlFile(c))
        cap = cv2.VideoCapture(ipAddress(c))
      #  while 1:
        ret, img = cap.read()
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
            roi_gray = gray[y:y + h, x:x + w]
            roi_color = img[y:y + h, x:x + w]
    
        if(h):
            print("active")
            activityScan("active",c)
        else:
            print("inactive")
            activityScan("inactive",c)
        cv2.imshow('img', img)
    #    if cv2.waitKey(1) & 0xFF == 27:
    #           break
    
       cap.release()
   #     cv2.destroyAllWindows()
    
    def sensorAddress(moe):
        ab = int(datetime.today().strftime('%S'))
        if (ab % 2) == 0 :
            return moe
    #       print("...")
        else:
            y = last_id() - 1 
            if(moe >= y and y % 2 == 0):
                moe = 1
                return moe 
               # print(y)
            elif(moe >= y and y % 2 !=0):
                moe = 1
                return moe 
      #          print(".. .. --")
            else:
                moe = moe + 2
                return moe 
    
    def last_id():                  ### Last ID in database
        sql = cursor.execute('SELECT max(id) FROM heavenStream')
        max_id = sql.fetchone()[0]
        return max_id 
    
    def moeValue(moe): ## Larry
        if(moe == last_id()):
            moe = 1
            return sensorAddress(moe)
        elif(moe == last_id() - 1 and moe % 2 != 0):
            moe = 1
            return sensorAddress(moe)
        elif(moe % 2 ) == 0:
            moe = moe + 1
            return sensorAddress(moe)
        else:
           moe = moe + 2
           return sensorAddress(moe)
    while True:
        row=cursor.execute("SELECT * FROM heavenStream WHERE id = ?",(x,))
        streamTarget = row.fetchone()
        if ('moe' == streamTarget[8]):
           c = moeValue(streamTarget[0])
           vidCapture(c)
        lastDBrecord = last_id()
        if(x >= lastDBrecord):
           x = 1
        else:
           x = x + 1

它可以正常工作长达一个小时然后失败。我使用来自以下网站 https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ 的代码作为基础,尽管我只有一个 xml 文件。我是 运行 最新版本的 Linux Mint。任何意见将不胜感激。

我已经添加了 if(ret == True): 到下面并且六个小时没有错误:

ret, img = cap.read()
if(ret == True):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = img[y:y + h, x:x + w]
    if(h):
        print("active")
        print(ret)
        activityScan("active",c)
    else:
        print("inactive")
        print(ret)
        activityScan("inactive",c)