Giter Club home page Giter Club logo

Comments (11)

aviadpinis avatar aviadpinis commented on August 15, 2024 28

Hi,
I work with Ubuntu 18.04,
and i changed the function to:

def export_images(db_path, out_dir, flat=False, limit=-1):
    print('Exporting', db_path, 'to', out_dir)
    env = lmdb.open(db_path, map_size=1099511627776,
                    max_readers=100, readonly=True)
    count = 0
    with env.begin(write=False) as txn:
        cursor = txn.cursor()
        for key, val in cursor:
            if not flat:
                image_out_dir = join(out_dir, '/'.join(key[:6].decode()))
            else:
                image_out_dir = out_dir
            if not exists(image_out_dir):
                os.makedirs(image_out_dir)
            print('Current key:', key)
            image_out_path = join(image_out_dir, key.decode() + '.jpg')
            img = cv2.imdecode(
                numpy.fromstring(val, dtype=numpy.uint8), 1)
            cv2.imwrite(image_out_path, img)
            count += 1
            if count == limit:
                break
            if count % 1000 == 0:
                print('Finished', count, 'images')

and it works, all images save in image_out_dir.
but the problem is each image saved in separately,
so i recommend to add in finish --flat to your command, for example:

python data.py export ./bedroom_val_lmdb --out_dir ./bedroom_val --flat

it extracts that all images into one folder, so this is a more neat..

from lsun.

cippall avatar cippall commented on August 15, 2024 20

The changes below fixes this issue when running the script in a windows environment. Using .decode() for the key and 'wb' for writing in binary mode seems to work in my case. Modify these lines in the data.py script:

LINE 44 : image_out_dir = join(out_dir, '/'.join(key[:6].decode()))

LINE 50 : with open(image_out_path, 'wb') as fp:

from lsun.

arthurmchr avatar arthurmchr commented on August 15, 2024 17

@patriciaVitoria
line 49 :
image_out_path = join(image_out_dir, key.decode() + '.webp')

from lsun.

fyu avatar fyu commented on August 15, 2024

Is this still a problem? Can you print the key on that line?

from lsun.

shirishr avatar shirishr commented on August 15, 2024

@fyu, @ratsuki
I can confirm it is still a problem.
My environment is python 3.6 & opencv 3.4.1

from lsun.

zikai1 avatar zikai1 commented on August 15, 2024

I encounter the same problem, my env is python 3.6 . Didyou solve the problem ?
@ratsuki @fyu @shirishr

from lsun.

cippall avatar cippall commented on August 15, 2024

Same issue. They key prints fine : b'000013'. Any solutions ?

from lsun.

patriciaVitoria avatar patriciaVitoria commented on August 15, 2024

When i modify both lines below i get the following error:

python data.py export bedroom_val_lmdb --out_dir data
Exporting bedroom_val_lmdb to data
Traceback (most recent call last):
File "data.py", line 89, in
main()
File "data.py", line 85, in main
export_images(lmdb_path, args.out_dir, args.flat)
File "data.py", line 49, in export_images
image_out_path = join(image_out_dir, key + '.webp')
TypeError: can't concat str to bytes

Anyone know how to solve it?

from lsun.

pkuanjie avatar pkuanjie commented on August 15, 2024

The changes below fixes this issue when running the script in a windows environment. Using .decode() for the key and 'wb' for writing in binary mode seems to work in my case. Modify these lines in the data.py script:

LINE 44 : image_out_dir = join(out_dir, '/'.join(key[:6].decode()))

LINE 50 : with open(image_out_path, 'wb') as fp:

It works on Ubuntu16.04 with python3.6, thanks!

from lsun.

i-chaochen avatar i-chaochen commented on August 15, 2024

it exports the image as webp, how do you convert it as jpg?

@arthurmchr @cippall

from lsun.

littlejuyan avatar littlejuyan commented on August 15, 2024

Hi,
I work with Ubuntu 18.04,
and i changed the function to:

def export_images(db_path, out_dir, flat=False, limit=-1):
    print('Exporting', db_path, 'to', out_dir)
    env = lmdb.open(db_path, map_size=1099511627776,
                    max_readers=100, readonly=True)
    count = 0
    with env.begin(write=False) as txn:
        cursor = txn.cursor()
        for key, val in cursor:
            if not flat:
                image_out_dir = join(out_dir, '/'.join(key[:6].decode()))
            else:
                image_out_dir = out_dir
            if not exists(image_out_dir):
                os.makedirs(image_out_dir)
            print('Current key:', key)
            image_out_path = join(image_out_dir, key.decode() + '.jpg')
            img = cv2.imdecode(
                numpy.fromstring(val, dtype=numpy.uint8), 1)
            cv2.imwrite(image_out_path, img)
            count += 1
            if count == limit:
                break
            if count % 1000 == 0:
                print('Finished', count, 'images')

and it works, all images save in image_out_dir.
but the problem is each image saved in separately,
so i recommend to add in finish --flat to your command, for example:

python data.py export ./bedroom_val_lmdb --out_dir ./bedroom_val --flat

it extracts that all images into one folder, so this is a more neat..

Hi, thanks for the sharing. When I use this code to extract jpg images from mdb, I got the error:
Traceback (most recent call last):
File "exportmdb.py", line 88, in
main()
File "exportmdb.py", line 84, in main
export_images(lmdb_path, args.out_dir, args.flat)
File "exportmdb.py", line 49, in export_images
img = cv2.imdecode(numpy.fromstring(val, dtype=numpy.uint8), 1)
cv2.error: OpenCV(4.5.2) ../modules/imgcodecs/src/loadsave.cpp:736: error: (-215:Assertion failed) !buf.e
mpty() in function 'imdecode_'

Do you know how to fix this? Thanks so much!

from lsun.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.