-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleCamera.py
217 lines (167 loc) · 5.17 KB
/
handleCamera.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
# from asyncio.windows_events import NULL
import json
import time
import sys
import traceback
import numpy as np
import cv2
from cscore import CameraServer, VideoSource, UsbCamera, MjpegServer, CvSink
from networktables import NetworkTablesInstance
import vision2022
configFile = "/boot/frc.json"
class CameraConfig: pass
team = None
server = True
cameraConfigs = []
print ("stufferiiinos")
"""Report parse error."""
def parseError(str):
print("config error in '" + configFile + "': " + str, file=sys.stderr)
"""Read single camera configuration."""
def readCameraConfig(config):
cam = CameraConfig()
# name
try:
cam.name = config["name"]
except KeyError:
parseError("could not read camera name")
return False
# path
try:
cam.path = config["path"]
except KeyError:
parseError("camera '{}': could not read path".format(cam.name))
return False
# stream properties
cam.streamConfig = config.get("stream")
cam.config = config
cameraConfigs.append(cam)
return True
"""Read configuration file."""
def readConfig():
global team
global server
# parse file
try:
with open(configFile, "rt") 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
return True
"""Start running the camera."""
def startCamera(config):
print("Starting camera '{}' on {}".format(config.name, config.path))
inst = CameraServer.getInstance()
camera = UsbCamera(config.name, config.path)
server = inst.startAutomaticCapture(camera=camera, return_server=True)
camera.setConfigJson(json.dumps(config.config))
camera.setConnectionStrategy(VideoSource.ConnectionStrategy.kKeepOpen)
#this should be added to config not a default!!!!
camera.setExposureManual(8)
if config.streamConfig is not None:
server.setConfigJson(json.dumps(config.streamConfig))
return camera
def mainRun():
#if __name__ == "__main__":
#print(len(cameraConfigs))
if len(sys.argv) >= 2:
configFile = sys.argv[1]
# read configuration
if not readConfig():
sys.exit(1)
# start NetworkTables
ntinst = NetworkTablesInstance.getDefault()
ntinst.startClient(("10.8.30.2", 1735))
table = ntinst.getTable("Shuffleboard")
dashboard = table.getSubTable("vision")
# start cameras
cameras = []
for cameraConfig in cameraConfigs:
cameras.append(startCamera(cameraConfig))
inst = CameraServer.getInstance()
#following are default values from dashboard
height = 120
width = 160
videoOutput = inst.putVideo("Camera Output", width, height)
visionOutput = inst.putVideo("Vision Processed", width, height)
videoSink = CvSink("Rasp PI Sink")
frame = np.ndarray((height,width,3)) #error
lastfrontCamera = None
dashboard.putNumber("Number of Cameras", len(cameras))
dashboard.putNumber("tapeLowerH", 40)
dashboard.putNumber("tapeLowerS", 150)
dashboard.putNumber("tapeLowerV", 100)
dashboard.putNumber("tapeUpperH", 80)
dashboard.putNumber("tapeUpperS", 255)
dashboard.putNumber("tapeUpperV", 255)
#camera height in inches
dashboard.putNumber("CameraHeight", 26)
#camera angle in degrees
dashboard.putNumber("CameraAngle", 33.92192950177638)
#camera vertical FOV in degrees
dashboard.putNumber("CameraVerticleFOV",35)
dashboard.putNumber("tapeToGapRatio", 0.93)
#calibration distance from the edge of the hub to the camera in inches
dashboard.putNumber("CalibrationDistance", 180)
dashboard.putNumber("Hub Center X Distance", -1)
dashboard.putNumber("Camera X Resolution", 1080)
dashboard.putNumber("Zero if calibrate", 1)
# vision processing
while True:
dashboard.putNumber("test", 101)
#print("In the while")
try:
frontCamera = True
#("Line 158") # debugging
if(frontCamera != lastfrontCamera):
#print("Line 160") # debugging
lastfrontCamera = frontCamera
#print("Line 162") # debugging
#print(lastfrontCamera)
if(frontCamera):
#print('Set source 0 (front camera) (ball)')
videoSink.setSource(cameras[0])
timeout = 0.225
timestamp, frame = videoSink.grabFrame((120, 160, 3), timeout) # this outputs a CvImage; IS ERROR
if not timestamp: # could not grab frame
print("Frame skipped.")
continue #continue, just to ensure that we don't procsess empty frame
#else:
#print("frame not skipped")
#*********************************
#calls to vision Manipulation here, everything above handles vision hardwere configuration
processedVideo = vision2022.ManipulateHubImage(frame, dashboard)
videoOutput.putFrame(processedVideo)
except Exception as e:
print (e)
print(traceback.format_exc())