why this code loop forever after a draw a rectangle

Gary Herron gherron at digipen.edu
Fri Sep 16 15:14:30 EDT 2016


On 09/16/2016 04:24 AM, meInvent bbird wrote:
> im = img.copy()
> cntcounter = 0
> for cnt in contours:
>          epsilon = 0.1*cv2.arcLength(cnt,True)
>          approx = cv2.approxPolyDP(cnt,epsilon,True)	
>          #peri = cv2.arcLength(cnt, True)
>          #approx = cv2.approxPolyDP(c, 0.5 * peri, True)
>          #print("len(approx)="+str(len(approx)))
>          if len(approx) == 4:
>              print("approx=" + str(approx))
>              cntcounter = cntcounter + 1
>              print("here1")
>              x,y,w,h = cv2.boundingRect(cnt)
>              print("here2")
>              while im is None:
>                  time.sleep(1)
>              if im is not None:
>                  print("here3")
>                  im = cv2.rectangle(im.copy(), (x,y), (x+w, y+h), (0,255,0), 2)
>                  #im = cv2.line(im,(x,y),(x+w,y),(0,255,0),2)
>                  #im = cv2.line(im,(x+w,y),(x+w,y+h),(0,255,0),2)
>                  #im = cv2.line(im,(x,y+h),(x+w,y+h),(0,255,0),2)
>                  #im = cv2.line(im,(x,y),(x,y+h),(0,255,0),2)
>
>
> cv2.imwrite(r'C:\Users\tester\Documents\masda.png',im)


These two lines:

   while im is None:
     time.sleep(1)

are an infinite loop if im is None;


Since you haven't told us what im (or img, contours, cv2) are, I can't 
tell how im might become None, but it does look like you (confusingly) 
use im for two different things:  an img.copy() and a cv2.rectangle, 
whatever those may be.

Pure guesswork:  if cv2.rectangle draws a rectangle, what does it 
return?  If it doesn't return anything, the line
     im = cv2.rectangle(...)
is how im gets the value of None.

-- 
Dr. Gary Herron
Professor of Computer Science
DigiPen Institute of Technology
(425) 895-4418




More information about the Python-list mailing list