【问题标题】:Global name 'camera' is not defined in python全局名称“相机”未在 python 中定义
【发布时间】:2017-02-15 08:55:36
【问题描述】:

在这个脚本中:-

camera_port = 0
ramp_frames = 400
camera = cv2.VideoCapture(camera_port) 
def get_image():
  global camera
  retval, im = camera.read()
  return im

def Camera():
    global camera
    for i in xrange(ramp_frames):
     temp = get_image()
    print("Taking image...")
    camera_capture = get_image()
    file = "opencv.png"
    cv2.imwrite(file, camera_capture)
    del(camera)

def Sendmail():
    loop_value = 1
    while loop_value==1:
        try:
            urllib2.urlopen("https://google.com")
        except urllib2.URLError, e:
            print "Network currently down." 
            sleep(20)
        else:
            print "Up and running." 
            loop_value = 0
def Email():
    loop_value = 2
    while loop_value==2:
        try:
            Camera()
            Sendmail()
            yag = yagmail.SMTP('email',   'pass')
            yag.send('amitaagarwal565@gmail.com', subject = "This is    opencv.png", contents = 'opencv.png')
            print "done"
        except smtplib.SMTPAuthenticationError:
            print 'Retrying in 30 seconds'
            sleep(30)
        else:
            print 'Sent!'
            sleep(20)
            loop_value = 2

我收到此错误:-

我做错了什么。我什至在全球范围内定义了相机,即 TWICE。有人可以指出我在代码中的错误吗?我使用带有 Opencv 模块的 python 2.7

File "C:\Python27\Scripts\Servers.py", line 22, in Camera
    temp = get_image()
  File "C:\Python27\Scripts\Servers.py", line 16, in get_image
    retval, im = camera.read()
NameError: global name 'camera' is not defined

更新 查看上面的更新代码

【问题讨论】:

  • 您实际上不需要将global camera 传递给函数即可使用它。
  • @Nf4r 但仍然没有解决问题
  • 我在这里看不到任何错误。这是您使用的原始代码吗?
  • @HellfireCharchitPb:我注意到错误消息中的行号与代码中的行号不对应。所以,还有一些代码!另外,请注意,如果您曾经调用过Camera(),您将删除对camera(最后一行)的引用。从现在开始,它不应该被定义。
  • @M.Wymann 还有一些其他代码,但我认为这并不重要,但我会更新它

标签: python python-2.7 opencv


【解决方案1】:

您还需要在方法范围之外定义cameraglobal 关键字的作用是告诉 Python 您将修改您在外部定义的变量。如果你没有,你会得到这个 错误。

编辑

我没有注意到您已经在外部声明了camera。但是,您删除了 Camera() 方法中的变量,当您再次尝试修改变量时,效果几乎相同。

编辑 2

现在我可以看到您的代码真正做了什么以及您打算做什么,我认为您根本不应该使用全局 camera,而是将其作为参数传递。这应该有效:

camera_port = 0
ramp_frames = 400

def get_image(camera):
    retval, im = camera.read()
    return im

def Camera(camera):
    for i in xrange(ramp_frames):
        temp = get_image(camera)
    print("Taking image...")
    camera_capture = get_image(camera)
    file = "opencv.png"
    cv2.imwrite(file, camera_capture)

def Sendmail():
    loop_value = 1
    while loop_value==1:
        try:
            urllib2.urlopen("https://google.com")
        except urllib2.URLError, e:
            print "Network currently down." 
            sleep(20)
        else:
            print "Up and running." 
            loop_value = 0

def Email():
    loop_value = 2
    while loop_value==2:
        try:
            camera = cv2.VideoCapture(camera_port) 
            Camera(camera)
            Sendmail()
            yag = yagmail.SMTP('email',   'pass')
            yag.send('amitaagarwal565@gmail.com', subject = "This is    opencv.png", contents = 'opencv.png')
            print "done"
        except smtplib.SMTPAuthenticationError:
            print 'Retrying in 30 seconds'
            sleep(30)
        else:
            print 'Sent!'
            sleep(20)
            loop_value = 2

【讨论】:

  • 你的意思是在函数之外?
  • 是的,但它已在 Camera 中删除。我忘了在我的答案中添加这个,抱歉。
  • 哦,是的。我错过了那部分。好收获(y)
  • @HellfireCharchitPb 是的,但我没有注意到你已经这样做了。我已经针对实际问题更新了答案。
  • 我现在收到此错误File "C:\Python27\Scripts\Servers.py", line 16, in get_image retval, im = camera.read() AttributeError: 'NoneType' object has no attribute 'read'
猜你喜欢
  • 2017-02-03
  • 2013-08-23
  • 2013-11-12
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-26
  • 2013-07-28
相关资源
最近更新 更多