-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjectDetection.py
More file actions
77 lines (54 loc) · 2.11 KB
/
Copy pathGameObjectDetection.py
File metadata and controls
77 lines (54 loc) · 2.11 KB
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
import math
from networktables import NetworkTables
import logging
import keyboard
import cv2
# #Init NetworkTables
# logging.basicConfig(level=logging.DEBUG)
# NetworkTables.initialize()
# sd = NetworkTables.getTable("SmartDashboard")
# sd.putNumberArray("pos", {-1, -1, -1})
# sd.putNumber("rot", -1)
# create a video capture
cap = cv2.VideoCapture(1)
cap.set(cv2.CAP_PROP_EXPOSURE, -6)
bounds = 50
colorBlueUnsaturated = 125
while True:
# read frame from the camera
_, img = cap.read()
#resize camera, used later with contours
img = cv2.resize(img, (800,500))
#image without masks
initImg = img
img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#only show things between certain saturations
mask = cv2.inRange(img, (0, 0, 0), (360, 255, 255))
if keyboard.is_pressed('d'):
colorBlueUnsaturated += 1
print(colorBlueUnsaturated)
if keyboard.is_pressed('a'):
colorBlueUnsaturated -= 1
print(colorBlueUnsaturated)
#only show things between color range
mask2 = cv2.inRange(img, (42 - bounds, 174 - bounds, 211 - bounds), (42 + bounds, 174 - bounds, 211 - bounds))
#show both masks at once, only shows pixels picked up by both masks
mask3 = cv2.bitwise_and(mask, mask2)
#improve accuracy of the mask detection
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ksize=(3,3))
mask3 = cv2.erode(mask3, kernel)
mask3 = cv2.dilate(mask3, kernel)
#find contours, basically finding the edges of the game piece
cnts = cv2.findContours(mask3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for i in cnts[0]:
M = cv2.moments(i)
cX = int((M["m10"] / M["m00"])) / 800
cY = int((M["m01"] / M["m00"])) / 500
initImg = cv2.circle(initImg, (math.floor(cX * 800), math.floor(cY * 500)), 20, (0, 0, 255), -1)
img = cv2.circle(initImg, (math.floor(cX * 800), math.floor(cY * 500)), 20, (0, 0, 255), -1)
break
img = cv2.bitwise_and(img, img, mask=mask3)
#show the capture from camera
cv2.imshow("image with masks", img)
cv2.imshow("initial image", initImg)
cv2.waitKey(5)