text: #!/usr/bin/env python
  
  from picamera.array import PiRGBArray
  from picamera import PiCamera
  import numpy as np
  import cv2
  import time
  
  def read_rgb_image(image_name, show):
      rgb_image = cv2.imread(image_name)
      if show: 
          cv2.imshow("RGB Image",rgb_image)
      return rgb_image
  
  def filter_color(rgb_image, lower_bound_color, upper_bound_color):
      #convert the image into the HSV color space
      hsv_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2HSV)
      cv2.imshow("hsv image",hsv_image)
  
      #define a mask using the lower and upper bounds of the yellow color 
      mask = cv2.inRange(hsv_image, lower_bound_color, upper_bound_color)
  
      return mask
  
  
      
  
  def getContours(binary_image):     
      #_, contours, hierarchy = cv2.findContours(binary_image, 
      #                                          cv2.RETR_TREE, 
      #                                           cv2.CHAIN_APPROX_SIMPLE)
      _, contours, hierarchy = cv2.findContours(binary_image.copy(), 
                                              cv2.RETR_EXTERNAL,
  	                                        cv2.CHAIN_APPROX_SIMPLE)
      return contours
  
  
  def draw_ball_contour(binary_image, rgb_image, contours):
      black_image = np.zeros([binary_image.shape[0], binary_image.shape[1],3],'uint8')
      
      for c in contours:
          area = cv2.contourArea(c)
          perimeter= cv2.arcLength(c, True)
          ((x, y), radius) = cv2.minEnclosingCircle(c)
          if (area>3000):
              cv2.drawContours(rgb_image, [c], -1, (150,250,150), 1)
              cv2.drawContours(black_image, [c], -1, (150,250,150), 1)
              cx, cy = get_contour_center(c)
              print("cx : ", str(cx/640), "cy : ", str(cy/480))
              cv2.circle(rgb_image, (cx,cy),(int)(radius),(0,0,255),1)
              cv2.circle(black_image, (cx,cy),(int)(radius),(0,0,255),1)
              cv2.circle(black_image, (cx,cy),5,(150,150,255),-1)
              #print ("Area: {}, Perimeter: {}".format(area, perimeter))
      #print ("number of contours: {}".format(len(contours)))
      cv2.imshow("RGB Image Contours",rgb_image)
      cv2.imshow("Black Image Contours",black_image)
  
  def get_contour_center(contour):
      M = cv2.moments(contour)
      cx=-1
      cy=-1
      if (M['m00']!=0):
          cx= int(M['m10']/M['m00'])
          cy= int(M['m01']/M['m00'])
      return cx, cy
  
  def detect_ball_in_a_frame(image_frame):
      yellowLower =(30, 100, 50)
      yellowUpper = (60, 255, 255)
      rgb_image = image_frame
      binary_image_mask = filter_color(rgb_image, yellowLower, yellowUpper)
      contours = getContours(binary_image_mask)
      draw_ball_contour(binary_image_mask, rgb_image,contours)
  
  
  
  def main():
  
      # initialize the camera and grab a reference to the raw camera capture
      camera = PiCamera()
      camera.resolution = (640, 480)
      camera.framerate = 32
      rawCapture = PiRGBArray(camera, size=(640, 480))
  
      # allow the camera to warmup
      time.sleep(0.1)
  
      for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
          # grab the raw NumPy array representing the image, then initialize the timestamp
          # and occupied/unoccupied text
  
          detect_ball_in_a_frame(frame.array)
          time.sleep(0.033)
  
          # image = frame.array
          # # show the frame
          # cv2.imshow("Frame", image)
          # key = cv2.waitKey(1) & 0xFF
          # # clear the stream in preparation for the next frame
          # rawCapture.truncate(0)
  
          # if the `q` key was pressed, break from the loop
          if key == ord("q"):
              break
      cv2.waitKey(0)
      cv2.destroyAllWindows()
  
  if __name__ == '__main__':
      main()
  
  
  
  cv2.waitKey(0)
  cv2.destroyAllWindows()