Import Lmdblmdb_img_name="Test.lmdb"Env= Lmdb.open (Lmdb_img_name, map_size=1e6) with Env.begin (write=True) as Txn:txn.put ("Key","xxxxxxxxxxxxxxxxxx") Txn.put ("Key2","?") Txn.put ("Key3","some value") #Txn.commit ()env.close () env= Lmdb.open (Lmdb_img_name, readonly=True) with Env.begin () as TXN:PrintTxn.get (b'Key') Cursor=txn.cursor () forKey, Valueinchcursor:Print(Key, value)
See more: http://blog.csdn.net/ayst123/article/details/44077903, excerpt as follows:
Write
db_img = lmdb.Environment(lmdb_img_name,map_size=int(1e12))txn_img = db_img.begin(write=True,buffers=True)datum_img = getImgDatum(img_path)txn_img.put(key,datum_img.SerializeToString())txn_img.commit()db_img.close()
Here, first establish the Lmdb instance, db_img. Then start TXN (there may be a lot of starts?) )。 Put storage, and finally must pay attention to commit (). Otherwise there is no deposit in, check the generated Lmdb file can be seen. Finally, close ()
Here the generated datum is the function with Caffe, do io.py, called Caffe.io.array_to_datum, the array into datum.
Note:
Datum must use Datum when it is saved. Serializetostring (), turn it into a string ()
Segmented Write
for ind,obj in enumerate(list): if ind%100=0: txn.commit() txn = env.begin(write=True)txn.commit()env.close()
After each commit, you need to define Txn=env.begin (Write=true) again
For loop out of the last commit
Read
img_lmdb = imdb.open("name")txn= img_lmdb.begin()cursor = txn.cursor()cursor.iternext()key = cursor.key()value = cursor.value()datum = caffe_pb2.Datum()datum.ParseFromString(value)
Note:
Cursor the first is empty, Iternext () is the first value
Loops
TXN = ...
cursor = Txn.cursor ()
For (IDX, (key, value)) in enumerate (cursor):
................
It's going to look like a loop.
Python Lmdb Demo This interface is as disgusting as BDB!