Python OpenCV 在 PyCharm Windows 上无法打开摄像头。

huangapple go评论99阅读模式
英文:

Pyhton OpenCv not opening webcam on pycharm windows

问题

我已经尝试了每一个解决方案... 它就是不起作用。

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

cv2.namedWindow("窗口")

while True:
    ret, frame = cap.read()
    cv2.imshow("窗口", frame)

    # 这会在按下 'q' 键时退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

即使在终端上运行代码也没有问题,我找不到任何错误。文件正常运行,但摄像头不会打开。摄像头已更新,是我的笔记本内置摄像头。我尝试了cv2.VideoCapture(-1)也不起作用。

主要问题是它不会打开摄像头窗口。除此之外一切都看起来正常。

使用Python-3.11.0
Pycharm 2022 版本
Windows 10

我已安装了OpenCV
我已安装了Mediapipe
我已安装了CVzone

什么都没用。

英文:

I have tried every single solution... It's just not working.

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

cv2.namedWindow("Window")

while True:
    ret, frame = video_capture.read()
    cv2.imshow("Window", frame)

    #This breaks on 'q' key
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

The code runs fine even on terminal i couldn't find anything wrong. The file is running perfectly but the webcam is not opening. The webcam is upadted and internal webcam of my laptop. I tried cv2.videoCapture(-1) that's also not working.

The main problem is it's not opening the webcam window. Otherwise everything seems fine.

Using Python-3.11.0
Pycharm 2022 version
windows 10

I have installed openCV
I have installed mediapipe
I have installed CVzone

nothing is doing.

答案1

得分: 1

video_capture 未定义,但您已经定义了 cap = cv2.VideoCapture(0)。

您可以将以下代码添加到您的程序中以解决此问题:

video_capture = cv2.VideoCapture(0)
ret, frame = video_capture.read()
英文:

You are calling

ret, frame = video_capture.read()

but video_capture has not been defined. You did define

cap = cv2.VideoCapture(0)

huangapple
  • 本文由 发表于 2023年7月3日 15:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定