def _http_request_func(self, path, payload): url_api = '%s%s' % (self.base_uri, path) http_headers = { 'Content-Type': 'application/x-msgpack', } mp_payload = ''.join(map(chr, umsgpack.packb(payload))) try: pool = urllib3.PoolManager(timeout=10.0) req = pool.urlopen( 'POST', url_api, headers=http_headers, body=mp_payload, ) return json.loads(req.data.decode('utf-8')) except urllib3.exceptions.MaxRetryError: fallback = self._fallback # or .... or ... if fallback: IkaUtils.dprint( '%s: Remote API Error. Falling back to local mode' % self) return self._local_request_func(path, payload) raise Exception( 'API Error: Failed to connect to API endpoint. (No fallback)')
def _http_request_func(self, path, payload): url_api = '%s%s' % (self.base_uri, path) http_headers = { 'Content-Type': 'application/x-msgpack', } mp_payload = ''.join(map(chr, umsgpack.packb(payload))) try: pool = urllib3.PoolManager(timeout=3.0) req = pool.urlopen( 'POST', url_api, headers=http_headers, body=mp_payload, ) return json.loads(req.data.decode('utf-8')) except urllib3.exceptions.MaxRetryError: fallback = self._fallback # or .... or ... if fallback: IkaUtils.dprint( '%s: Remote API Error. Falling back to local mode' % self) return self._local_request_func(path, payload) raise Exception( 'API Error: Failed to connect to API endpoint. (No fallback)')
def print_language_settings(): from ikalog.utils import IkaUtils IkaUtils.dprint( 'IkaLog Primary CLI Language: %s (set LANG to override)' % Localization.get_languages()[0]) IkaUtils.dprint( 'IkaLog Game Language: %s (set IKALOG_LANG to override)' % Localization.get_game_languages()[0])
def init_for_cvfile(args, capture): if not capture.is_active(): IkaUtils.dprint('Failed to initialize with: %s' % capture._source_file) sys.exit(1) pos_msec = args.get('time_msec') or time_to_msec(args.get('time') or '0:0') if pos_msec: capture.set_pos_msec(pos_msec)
def __init__(self, bbox=None): ''' bbox -- bbox Crop bounding box as (x, y, w, h) ''' self._check_import() self._bbox = bbox self._launch = self._time() IkaUtils.dprint('%s: initalizing screen capture' % (self))
def evaluate(self, img_bgr=None, img_gray=None): # if not hasattr(self, '_warned_evaluate_is_deprecated'): if not self._warned_evaluate_is_deprecated: IkaUtils.dprint('%s: evaluate() is depricated.' % self) self._warned_evaluate_is_deprecated = True return self(img_bgr=img_bgr, img_gray=img_gray)
def write_answer_file(self, video_file, record): answer_fullpath = self.answer_filename(video_file, self.answer_type, None) f = open(answer_fullpath, "w") f.write(json.dumps(record)) f.close() IkaUtils.dprint("wrote answer file %s" % answer_fullpath) return True
def IkaUI_main(): IkaUtils.dprint(_('Hello!')) application = wx.App() input_plugin = VideoCapture() engine = IkaEngine(keep_alive=True) engine.close_session_at_eof = True engine.set_capture(input_plugin) # 設定画面を持つ各種 Output Plugin # -> 設定画面の生成とプラグインリストへの登録 outputs_with_gui = [ outputs.CSV(), # outputs.Fluentd(), outputs.JSON(), # outputs.Hue(), outputs.OBS(), outputs.Twitter(), outputs.Screenshot(), outputs.Boyomi(), outputs.Slack(), outputs.StatInk(), outputs.DebugVideoWriter(), outputs.WebSocketServer(), ] gui = IkaLogGUI(engine, outputs_with_gui) plugins = [] # とりあえずデバッグ用にコンソールプラグイン plugins.append(outputs.Console()) # 各パネルをプラグインしてイベントを受信する plugins.append(gui.preview) plugins.append(gui.last_result) # 設定画面を持つ input plugin もイベントを受信する plugins.append(input_plugin) # UI 自体もイベントを受信 plugins.append(gui) plugins.extend(outputs_with_gui) # 本当に困ったときのデバッグログ増加モード if 'IKALOG_DEBUG' in os.environ: plugins.append(outputs.DebugLog()) # プラグインリストを登録 engine.set_plugins(plugins) gui.run() application.MainLoop() IkaUtils.dprint(_('Bye!'))
def write_answer_file(self, video_file, record): answer_fullpath = self.answer_filename( video_file, self.answer_type, None) f = open(answer_fullpath, 'w') f.write(json.dumps(record)) f.close() IkaUtils.dprint('wrote answer file %s' % answer_fullpath) return True
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_frame(self): try: self.lock.acquire() if not self.is_active(): return None next_tick = None img = self._read_frame_func() # Skip some frames for performance. try: if self.cap_recorded_video: self._skip_frame_recorded() else: next_tick = self._skip_frame_realtime() except EOFError: pass # EOFError should be captured by the next cycle. finally: self.lock.release() if img is None: return None if self.cap_optimal_input_resolution: res720p = (img.shape[0] == 720) and (img.shape[1] == 1280) res1080p = (img.shape[0] == 1080) and (img.shape[1] == 1920) if not (res720p or res1080p): IkaUtils.dprint( 'Invalid input resolution (%dx%d). Acceptable res: 1280x720 or 1920x1080' % (img.shape[1], img.shape[0]) ) return None if next_tick is not None: self.last_tick = next_tick # need stratch? stratch = ( img.shape[0] != self.output_geometry[0] or img.shape[1] != self.output_geometry[1]) if stratch: img = cv2.resize( img, (self.output_geometry[1], self.output_geometry[0]), # fixme ) img = self._offset_filter.execute(img) return img
def read_frame(self): try: self.lock.acquire() if not self.is_active(): return None next_tick = None img = self._read_frame_func() # Skip some frames for performance. try: if self.cap_recorded_video: self._skip_frame_recorded() else: next_tick = self._skip_frame_realtime() except EOFError: pass # EOFError should be captured by the next cycle. finally: self.lock.release() if img is None: return None if self.cap_optimal_input_resolution: res720p = (img.shape[0] == 720) and (img.shape[1] == 1280) res1080p = (img.shape[0] == 1080) and (img.shape[1] == 1920) if not (res720p or res1080p): IkaUtils.dprint( 'Invalid input resolution (%dx%d). Acceptable res: 1280x720 or 1920x1080' % (img.shape[1], img.shape[0])) return None if next_tick is not None: self.last_tick = next_tick # need stratch? stratch = (img.shape[0] != self.output_geometry[0] or img.shape[1] != self.output_geometry[1]) if stratch: img = cv2.resize( img, (self.output_geometry[1], self.output_geometry[0]), # fixme ) img = self._offset_filter.execute(img) return img
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))
def predict(self, img_weapon): t1 = time.time() feat_weapon = self.image_to_feature(img_weapon) # print(feat_weapon.shape) y = forward_mlp(feat_weapon, self._layers) y_id = np.argmax(y, axis=1) # print(y) # print(y_id) # print(self._weapons_keys[y_id[0]]) # print(self._weapons_keys.index('sshooter')) t2 = time.time() IkaUtils.dprint('%s: predict %s took %s seconds' % (self, self._weapons_keys[y_id[0]], t2-t1)) return self._weapons_keys[y_id[0]], 0
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 predict(self, img_weapon): t1 = time.time() feat_weapon = self.image_to_feature(img_weapon) # print(feat_weapon.shape) y = forward_mlp(feat_weapon, self._layers) y_id = np.argmax(y, axis=1) # print(y) # print(y_id) # print(self._weapons_keys[y_id[0]]) # print(self._weapons_keys.index('sshooter')) t2 = time.time() IkaUtils.dprint('%s: predict %s took %s seconds' % (self, self._weapons_keys[y_id[0]], t2 - t1)) return self._weapons_keys[y_id[0]], 0
def _set_values(fields, dest, src): for field in fields: f_type = field[0] f_statink = field[1] f_ikalog = field[2] if (f_ikalog in src) and (src[f_ikalog] is not None): if f_type == 'int': try: dest[f_statink] = int(src[f_ikalog]) except: # ValueError IkaUtils.dprint('%s: field %s failed: src[%s] == %s' % (self, f_statink, f_ikalog, src[f_ikalog])) pass elif f_type == 'str': dest[f_statink] = str(src[f_ikalog]) elif f_type == 'str_lower': dest[f_statink] = str(src[f_ikalog]).lower()
def _set_values(fields, dest, src): for field in fields: f_type = field[0] f_statink = field[1] f_ikalog = field[2] if (f_ikalog in src) and (src[f_ikalog] is not None): if f_type == 'int': try: dest[f_statink] = int(src[f_ikalog]) except: # ValueError IkaUtils.dprint('%s: field %s failed: src[%s] == %s' % ( self, f_statink, f_ikalog, src[f_ikalog])) pass elif f_type == 'str': dest[f_statink] = str(src[f_ikalog]) elif f_type == 'str_lower': dest[f_statink] = str(src[f_ikalog]).lower()
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 auto_calibrate(self, img): r = self._warp_filter.calibrateWarp( img, validation_func=self.on_validate_warp ) if r: self._warp_filter.enable() IkaUtils.dprint('キャリブレーション成功') return True else: msg = '\n'.join([ 'WiiU の画面が検知できませんでした', '入力サイズは 1280 x 720 もしくは 1920 x 1080 です。', 'キャリブレーションを中断します。' ]) try: r = wx.MessageDialog(None, msg, 'Warining', wx.OK | wx.ICON_ERROR).ShowModal() except: IkaUtils.dprint(msg) return False
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))
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 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([ 'キャリブレーションできましたが、期待する画面サイズと異なります (%d x %d)' % (w, h), 'IkaLog への入力サイズは 1280 x 720 もしくは 1920 x 1080 です。', '各種認識が正常に動作しない可能性があります。', '', '再度キャリブレーションすると改善する場合があります。', '', ]) try: r = wx.MessageDialog(None, msg, 'Warining', wx.OK | wx.ICON_ERROR).ShowModal() except: IkaUtils.dprint(msg) else: return False self.last_capture_geom = (h, w) return True
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 get_pos_msec(args): if args['time_msec']: return args['time_msec'] elif args['time']: minute, sec = args['time'].split(':') return (int(minute) * 60 + int(sec)) * 1000 else: return 0 if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler) args = get_args() capture, output_plugins = config_loader.config(args) capture.set_pos_msec(get_pos_msec(args)) engine = IkaEngine(enable_profile=args.get('profile'), keep_alive=args.get('keep_alive')) engine.pause(False) engine.set_capture(capture) engine.set_plugins(output_plugins) for op in output_plugins: engine.enable_plugin(op) engine.close_session_at_eof = True IkaUtils.dprint('IkaLog: start.') engine.run() IkaUtils.dprint('bye!')
# UI 自体もイベントを受信 plugins.append(gui) plugins.extend(outputs_with_gui) # 本当に困ったときのデバッグログ増加モード if 'IKALOG_DEBUG' in os.environ: plugins.append(outputs.DebugLog()) # プラグインリストを登録 engine.set_plugins(plugins) gui.run() application.MainLoop() IkaUtils.dprint(_('Bye!')) if __name__ == '__main__': try: IkaUI_main() except: IkaUtils.dprint('\n\nIkaUI got an exception and crashed >>>>') IkaUtils.dprint(traceback.format_exc()) IkaUtils.dprint('<<<<') if hasattr(sys, 'frozen'): while (True): time.sleep(1)
def signal_handler(num, frame): IkaUtils.dprint('IkaLog: got signal %d' % num) if num == 2: engine.stop()
def IkaUI_main(): IkaUtils.dprint(_('Hello!')) application = wx.App() input_plugin = VideoCapture() gui = IkaLogGUI() input_plugin.on_option_tab_create(gui.options.notebookOptions) gui.frame.Show() engine = gui.create_engine() engine.close_session_at_eof = True engine.set_capture(input_plugin) plugins = [] # とりあえずデバッグ用にコンソールプラグイン plugins.append(outputs.Console()) # 各パネルをプラグインしてイベントを受信する plugins.append(gui.preview) plugins.append(gui.last_result) plugins.append(gui.timeline) # 設定画面を持つ input plugin もイベントを受信する plugins.append(input_plugin) # UI 自体もイベントを受信 plugins.append(gui) # 設定画面を持つ各種 Output Plugin # -> 設定画面の生成とプラグインリストへの登録 for plugin in [ outputs.CSV(), # outputs.Fluentd(), outputs.JSON(), # outputs.Hue(), outputs.OBS(), outputs.Twitter(), outputs.Screenshot(), outputs.Boyomi(), outputs.Slack(), outputs.StatInk(), outputs.DebugVideoWriter(), outputs.WebSocketServer(), ]: print('Initializing %s' % plugin) plugin.on_option_tab_create(gui.options.notebookOptions) plugins.append(plugin) # 本当に困ったときのデバッグログ増加モード if 'IKALOG_DEBUG' in os.environ: plugins.append(outputs.DebugLog()) # プラグインリストを登録 engine.set_plugins(plugins) # IkaLog GUI 起動時にキャプチャが enable 状態かどうか gui.set_enable(True) # Loading config engine.call_plugins('on_config_reset', debug=True) gui.load_config(engine.context) engine.call_plugins('on_config_load_from_context', debug=True) gui.start_engine() application.MainLoop() IkaUtils.dprint(_('Bye!'))
plugins.append(input_plugin) # UI 自体もイベントを受信 plugins.append(gui) plugins.extend(outputs_with_gui) # 本当に困ったときのデバッグログ増加モード if 'IKALOG_DEBUG' in os.environ: plugins.append(outputs.DebugLog()) # プラグインリストを登録 engine.set_plugins(plugins) gui.run() application.MainLoop() IkaUtils.dprint(_('Bye!')) if __name__ == '__main__': try: IkaUI_main() except: IkaUtils.dprint('\n\nIkaUI got an exception and crashed >>>>') IkaUtils.dprint(traceback.format_exc()) IkaUtils.dprint('<<<<') if hasattr(sys, 'frozen'): while(True): time.sleep(1)
def start_camera(self, source): IkaUtils.dprint( '%s: start_camera() is deprcated. Use select_source().' % self) IkaUtils.dprint(' start_camera(index=1)') IkaUtils.dprint(' start_camera(name="my capture device")') raise Exception()
def get_pos_msec(args): if args["time_msec"]: return args["time_msec"] elif args["time"]: minute, sec = args["time"].split(":") return (int(minute) * 60 + int(sec)) * 1000 else: return 0 if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler) args = get_args() capture, output_plugins = config_loader.config(args) capture.set_pos_msec(get_pos_msec(args)) engine = IkaEngine(enable_profile=args.get("profile"), keep_alive=args.get("keep_alive")) engine.pause(False) engine.set_capture(capture) engine.set_plugins(output_plugins) for op in output_plugins: engine.enable_plugin(op) engine.close_session_at_eof = True engine.run() IkaUtils.dprint("bye!")
def print_language_settings(): from ikalog.utils import IkaUtils IkaUtils.dprint("IkaLog Primary CLI Language: %s (set LANG to override)" % Localization.get_languages()[0]) IkaUtils.dprint("IkaLog Game Language: %s (set IKALOG_LANG to override)" % Localization.get_game_languages()[0])
def get_pos_msec(args): if args['time_msec']: return args['time_msec'] elif args['time']: minute, sec = args['time'].split(':') return (int(minute) * 60 + int(sec)) * 1000 else: return 0 if __name__ == "__main__": signal.signal(signal.SIGINT, signal_handler) args = get_args() capture, output_plugins = config_loader.config(args) capture.set_pos_msec(get_pos_msec(args)) engine = IkaEngine(enable_profile=args.get('profile')) engine.pause(False) engine.set_capture(capture) engine.set_plugins(output_plugins) for op in output_plugins: engine.enable_plugin(op) engine.close_session_at_eof = True engine.run() IkaUtils.dprint('bye!')
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from ikalog.utils import Localization, IkaUtils Localization.print_language_settings() import signal from ikalog.engine import * from IkaConfig import * from ikalog.utils import * engine = IkaEngine() def signal_handler(num, frame): IkaUtils.dprint('IkaLog: got signal %d' % num) if num == 2: engine.stop() signal.signal(signal.SIGINT, signal_handler) capture, OutputPlugins = IkaConfig().config() engine.pause(False) engine.set_capture(capture) engine.set_plugins(OutputPlugins) engine.close_session_at_eof = True engine.run() IkaUtils.dprint('bye!')