class TestOtama(unittest.TestCase): def setUp(self): if not os.path.exists(DATA_DIR): os.mkdir(DATA_DIR) self.otama = Otama() self.db = self.otama.open(CONFIG) def tearDown(self): shutil.rmtree(DATA_DIR) def test_open(self): o = Otama() db = o.open(CONFIG) self.assertEqual(type(db), Otama) self.assertEqual(True, os.path.exists(DATA_DIR)) def test_close(self): self.assertEqual(None, self.db.close()) def test_create_database(self): self.assertEqual(None, self.db.create_database()) def test_drop_database(self): self.assertEqual(None, self.db.drop_database()) def test_drop_index(self): self.assertEqual(None, self.db.drop_index()) def test_vacuum_index(self): self.assertEqual(None, self.db.vacuum_index()) def test_has_libotama_version_string(self): self.assertEqual(str, type(otama.__libotama_version__))
class TestOtamaWithLevelDB(unittest.TestCase): def setUp(self): if not os.path.exists(DATA_DIR): os.mkdir(DATA_DIR) self.otama = Otama() self.db = self.otama.open(INVOKE_CONFIG) def tearDown(self): shutil.rmtree(DATA_DIR) def test_invoke(self): # FIXME self.assertEqual(None, self.db.invoke('update_idf', 0))
BASE_DIR = os.path.abspath(os.path.dirname(__file__)) DATA_DIR = os.path.join(BASE_DIR, 'data') CONFIG_FILE = os.path.join(BASE_DIR, 'test.conf') IMAGE_DIR = os.path.join(BASE_DIR, 'image') config = {'namespace': 'testnamespace', 'driver': {'name': 'color', 'data_dir': DATA_DIR, 'color_weight': 0.2}, 'database': {'driver': 'sqlite3', 'name': os.path.join(DATA_DIR, 'store.sqlite3')} } if not os.path.exists(DATA_DIR): os.mkdir(DATA_DIR) #db = Otama.open(CONFIG_FILE) db = Otama.open(config) db.create_database() files = glob(os.path.join(IMAGE_DIR, '*.jpg')) files += glob(os.path.join(IMAGE_DIR, '*.png')) kvs = {} for filename in files: kvs[db.insert(filename)] = filename db.pull() with open(os.path.join(DATA_DIR, 'kvs.json'), 'w') as fp: json.dump(kvs, fp) db.close()
import os import json from otama import Otama BASE_DIR = os.path.abspath(os.path.dirname(__file__)) TARGET_FILE = os.path.join(BASE_DIR, 'image/lena.jpg') CONFIG_FILE = os.path.join(BASE_DIR, 'test.conf') DATA_DIR = os.path.join(BASE_DIR, 'data') kvs = None with open(os.path.join(DATA_DIR, 'kvs.json')) as fp: kvs = json.load(fp) last_id = None db = Otama(CONFIG_FILE) for result in db.search(10, TARGET_FILE): key = result['id'] print("sim=%.3f, file=%s" % (result['similarity'], kvs[key])) last_id = result['id'] print(db.exists(last_id)) print(db.exists('a' + last_id[1:-1] + 'b'))
def setUp(self): if not os.path.exists(DATA_DIR): os.mkdir(DATA_DIR) self.otama = Otama() self.db = self.otama.open(INVOKE_CONFIG)
def test_open(self): o = Otama() db = o.open(CONFIG) self.assertEqual(type(db), Otama) self.assertEqual(True, os.path.exists(DATA_DIR))
import os import json from otama import Otama BASE_DIR = os.path.abspath(os.path.dirname(__file__)) TARGET_FILE1 = os.path.join(BASE_DIR, "image/lena.jpg") TARGET_FILE2 = os.path.join(BASE_DIR, "image/lena-affine.jpg") CONFIG_FILE = os.path.join(BASE_DIR, "test.conf") DATA_DIR = os.path.join(BASE_DIR, "data") db = Otama({"driver": {"name": "vlad_nodb"}}) print(db.similarity({"data": open(TARGET_FILE1, "rb").read()}, {"data": open(TARGET_FILE2, "rb").read()})) fv = db.feature_raw({"file": TARGET_FILE1}) print(fv) print(db.similarity({"raw": fv}, {"file": TARGET_FILE1})) print(db.similarity({"raw": fv}, {"file": TARGET_FILE2})) print(db.similarity({"raw": fv}, {"file": str(TARGET_FILE2)})) fv.dispose() db.close()
import os import json from otama import Otama BASE_DIR = os.path.abspath(os.path.dirname(__file__)) TARGET_FILE = os.path.join(BASE_DIR, 'image/lena.jpg') CONFIG_FILE = os.path.join(BASE_DIR, 'test.conf') DATA_DIR = os.path.join(BASE_DIR, 'data') kvs = None with open(os.path.join(DATA_DIR, 'kvs.json')) as fp: kvs = json.load(fp) db = Otama(CONFIG_FILE) for result in db.search(10, TARGET_FILE): key = result['id'] last_id = result['id'] print("sim=%.3f, file=%s" % (result['similarity'], kvs[key])) print("=*" * 30) db.remove(last_id) db.pull() for result in db.search(10, TARGET_FILE): key = result['id'] print("sim=%.3f, file=%s" % (result['similarity'], kvs[key]))
import time import os.path from otama import Otama BASEPATH = os.path.abspath(os.path.dirname(__file__)) print(BASEPATH) while True: db = Otama(os.path.join(BASEPATH, 'test.conf')) db.search(5, os.path.join(BASEPATH, 'image/baboon.png')) #db.close() #time.sleep(0.1)