Goal detection bugs fixed

This commit is contained in:
2018-06-23 14:11:02 +02:00
parent abb9750413
commit 700c7a73c7
4 changed files with 22 additions and 14 deletions

View File

@@ -5,7 +5,7 @@ import argparse
import cv2 import cv2
from .utils import read_config, imresize, field_mask from .utils import read_config, imresize
from .imagereaders import NaoImageReader, VideoReader, PictureReader from .imagereaders import NaoImageReader, VideoReader, PictureReader
from .finders import BallFinder, GoalFinder, FieldFinder from .finders import BallFinder, GoalFinder, FieldFinder
@@ -100,10 +100,9 @@ if __name__ == '__main__':
frame = imresize(frame, width=args.width) frame = imresize(frame, width=args.width)
field = field_finder.find(frame) field = field_finder.find(frame)
not_field = cv2.bitwise_not(field)
ball_frame = field_finder.draw(frame, field) ball_frame = field_finder.mask_it(frame, field)
goal_frame = field_finder.draw(frame, not_field) goal_frame = field_finder.mask_it(frame, field, inverse=True)
ball = ball_finder.find(ball_frame) ball = ball_finder.find(ball_frame)
goal = goal_finder.find(goal_frame) goal = goal_finder.find(goal_frame)

View File

@@ -29,13 +29,22 @@ class FieldFinder(object):
return None return None
field = max(cnts, key=cv2.contourArea) field = max(cnts, key=cv2.contourArea)
field = cv2.convexHull(field) field = cv2.convexHull(field)
mask = np.zeros(thr.shape, dtype=np.uint8) return field
cv2.drawContours(mask, (field,), -1, 255, -1)
return mask
def draw(self, frame, field): def draw(self, frame, field):
if field is not None: if field is not None:
frame = cv2.bitwise_and(frame, frame, mask=field) frame = frame.copy()
cv2.drawContours(frame, (field,), -1, (0, 0, 255), 2)
return frame
def mask_it(self, frame, field, inverse=False):
if field is not None:
print(frame.shape)
mask = np.zeros(frame.shape[:2], dtype=np.uint8)
cv2.drawContours(mask, (field,), -1, 255, -1)
if inverse:
mask = cv2.bitwise_not(mask)
frame = cv2.bitwise_and(frame, frame, mask=mask)
return frame return frame

View File

@@ -29,11 +29,11 @@
"field": [ "field": [
[ [
19, 19,
57, 51,
84 60
], ],
[ [
65, 67,
255, 255,
255 255
] ]

View File

@@ -1,6 +1,8 @@
from __future__ import print_function from __future__ import print_function
from __future__ import division from __future__ import division
import argparse
from threading import Thread
from math import pi from math import pi
from time import sleep, time from time import sleep, time
@@ -8,9 +10,7 @@ from .utils import read_config
from .imagereaders import NaoImageReader from .imagereaders import NaoImageReader
from .finders import BallFinder, GoalFinder from .finders import BallFinder, GoalFinder
from .movements import NaoMover from .movements import NaoMover
import argparse
from naoqi import ALProxy from naoqi import ALProxy
from threading import Thread
class Striker(object): class Striker(object):
@@ -72,7 +72,7 @@ class Striker(object):
"""Detect the ball and return its angles in camera coordinates.""" """Detect the ball and return its angles in camera coordinates."""
try: try:
ball = self.ball_finder.find(cam.get_frame()) ball = self.ball_finder.find(cam.get_frame())
except RuntimeError as e: except RuntimeError as e: # Sometimes camera doesn't return an image
print(e) print(e)
return None return None