打开Cv错误:(-215:断言失败)!_src.empty()在函数'cv :: cvtColor'中 [英] Open Cv Error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

查看:1327
本文介绍了打开Cv错误:(-215:断言失败)!_src.empty()在函数'cv :: cvtColor'中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前在Windows 10和Python 3.7上使用
我尝试使用此代码运行以使网络摄像头能够用于数字识别

Currently using Windows 10 with Python 3.7 I tried running with this code in order to get the webcam to function for use in number recognition

def get_img_contour_thresh(img):
   x, y, w, h = 0, 0, 300, 300
   gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
   blur = cv2.GaussianBlur(gray, (5, 5), 0)
   ret, thresh1 = cv2.threshold(blur, 175, 255, cv2.THRESH_BINARY_INV + 
     cv2.THRESH_OTSU)
   thresh1 = thresh1[y:y + h, x:x + w]
   contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, 
     cv2.CHAIN_APPROX_SIMPLE)[-2:]
   return img, contours, thresh1

def show_webcam(mirror=False):
   # load json and create model
   json_file = open('model.json', 'r')
   loaded_model_json = json_file.read()
   json_file.close()
   model = model_from_json(loaded_model_json)
   # load weights into new model
   model.load_weights("model.h5")
   print("Loaded model from disk")

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

cap = cv2.VideoCapture(0)
while True:
    ret, img = cap.read()
    img, contours, thresh = get_img_contour_thresh(img)
    ans = ''


    if len(contours) > 0:
        contour = max(contours, key=cv2.contourArea)
        if cv2.contourArea(contour) > 500:
            x, y, w, h = cv2.boundingRect(contour)
            newImage = thresh[y:y + h, x:x + w]
            newImage = cv2.resize(newImage, (28, 28))
            newImage = np.array(newImage)
            newImage = newImage.astype('float32')
            newImage /= 255

            if K.image_data_format() == 'channels_first':
                newImage = newImage.reshape(1, 28, 28)
            else:
                newImage = newImage.reshape(28, 28, 1)
            newImage = np.expand_dims(newImage, axis=0)
            ans = model.predict(newImage).argmax()


    x, y, w, h = 0, 0, 300, 300
    cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
    cv2.putText(img, "CNN : " + str(ans), (10, 320),
                cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
    cv2.imshow("Frame", img)
    cv2.imshow("Contours", thresh)     


    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()


def main():
   show_webcam(mirror=True)


if __name__ == '__main__':
   main()

我收到此错误

 File "cam.py", line 75, in <module>
   main()
 File "cam.py", line 71, in main
   show_webcam(mirror=True)
 File "cam.py", line 34, in show_webcam
   img, contours, thresh = get_img_contour_thresh(img)
 File "cam.py", line 10, in get_img_contour_thresh
   gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 cv2.error: OpenCV(3.4.4) C:\projects\opencv- 
   python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion 
   failed) !_src.empty() in function 'cv::cvtColor'

Iv一直在寻找解决方案,但是还没有运气来解决这个问题,尝试环顾四周,但是却无法解决问题,你们是怎么想的?感谢提供的帮助

Iv been looking for a solution for a while but havent had any luck in figuring this out, tried looking around but just cant figure it out, what do you guys think? Appreciate the help given

推荐答案

您看到的错误是如果 None 通过 cv2.cvtColor 传递。

The error you're seeing is one that happens if None gets passed on cv2.cvtColor.

之后

ret, img = cap.read()

在继续操作之前,先检查 img是否为None 是一个好主意。根据您的输入源, cap.read()可能会失败。在我的一台笔记本电脑上,它至少失败了一次,然后才开始返回有效的图像。

it's a good idea to check that img is not None before proceeding. Depending on your input source, cap.read() can fail. On one my laptops, it fails at least once before starting to return valid images.

这篇关于打开Cv错误:(-215:断言失败)!_src.empty()在函数'cv :: cvtColor'中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆