Created
December 15, 2018 05:33
-
-
Save gcambridge/f06b1055eafbb520421172721ca432e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
from networktables import NetworkTables | |
import numpy as np | |
# To see messages from networktables, you must setup logging | |
import logging | |
logging.basicConfig(level=logging.DEBUG) | |
ip = "10.55.87.2" | |
NetworkTables.initialize(server=ip) | |
sd = NetworkTables.getTable("SmartDashboard") | |
cam = cv2.VideoCapture(0) | |
# Lower color limits | |
red = ([160, 100, 100], [179, 255, 255]) | |
blue = ([95, 100, 100], [115, 255, 255]) | |
def color_found(cam, color_bounds): | |
frame = cam.read()[1] | |
color_lower = np.array(color_bounds[0], dtype="uint8") | |
color_upper = np.array(color_bounds[1], dtype="uint8") | |
# switch color space to HSV | |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
# Make mask of image, only showing color in between upper | |
# and lower color values | |
mask = cv2.inRange(hsv, color_lower, color_upper) | |
# find contours of colors in respective masks | |
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, | |
cv2.CHAIN_APPROX_SIMPLE) | |
# get the sizes of all contours | |
contour_sizes = [(cv2.contourArea(contour), contour) for contour | |
in contours] | |
if(len(contour_sizes) > 0): | |
biggest_contour = max(contour_sizes, key=lambda x: x[0])[1] | |
x,y,w,h = cv2.boundingRect(biggest_contour) | |
return y + int(h/2) #distance from the top of the frame to center of ball | |
while True: | |
red_distance = color_found(cam, red) | |
blue_distance = color_found(cam, blue) | |
if red_distance > blue_distance: | |
ball_color = "red" | |
elif blue_distance > red_distance: | |
ball_color = "blue" | |
else: | |
ball_color = "none" | |
sd.putString("Closest Ball", ball_color) | |
print("Closest Ball: " + ball_color) | |
# Runs if while loop is exited | |
cam.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment