def _showloop(self): while cv2.waitKey(10) not in [Keycode.ESCAPE, Keycode.Q, Keycode.q]: image = self.capture.read() image = image.transpose(Image.FLIP_LEFT_RIGHT) image = Util.resize_image(image, self.size) array = np.asarray(image) array = Util.mount_roi(array, self.roi, color=(74, 20, 140), thickness=2) crop = _crop_array(array, self.roi) # process image for any gestures if self.verbose: segments, event = grdetect(crop, verbose=self.verbose) self.image = Image.fromarray(segments) self.event = event else: event = grdetect(crop, verbose=self.verbose) #self.image = Image.fromarray(segments) self.event = event #self.image = Image.fromarray(segments) #self.event = event cv2.imshow(PyGR.TITLE, array) cv2.destroyWindow(PyGR.TITLE)
def grdetect(array, verbose=False): event = Event(Event.NONE) copy = array.copy() array = _remove_background(array) # 移除背景, add by wnavy ''' gray = Util.to_grayscale(array) # 转化为灰度图像 blur = cv2.GaussianBlur(gray, ksize = _DEFAULT_GAUSSIAN_BLUR_KERNEL, sigmaX = 0) # 高斯滤波 _, thresh = cv2.threshold(blur, 0, 255, _DEFAULT_THRESHOLD_TYPE) # 图像二值化 ''' thresh = _bodyskin_detetc(array) if verbose: cv2.imshow('pygr.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 = Util.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) event.setType(Event.ZERO) elif ndefects == 1: # TODO: check for an Event.LIZARD # event.setType(Event.SCISSOR) event.setType(Event.TWO) elif ndefects == 2: # event.setType(Event.SPOCK) event.setType(Event.THREE) elif ndefects == 3: event.setType(Event.FOUR) elif ndefects == 4: # event.setType(Event.PAPER) event.setType(Event.FIVE) if verbose: cv2.imshow('pygr.HoverPad.roi', copy) if verbose: return copy, event else: return event