0

I am trying convert jpeg files to 'lmdb' format. But I got this error:

>>> import lmdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "lmdb.py", line 25, in <module>
    write_images_to_lmdb('/home/anaca/ferjpg' , 'fer2013')
  File "lmdb.py", line 13, in write_images_to_lmdb
    env = lmdb.Environment(db_name, map_size=map_size)
AttributeError: 'module' object has no attribute 'Environment'

Things that I try :

 pip install lmdb
 sudo apt-get install liblmdv-dev

Here's the code:

import caffe
import lmdb
import os
import numpy as np
import matplotlib.pyplot as plt
from caffe.proto import caffe_pb2
from caffe.io import datum_to_array, array_to_datum
def write_images_to_lmdb(img_dir, db_name):
    for root, dirs, files in os.walk(img_dir, topdown = False):
        if root != img_dir:
            continue
        map_size = 64*64*3*2*len(files)
        env = lmdb.Environment(db_name, map_size=map_size)
        txn = env.begin(write=True,buffers=True)
        for idx, name in enumerate(files):
            X = mp.imread(os.path.join(root, name))
            y = 1
            datum = array_to_datum(X,y)
            str_id = '{:08}'.format(idx)
            txn.put(str_id.encode('ascii'), datum.SerializeToString())   
    txn.commit()
    env.close()
    print " ".join(["Writing to", db_name, "done!"])

write_images_to_lmdb('/home/anaca/ferjpg' , 'fer2013')
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
user1234
  • 145
  • 5
  • 20
  • which version of python are you using? are you using a virtualenv? which version of pip? – nir0s Mar 11 '17 at 14:22

1 Answers1

1

From your traceback it seems as if the script you are trying to run is named lmdb.py. Change its name to something else.

Otherwise, when Python sees import lmdb it assumes you mean your script rather than the lmdb module you have installed.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104