OpenCv 错误无法通过视频捕获打开相机 [英] OpenCv error can't open camera through video capture

查看:1017
本文介绍了OpenCv 错误无法通过视频捕获打开相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过 opencv 使用我的摄像头,然后在重新启动后突然运行我的代码,它显示以下错误:

I was using my cam through opencv and suddenly after restarting I ran my code it shows below error:

[ WARN:0] global /io/opencv/modules/videoio/src/cap_v4l.cpp (802) open VIDEOIO ERROR: V4L: can't open camera by index 0
Traceback (most recent call last):
  File "test.py", line 20, in <module>
    retval, buffer_img = cv2.imencode('.jpg', frame)
cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:877: error: (-215:Assertion failed) !image.empty() in function 'imencode'

cap = cv2.VideoCapture(0)  # here it throws an error


import json
while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    retval, buffer_img = cv2.imencode('.jpg', frame)

    resdata = base64.b64encode(buffer_img)

    resdata = "data:image/png;base64,"+ str(resdata.decode("utf-8"))
    PARAMS = {'image': resdata}

    # Our operations on the frame come here
    #gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

我也尝试过 cap = cv2.VideoCapture(1) 但它显示找不到相机

I also tried with cap = cv2.VideoCapture(1) but then it shows can't find camera

我该如何解决这个问题?

How can I fix this issue?

推荐答案

我遇到了同样的错误.尝试将 0 更改为 -1

I got the same error. Try changing 0 to -1

cap = cv2.VideoCapture(-1)

这解决了问题.

这篇关于OpenCv 错误无法通过视频捕获打开相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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