Advertisement
Guest User

Untitled

a guest
Dec 25th, 2016
19,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.91 KB | None | 0 0
  1. #python color_tracking.py --video balls.mp4
  2. #python color_tracking.py
  3.  
  4. # import the necessary packages
  5. from collections import deque
  6. import numpy as np
  7. import argparse
  8. import imutils
  9. import cv2
  10. import urllib #for reading image from URL
  11.  
  12.  
  13. # construct the argument parse and parse the arguments
  14. ap = argparse.ArgumentParser()
  15. ap.add_argument("-v", "--video",
  16.     help="path to the (optional) video file")
  17. ap.add_argument("-b", "--buffer", type=int, default=64,
  18.     help="max buffer size")
  19. args = vars(ap.parse_args())
  20.  
  21. # define the lower and upper boundaries of the colors in the HSV color space
  22. lower = {'red':(166, 84, 141), 'green':(66, 122, 129), 'blue':(97, 100, 117), 'yellow':(23, 59, 119), 'orange':(0, 50, 80)} #assign new item lower['blue'] = (93, 10, 0)
  23. upper = {'red':(186,255,255), 'green':(86,255,255), 'blue':(117,255,255), 'yellow':(54,255,255), 'orange':(20,255,255)}
  24.  
  25. # define standard colors for circle around the object
  26. colors = {'red':(0,0,255), 'green':(0,255,0), 'blue':(255,0,0), 'yellow':(0, 255, 217), 'orange':(0,140,255)}
  27.  
  28. #pts = deque(maxlen=args["buffer"])
  29.  
  30. # if a video path was not supplied, grab the reference
  31. # to the webcam
  32. if not args.get("video", False):
  33.     camera = cv2.VideoCapture(0)
  34.    
  35.  
  36. # otherwise, grab a reference to the video file
  37. else:
  38.     camera = cv2.VideoCapture(args["video"])
  39. # keep looping
  40. while True:
  41.     # grab the current frame
  42.     (grabbed, frame) = camera.read()
  43.     # if we are viewing a video and we did not grab a frame,
  44.     # then we have reached the end of the video
  45.     if args.get("video") and not grabbed:
  46.         break
  47.  
  48.     #IP webcam image stream
  49.     #URL = 'http://10.254.254.102:8080/shot.jpg'
  50.     #urllib.urlretrieve(URL, 'shot1.jpg')
  51.     #frame = cv2.imread('shot1.jpg')
  52.  
  53.  
  54.     # resize the frame, blur it, and convert it to the HSV
  55.     # color space
  56.     frame = imutils.resize(frame, width=600)
  57.  
  58.     blurred = cv2.GaussianBlur(frame, (11, 11), 0)
  59.     hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
  60.     #for each color in dictionary check object in frame
  61.     for key, value in upper.items():
  62.         # construct a mask for the color from dictionary`1, then perform
  63.         # a series of dilations and erosions to remove any small
  64.         # blobs left in the mask
  65.         kernel = np.ones((9,9),np.uint8)
  66.         mask = cv2.inRange(hsv, lower[key], upper[key])
  67.         mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
  68.         mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
  69.                
  70.         # find contours in the mask and initialize the current
  71.         # (x, y) center of the ball
  72.         cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
  73.             cv2.CHAIN_APPROX_SIMPLE)[-2]
  74.         center = None
  75.        
  76.         # only proceed if at least one contour was found
  77.         if len(cnts) > 0:
  78.             # find the largest contour in the mask, then use
  79.             # it to compute the minimum enclosing circle and
  80.             # centroid
  81.             c = max(cnts, key=cv2.contourArea)
  82.             ((x, y), radius) = cv2.minEnclosingCircle(c)
  83.             M = cv2.moments(c)
  84.             center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
  85.        
  86.             # only proceed if the radius meets a minimum size. Correct this value for your obect's size
  87.             if radius > 0.5:
  88.                 # draw the circle and centroid on the frame,
  89.                 # then update the list of tracked points
  90.                 cv2.circle(frame, (int(x), int(y)), int(radius), colors[key], 2)
  91.                 cv2.putText(frame,key + " ball", (int(x-radius),int(y-radius)), cv2.FONT_HERSHEY_SIMPLEX, 0.6,colors[key],2)
  92.  
  93.      
  94.     # show the frame to our screen
  95.     cv2.imshow("Frame", frame)
  96.    
  97.     key = cv2.waitKey(1) & 0xFF
  98.     # if the 'q' key is pressed, stop the loop
  99.     if key == ord("q"):
  100.         break
  101.  
  102. # cleanup the camera and close any open windows
  103. camera.release()
  104. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement