Package: captionizer
Captions from filenames - for Stable DiffusionThis module creates captions from images in a folder based on
the folder names and the name of the image file.>>>fromcaptionizerimportcaption_from_pathIn the following examples we assume that:Token is 'randoguy'Class for the token is 'person'The base of our test data is/sd/data>>>class_token='person'>>>token='randoguy'>>>base_path='/sd/data'In the simplest case - if a file is in the root of the folder -
it will behave the same way as it does today. So:>>>print(caption_from_path('/sd/data/img-001.jpg',base_path,class_token,token))randoguypersonNext up - if you have an image file in/sd/data/joepenna/img-001.jpgyourtoken, if
passed, will be ignored andjoepennais going to be used to create the caption.>>>print(caption_from_path('/sd/data/joepenna/img-001.jpg',base_path,class_token,token))joepennapersonOne deeper: if you have an image file in/sd/data/joepenna/person/img-001.jpgthings get
more interesting: the first part is used as a token the second as a class, so you get:>>>print(caption_from_path('/sd/data/joepenna/man/img-001.jpg',base_path,class_token,token))joepennamanThis allows you to train the same subject under different classes (man,person,biped)Finally: you can customize the caption by adding an annotation to the filename, so:/sd/data/joepenna/person/img-001@a_picture_of_S.jpgwould look like:>>>print(caption_from_path('/sd/data/joepenna/person/img-001@a_picture_of_S.jpg',base_path,class_token,token))apictureofjoepennaYou can see that 'S' was automatically converted to the subject token. I can hear you scream: 'But what if I
want to have an "S" in my caption?' - well:tough luck! And wait until I tell you that I also eating all yourCs! Yes: if you have a file in/sd/data/joepenna/dude/img-001@S_the_C_hanging_out_by_the_pool.jpgthen
your caption becomes:>>>filename='/sd/data/joepenna/dude/img-001@S_the_C_hanging_out_by_the_pool.jpg'>>>print(caption_from_path(filename,base_path,class_token,token))joepennathedudehangingoutbythepoolBut wait! There is more!If you don't want to caption all of your images -- who has the time anyhow, right? -- you can have
something along these lines:/sd/data/joepenna/dude/a_picture_of_S_being_a_C/img-001@as_a_C_can_be.jpgto get:>>>filename='/sd/data/joepenna/dude/a_picture_of_S_being_a_C/img-001@as_a_C_can_be.jpg'>>>print(caption_from_path(filename,base_path,class_token,token))apictureofjoepennabeingadudeasadudecanbeNoteAll the examples show and underscore -_- in the filename instead of a space. You can use
regular spaces if your operative system supports it. So:>>>filename='/sd/data/joepenna/dude/a picture of S being a C/img-001@as a C can be.jpg'>>>print(caption_from_path(filename,base_path,class_token,token))apictureofjoepennabeingadudeasadudecanbeIf you used one of the tokens as part of the path to the file, the tokens will not be added to the caption, so
if you have/sd/data/joepenna/dude/a_photo_of_S_milling_about/img-001.jpg, the result will be:>>>filename='/sd/data/joepenna/dude/a_photo_of_S_milling_about/img-001.jpg'>>>print(caption_from_path(filename,base_path,class_token,token))aphotoofjoepennamillingaboutFinally - I promise - you can use captions directly in the old fashionedlet's dump all of our
images in the same bucketmethod, so/sd/data/img-001@S_being_a_handsome_C.jpgwould spit out:>>>filename='/sd/data/img-001@S_being_a_handsome_C.jpg'>>>print(caption_from_path(filename,base_path,class_token,token))randoguybeingahandsomepersonThe finderNow that we have a more complex structure you can see that listing the files in a folder is no
longer enough, we need to be able to recursively find them given a root data folder. Lucky us:fromcaptionizerimportfind_imagesprint(find_images('/sd/data'))If you want to see what all of this sums up to, you can run:captionizer<folderwithdata>and it will spit out a list of the files it found and the captions generated for them.Running this document as a python testYou what? Really? You don't trust me?? OK:python3captionizer/run_tests.pyCaptions from filenames - for everyone elseIf you have your own set of tokens to replace - or if you want better tokens than the ones I picked -
you can use thegeneric_captions_from_pathmethod. the method allows to setup your own mappings.This is an example of usage:>>>fromcaptionizerimportgeneric_captions_from_path>>>fromtypingimportOrderedDict>>>filename='/sd/data/img-001@X_is_better_than_S.jpg'>>>tokens=OrderedDict([('X','Mine'),('S','Yours')])>>>print(generic_captions_from_path(filename,base_path,tokens))MineisbetterthanYours