Irregular Error - python cv2.imshow NameError: name "definition" is not defined
<p>I am running a video feed through a <code>cv2.imshow</code> operator. Most of the time, the code runs when I call it in command line, but occasionally I get the following NameError:</p>

<pre><code>NameError: name 'masked' is not defined
</code></pre>

<p>However, 'masked' is absolutely defined before it is called in cv2.imshow:</p>

<pre><code>image = frame.array

miniframe = image

masked = cv2.bitwise_and(image, image, mask=circle_img)

cv2.imshow("frame", masked)
</code></pre>

<p>same thing happens when 'masked' is defined this way:</p>

<pre><code>image = frame.array

miniframe = image

M = np.float32([[1,0,x+w/2],[0,1,y+h/2]])
masked  = cv2.warpAffine(image, M, (x+w, y+h))

cv2.imshow("frame", masked)
</code></pre>

<p>When <code>print masked</code> is inserted above the <code>cv2.imshow</code> line, it results in the array printed as numbers - when the code is working. When it is not working, it results in the NameError traceback pointing to <code>print masked</code>.</p>

<p>And to be clear, in either scenario the NameError happens seemingly randomly, without modification to the code. So, I can run it once or 20 times just fine, but then it will suddenly not work in command line, with a traceback to the <code>cv2.imshow</code> line. Sometimes, copy-pasting identical blocks of code from previous versions will get it to work again, but after a few unmodified tries, back to <code>NameError</code>. </p>

<p>I have read over <a href="https://stackoverflow.com/questions/5756777/python-nameerror-when-var-is-most-definitely-defined">Python NameError when var IS most definitely defined</a>, and used <code>cat -A filename.py</code> to check the invisible control characters. After de- and re-indenting, it worked 2 more times, and then right back to the NameError.</p>

<p>Any help would be much appreciated!</p>