在这个脚本中:-
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
我收到了这个错误:
我做错了什么?我甚至全局定义了相机,而且是两次。请有人指出我的代码错误在哪里?我使用Python 2.7和Opencv模块。
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
更新 请查看上面的更新代码
Camera()
,则会删除对camera
(最后一行)的引用。从这一点开始,它不应该被定义。 - M. WymannEmail()
,应该发生以下情况。在第一次迭代中,当loop_value == 2
时,将调用函数Camera()
并删除camera
。在第二次迭代中,当遇到相同的调用时,camera
不再被定义(因为它已经被del(camera)
删除)。也许您应该通过将loop_value
设置为非2的值来避免第二次迭代。 - M. Wymannloop_value == 1
。 - Hellfire Charchit Pb