首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在cv2操作和操作后得到以下错误?错误:(-215:断言失败) !src.empty()在函数'cvtColor‘中

为什么在cv2操作和操作后得到以下错误?错误:(-215:断言失败) !src.empty()在函数'cvtColor‘中
EN

Stack Overflow用户
提问于 2022-02-15 07:51:15
回答 1查看 66关注 0票数 0

事情正常工作30分钟到一个小时,然后我得到以下错误:

代码语言:javascript
复制
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'

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

代码语言:javascript
复制
    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的最新版本。如有任何意见,将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-16 08:08:48

我已经将if(ret == True)添加到以下内容中,并且有6个小时没有出现错误:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71122785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档