title: Python opencv code freezes after around 30 seconds of running
body: <p>My object detection opencv code runs well for around half a minute, then freezes. When I move the object that it's supposed to be detecting off the camera, the windows showing the frame (and I'm assuming the code aswell) unfreezes until I bring the object back into the camera's view (it immediately freezes again if it recognizes the object).</p>
  
  <p>I have serial communication hooked up from the opencv script to an arduino that controls motors that orient the camera so the object I'm detecting gets moved to the center of the camera POV. </p>
  
  <p>I thought it might be a processing issue so I got rid of the <code>.erode()</code> and <code>.dilate()</code> commands but it didn't change anything. It seems like the code is freezing either when the color mask is being made or when the contours are being searched for.</p>
  
  <p>Python code</p>
  
  <pre><code>import cv2
  import numpy
  import serial
  import time
  
  ser=serial.Serial("COM11", 9600) #open serial port
  
  def nothing(x):
      pass
  
  capture=cv2.VideoCapture(0)
  
  while capture.isOpened:
  
      lower_hsv=numpy.array([60, 90, 116]) #hsv values set so that it only sees a certain shade of blue
      upper_hsv=numpy.array([125, 251, 255])
  
      ret, frame=capture.read()
      blur=cv2.GaussianBlur(frame, (5,5), 0) #blurs the BGR
      hsv=cv2.cvtColor(blur,cv2.COLOR_BGR2HSV) #changes blurred BGR to HSV
  
      mask=cv2.inRange(hsv, lower_hsv, upper_hsv)
      mask=cv2.erode(mask, None, iterations=2)
      mask=cv2.dilate(mask, None, iterations=2)
  
      contours,_=cv2.findContours(mask.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
      if len(contours)&gt;0:
          for contour in contours:
              if cv2.contourArea(contour) &lt; 800:
                  continue
              else:
                  (x,y), radius=cv2.minEnclosingCircle(contour)
                  center=(int(x), int(y))
                  radius=int(radius)
                  cv2.circle(frame, center, radius, (0, 255, 0), 2)
                  print(center)
                  if center[0]&lt;300:
                      ser.write('L'.encode()) #if ball is to the left, rotate the motor to the left
                  elif center[0]&gt;340:
                      ser.write('R'.encode()) #if ball is to the right, rotate motor to the right
                  elif center[1]&lt;220:
                      ser.write('D'.encode()) #if the ball is below, rotate the motor down
                  elif center[1]&gt;260:
                      ser.write('U'.encode())
                  else:
                      print('not sending anything')
  
      result=cv2.bitwise_and(frame, frame, mask=mask)
  
      cv2.imshow('original',frame)
  
      if cv2.waitKey(1)==27:
          break
  
  
  cv2.destroyAllWindows()
  capture.release()
  </code></pre>
  
  <p>Arduino code</p>
  
  <pre><code>const int motorA_1 = 9;
  const int motorA_2 = 10;
  const int motorB_1 = 5;
  const int motorB_2 = 6;
  
  char inputData;
  
  void setup() {
    // put your setup code here, to run once:
  
    pinMode(motorA_1, OUTPUT);
    pinMode(motorA_2, OUTPUT);
    pinMode(motorB_1, OUTPUT);
    pinMode(motorB_2, OUTPUT);
  
    Serial.begin(9600);
    Serial.println("&lt;Arduino is ready&gt;");
  }
  
  void loop() {
    // put your main code here, to run repeatedly:
    if (Serial.available()&gt;0){
      inputData=Serial.read();
      if (inputData == 'R'){
        digitalWrite(motorA_1,HIGH);
        digitalWrite(motorA_2,LOW);
        delay(10);
      }  
      else if(inputData=='L'){
        digitalWrite(motorA_1,LOW);
        digitalWrite(motorA_2,HIGH);
        delay(10);
      }
      else if(inputData=='U'){
        digitalWrite(motorB_1,HIGH);
        digitalWrite(motorB_2,LOW);
        delay(10);
      }
      else if(inputData=='D'){
        digitalWrite(motorB_1,LOW);
        digitalWrite(motorB_2,HIGH);  
        delay(10);
      }
    }
    else{
      doNothing();
    }
  }
  
  void doNothing(){
    Serial.println("doing nothing");
    digitalWrite(motorA_1, LOW);
    digitalWrite(motorA_2, LOW);
    digitalWrite(motorB_1, LOW);
    digitalWrite(motorB_2, LOW);
    delay(20);
  </code></pre>
  
  <p>I want the script to run until I terminate it by pressing 'esc', but it crashes if I run it for more than 30 seconds.</p>