Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

re-order my code into a function

I'm trying to re order my code and move the commented out lines to the function accu_w(frame). When I do this I get the error:

if avg1 is None: avg1 = np.float32(frame)
UnboundLocalError: local variable 'avg1' referenced before assignment

I'm struggling to understand:

1) What this means?

2) How I can fix it?

The code:

import cv2
import numpy as np

def accu_w(frame):

    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            accu_w(frame)

            '''
            if avg1 is None: avg1 = np.float32(f)
            if avg2 is None: avg2 = np.float32(f)

            cv2.accumulateWeighted(f, avg1, 0.05)
            cv2.accumulateWeighted(f, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)

            cv2.imshow('img',f)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

re-order my code into a function

I'm trying to re order my code and move the commented out lines to the function accu_w(frame). When I do this I get the error:

if avg1 is None: avg1 = np.float32(frame)
UnboundLocalError: local variable 'avg1' referenced before assignment

I'm struggling to understand:

1) What this means?

2) How I can fix it?

The code:

import cv2
import numpy as np

def accu_w(frame):

    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            accu_w(frame)

            '''
            if avg1 is None: avg1 = np.float32(f)
np.float32(frame)
            if avg2 is None: avg2 = np.float32(f)

            cv2.accumulateWeighted(f, np.float32(frame)

            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(f, cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)

            cv2.imshow('img',f)
cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

re-order my code into a function

I'm trying to re order my code and move the commented out lines to the function accu_w(frame). When I do this I get the error:

if avg1 is None: avg1 = np.float32(frame)
UnboundLocalError: local variable 'avg1' referenced before assignment

I'm struggling to understand:

1) What this means?

2) How I can fix it?

The code:

import cv2
import numpy as np

def accu_w(frame):

    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            accu_w(frame)

            '''
            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)

            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

Iv'e managed to fix it by moving:

if avg1 is None: avg1 = np.float32(frame)
if avg2 is None: avg2 = np.float32(frame)

outside the function, but I still don't understand what the original error meant.

import cv2
import numpy as np

#REDUCED_SIZE       = (800, 600)# (1280x720)

def accu_w(frame):

    #if avg1 is None: avg1 = np.float32(frame)
    #if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)
    #sub = res1 - f
    #sub1 = res1*f 

    #cv2.imshow('sub',sub)
    #cv2.imshow('sub',sub1)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    #c.set(3,REDUCED_SIZE[0])
    #c.set(4,REDUCED_SIZE[1])


    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            accu_w(frame)
            '''

            cv2.accumulateWeighted(f, avg1, 0.05)
            cv2.accumulateWeighted(f, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)
            #sub = res1 - f
            #sub1 = res1*f 

            #cv2.imshow('sub',sub)
            #cv2.imshow('sub',sub1)

            cv2.imshow('img',f)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)



            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

re-order my code into a function

I'm trying to re order my code and move the commented out lines to the function accu_w(frame). When I do this I get the error:

if avg1 is None: avg1 = np.float32(frame)
UnboundLocalError: local variable 'avg1' referenced before assignment

I'm struggling to understand:

1) What this means?

2) How I can fix it?

The code:

import cv2
import numpy as np

def accu_w(frame):

    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            accu_w(frame)

            '''
            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)

            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

Iv'e managed to fix it by moving:

if avg1 is None: avg1 = np.float32(frame)
if avg2 is None: avg2 = np.float32(frame)

outside the function, but I still don't understand what the original error meant.

import cv2
import numpy as np

#REDUCED_SIZE       = (800, 600)# (1280x720)

def accu_w(frame):

    #if avg1 is None: avg1 = np.float32(frame)
    #if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)
    #sub = res1 - f
    #sub1 = res1*f 

    #cv2.imshow('sub',sub)
    #cv2.imshow('sub',sub1)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)



 def main():

    c = cv2.VideoCapture(0)

    #c.set(3,REDUCED_SIZE[0])
    #c.set(4,REDUCED_SIZE[1])


    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):

         got_frame,frame = c.read()

        if got_frame:


            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            accu_w(frame)
            '''

            cv2.accumulateWeighted(f, 
            '''        
            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(f, cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)
            #sub = res1 - f
            #sub1 = res1*f 

            #cv2.imshow('sub',sub)
            #cv2.imshow('sub',sub1)

            cv2.imshow('img',f)


            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)
 
            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

re-order my code into a function

I'm trying to re order my code and move the commented out lines to the function accu_w(frame). When I do this I get the error:

if avg1 is None: avg1 = np.float32(frame)
UnboundLocalError: local variable 'avg1' referenced before assignment

I'm struggling to understand:

1) What this means?

2) How I can fix it?

The code:

import cv2
import numpy as np

def accu_w(frame):

    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            accu_w(frame)

            '''
            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)

            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

Iv'e managed to fix it by moving:

if avg1 is None: avg1 = np.float32(frame)
if avg2 is None: avg2 = np.float32(frame)

outside the function, but I still don't understand what the original error meant.

import cv2
import numpy as np

#REDUCED_SIZE       = (800, 600)# (1280x720)
 
def accu_w(frame):

    #if cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)



def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):

        got_frame,frame = c.read()

        if got_frame:


            if avg1 is None: avg1 = np.float32(frame)
    #if         if avg2 is None: avg2 = np.float32(frame)

            accu_w(frame)

            '''        
            cv2.accumulateWeighted(frame, avg1, 0.05)
     cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)
    #sub = res1 - f
    #sub1 = res1*f 

    #cv2.imshow('sub',sub)
    #cv2.imshow('sub',sub1)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)



def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):

        got_frame,frame = c.read()

        if got_frame:


            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            accu_w(frame)

            '''        
            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)


            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

re-order my code into a function

I'm trying to re order my code and move the commented out lines to the function accu_w(frame). When I do this I get the error:

if avg1 is None: avg1 = np.float32(frame)
UnboundLocalError: local variable 'avg1' referenced before assignment

I'm struggling to understand:

1) What this means?

2) How I can fix it?

The code:

import cv2
import numpy as np

def accu_w(frame):

    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:


            accu_w(frame)

            '''
            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)

            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

Iv'e managed to fix it by moving:

if avg1 is None: avg1 = np.float32(frame)
if avg2 is None: avg2 = np.float32(frame)

outside the function, but I still don't understand what the original error meant.

import cv2
import numpy as np



def accu_w(frame):

    cv2.accumulateWeighted(frame, avg1, 0.05)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)

    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)



def main():

    c = cv2.VideoCapture(0)

    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):

        got_frame,frame = c.read()

        if got_frame:


            if avg1 is None: avg1 = np.float32(frame)
            if avg2 is None: avg2 = np.float32(frame)

            accu_w(frame)

            '''        
            cv2.accumulateWeighted(frame, avg1, 0.05)
            cv2.accumulateWeighted(frame, avg2, 0.0006)

            res1 = cv2.convertScaleAbs(avg1)
            res2 = cv2.convertScaleAbs(avg2)


            cv2.imshow('img',frame)
            cv2.imshow('avg1',res1)
            cv2.imshow('avg2',res2)

            '''

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()

This appeared to work too, I think its because I have 2 functions and I need to declare the global variables inside each function. I thought if I declare something as global anywhere it could be seen by everything; but one has to declare global variables from within a function for that function to see it from outside.

import cv2
import numpy as np

def accu_w(frame):

    global avg1
    global avg2


    if avg1 is None: avg1 = np.float32(frame)
    if avg2 is None: avg2 = np.float32(frame)

    cv2.accumulateWeighted(frame, avg1, 0.1)
    cv2.accumulateWeighted(frame, avg2, 0.0006)

    res1 = cv2.convertScaleAbs(avg1)
    res2 = cv2.convertScaleAbs(avg2)


    cv2.imshow('img',frame)
    cv2.imshow('avg1',res1)
    cv2.imshow('avg2',res2)




def main():

    c = cv2.VideoCapture(0)


    global avg1
    avg1 = None
    global avg2
    avg2 = None

    while(True):


        got_frame,frame = c.read()

        if got_frame:

            accu_w(frame)

            k = cv2.waitKey(20)

            if k == 27:
                break

    cv2.destroyAllWindows()
    c.release()


main()