Opencv:用python导入highgui

发布于 2024-11-03 22:06:29 字数 847 浏览 0 评论 0原文

使用以下代码:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  frame = cv.QueryFrame(capture)
  cv.ShowImage("w1", frame)
  c = highgui.cvWaitKey(10)
  if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index = 0
        capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()

Traceback(最近一次调用最后一次): 文件“pycam.py”,第 21 行,位于 重复() 文件“pycam.py”,第 12 行,重复 c = highgui.cvWaitKey(10) NameError:未定义全局名称“highgui” 清理了相机。

With the following code:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  frame = cv.QueryFrame(capture)
  cv.ShowImage("w1", frame)
  c = highgui.cvWaitKey(10)
  if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index = 0
        capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()

Traceback (most recent call last):
File "pycam.py", line 21, in
repeat()
File "pycam.py", line 12, in repeat
c = highgui.cvWaitKey(10)
NameError: global name 'highgui' is not defined
Cleaned up camera.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-11-10 22:06:29

新的 API 发生了相当多的变化。以下内容将起作用:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  frame = cv.QueryFrame(capture)
  cv.ShowImage("w1", frame)
  c = cv.WaitKey(10)
  if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index = 0
        capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()

这是一种更简单、更清晰的语法!

There have been quite a few changes in the new API. The following will work:

import cv

cv.NamedWindow("w1", cv.CV_WINDOW_AUTOSIZE)
camera_index = 0
capture = cv.CaptureFromCAM(camera_index)

def repeat():
  global capture #declare as globals since we are assigning to them now
  global camera_index
  frame = cv.QueryFrame(capture)
  cv.ShowImage("w1", frame)
  c = cv.WaitKey(10)
  if(c=="n"): #in "n" key is pressed while the popup window is in focus
    camera_index += 1 #try the next camera index
    capture = cv.CaptureFromCAM(camera_index)
    if not capture: #if the next camera index didn't work, reset to 0.
        camera_index = 0
        capture = cv.CaptureFromCAM(camera_index)

while True:
    repeat()

It is a simpler, cleaner syntax!

千笙结 2024-11-10 22:06:29

这意味着 highgui 不存在。尝试通过以下方式导入:from opencv import *

如果这不起作用,请尝试检查您的 opencv 安装。

This means that highgui doesn't exist. Try importing this way: from opencv import *.

If that doesn't work try and check your opencv instalation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文