def detect(array, verbose = False):
event = Event(Event.NONE)
copy = array.copy()
gray = _to_grayscale(array)
blur = cv2.GaussianBlur(gray, ksize = _DEFAULT_GAUSSIAN_BLUR_KERNEL, sigmaX = 0)
_, thresh = cv2.threshold(blur, 127, 255, _DEFAULT_THRESHOLD_TYPE)
if verbose:
cv2.imshow('spockpy.HoverPad.roi.threshold', thresh)
contours = _get_contours(thresh.copy())
largecont = max(contours, key = lambda contour: cv2.contourArea(contour))
if verbose:
roi = cv2.boundingRect(largecont)
copy = _mount_roi(copy, roi, color = _COLOR_RED)
convexHull = cv2.convexHull(largecont)
if verbose:
_draw_contours(copy, contours ,-1, _COLOR_RED , 0)
_draw_contours(copy, [largecont] , 0, _COLOR_GREEN, 0)
_draw_contours(copy, [convexHull], 0, _COLOR_GREEN, 0)
hull = cv2.convexHull(largecont, returnPoints = False)
defects = cv2.convexityDefects(largecont, hull)
if defects is not None:
copy, ndefects = _get_defects_count(copy, largecont, defects, verbose = verbose)
if ndefects == 0:
copy, tip = _get_tip_position(copy, largecont, verbose = verbose)
event.setTip(tip)
# TODO: check for a single finger.
event.setType(Event.ROCK)
elif ndefects == 1:
# TODO: check for an Event.LIZARD
event.setType(Event.SCISSOR)
elif ndefects == 2:
event.setType(Event.SPOCK)
elif ndefects == 4:
event.setType(Event.PAPER)
if verbose:
cv2.imshow('spockpy.HoverPad.roi', copy)
if verbose:
return copy, event
else:
return event
评论列表
文章目录