【问题标题】:camera not responding for opencv videocapture相机对opencv视频捕获没有响应
【发布时间】:2022-11-10 06:27:25
【问题描述】:

我打算将 opencv 工作作为我项目的一部分。 我想从网络摄像头拍摄图像并进行处理。所以我使用了videocapture()。 当我使用它时,相机没有响应。 同一个程序,我在 Visual Studio 和 jupyter notbook 都试过。两者结果相同。 代码如下:

import cv2 
import matplotlib.pyplot as plt
key = cv2. waitKey(1)
webcam = cv2.VideoCapture(-1)
while True:
    try:
        check, frame = webcam.read()
        print(check) #prints true as long as the webcam is running
        #print(frame) #prints matrix values of each framecd 
        cv2.imshow("Capturing", frame)
        key = cv2.waitKey(1)
        if key == ord('s'): 
            cv2.imwrite(filename='saved_img.jpg', img=frame)
            webcam.release()
            img_new = cv2.imread('saved_img.jpg', cv2.IMREAD_GRAYSCALE)
            img_new = cv2.imshow("Captured Image", img_new)
            cv2.waitKey(1650)
            cv2.destroyAllWindows()
            print("Processing image...")
            img_ = cv2.imread('saved_img.jpg', cv2.IMREAD_ANYCOLOR)
            print("Converting RGB image to grayscale...")
            gray = cv2.cvtColor(img_, cv2.COLOR_BGR2GRAY)
            print("Converted RGB image to grayscale...")
            print("Resizing image to 28x28 scale...")
            img_ = cv2.resize(gray,(28,28))
            print("Resized...")
            img_resized = cv2.imwrite(filename='saved_img-final.jpg', img=img_)
            print("Image saved!")
            plt.show()
            break
        elif key == ord('q'):
            print("Turning off camera.")
            webcam.release()
            print("Camera off.")
            print("Program ended.")
            cv2.destroyAllWindows()
            break
        
    except(KeyboardInterrupt):
        print("Turning off camera.")
        webcam.release()
        print("Camera off.")
        print("Program ended.")
        cv2.destroyAllWindows()
        break

print(check)
print(frame)

正在返回

False
None

我什至尝试过 videocapture(0) 和 videocapture(-1) 我的系统或代码中是否存在问题 如何解决这个问题。

【问题讨论】:

  • 它可以在我的笔记本电脑上使用cv.videoCapture(0)。你确定你的相机连接正确吗?如果您使用的是笔记本电脑,则可访问性可能存在一些问题。尝试以管理员或类似的方式运行 IDE。
  • 始终在创建 assert webcam.isOpened() 后立即检查它。如果这甚至不起作用,那么您的所有其余代码都是无关紧要的。

标签: python opencv


【解决方案1】:

提示 1:当您的唯一目标是测试网络摄像头时,您的代码量过多 - 请参阅最小化脚本来检查摄像头。
提示2:关闭或暂停杀毒。我多次看到反病毒(卡巴斯基和可能是 AVG)阻止 python 打开网络摄像头(我想是为了避免有人入侵你的摄像头)。

import cv2


def cam_test(port: int = 0) -> None:
    cap = cv2.VideoCapture(port)
    if not cap.isOpened():  # Check if the web cam is opened correctly
        print("failed to open cam")
    else:
        print('cam opened on port {}'.format(port))

        for i in range(10 ** 10):
            success, cv_frame = cap.read()
            if not success:
                print('failed to capture frame on iter {}'.format(i))
                break
            cv2.imshow('Input', cv_frame)
            k = cv2.waitKey(1)
            if k == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()
    return


if __name__ == '__main__':
    cam_test()

【讨论】:

    【解决方案2】:

    在将 cv2.VideoCapture(0) 与此代码一起使用时,我也遇到了同样的问题:

    cap = cv2.VideoCapture(0)
    while True:
        success, img = cap.read()
        cv2.imshow("Result", img)
        key = cv2.waitKey(0)
        if key == 27:
            break
    cap.release()
    cv2.destroyAllWindows()
    

    但在我之前的项目中,我只是使用了变量 web_cam 作为 web_cam = cv2.VideoCapture(0),之前在另一个项目中成功运行。 上一个项目的代码:

    web_cam = cv2.VideoCapture(0)
    while True:
        success, img = web_cam.read()
        cv2.imshow('WebCam', img) 
        key = cv2.waitKey(1)
        if key == 27:
            break
    web_cam.release()
    cv2.destroyAllWindows() 
    print("Success !!!") 
    

    当我复制该代码时,令人惊讶的是,它可以正常工作而不会出现“相机没有响应”的错误。两个块中的所有代码都相同,但差异仅在于变量“cap”和“web_cam”。尝试更改变量名一次。愿它起作用。

    这不是确切的解决方案,但我在这里分享了我在工作阶段所经历的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多