Skip to content

Instantly share code, notes, and snippets.

Avatar

Yuta Okamoto okapies

View GitHub Profile
@okapies
okapies / build-opencv-for-pyenv.py
Last active May 13, 2021 — forked from pohmelie/build-opencv-for-pyenv.py
Build opencv for ubuntu 16.04 with pyenv
View build-opencv-for-pyenv.py
import getpass
import os
import pathlib
import sys
import sh
BASE_DIR = os.path.abspath(os.path.join(__file__, '..'))
OPENCV_VERSION = '4.5.2'
@okapies
okapies / dataclass_util.py
Created Mar 12, 2021
Construct a dataclass from a dict with unexpected keys
View dataclass_util.py
from dataclasses import asdict, fields
from typing import Any, Dict
class DataClassBase:
@classmethod
def fromdict(cls, d: Dict[str, Any]):
"""Return a dataclass from a dict which may include unexpected keys."""
class_fields = {f.name for f in fields(cls)}
return cls(**{k: v for k, v in d.items() if k in class_fields})
@okapies
okapies / gen_text.py
Last active Jun 18, 2020
Japanese text generator using Markov chain algorithm
View gen_text.py
# -*- coding: utf-8 -*-
# ref. https://qiita.com/k-jimon/items/f02fae75e853a9c02127
from collections import deque, defaultdict, Counter
from itertools import chain, islice, takewhile
import MeCab
import os
import random
import re
@okapies
okapies / CachedContext.js
Last active Jun 4, 2020
A custom React Context persisting its state to localStorage
View CachedContext.js
// The original idea comes from a post by Alex Krush.
// https://medium.com/@akrush95/global-cached-state-in-react-using-hooks-context-and-local-storage-166eacf8ab46
import React, { useCallback, useEffect, useReducer, useRef } from 'react';
export const createCachedContext = ({ storageKey, defaultValue }) => {
const initializer = (initialState) => {
const localState = localStorage.getItem(storageKey);
if (localState) {
try {
@okapies
okapies / INSTALL.md
Last active Apr 1, 2021
How to install akvcam on Ubuntu 18.04
View INSTALL.md

Install DKMS

$ apt install dkms

Prepare for signing kernel modules

TODO

Install

Download the source code.

@okapies
okapies / csg_to_dxf.py
Last active Feb 18, 2021
A converter script from OpenSCAD's .csg or .scad to .dxf using FreeCAD
View csg_to_dxf.py
# -*- coding: utf-8 -*-
import FreeCAD
import importCSG
import importDXF
def csg_to_dxf(src, dst):
doc = importCSG.open(src)
importDXF.export([doc.TopologicalSortedObjects[0]], dst) # assumes it has single root object
FreeCAD.closeDocument(doc.Name)
@okapies
okapies / train_mnist_logreport.py
Created Jun 12, 2019
A customized train_mnist example to measure the performance of extension
View train_mnist_logreport.py
#!/usr/bin/env python
import argparse
import chainer
import chainer.functions as F
import chainer.links as L
from chainer import training
from chainer.training import extensions
import numpy as np
@okapies
okapies / np_array_order.py
Last active Mar 20, 2019
How `numpy.array(order='F')` works
View np_array_order.py
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b')
array([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]], dtype=int8)
>>> ctypes.string_at(np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b').ctypes.data, 8) # .data.tobytes() doesn't work properly
b'\x00\x04\x02\x06\x01\x05\x03\x07'
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b').__array_interface__
{'data': (23426096, False), 'strides': None, 'descr': [('', '|i1')], 'typestr': '|i1', 'shape': (2, 2, 2), 'version': 3}
>>> np.array([[[0, 4], [2, 6]], [[1, 5], [3, 7]]], dtype='b').flags
@okapies
okapies / xp_nested_array-usage.md
Last active Jun 12, 2019
A performance evaluation of creating a (numpy|cupy) array from nested arrays
View xp_nested_array-usage.md
$ python xp_nested_array.py --src-xp numpy --dst-xp numpy --shape "(3, 224, 224)" --batch-size 10
Shape: (3, 224, 224)
Batch size: 10
Running numpy.array(<List[numpy.ndarray]>) in 10000 times...
3.857709832955152
@okapies
okapies / keymap.c
Last active Nov 2, 2020
ErgoDash keymap implementing “correct” dual role RAISE/LOWER keys without `LT` advanced keycode
View keymap.c
#include QMK_KEYBOARD_H
extern keymap_config_t keymap_config;
#define _QWERTY 0
#define _LOWER 1
#define _RAISE 2
#define _ADJUST 16
enum custom_keycodes {