class ScreenCapture(VideoInput): cap_optimal_input_resolution = False # FIXME: Filter classes refer these variables. out_width = 1280 out_height = 720 # on_valide_warp # Handler being called by warp calibration process. # Caller passes the four edge points in raw image to crop game screen. # # Return True to approve the points. Calibration process can be canceled # by passing False. # # @param self The object pointer. # @param points Points. [ left_top, right_top, right_bottom, left_bottom ] # @return Approve (True) or delcline (False). def on_validate_warp(self, points): w = int(points[1][0] - points[0][0]) h = int(points[2][1] - points[1][1]) acceptable_geoms = [[720, 1280], [1080, 1920]] acceptable = False exact = False for geom in acceptable_geoms: if (geom[0] - 3 < h) and (h < geom[0] + 3): if (geom[1] - 3 < w) and (w < geom[1] + 3): acceptable = True if geom[0] == h and geom[1] == w: exact = True if exact: pass elif acceptable: msg = '\n'.join([ _('Due to the input resultion (%d x %d) some recognition may fail unexpectedly.' ) % (w, h), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.' ), ]) IkaUtils.dprint(msg) else: return False self.last_capture_geom = (h, w) return True def reset(self): self._warp_filter = WarpFilter(self) self._calibration_requested = False super(ScreenCapture, self).reset() def auto_calibrate(self, img): try: r = self._warp_filter.calibrateWarp( img, validation_func=self.on_validate_warp) except WarpCalibrationUnacceptableSize as e: (w, h) = e.shape msg = '\n'.join([ _('Current image size (%d x %d) cannot be accepted.') % (w, h), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.' ), _('Calibration Failed!'), ]) IkaUtils.dprint(msg) return False except WarpCalibrationNotFound: msg = '\n'.join([ _('Could not find any WiiU image from the desktop.'), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.' ), _('Calibration Failed!'), ]) IkaUtils.dprint(msg) return False if not r: msg = '\n'.join([ _('No description provided. (could be a bug)'), _('Calibration Failed!'), ]) return False IkaUtils.dprint(msg) self._warp_filter.enable() IkaUtils.dprint(_('Calibration succeeded!')) return True def capture_screen(self): from PIL import ImageGrab try: img = ImageGrab.grab(None) except TypeError: # なぜ発生することがあるのか、よくわからない IkaUtils.dprint('%s: Failed to grab desktop image' % self) return None return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # override def _read_frame_func(self): img = self.capture_screen() if self._calibration_requested: self._calibration_requested = False self.auto_calibrate(img) img = self._warp_filter.execute(img) return img def on_key_press(self, context, key): if (key == ord('c') or key == ord('C')): # 次回キャリブレーションを行う self._calibration_requested = True # override def _is_active_func(self): return True # override def _initialize_driver_func(self): pass # override def _cleanup_driver_func(self): pass # override def _select_device_by_index_func(self, source): IkaUtils.dprint('%s: Does not support _select_device_by_index_func()' % self) # override def _select_device_by_name_func(self, source): IkaUtils.dprint('%s: Does not support _select_device_by_name_func()' % self)
class ScreenCapture(object): ''' ScreenCapture input plugin This plugin depends on python-pillow ( https://pillow.readthedocs.org/ ). This plugin now supports only IkaLog.py. You can use this plugin if you modify IkaConfig.py ``` from ikalog.inputs.screencapture import ScreenCapture class IkaConfig: def config(self): source = ScreenCapture() ``` ''' from_file = False _out_width = 1280 _out_height = 720 # FIXME: Filter classes refer these variables. out_width = 1280 out_height = 720 _launch = 0 # on_valide_warp # Handler being called by warp calibration process. # Caller passes the four edge points in raw image to crop game screen. # # Return True to approve the points. Calibration process can be canceled # by passing False. # # @param self The object pointer. # @param points Points. [ left_top, right_top, right_bottom, left_bottom ] # @return Approve (True) or delcline (False). def on_validate_warp(self, points): w = int(points[1][0] - points[0][0]) h = int(points[2][1] - points[1][1]) print('on_validate_warp: ', w, h) acceptable_geoms = [[720, 1280], [1080, 1920]] acceptable = False exact = False for geom in acceptable_geoms: if (geom[0] - 3 < h) and (h < geom[0] + 3): if (geom[1] - 3 < w) and (w < geom[1] + 3): acceptable = True if geom[0] == h and geom[1] == w: exact = True if exact: pass elif acceptable: msg = '\n'.join([ _('Calibration succeeded!'), _('Due to the input resultion (%d x %d) some recognition may fail unexpectedly.' ) % (w, h), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.' ), ]) try: r = wx.MessageDialog(None, msg, _('Warning'), wx.OK | wx.ICON_ERROR).ShowModal() except: IkaUtils.dprint(msg) else: return False self.last_capture_geom = (h, w) return True def auto_calibrate(self, img): r = self._warp_filter.calibrateWarp( img, validation_func=self.on_validate_warp) if r: self._warp_filter.enable() IkaUtils.dprint(_('Calibration succeeded!')) return True else: msg = '\n'.join([ _('Calibration failed! Cannot detect WiiU display.'), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.' ), ]) try: r = wx.MessageDialog(None, msg, _('Warning'), wx.OK | wx.ICON_ERROR).ShowModal() except: IkaUtils.dprint(msg) return False def read_raw(self): from PIL import ImageGrab try: img = ImageGrab.grab(None) except TypeError: # なぜ発生することがあるのか、よくわからない IkaUtils.dprint('%s: Failed to grab desktop image' % self) return None img = np.asarray(img) return img def read(self): img = self.read_raw() if img is None: return None, None if self._calibration_requested: self._calibration_requested = False img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) self.auto_calibrate(img_bgr) img = self._warp_filter.execute(img) img = cv2.resize(img, (self._out_width, self._out_height)) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) t = self._time() - self._launch return (img, t) def on_key_press(self, context, key): if (key == ord('c') or key == ord('C')): # 次回キャリブレーションを行う self._calibration_requested = True def _time(self): return int(time.time() * 1000) def _check_import(self): try: from PIL import ImageGrab except: sys.exit("モジュール python-pillow がロードできませんでした。\n" + "インストールするには以下のコマンドを利用してください。\n" + "pip install Pillow") def reset(self): self._warp_filter = WarpFilter(self) def __init__(self, bbox=None): ''' bbox -- bbox Crop bounding box as (x, y, w, h) ''' self._check_import() self.last_input_geom = None self._launch = self._time() self._warp_filter = WarpFilter(self) self._calibration_requested = False if bbox is not None: self._warp_filter.set_bbox( bbox[0], bbox[1], bbox[2], bbox[3], ) self._warp_filter.enable() IkaUtils.dprint('%s: initalizing screen capture' % (self))
class ScreenCapture(object): ''' ScreenCapture input plugin This plugin depends on python-pillow ( https://pillow.readthedocs.org/ ). This plugin now supports only IkaLog.py. You can use this plugin if you modify IkaConfig.py ``` from ikalog.inputs.screencapture import ScreenCapture class IkaConfig: def config(self): source = ScreenCapture() ``` ''' from_file = False _out_width = 1280 _out_height = 720 # FIXME: Filter classes refer these variables. out_width = 1280 out_height = 720 _launch = 0 # on_valide_warp # Handler being called by warp calibration process. # Caller passes the four edge points in raw image to crop game screen. # # Return True to approve the points. Calibration process can be canceled # by passing False. # # @param self The object pointer. # @param points Points. [ left_top, right_top, right_bottom, left_bottom ] # @return Approve (True) or delcline (False). def on_validate_warp(self, points): w = int(points[1][0] - points[0][0]) h = int(points[2][1] - points[1][1]) print('on_validate_warp: ', w, h) acceptable_geoms = [[720, 1280], [1080, 1920]] acceptable = False exact = False for geom in acceptable_geoms: if (geom[0] - 3 < h) and (h < geom[0] + 3): if (geom[1] - 3 < w) and (w < geom[1] + 3): acceptable = True if geom[0] == h and geom[1] == w: exact = True if exact: pass elif acceptable: msg = '\n'.join([ _('Calibration succeeded!'), _('Due to the input resultion (%d x %d) some recognition may fail unexpectedly.') % (w, h), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.'), ]) try: r = wx.MessageDialog(None, msg, _('Warning'), wx.OK | wx.ICON_ERROR).ShowModal() except: IkaUtils.dprint(msg) else: return False self.last_capture_geom = (h, w) return True def auto_calibrate(self, img): r = self._warp_filter.calibrateWarp( img, validation_func=self.on_validate_warp ) if r: self._warp_filter.enable() IkaUtils.dprint(_('Calibration succeeded!')) return True else: msg = '\n'.join([ _('Calibration failed! Cannot detect WiiU display.'), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.'), ]) try: r = wx.MessageDialog(None, msg, _('Warning'), wx.OK | wx.ICON_ERROR).ShowModal() except: IkaUtils.dprint(msg) return False def read_raw(self): from PIL import ImageGrab try: img = ImageGrab.grab(None) except TypeError: # なぜ発生することがあるのか、よくわからない IkaUtils.dprint('%s: Failed to grab desktop image' % self) return None img = np.asarray(img) return img def read(self): img = self.read_raw() if img is None: return None, None if self._calibration_requested: self._calibration_requested = False img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) self.auto_calibrate(img_bgr) img = self._warp_filter.execute(img) img = cv2.resize(img, (self._out_width, self._out_height)) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) t = self._time() - self._launch return (img, t) def on_key_press(self, context, key): if (key == ord('c') or key == ord('C')): # 次回キャリブレーションを行う self._calibration_requested = True def _time(self): return int(time.time() * 1000) def _check_import(self): try: from PIL import ImageGrab except: sys.exit( "モジュール python-pillow がロードできませんでした。\n" + "インストールするには以下のコマンドを利用してください。\n" + "pip install Pillow" ) def reset(self): self._warp_filter = WarpFilter(self) def __init__(self, bbox=None): ''' bbox -- bbox Crop bounding box as (x, y, w, h) ''' self._check_import() self.last_input_geom = None self._launch = self._time() self._warp_filter = WarpFilter(self) self._calibration_requested = False if bbox is not None: self._warp_filter.set_bbox( bbox[0], bbox[1], bbox[2], bbox[3], ) self._warp_filter.enable() IkaUtils.dprint('%s: initalizing screen capture' % (self))
class ScreenCapture(VideoInput): cap_optimal_input_resolution = False # FIXME: Filter classes refer these variables. out_width = 1280 out_height = 720 # on_valide_warp # Handler being called by warp calibration process. # Caller passes the four edge points in raw image to crop game screen. # # Return True to approve the points. Calibration process can be canceled # by passing False. # # @param self The object pointer. # @param points Points. [ left_top, right_top, right_bottom, left_bottom ] # @return Approve (True) or delcline (False). def on_validate_warp(self, points): w = int(points[1][0] - points[0][0]) h = int(points[2][1] - points[1][1]) acceptable_geoms = [[720, 1280], [1080, 1920]] acceptable = False exact = False for geom in acceptable_geoms: if (geom[0] - 3 < h) and (h < geom[0] + 3): if (geom[1] - 3 < w) and (w < geom[1] + 3): acceptable = True if geom[0] == h and geom[1] == w: exact = True if exact: pass elif acceptable: msg = '\n'.join([ _('Due to the input resultion (%d x %d) some recognition may fail unexpectedly.') % ( w, h), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.'), ]) IkaUtils.dprint(msg) else: return False self.last_capture_geom = (h, w) return True def reset(self): self._warp_filter = WarpFilter(self) self._calibration_requested = False super(ScreenCapture, self).reset() def auto_calibrate(self, img): try: r = self._warp_filter.calibrateWarp( img, validation_func=self.on_validate_warp ) except WarpCalibrationUnacceptableSize as e: (w, h) = e.shape msg = '\n'.join([ _('Current image size (%d x %d) cannot be accepted.') % (w, h), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.'), _('Calibration Failed!'), ]) IkaUtils.dprint(msg) return False except WarpCalibrationNotFound: msg = '\n'.join([ _('Could not find any WiiU image from the desktop.'), _('IkaLog expects 1280 x 720, or 1920 x 1080 as input resolution.'), _('Calibration Failed!'), ]) IkaUtils.dprint(msg) return False if not r: msg = '\n'.join([ _('No description provided. (could be a bug)'), _('Calibration Failed!'), ]) return False IkaUtils.dprint(msg) self._warp_filter.enable() IkaUtils.dprint(_('Calibration succeeded!')) return True def capture_screen(self): from PIL import ImageGrab try: img = ImageGrab.grab(None) except TypeError: # なぜ発生することがあるのか、よくわからない IkaUtils.dprint('%s: Failed to grab desktop image' % self) return None return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) # override def _read_frame_func(self): img = self.capture_screen() if self._calibration_requested: self._calibration_requested = False self.auto_calibrate(img) img = self._warp_filter.execute(img) return img def on_key_press(self, context, key): if (key == ord('c') or key == ord('C')): # 次回キャリブレーションを行う self._calibration_requested = True # override def _is_active_func(self): return True # override def _initialize_driver_func(self): pass # override def _cleanup_driver_func(self): pass # override def _select_device_by_index_func(self, source): IkaUtils.dprint( '%s: Does not support _select_device_by_index_func()' % self) # override def _select_device_by_name_func(self, source): IkaUtils.dprint( '%s: Does not support _select_device_by_name_func()' % self)
class ScreenCapture(object): ''' ScreenCapture input plugin This plugin depends on python-pillow ( https://pillow.readthedocs.org/ ). This plugin now supports only IkaLog.py. You can use this plugin if you modify IkaConfig.py ``` from ikalog.inputs.screencapture import ScreenCapture class IkaConfig: def config(self): source = ScreenCapture() ``` ''' from_file = False _out_width = 1280 _out_height = 720 # FIXME: Filter classes refer these variables. out_width = 1280 out_height = 720 _launch = 0 def auto_calibrate(self, img): self._offset_filter.calibrateWarp(img) self._offset_filter.enable() def read(self): from PIL import ImageGrab img = ImageGrab.grab(None) img = np.asarray(img) if self._calibration_requested: self._calibration_requested = False img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) self.auto_calibrate(img_bgr) img = self._offset_filter.execute(img) img = cv2.resize(img, (self._out_width, self._out_height)) img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) t = self._time() - self._launch return (img, t) def on_key_press(self, context, key): if (key == ord('c') or key == ord('C')): # 次回キャリブレーションを行う self._calibration_requested = True def _time(self): return int(time.time() * 1000) def _check_import(self): try: from PIL import ImageGrab except: sys.exit( "モジュール python-pillow がロードできませんでした。\n" + "インストールするには以下のコマンドを利用してください。\n" + "pip install Pillow" ) def __init__(self, bbox=None): ''' bbox -- bbox Crop bounding box as (x, y, w, h) ''' self._check_import() self._launch = self._time() self._offset_filter = WarpFilter(self) self._calibration_requested = False if bbox is not None: self._offset_filter.set_bbox( bbox[0], bbox[1], bbox[2], bbox[3], ) self._offset_filter.enable() IkaUtils.dprint('%s: initalizing screen capture' % (self))