Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compat and networktables #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions compat.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions nt.py
Original file line number Diff line number Diff line change
@@ -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)