Advertisement
Guest User

Untitled

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