-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
162 lines (126 loc) · 4.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from cscore import CameraServer, UsbCamera
# from networktables import NetworkTables
from ntcore import NetworkTableInstance
import cv2 as cv
import numpy as np
import time
import json
import sys
import ConeDetection as CDetect
configFile = "/boot/frc.json"
team = None
server = False
cameraConfigs = []
switchedCameraConfigs = []
cameras = []
def parseError(str):
"""Report parse error."""
print("config error in '" + configFile + "': " + str, file=sys.stderr)
def readConfig():
"""Read configuration file."""
global team
global server
# parse file
try:
with open(configFile, "rt", encoding="utf-8") as f:
j = json.load(f)
except OSError as err:
print("could not open '{}': {}".format(configFile, err), file=sys.stderr)
return False
# top level must be an object
if not isinstance(j, dict):
parseError("must be JSON object")
return False
# team number
try:
team = j["team"]
except KeyError:
parseError("could not read team number")
return False
# ntmode (optional)
if "ntmode" in j:
str = j["ntmode"]
if str.lower() == "client":
server = False
elif str.lower() == "server":
server = True
else:
parseError("could not understand ntmode value '{}'".format(str))
# # cameras
# try:
# cameras = j["cameras"]
# except KeyError:
# parseError("could not read cameras")
# return False
# for camera in cameras:
# if not readCameraConfig(camera):
# return False
# # switched cameras
# if "switched cameras" in j:
# for camera in j["switched cameras"]:
# if not readSwitchedCameraConfig(camera):
# return False
return True
class CameraConfig: pass
def main():
global configFile
with open(configFile, 'r') as j:
config = json.load(j)
cam_configs = config['cameras']
for camcfg in cam_configs:
cam = CameraConfig()
cam.name = camcfg['name']
cam.path = camcfg['path']
cam.width = camcfg['width']
cam.height = camcfg['height']
cam.fps = camcfg['fps']
cameras.append(cam)
width = cameras[0].width
height = cameras[0].height
fps = cameras[0].fps
# width = camera_data['width']
# height = camera_data['height']
CameraServer.enableLogging()
camera = UsbCamera(name=cameras[0].name, path=cameras[0].path)
CameraServer.startAutomaticCapture(camera=camera)
input_stream = CameraServer.getVideo()
output_stream = CameraServer.putVideo('Processed', width, height)
# vision_nt = NetworkTables.getTable("Vision")
img = np.zeros(shape=(height, width, 3), dtype=np.uint8)
output_img = np.copy(img)
if len(sys.argv) >= 2:
configFile = sys.argv[1]
# read configuration
if not readConfig():
sys.exit(1)
ntinst = NetworkTableInstance.getDefault()
if server:
print("Setting up NetworkTables server")
ntinst.startServer()
else:
print("Setting up NetworkTables client for team {}".format(team))
ntinst.startClient4("wpilibpi")
ntinst.setServerTeam(team)
ntinst.startDSClient()
vision_nt = ntinst.getTable('Vision')
while True:
# team_color = NetworkTables.getTable("team_color")
start_time = time.time()
frame_time, frame = input_stream.grabFrame(img)
output_img = np.copy(frame) # copy the same img for processing
if frame_time == 0:
output_stream.notifyError(input_stream.getError())
continue
angle, isCone = CDetect.ConeDetection(frame, output_img, width, height);
pocessing_time = time.time() - start_time
fps = 1/pocessing_time
cv.putText(output_img, str(round(fps, 1)), (0, 40),
cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
output_stream.putFrame(output_img)
# cv.imshow('ttt', frame)
vision_nt.putNumber("TestFPS", fps)
vision_nt.putNumber("ConeAngle", angle)
vision_nt.putBoolean("isCone", isCone)
# TODO: isCone
if __name__ == '__main__':
main()