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()