-
Notifications
You must be signed in to change notification settings - Fork 0
/
SharkSight.py
291 lines (232 loc) · 10.1 KB
/
SharkSight.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import jetson.inference
import jetson.utils
from cscore import CameraServer
import cscore
from networktables import NetworkTables
import json
import time
import math
import logging
import argparse
import os
import cv2
import numpy as np
def drawCrossHairs(image, x, y, r, g, b, a, size, gapSize, thickness):
jetson.utils.cudaDrawLine(image, (x, y - size // 2), (x, y - gapSize // 2), (r,g,b,a), thickness)
jetson.utils.cudaDrawLine(image, (x, y + size // 2), (x, y + gapSize // 2), (r,g,b,a), thickness)
jetson.utils.cudaDrawLine(image, (x - size // 2, y), (x - gapSize // 2, y), (r,g,b,a), thickness)
jetson.utils.cudaDrawLine(image, (x + size // 2, y), (x + gapSize // 2, y), (r,g,b,a), thickness)
parser = argparse.ArgumentParser(description='Shark Sight')
parser.add_argument('--threshold', type=float, default=0.5, help='Confidence Minimum')
parser.add_argument('--capture-height', type=int, default=720,
help='The resolution height to capture images from the camera.')
parser.add_argument('--capture-width', type=int, default=1280,
help='The resolution width to capture images from the camera.')
parser.add_argument('--stream-height', type=int, default=270,
help='The resolution to stream to the CameraServer.')
parser.add_argument('--stream-width', type=int, default=480,
help='The resolution to stream to the CameraServer.')
parser.add_argument('--stream-compression', type=int, default=30,
help='The compression to stream.')
parser.add_argument('--display', '-d', action='store_true',
help='Default: False.')
args = parser.parse_args()
captureScale = args.capture_height / 720
captureArea = args.capture_height * args.capture_width
crosshairX = args.capture_width // 2
crosshairY = args.capture_height // 2
logging.basicConfig(level=logging.DEBUG)
cs = CameraServer.getInstance()
cs.enableLogging()
csSource = cscore.CvSource("Driver", cscore.VideoMode.PixelFormat.kMJPEG, args.stream_width, args.stream_height, 24)
server = cs.startAutomaticCapture(camera=csSource, return_server=True)
server.setCompression(args.stream_compression)
csSource2 = cscore.CvSource("Driver2", cscore.VideoMode.PixelFormat.kMJPEG, args.stream_width, args.stream_height, 24)
server2 = cs.startAutomaticCapture(camera=csSource2, return_server=True)
server2.setCompression(args.stream_compression)
NetworkTables.startClientTeam(226)
sd = NetworkTables.getTable("SharkSight")
net = jetson.inference.detectNet(argv=["--model=/home/hammerheads/jetson-inference/python/training/detection/ssd/models/cubes/ssd-mobilenet.onnx", "--labels=/home/hammerheads/jetson-inference/python/training/detection/ssd/models/cubes/labels.txt", "--input-blob=input_0", "--output-cvg=scores", "--output-bbox=boxes"], threshold=0.5)
camera = jetson.utils.videoSource(
"/dev/video0",
argv=[
f"--input-width={str(args.capture_width)}",
f"--input-height={str(args.capture_height)}",
],
)
camera2 = jetson.utils.videoSource(
"/dev/video1",
argv=[
f"--input-width={str(args.capture_width)}",
f"--input-height={str(args.capture_height)}",
],
)
sd.putString("Intake Camera FPS", camera.GetFrameRate())
sd.putString("Gripper Camera FPS", camera2.GetFrameRate())
sd.putBoolean("Shutdown", False)
sd.putBoolean("Enabled", True)
display = jetson.utils.videoOutput("display://0") if args.display else None
smallImg = None
bgrSmallImg = None
smallImg2 = None
bgrSmallImg2 = None
startTime = time.time()
while True:
if sd.getBoolean("Shutdown", False):
os.system('systemctl poweroff')
if (sd.getNumber("CPU Temp", 0) > 85 or sd.getNumber("GPU Temp", 0) > 85):
time.sleep(.02)
continue
if not sd.getBoolean("Enabled", True):
time.sleep(.02)
continue
sd.putString("Status", "Running")
img = camera.Capture()
img2 = camera2.Capture()
detections = net.Detect(img, overlay='none')
detections2 = net.Detect(img2, overlay='none')
closestIntakeDetection = None
closestIntakeDetectionDistance = 10000
ntIntakeDetections = []
for detection in detections:
areaPercent = ((detection.Area / captureArea) * 100)
targetX = detection.Center[0] - crosshairX
targetY = detection.Center[1] - crosshairY
targetDistance = math.sqrt(targetX**2 + targetY**2)
className = "cubes"
if (detection.ClassID == 2):
className = "cones"
ntDetection = {
"ClassID": detection.ClassID,
"ClassName": className,
"InstanceID": detection.Instance,
"Area": detection.Area,
"Bottom": detection.Bottom,
"CenterX": detection.Center[0],
"CenterY": detection.Center[1],
"Confidence": detection.Confidence,
"Height": detection.Height,
"Width": detection.Width,
"Left": detection.Left,
"Right": detection.Right,
"Top": detection.Top,
"Timestamp": time.time(),
"TargetX": targetX,
"TargetY": targetY,
"TargetDistance": targetDistance,
"AreaPercent": areaPercent
}
ntIntakeDetections.append(ntDetection)
if targetDistance < closestIntakeDetectionDistance:
closestIntakeDetection = ntDetection
closestIntakeDetectionDistance = targetDistance
sd.putString("Intake Detections", json.dumps(ntIntakeDetections))
if closestIntakeDetection is None:
sd.putString("Intake Closest Detection", "")
else:
sd.putString("Intake Closest Detection", json.dumps(closestIntakeDetection))
drawCrossHairs(img, closestIntakeDetection["CenterX"], closestIntakeDetection["CenterY"], 255, 255, 255, 255, 120 * captureScale, 30 * captureScale, 1)
closestGripperDetection = None
closestGripperDetectionDistance = 10000
ntGripperDetections = []
for detection in detections2:
areaPercent = ((detection.Area / captureArea) * 100)
targetX = detection.Center[0] - crosshairX
targetY = detection.Center[1] - crosshairY
targetDistance = math.sqrt(targetX**2 + targetY**2)
className = "cubes"
if (detection.ClassID == 2):
className = "cones"
imgCropped = jetson.utils.cudaAllocMapped(width=detection.Width, height=detection.Height, format=img.format)
crop_roi = (detection.Left * 0.9, detection.Top * 0.9, detection.Right * 0.9, detection.Bottom * 0.9)
jetson.utils.cudaCrop(img2, imgCropped, crop_roi)
bgr_img = jetson.utils.cudaAllocMapped(width=imgCropped.width, height=imgCropped.height, format='bgr8')
jetson.utils.cudaConvertColor(imgCropped, bgr_img)
jetson.utils.cudaDeviceSynchronize()
cv_img = jetson.utils.cudaToNumpy(bgr_img)
hsv_img = cv2.cvtColor(cv_img, cv2.COLOR_BGR2HSV)
height, width, _ = hsv_img.shape
upper_half = hsv_img[0:height//2, :]
lower_half = hsv_img[height//2:, :]
lower_yellow = np.array([20, 100, 100], dtype=np.uint8)
upper_yellow = np.array([30, 255, 255], dtype=np.uint8)
mask_upper = cv2.inRange(upper_half, lower_yellow, upper_yellow)
mask_lower = cv2.inRange(lower_half, lower_yellow, upper_yellow)
yellow_pixels_upper = cv2.countNonZero(mask_upper)
yellow_pixels_lower = cv2.countNonZero(mask_lower)
shape = None
if (yellow_pixels_upper > yellow_pixels_lower):
sd.putString("Cone Rotation", "Upside Down")
elif (yellow_pixels_lower > yellow_pixels_upper):
sd.putString("Cone Rotation", "Normal")
else:
sd.putString("Cone Rotation", "Unknown")
del imgCropped
del bgr_img
del cv_img
else:
sd.putString("Cone Rotation", "Not Cone")
ntDetection = {
"ClassID": detection.ClassID,
"ClassName": className,
"InstanceID": detection.Instance,
"Area": detection.Area,
"Bottom": detection.Bottom,
"CenterX": detection.Center[0],
"CenterY": detection.Center[1],
"Confidence": detection.Confidence,
"Height": detection.Height,
"Width": detection.Width,
"Left": detection.Left,
"Right": detection.Right,
"Top": detection.Top,
"Timestamp": time.time(),
"TargetX": targetX,
"TargetY": targetY,
"TargetDistance": targetDistance,
"AreaPercent": areaPercent
}
ntGripperDetections.append(ntDetection)
if targetDistance < closestGripperDetectionDistance:
closestGripperDetection = ntDetection
closestGripperDetectionDistance = targetDistance
sd.putString("Gripper Detections", json.dumps(ntGripperDetections))
sd.putNumber("Net FPS", net.GetNetworkFPS())
if closestGripperDetection is None:
sd.putString("Gripper Closest Detection", "")
else:
sd.putString("Gripper Closest Detection", json.dumps(closestGripperDetection))
drawCrossHairs(img2, closestGripperDetection["CenterX"], closestGripperDetection["CenterY"], 255, 255, 255, 255, 120 * captureScale, 30 * captureScale, 1)
drawCrossHairs(img, crosshairX, crosshairY, 0, 255, 0, 255, 120 * captureScale, 30 * captureScale, 1)
drawCrossHairs(img2, crosshairX, crosshairY, 0, 255, 0, 255, 120 * captureScale, 30 * captureScale, 1)
if display is not None:
display.Render(img)
display.SetStatus("Object Detection | Network {:.0f} FPS".format(net.GetNetworkFPS()))
if smallImg is None:
smallImg = jetson.utils.cudaAllocMapped(width=args.stream_width, height=args.stream_height, format=img.format)
smallImg2 = jetson.utils.cudaAllocMapped(width=args.stream_width, height=args.stream_height, format=img.format)
jetson.utils.cudaResize(img, smallImg)
jetson.utils.cudaResize(img2, smallImg2)
del img
del img2
if bgrSmallImg is None:
bgrSmallImg = jetson.utils.cudaAllocMapped(width=args.stream_width, height=args.stream_height, format="bgr8")
bgrSmallImg2 = jetson.utils.cudaAllocMapped(width=args.stream_width, height=args.stream_height, format="bgr8")
jetson.utils.cudaConvertColor(smallImg, bgrSmallImg)
jetson.utils.cudaConvertColor(smallImg2, bgrSmallImg2)
jetson.utils.cudaDeviceSynchronize()
numpyImg = jetson.utils.cudaToNumpy(bgrSmallImg, args.stream_width, args.stream_height, 4)
numpyImg2 = jetson.utils.cudaToNumpy(bgrSmallImg2, args.stream_width, args.stream_height, 4)
csSource.putFrame(numpyImg)
csSource2.putFrame(numpyImg2)
del numpyImg
del numpyImg2
cpu = float(os.popen("cat /sys/devices/virtual/thermal/thermal_zone0/temp").read()) / 1000
gpu = int(os.popen("cat /sys/devices/virtual/thermal/thermal_zone1/temp").read()) / 1000
endTime = time.time()
elapseTime = (endTime - startTime) * 1000
startTime = endTime
sd.putNumber("Latency", elapseTime)
sd.putNumber("Pipeline FPS", 1000 / elapseTime)
sd.putNumber("CPU Temp", cpu)
sd.putNumber("GPU Temp", gpu)