revert documentation code to working code

This commit is contained in:
2018-08-09 13:45:41 +02:00
parent 6212380893
commit 1274172c18
5 changed files with 18 additions and 65 deletions

View File

@@ -5,7 +5,6 @@ import json
import argparse
import cv2
import numpy as np
from .imagereaders import VideoReader, NaoImageReader, PictureReader
from .finders import GoalFinder, BallFinder, FieldFinder
@@ -13,7 +12,8 @@ from .utils import read_config, imresize, hsv_mask, InterruptDelayed
class Colorpicker(object):
WINDOW_DETECTION_NAME = 'Colorpicker'
WINDOW_CAPTURE_NAME = 'Object Detection (or not)'
WINDOW_DETECTION_NAME = 'Primary Mask'
def __init__(self, target=None):
parameters = ['low_h', 'low_s', 'low_v', 'high_h', 'high_s', 'high_v']
@@ -53,6 +53,7 @@ class Colorpicker(object):
else:
self.marker = None
cv2.namedWindow(self.WINDOW_CAPTURE_NAME)
cv2.namedWindow(self.WINDOW_DETECTION_NAME)
self.trackers = [
cv2.createTrackbar(
@@ -98,13 +99,9 @@ class Colorpicker(object):
tuple(map(self.settings.get, ('high_h', 'high_s', 'high_v')))
)
thr = cv2.cvtColor(thr, cv2.COLOR_GRAY2BGR)
# thr = self.marker.draw_last_contours(thr)
resulting = np.concatenate((frame, thr), axis=1)
cv2.imshow(self.WINDOW_DETECTION_NAME, resulting)
# cv2.imshow(self.WINDOW_DETECTION_NAME, thr)
return cv2.waitKey(0 if manual else 50)
cv2.imshow(self.WINDOW_CAPTURE_NAME, frame)
cv2.imshow(self.WINDOW_DETECTION_NAME, thr)
return cv2.waitKey(0 if manual else 1)
def save(self, filename, color):
try:

View File

@@ -4,7 +4,6 @@ from __future__ import division
import argparse
import cv2
import numpy as np
from .utils import read_config, imresize
from .imagereaders import NaoImageReader, VideoReader, PictureReader
@@ -119,10 +118,9 @@ if __name__ == '__main__':
ball_frame = ball_finder.draw(ball_frame, ball)
goal_frame = goal_finder.draw(goal_frame, goal)
combined = np.concatenate((ball_frame, goal_frame), axis=1)
cv2.imshow(ball_window, combined)
# cv2.imshow(goal_window, goal_frame)
cv2.imshow(ball_window, ball_frame)
cv2.imshow(goal_window, goal_frame)
key = cv2.waitKey(0 if args.manual else 1)
if key == ord('q') or key == 27:

View File

@@ -6,7 +6,7 @@ from collections import deque
import cv2
import numpy as np
from .utils import hsv_mask, contour_center
from .utils import hsv_mask
class FieldFinder(object):
@@ -51,12 +51,9 @@ class FieldFinder(object):
class GoalFinder(object):
def __init__(self, hsv_lower, hsv_upper, goal_thr=0.45):
def __init__(self, hsv_lower, hsv_upper):
self.hsv_lower = tuple(hsv_lower)
self.hsv_upper = tuple(hsv_upper)
self.goal_thr = goal_thr
self.last_detection = []
self.last_contours = []
def primary_mask(self, frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
@@ -89,11 +86,9 @@ class GoalFinder(object):
return final_score
def find(self, frame):
self.last_detection = []
thr = self.primary_mask(frame)
cnts, _ = cv2.findContours(thr, cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
self.last_contours = cnts
cnts.sort(key=cv2.contourArea, reverse=True)
top_x = 6
cnts = cnts[:top_x]
@@ -113,22 +108,15 @@ class GoalFinder(object):
return None
similarities = [self.goal_similarity(cnt) for cnt in good_cnts]
self.last_detection = list(zip(good_cnts, similarities))
best = min(similarities)
print('Final goal score:', best)
print()
if best > self.goal_thr:
if best > 0.45:
return None
# Find the contour with the shape closest to that of the goal
goal = good_cnts[similarities.index(best)]
return goal
def draw_last_contours(self, frame):
frame = frame.copy()
for cnt in self.last_contours:
cv2.drawContours(frame, (cnt,), -1, (255, 0, 0), 2)
return frame
def left_right_post(self, contour):
return contour[...,0].min(), contour[...,0].max()
@@ -139,27 +127,9 @@ class GoalFinder(object):
return (l + r) / 2
def draw(self, frame, goal):
frame = frame.copy()
cv2.putText(frame,
'Upper threshold: ' + '%.2f' % self.goal_thr, (10, 50),
cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0))
if self.last_detection:
cnts = sorted(self.last_detection,
key=lambda x: x[1])
if cnts[0][1] < self.goal_thr:
goal, score = cnts[0]
cnts = cnts[1:]
for cnt, sim in cnts[1:]:
print(sim)
cv2.drawContours(frame, (cnt,), -1, (0, 0, 255), 1)
cv2.putText(frame, '%.2f' % sim, contour_center(cnt),
cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))
if goal is not None:
print(goal)
frame = frame.copy()
cv2.drawContours(frame, (goal,), -1, (0, 255, 0), 2)
cv2.putText(frame, '%.2f' % score, contour_center(goal),
cv2.FONT_HERSHEY_DUPLEX, 1, (0, 255, 0))
return frame
@@ -209,15 +179,8 @@ class BallFinder(object):
return center, int(radius)
def draw(self, frame, ball):
frame = frame.copy()
if ball is not None:
frame = frame.copy()
center, radius = ball
cv2.circle(frame, center, radius, (255, 255, 0), 2)
# for i in range(1, len(self.history)):
# if self.history[i - 1] is None or self.history[i] is None:
# continue
# center_now = self.history[i - 1][0]
# center_prev = self.history[i][0]
# thickness = int((64 / (i + 1))**0.5 * 1.25)
# cv2.line(frame, center_now, center_prev, (0, 0, 255), thickness)
cv2.circle(frame, center, radius, (255, 255, 0), 1)
return frame

View File

@@ -15,11 +15,11 @@
[
0,
0,
159
89
],
[
180,
62,
73,
255
]
],
@@ -28,8 +28,8 @@
"ball_min_radius": 0.01,
"field": [
[
17,
57,
31,
60,
60
],
[

View File

@@ -41,11 +41,6 @@ def hsv_mask(hsv, hsv_lower, hsv_upper):
else:
return cv2.inRange(hsv, tuple(hsv_lower), tuple(hsv_upper))
def contour_center(contour):
M = cv2.moments(contour)
return int(M['m10'] / M['m00']) - 30, int(M['m01'] / M['m00']) + 30
class InterruptDelayed(object):
def __enter__(self):