|
| 1 | +# import the necessary packages |
| 2 | +import argparse |
| 3 | +import imutils |
| 4 | +import cv2 |
| 5 | + |
| 6 | +# construct the argument parse and parse the arguments |
| 7 | +ap = argparse.ArgumentParser() |
| 8 | +ap.add_argument("-v", "--video", help="path to the (optional) video file") |
| 9 | +args = vars(ap.parse_args()) |
| 10 | + |
| 11 | +# define the color ranges |
| 12 | +colorRanges = [ |
| 13 | + ((29, 86, 6), (64, 255, 255), "green"), |
| 14 | + ((57, 68, 0), (151, 255, 255), "blue")] |
| 15 | + |
| 16 | +# if a video path was not supplied, grab the reference to the webcam |
| 17 | +if not args.get("video", False): |
| 18 | + camera = cv2.VideoCapture(0) |
| 19 | + |
| 20 | +# otherwise, grab a reference to the video file |
| 21 | +else: |
| 22 | + camera = cv2.VideoCapture(args["video"]) |
| 23 | + |
| 24 | +# keep looping |
| 25 | +while True: |
| 26 | + # grab the current frame |
| 27 | + (grabbed, frame) = camera.read() |
| 28 | + |
| 29 | + # if we are viewing a video and we did not grab a frame, then we have |
| 30 | + # reached the end of the video |
| 31 | + if args.get("video") and not grabbed: |
| 32 | + break |
| 33 | + |
| 34 | + # resize the frame, blur it, and convert it to the HSV color space |
| 35 | + frame = imutils.resize(frame, width=600) |
| 36 | + blurred = cv2.GaussianBlur(frame, (11, 11), 0) |
| 37 | + hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |
| 38 | + |
| 39 | + # loop over the color ranges |
| 40 | + for (lower, upper, colorName) in colorRanges: |
| 41 | + # construct a mask for all colors in the current HSV range, then |
| 42 | + # perform a series of dilations and erosions to remove any small |
| 43 | + # blobs left in the mask |
| 44 | + mask = cv2.inRange(hsv, lower, upper) |
| 45 | + mask = cv2.erode(mask, None, iterations=2) |
| 46 | + mask = cv2.dilate(mask, None, iterations=2) |
| 47 | + |
| 48 | + # find contours in the mask |
| 49 | + cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, |
| 50 | + cv2.CHAIN_APPROX_SIMPLE) |
| 51 | + cnts = imutils.grab_contours(cnts) |
| 52 | + |
| 53 | + # only proceed if at least one contour was found |
| 54 | + if len(cnts) > 0: |
| 55 | + # find the largest contour in the mask, then use it to compute |
| 56 | + # the minimum enclosing circle and centroid |
| 57 | + c = max(cnts, key=cv2.contourArea) |
| 58 | + ((x, y), radius) = cv2.minEnclosingCircle(c) |
| 59 | + M = cv2.moments(c) |
| 60 | + (cX, cY) = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) |
| 61 | + |
| 62 | + # only draw the enclosing circle and text if the radious meets |
| 63 | + # a minimum size |
| 64 | + if radius > 10: |
| 65 | + cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2) |
| 66 | + cv2.putText(frame, colorName, (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, |
| 67 | + 1.0, (0, 255, 255), 2) |
| 68 | + |
| 69 | + # show the frame to our screen |
| 70 | + cv2.imshow("Frame", frame) |
| 71 | + key = cv2.waitKey(1) & 0xFF |
| 72 | + |
| 73 | + # if the 'q' key is pressed, stop the loop |
| 74 | + if key == ord("q"): |
| 75 | + break |
| 76 | + |
| 77 | +# cleanup the camera and close any open windows |
| 78 | +camera.release() |
| 79 | +cv2.destroyAllWindows() |
0 commit comments