title: Select a static ROI on webcam video on python openCV
body: <p>I need to selec a ROI (region of interest), or work area, on a live video from my webcam and take a snapshot of ONLY this work area or ROI, but I can't found how to do this.</p>
  
  <p>In this page <a href="https://www.learnopencv.com/how-to-select-a-bounding-box-roi-in-opencv-cpp-python/" rel="nofollow noreferrer">https://www.learnopencv.com/how-to-select-a-bounding-box-roi-in-opencv-cpp-python/</a> have a code for draw a ROI but only with images, not a live video. </p>
  
  <pre><code>import cv2
  cam = cv2.VideoCapture(0)
  cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
  cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
  cv2.namedWindow("test")
  
  img_counter = 0
  
  while True:
      ret, frame = cam.read()
      cv2.imshow("test", frame)
      if not ret:
          break
      k = cv2.waitKey(1)
  
      if k % 256 == 27:
          # ESC pressed
          print("Escape hit, closing...")
          break
      elif k % 256 == 32:
          # SPACE pressed
          img_name = "opencv_frame_{}.png".format(img_counter)
          cv2.imwrite(img_name, frame)
          print("{} written!".format(img_name))
          img_counter += 1
  
  cam.release()
  
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>This code take a snapshot with space key but not draw a ROI area. 
  Thanks in advance!</p>