forked from FRC2706/Vision2020-Competition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeViewer.py
781 lines (598 loc) · 28.5 KB
/
MergeViewer.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# ----------------------------------------------------------------------------
# Copyright (c) 2018 FIRST. All Rights Reserved.
# Open Source Software - may be modified and shared by FRC teams. The code
# must be accompanied by the FIRST BSD license file in the root directory of
# the project.
# My 2020 license: use it as much as you want. Crediting is recommended because it lets me know
# that I am being useful.
# Some parts of pipeline are based on 2019 code created by the Screaming Chickens 3997
# This is meant to be used in conjuction with WPILib Raspberry Pi image: https://github.com/wpilibsuite/FRCVision-pi-gen
# ----------------------------------------------------------------------------
import json
import time
import sys
from threading import Thread
import random
import cv2
import numpy as np
import math
import os
# from https://www.pyimagesearch.com/2015/08/10/checking-your-opencv-version-using-python/
def is_cv3():
# if we are using OpenCV 3.X, then our cv2.__version__ will start
# with '3.'
return check_opencv_version("3.")
def is_cv4():
# if we are using OpenCV 4.X, then our cv2.__version__ will start
# with '4.'
return check_opencv_version("4.")
def check_opencv_version(major, lib=None):
# if the supplied library is None, import OpenCV
if lib is None:
import cv2 as lib
# return whether or not the current OpenCV version matches the
# major version number
return lib.__version__.startswith(major)
########### SET RESOLUTION TO 640x480 !!!! ############
# import the necessary packages
import datetime
# Class to examine Frames per second of camera stream. Currently not used.
###################### PROCESSING OPENCV ################################
# counts frames for writing images
frameStop = 0
ImageCounter = 0
# Angles in radians
# image size ratioed to 4:3
# Lifecam 3000 from datasheet
# Datasheet: https://dl2jx7zfbtwvr.cloudfront.net/specsheets/WEBC1010.pdf
def load_images_from_folder(folder):
images = []
for filename in os.listdir(folder):
img = cv2.imread(os.path.join(folder,filename))
if img is not None:
images.append(img)
return images
# Power Cell Images
#images = load_images_from_folder("./PowerCell25Scale")
#images = load_images_from_folder("./PowerCellImages")
#images = load_images_from_folder("./PowerCellFullScale")
#images = load_images_from_folder("./PowerCellFullMystery")
#images = load_images_from_folder("./PowerCellSketchup")
#images = load_images_from_folder("./LifeCamPhotos")
# Outer Target Images
#images = load_images_from_folder("./OuterTargetImages")
images = load_images_from_folder("./OuterTargetHalfScale")
#images = load_images_from_folder("./OuterTargetHalfDistance")
#images = load_images_from_folder("./OuterTargetSketchup")
# finds height/width of camera frame (eg. 640 width, 480 height)
image_height, image_width = images[0].shape[:2]
print(image_height, image_width)
# FOV of microsoft camera (68.5 is camera spec)
diagonalView = math.radians(68.5)
print("Diagonal View:" + str(diagonalView))
# 4:3 aspect ratio
horizontalAspect = 4
verticalAspect = 3
# Reasons for using diagonal aspect is to calculate horizontal field of view.
diagonalAspect = math.hypot(horizontalAspect, verticalAspect)
# Calculations: http://vrguy.blogspot.com/2013/04/converting-diagonal-field-of-view-and.html
horizontalView = math.atan(math.tan(diagonalView / 2) * (horizontalAspect / diagonalAspect)) * 2
verticalView = math.atan(math.tan(diagonalView / 2) * (verticalAspect / diagonalAspect)) * 2
# Focal Length calculations: https://docs.google.com/presentation/d/1ediRsI-oR3-kwawFJZ34_ZTlQS2SDBLjZasjzZ-eXbQ/pub?start=false&loop=false&slide=id.g12c083cffa_0_165
H_FOCAL_LENGTH = image_width / (2 * math.tan((horizontalView / 2)))
V_FOCAL_LENGTH = image_height / (2 * math.tan((verticalView / 2)))
# blurs have to be odd
green_blur = 1
orange_blur = 27
yellow_blur = 1
# define range of green of retroreflective tape in HSV
lower_green = np.array([55, 45, 40])
upper_green = np.array([100, 255, 255])
lower_yellow = np.array([14, 150, 100])
upper_yellow = np.array([30, 255, 255])
switch = 1
# Flip image if camera mounted upside down
def flipImage(frame):
return cv2.flip(frame, -1)
# Blurs frame
def blurImg(frame, blur_radius):
img = frame.copy()
blur = cv2.blur(img, (blur_radius, blur_radius))
return blur
def threshold_range(im, lo, hi):
unused, t1 = cv2.threshold(im, lo, 255, type=cv2.THRESH_BINARY)
unused, t2 = cv2.threshold(im, hi, 255, type=cv2.THRESH_BINARY_INV)
return cv2.bitwise_and(t1, t2)
# Masks the video based on a range of hsv colors
# Takes in a frame, range of color, and a blurred frame, returns a masked frame
def threshold_video(lower_color, upper_color, blur):
# Convert BGR to HSV
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
h = threshold_range(h, lower_color[0], upper_color[0])
s = threshold_range(s, lower_color[1], upper_color[1])
v = threshold_range(v, lower_color[2], upper_color[2])
combined_mask = cv2.bitwise_and(h, cv2.bitwise_and(s, v))
#show the mask
cv2.imshow("mask", combined_mask)
# hold the HSV image to get only red colors
# mask = cv2.inRange(combined, lower_color, upper_color)
# Returns the masked imageBlurs video to smooth out image
return combined_mask
# Finds the tape targets from the masked image and displays them on original stream + network tales
def findTargets(frame, mask):
# Finds contours
if is_cv3():
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
else:
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
# Take each frame
# Gets the shape of video
screenHeight, screenWidth, _ = frame.shape
# Gets center of height and width
centerX = (screenWidth / 2) - .5
centerY = (screenHeight / 2) - .5
# Copies frame and stores it in image
image = frame.copy()
# Processes the contours, takes in (contours, output_image, (centerOfImage)
if len(contours) != 0:
image = findTape(contours, image, centerX, centerY)
# Shows the contours overlayed on the original video
return image
# Finds the balls from the masked image and displays them on original stream + network tables
def findPowerCell(frame, mask):
# Finds contours
if is_cv3():
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
else:
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
# Take each frame
# Gets the shape of video
screenHeight, screenWidth, _ = frame.shape
# Gets center of height and width
centerX = (screenWidth / 2) - .5
centerY = (screenHeight / 2) - .5
# Copies frame and stores it in image
image = frame.copy()
# Processes the contours, takes in (contours, output_image, (centerOfImage)
if len(contours) != 0:
image = findBall(contours, image, centerX, centerY)
# Shows the contours overlayed on the original video
return image
# Draws Contours and finds center and yaw of orange ball
# centerX is center x coordinate of image
# centerY is center y coordinate of image
def findBall(contours, image, centerX, centerY):
screenHeight, screenWidth, channels = image.shape
# Seen vision targets (correct angle, adjacent to each other)
#cargo = []
if len(contours) > 0:
# Sort contours by area size (biggest to smallest)
cntsSorted = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)[:5]
cntHeight = 0
biggestPowerCell = []
for cnt in cntsSorted:
x, y, w, h = cv2.boundingRect(cnt)
#print("bounding rec height: " + str(h))
#print("bounding rec width: " + str(w))
#print("bounding rec x: " + str(y))
#print("bounding rec y: " + str(x))
print("bounding rec height: " + str(h))
print("bounding rec width: " + str(w))
cntHeight = h
aspect_ratio = float(w) / h
# Get moments of contour; mainly for centroid
M = cv2.moments(cnt)
# Get convex hull (bounding polygon on contour)
#hull = cv2.convexHull(cnt)
# Calculate Contour area
cntArea = cv2.contourArea(cnt)
# Filters contours based off of size
if (checkBall(cntArea, aspect_ratio)):
### MOSTLY DRAWING CODE, BUT CALCULATES IMPORTANT INFO ###
# Gets the centeroids of contour
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
else:
cx, cy = 0, 0
if (len(biggestPowerCell) < 3):
##### DRAWS CONTOUR######
# Gets rotated bounding rectangle of contour
rect = cv2.minAreaRect(cnt)
# Creates box around that rectangle
box = cv2.boxPoints(rect)
# Covert boxpoints to integer
box = np.int0(box)
# Draws rotated rectangle
#cv2.drawContours(image, [box], 0, (23, 184, 80), 3)
# Draws a vertical white line passing through center of contour
cv2.line(image, (cx, screenHeight), (cx, 0), (255, 255, 255))
# Draws a white circle at center of contour
cv2.circle(image, (cx, cy), 6, (255, 255, 255))
# Draws the contours
cv2.drawContours(image, [cnt], 0, (23, 184, 80), 1)
# Gets the (x, y) and radius of the enclosing circle of contour
#(x, y), radius = cv2.minEnclosingCircle(cnt)
# Rounds center of enclosing circle
#center = (int(x), int(y))
# Rounds radius of enclosning circle
#radius = int(radius)
# Makes bounding rectangle of contour
#rx, ry, rw, rh = cv2.boundingRect(cnt)
#x, y, w, h = cv2.boundingRect(cnt)
# Draws contour of bounding rectangle in red
#cv2.rectangle(image, (rx, ry), (rx + rw, ry + rh), (0, 0, 255), 1)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 1)
# Draws circle in cyan
#cv2.circle(image, center, radius, (255, 255,0), 1)
# Appends important info to array
if [cx, cy, cnt, cntHeight] not in biggestPowerCell:
biggestPowerCell.append([cx, cy, cnt, cntHeight, aspect_ratio])
# Check if there are PowerCell seen
if (len(biggestPowerCell) > 0):
# pushes that it sees cargo to network tables
finalTarget = []
# Sorts targets based on largest height
biggestPowerCell.sort(key=lambda height: math.fabs(height[3]))
#sorts closestPowerCell - contains center-x, center-y, contour and contour height from the
#bounding rectangle. The closest one has the largest height
closestPowerCell = min(biggestPowerCell, key=lambda height: (math.fabs(height[3] - centerX)))
# extreme points
leftmost = tuple(closestPowerCell[2][closestPowerCell[2][:,:,0].argmin()][0])
rightmost = tuple(closestPowerCell[2][closestPowerCell[2][:,:,0].argmax()][0])
topmost = tuple(closestPowerCell[2][closestPowerCell[2][:,:,1].argmin()][0])
bottommost = tuple(closestPowerCell[2][closestPowerCell[2][:,:,1].argmax()][0])
# draw extreme points
# from https://www.pyimagesearch.com/2016/04/11/finding-extreme-points-in-contours-with-opencv/
cv2.circle(image, leftmost, 6, (0,255,0), -1)
cv2.circle(image, rightmost, 6, (0,0,255), -1)
cv2.circle(image, topmost, 6, (255,255,255), -1)
cv2.circle(image, bottommost, 6, (255,0,0), -1)
#print('extreme points', leftmost,rightmost,topmost,bottommost)
print("topmost: " + str(topmost[0]))
print("bottommost: " + str(bottommost[0]))
#xCoord of the closest ball will be the x position differences between the topmost and
#bottom most points
if (topmost[0] > bottommost[0]):
xCoord = int(round((topmost[0]-bottommost[0])/2)+bottommost[0])
else:
xCoord = int(round((bottommost[0]-topmost[0])/2)+topmost[0])
print(xCoord)
if (closestPowerCell[4] > 0.9 and closestPowerCell[4] < 1.2):
xCoord = closestPowerCell[0]
print ("aspect ratio of ball: " + str(closestPowerCell[4]))
finalTarget.append(calculateYaw(xCoord, centerX, H_FOCAL_LENGTH))
finalTarget.append(calculateDistWPILib(closestPowerCell[3]))
#print("Yaw: " + str(finalTarget[0]))
# Puts the yaw on screen
# Draws yaw of target + line where center of target is
finalYaw = round(finalTarget[1]*1000)/1000
cv2.putText(image, "Yaw: " + str(finalTarget[0]), (40, 40), cv2.FONT_HERSHEY_COMPLEX, .6,
(255, 255, 255))
cv2.putText(image, "Dist: " + str(finalYaw), (40, 100), cv2.FONT_HERSHEY_COMPLEX, .6,
(255, 255, 255))
cv2.line(image, (xCoord, screenHeight), (xCoord, 0), (255, 0, 0), 2)
currentAngleError = finalTarget[0]
# pushes cargo angle to network tables
cv2.line(image, (round(centerX), screenHeight), (round(centerX), 0), (255, 255, 255), 2)
return image
# Draws Contours and finds center and yaw of vision targets
# centerX is center x coordinate of image
# centerY is center y coordinate of image
def findTape(contours, image, centerX, centerY):
screenHeight, screenWidth, channels = image.shape
# Seen vision targets (correct angle, adjacent to each other)
targets = []
if len(contours) >= 2:
# Sort contours by area size (biggest to smallest)
cntsSorted = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
cntHeight = 0
biggestCnts = []
for cnt in cntsSorted:
# Get moments of contour; mainly for centroid
M = cv2.moments(cnt)
# Get convex hull (bounding polygon on contour)
hull = cv2.convexHull(cnt)
# Calculate Contour area
cntArea = cv2.contourArea(cnt)
# calculate area of convex hull
hullArea = cv2.contourArea(hull)
x, y, w, cntHeight = cv2.boundingRect(cnt)
pts, dim, a = cv2.minAreaRect(cnt)
x = pts[0]
y = pts[1]
if dim[0] > dim[1]:
cntHeight = dim[0]
else:
cntHeight = dim[1]
# print("The contour height is, ", cntHeight)
# Filters contours based off of size
if (checkContours(cntArea, hullArea)):
### MOSTLY DRAWING CODE, BUT CALCULATES IMPORTANT INFO ###
# Gets the centeroids of contour
if M["m00"] != 0:
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
theCX = cx
theCY = cy
else:
cx, cy = 0, 0
if (len(biggestCnts) < 13):
#### CALCULATES ROTATION OF CONTOUR BY FITTING ELLIPSE ##########
rotation = getEllipseRotation(image, cnt)
# Calculates yaw of contour (horizontal position in degrees)
yaw = calculateYaw(cx, centerX, H_FOCAL_LENGTH)
# Calculates yaw of contour (horizontal position in degrees)
pitch = calculatePitch(cy, centerY, V_FOCAL_LENGTH)
# Calculates Distance
dist = calculateDistance(1, 2, pitch);
##### DRAWS CONTOUR######
# Gets rotated bounding rectangle of contour
rect = cv2.minAreaRect(cnt)
# Creates box around that rectangle
box = cv2.boxPoints(rect)
# Not exactly sure
box = np.int0(box)
# Draws rotated rectangle
cv2.drawContours(image, [box], 0, (23, 184, 80), 3)
# Calculates yaw of contour (horizontal position in degrees)
yaw = calculateYaw(cx, centerX, H_FOCAL_LENGTH)
# Calculates yaw of contour (horizontal position in degrees)
pitch = calculatePitch(cy, centerY, V_FOCAL_LENGTH)
# Calculates Distance
dist = calculateDistance(1, 2, pitch);
# Draws a vertical white line passing through center of contour
cv2.line(image, (cx, screenHeight), (cx, 0), (255, 255, 255))
# Draws a white circle at center of contour
cv2.circle(image, (cx, cy), 6, (255, 255, 255))
# Draws the contours
cv2.drawContours(image, [cnt], 0, (23, 184, 80), 1)
# Gets the (x, y) and radius of the enclosing circle of contour
(x, y), radius = cv2.minEnclosingCircle(cnt)
# Rounds center of enclosing circle
center = (int(x), int(y))
# Rounds radius of enclosning circle
radius = int(radius)
# Makes bounding rectangle of contour
rx, ry, rw, rh = cv2.boundingRect(cnt)
boundingRect = cv2.boundingRect(cnt)
# Draws countour of bounding rectangle and enclosing circle in green
cv2.rectangle(image, (rx, ry), (rx + rw, ry + rh), (23, 184, 80), 1)
cv2.circle(image, center, radius, (23, 184, 80), 1)
# Appends important info to array
if [cx, cy, rotation, cnt, cntHeight] not in biggestCnts:
biggestCnts.append([cx, cy, rotation, cnt, cntHeight])
# Sorts array based on coordinates (leftmost to rightmost) to make sure contours are adjacent
biggestCnts = sorted(biggestCnts, key=lambda x: x[0])
# Target Checking
for i in range(len(biggestCnts) - 1):
# Rotation of two adjacent contours
tilt1 = biggestCnts[i][2]
tilt2 = biggestCnts[i + 1][2]
# x coords of contours
cx1 = biggestCnts[i][0]
cx2 = biggestCnts[i + 1][0]
cy1 = biggestCnts[i][1]
cy2 = biggestCnts[i + 1][1]
# If contour angles are opposite
if (np.sign(tilt1) != np.sign(tilt2)):
centerOfTarget = math.floor((cx1 + cx2) / 2)
# ellipse negative tilt means rotated to right
# Note: if using rotated rect (min area rectangle)
# negative tilt means rotated to left
# If left contour rotation is tilted to the left then skip iteration
if (tilt1 > 0):
if (cx1 < cx2):
continue
# If left contour rotation is tilted to the left then skip iteration
if (tilt2 > 0):
if (cx2 < cx1):
continue
# Angle from center of camera to target (what you should pass into gyro)
yawToTarget = calculateYaw(centerOfTarget, centerX, H_FOCAL_LENGTH)
pitchToTarget = calculatePitch(theCY, centerY, H_FOCAL_LENGTH)
# distToTarget = calculateDistance(1, 2, pitchToTarget)
distToTarget = calculateDistWPILib(biggestCnts[i][4])
# Make sure no duplicates, then append
if [centerOfTarget, yawToTarget, distToTarget] not in targets:
targets.append([centerOfTarget, yawToTarget, distToTarget])
# Check if there are targets seen
if (len(targets) > 0):
# pushes that it sees vision target to network tables
# Sorts targets based on x coords to break any angle tie
targets.sort(key=lambda x: math.fabs(x[0]))
finalTarget = min(targets, key=lambda x: math.fabs(x[1]))
# Puts the yaw on screen
# Draws yaw of target + line where center of target is
cv2.putText(image, "Yaw: " + str(finalTarget[1]), (40, 40), cv2.FONT_HERSHEY_COMPLEX, .6,
(255, 255, 255))
cv2.putText(image, "Dist: " + str(finalTarget[2]), (40, 90), cv2.FONT_HERSHEY_COMPLEX, .6,
(255, 255, 255))
cv2.line(image, (finalTarget[0], screenHeight), (finalTarget[0], 0), (255, 0, 0), 2)
currentAngleError = finalTarget[1]
# pushes vision target angle to network table
cv2.line(image, (round(centerX), screenHeight), (round(centerX), 0), (255, 255, 255), 2)
return image
# Finds the balls from the masked image and displays them on original stream + network tables
def findControlPanel(frame, mask):
# Finds contours
if is_cv3:
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
else:
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
# Take each frame
# Gets the shape of video
screenHeight, screenWidth, _ = frame.shape
# Gets center of height and width
centerX = (screenWidth / 2) - .5
centerY = (screenHeight / 2) - .5
# Copies frame and stores it in image
image = frame.copy()
# Processes the contours, takes in (contours, output_image, (centerOfImage)
if len(contours) != 0:
image = findControlPanelColour(contours, image, centerX, centerY)
# Shows the contours overlayed on the original video
return image
# Draws Contours and finds the colour the control panel wheel is resting at
# centerX is center x coordinate of image
# centerY is center y coordinate of image
def findControlPanelColour(contours, image, centerX, centerY):
#ToDo, Add code to publish wheel colour
return image
# Checks if tape contours are worthy based off of contour area and (not currently) hull area
def checkContours(cntSize, hullSize):
print(cntSize, image_width / 7)
return cntSize > (image_width / 7)
# Checks if ball contours are worthy based off of contour area and (not currently) hull area
def checkBall(cntSize, cntAspectRatio):
#this checks that the area of the contour is greater than the image width divide by 2
#And that the aspect ratio of the bounding rectangle (width / height) is close to 1 which
#is basically a circle however this would filter out 'tadpoles'
# return (cntSize > (image_width / 2)) and (round(cntAspectRatio) > 1)
return (cntSize > (image_width / 2)) and (cntAspectRatio > 0.75)
# Forgot how exactly it works, but it works!
def translateRotation(rotation, width, height):
if (width < height):
rotation = -1 * (rotation - 90)
if (rotation > 90):
rotation = -1 * (rotation - 180)
rotation *= -1
return round(rotation)
def calculateDistance(heightOfCamera, heightOfTarget, pitch):
heightOfTargetFromCamera = heightOfTarget - heightOfCamera
# Uses trig and pitch to find distance to target
'''
d = distance
h = height between camera and target
a = angle = pitch
tan a = h/d (opposite over adjacent)
d = h / tan a
.
/|
/ |
/ |h
/a |
camera -----
d
'''
divisor = math.tan(math.radians(pitch))
distance = 0
if (divisor != 0):
distance = math.fabs(heightOfTargetFromCamera / divisor)
return distance
avg = [0 for i in range(0, 1)]
#8 is number of frames to calculated average pixel height
def calculateDistWPILib(cntHeight):
global image_height, avg
for cnt in avg:
if cnt == 0:
cnt = cntHeight
del avg[len(avg) - 1]
avg.insert(0, cntHeight)
PIX_HEIGHT = 0
for cnt in avg:
PIX_HEIGHT += cnt
PIX_HEIGHT = PIX_HEIGHT / len(avg)
print (PIX_HEIGHT)
print(PIX_HEIGHT, avg) # print("The contour height is: ", cntHeight)
#TARGET_HEIGHT is actual height (for balls 7/12 7 inches)
TARGET_HEIGHT = 0.583
#image height is the y resolution calculated from image size
#15.81 was the pixel height of a a ball found at a measured distance (which is 6 feet away)
#65 is the pixel height of a scale image 6 feet away
KNOWN_OBJECT_PIXEL_HEIGHT = 65
KNOWN_OBJECT_DISTANCE = 6
VIEWANGLE = math.atan((TARGET_HEIGHT * image_height) / (2 * KNOWN_OBJECT_PIXEL_HEIGHT * KNOWN_OBJECT_DISTANCE))
# print("after 2: ", VIEWANGLE)
# VIEWANGLE = math.radians(68.5)
distance = ((TARGET_HEIGHT * image_height) / (2 * PIX_HEIGHT * math.tan(VIEWANGLE)))
return distance
# Uses trig and focal length of camera to find yaw.
# Link to further explanation: https://docs.google.com/presentation/d/1ediRsI-oR3-kwawFJZ34_ZTlQS2SDBLjZasjzZ-eXbQ/pub?start=false&loop=false&slide=id.g12c083cffa_0_298
def calculateYaw(pixelX, centerX, hFocalLength):
yaw = math.degrees(math.atan((pixelX - centerX) / hFocalLength))
return round(yaw)
# Link to further explanation: https://docs.google.com/presentation/d/1ediRsI-oR3-kwawFJZ34_ZTlQS2SDBLjZasjzZ-eXbQ/pub?start=false&loop=false&slide=id.g12c083cffa_0_298
def calculatePitch(pixelY, centerY, vFocalLength):
pitch = math.degrees(math.atan((pixelY - centerY) / vFocalLength))
# Just stopped working have to do this:
pitch *= -1
return round(pitch)
def getEllipseRotation(image, cnt):
try:
# Gets rotated bounding ellipse of contour
ellipse = cv2.fitEllipse(cnt)
centerE = ellipse[0]
# Gets rotation of ellipse; same as rotation of contour
rotation = ellipse[2]
# Gets width and height of rotated ellipse
widthE = ellipse[1][0]
heightE = ellipse[1][1]
# Maps rotation to (-90 to 90). Makes it easier to tell direction of slant
rotation = translateRotation(rotation, widthE, heightE)
cv2.ellipse(image, ellipse, (23, 184, 80), 3)
return rotation
except:
# Gets rotated bounding rectangle of contour
rect = cv2.minAreaRect(cnt)
# Creates box around that rectangle
box = cv2.boxPoints(rect)
# Not exactly sure
box = np.int0(box)
# Gets center of rotated rectangle
center = rect[0]
# Gets rotation of rectangle; same as rotation of contour
rotation = rect[2]
# Gets width and height of rotated rectangle
width = rect[1][0]
height = rect[1][1]
# Maps rotation to (-90 to 90). Makes it easier to tell direction of slant
rotation = translateRotation(rotation, width, height)
return rotation
team = 2706
server = False
cameraConfigs = []
currentImg = 0
def draw_circle(event,x,y,flags,param):
if event == cv2.EVENT_LBUTTONDOWN:
green = np.uint8([[[img[y, x, 0], img[y, x, 1], img[y, x, 2]]]])
print(img[y, x, 2], img[y, x, 1], img[y, x, 0], cv2.cvtColor(green,cv2.COLOR_BGR2HSV))
Driver = False
Tape = True
PowerCell = False
ControlPanel = False
img = images[0]
imgLength = len(images)
print("Hello Vision Team!")
while True:
frame = img
if Driver:
processed = frame
else:
if Tape:
threshold = threshold_video(lower_green, upper_green, frame)
processed = findTargets(frame, threshold)
else:
if PowerCell:
boxBlur = blurImg(frame, yellow_blur)
threshold = threshold_video(lower_yellow, upper_yellow, boxBlur)
processed = findPowerCell(frame, threshold)
elif ControlPanel:
boxBlur = blurImg(frame, yellow_blur)
# cv2.putText(frame, "Find Cargo", (40, 40), cv2.FONT_HERSHEY_COMPLEX, .6, (255, 255, 255))
threshold = threshold_video(lower_yellow, upper_yellow, frame)
processed = findControlPanel(frame, threshold)
cv2.imshow("raw", img)
cv2.imshow("processed", processed)
cv2.setMouseCallback('raw', draw_circle)
key = cv2.waitKey(0)
print(key)
if key == 27:
break
currentImg += 1
print(imgLength)
if (currentImg == imgLength):
currentImg = 0
img = images[currentImg]