7

In this script :-

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('[email protected]', 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

I get this error :-

What am I doing wrong. I have even defined camera globally, that to TWICE. Can somone please point out my mistake in the code? I use python 2.7 with Opencv module

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

UPDATE Look above for updated code

8
  • You don't really need to pass global camera to the function in order to use it. Commented Oct 6, 2016 at 14:40
  • @Nf4r But still, it dosen't solve the problem Commented Oct 6, 2016 at 14:41
  • I do not see any error here. Is this the original code which you are using? Commented Oct 6, 2016 at 14:45
  • @HellfireCharchitPb: I notice that the line numbers in the error message do not correspond to the line numbers in your code. So, there is some more code! Also, notice that if you ever call Camera() you will delete the reference to camera(last line). From this point onward it should not be defined. Commented Oct 6, 2016 at 14:47
  • @M.Wymann There is some other code to, but I do not think that it will matter, however i will update it Commented Oct 6, 2016 at 14:49

1 Answer 1

5

You need to have defined camera outside the scope of your methods as well. What the global keyword does is tell Python that you will modify that variable which you defined externally. If you haven't, you get this error.

EDIT

I didn't notice that you had already declared camera externally. However, you delete the variable inside the Camera() method, which has pretty much the same effect when you try to modify the variable again.

EDIT 2

Now that I can see what your code really does and what you intend to do, I don't think you should be working with a global camera at all, but pass it as parameter instead. This should work:

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('[email protected]', 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
Sign up to request clarification or add additional context in comments.

11 Comments

You mean outside the functions?
Yes, but it's deleted inside Camera. I forgot to add this to my answer, sorry.
Oh yes. I missed that part. Good Catch (y)
@HellfireCharchitPb Yes, but I didn't notice you had already done that. I've updated my answer targetting the actual problem.
I get this error now File "C:\Python27\Scripts\Servers.py", line 16, in get_image retval, im = camera.read() AttributeError: 'NoneType' object has no attribute 'read'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.