【发布时间】: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