From ca98b6664ba2331da30144fd91694baa28ca3adb Mon Sep 17 00:00:00 2001 From: eric Date: Sat, 3 Feb 2018 16:37:13 -0500 Subject: [PATCH] compat and networktables --- compat.py | 27 +++++++++++++++++++++++++++ nt.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 compat.py create mode 100644 nt.py diff --git a/compat.py b/compat.py new file mode 100644 index 0000000..073cb67 --- /dev/null +++ b/compat.py @@ -0,0 +1,27 @@ +# OPENCV FUNCTIONS WITH COMPATIBILITY FOR BOTH OPENCV2 AND OPENCV3 +import cv2 + +# Arguments: +# *Same as cv2.findContours(...) +# +# Returns: contours found in image. Does NOT return hierarchy. +def findContours(*args, **kwargs): + cv_version = cv2.__version__.split(".")[0] + if cv_version == "2": + return cv2.findContours(*args, **kwargs)[0] + elif cv_version == "3": + return cv2.findContours(*args, **kwargs)[1] + +def boxPoints(rect): + cv_version = cv2.__version__.split(".")[0] + if cv_version == "2": + return cv2.cv.BoxPoints(rect) + elif cv_version == "3": + return cv2.boxPoints(rect) + +def fourcc(*args): + cv_version = cv2.__version__.split(".")[0] + if cv_version == "2": + return cv2.cv.FOURCC(*args) + elif cv_version == "3": + return cv2.VideoWriter_fourcc(*args) diff --git a/nt.py b/nt.py new file mode 100644 index 0000000..9eb27b7 --- /dev/null +++ b/nt.py @@ -0,0 +1,39 @@ +import cv2 +import numpy as np +import numbers +import decimal +import logging + +from networktables import NetworkTables + +# Inits NetworkTable. If no arguments are passed, default values will be used. +# +# Argments: +# *server: server IP address or hostname you're connecting you as a client. +# *key: name of NetworkTable instance. +# +# Returns: NetworkTable instance of specified key. +def initTable(server="10.3.34.50", key="vision"): + # necessary to see networkTables + logging.basicConfig(level=logging.DEBUG) + + print("Initialized NetworkTables ", NetworkTables.initialize(server)) + nt = NetworkTables.getTable(key) + + return nt + +# Send data values to a NetworkTable. +# +# Arguments: +# *nt = a NetworkTable instance. +# *data = values (any type) to put on nt. Stored as a dictionary: +# { key1 : value1, key2 : value2, ...} +def sendData(nt, data): + for key, value in data.iteritems(): + print ("key", key, "value", value) + if isinstance(value, bool): + nt.putBoolean(key, value) + elif isinstance(value, numbers.Number): + nt.putNumber(key, value) + elif isinstance(value, str): + nt.putString(key, value)