Montspy / LooPyGen

Loopring Python Minter on Layer 2
17 stars 6 forks source link

Checking `weights` count fails after `layer03` #24

Closed sk33z3r closed 2 years ago

sk33z3r commented 2 years ago

I'm running some test generations for a PG thing, and running into an issue I can't seem to sort out.

Using the new Background setup, I have traits from Background to layer03 setup. Generates without issue all the way through.

However, as soon as I add layer04, I get the following error:

Traceback (most recent call last):
  File "/var/www/html/generator/generate.py", line 248, in <module>
    main()
  File "/var/www/html/generator/generate.py", line 173, in main
    this_batch = img_gen.generate_images(starting_id=starting_id, image_cnt=total_image)
  File "/var/www/html/generator/generate.py", line 66, in generate_images
    unique_image = self.create_new_image(id=starting_id + i)
  File "/var/www/html/generator/generate.py", line 53, in create_new_image
    new_image[l["layer_name"]] = random.choices(l["names"], l["weights"])[0]
  File "/usr/lib/python3.9/random.py", line 499, in choices
    raise ValueError('The number of weights does not match the population')
ValueError: The number of weights does not match the population

I've triple checked my new layer, but I am quite sure the syntax is right, and the weights to filenames ratio is 1:1. When I remove this layer from traits, the script generates without issue. Here is the layer that causes the error, maybe I am missing something obvious:

    {   # layer04
        "layer_name": "Belt",
        "filenames": {
            "Black Belt": "01-belt.png",
            "Purple Belt": "02-belt.png",
            "Red Belt": "03-belt.png",
            "Green Belt": "04-belt.png",
            "Yellow Belt": "05-belt.png",
            "Purple Belt": "06-belt.png"
        },
        "weights": [
            1,
            2,
            4,
            3,
            2,
            2
        ]
    }
sk33z3r commented 2 years ago

Seems like it might be breaking down somewhere around these lines https://github.com/sk33z3r/LooPyGen/blob/main/generator/generate.py#L166-L169

But I don't see how exactly. Otherwise I don't really see how it's mis-matching in the code unless I have something set wrong in traits. Just for posterity, here's the whole file. I'm baffled why layer00-layer03 works, but layer04 fails.

## Give the collection a name
COLLECTION_NAME="Tropical JEFF"

## Layers dictionary
layers = [
    {   # Background color
        "layer_name": "Background color",
        "rgba": {
            "Red":    (192,  54,  54, 255),
            "Green":  ( 54, 192,  54, 255),
            "Blue":   ( 54,  54, 192, 255),
            "None":   (  0,   0,   0,   0),
            "Black":  (  0,   0,   0, 255),
            "White":  (255, 255, 255, 255)
        },
        "weights": [
            2,
            2,
            2,
            1,
            3,
            4
        ],
        "size": (4820, 4873)  # Adjust to the size of your images
    },
    {   # layer01
        "layer_name": "Body",
        "filenames": {
            "JEFF": "01-base.png"
        },
        "weights": [
            100
        ]
    },
    {   # layer02
        "layer_name": "Top",
        "filenames": {
            "Purple Bra": "05-shirt.png",
            "Green Bra": "06-shirt.png",
            "Peach Bra": "07-shirt.png",
            "Yellow Bra": "08-shirt.png",
            "Red Bra": "09-shirt.png",
            "Pink Bra": "10-shirt.png",
            "Pink Shirt": "11-shirt.png",
            "Green Shirt": "12-shirt.png",
            "Blue Shirt": "13-shirt.png",
            "Purple Shirt": "14-shirt.png",
            "Orange Shirt": "15-shirt.png"
        },
        "weights": [
            1,
            2,
            4,
            3,
            2,
            2,
            4,
            3,
            1,
            1,
            2
        ]
    },
    {   # layer03
        "layer_name": "Pants",
        "filenames": {
            "Blue Swimpants": "swimpants/01-swimpant.png",
            "Green Speedo": "swimpants/02-swimpant.png",
            "Pink Speedo": "swimpants/03-swimpant.png",
            "Orange Speedo": "swimpants/04-swimpant.png",
            "Red Speedo": "swimpants/05-swimpant.png",
            "Blue Speedo": "swimpants/06-swimpant.png",
            "Green Swimpants": "swimpants/07-swimpant.png",
            "Yellow Swimpants": "swimpants/08-swimpant.png",
            "Orange Swimpants": "swimpants/09-swimpant.png",
            "Red Swimpants": "swimpants/10-swimpant.png",
            "Hula Skirt 01": "hula-skirt/01-skirt.png",
            "Hula Skirt 02": "hula-skirt/02-skirt.png",
            "Hula Skirt 03": "hula-skirt/03-skirt.png",
            "Hula Skirt 04": "hula-skirt/04-skirt.png",
            "Hula Skirt 05": "hula-skirt/05-skirt.png",
            "Hula Skirt 06": "hula-skirt/06-skirt.png",
            "Hula Skirt 07": "hula-skirt/07-skirt.png",
            "Hula Skirt 08": "hula-skirt/08-skirt.png",
            "Hula Skirt 09": "hula-skirt/09-skirt.png",
            "Hula Skirt 10": "hula-skirt/10-skirt.png",
            "Hula Skirt 11": "hula-skirt/11-skirt.png",
            "Hula Skirt 12": "hula-skirt/12-skirt.png",
            "Hula Skirt 13": "hula-skirt/13-skirt.png",
            "Hula Skirt 14": "hula-skirt/14-skirt.png",
            "Hula Skirt 15": "hula-skirt/15-skirt.png",
            "Hula Skirt 16": "hula-skirt/16-skirt.png",
            "Green Pants": "pants/01-pants.png",
            "Teal Pants": "pants/02-pants.png",
            "Purple Pants": "pants/03-pants.png",
            "Red Pants": "pants/04-pants.png",
            "Yellow Pants": "pants/05-pants.png",
            "Orange Pants": "pants/06-pants.png",
            "Blue Pants": "pants/07-pants.png"
        },
        "weights": [
            25,
            15,
            15,
            15,
            15,
            15,
            25,
            25,
            25,
            25,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            10,
            40,
            40,
            40,
            40,
            40,
            40,
            40
        ]
    },
    {   # layer04
        "layer_name": "Belt",
        "filenames": {
            "Black Belt": "01-belt.png",
            "Purple Belt": "02-belt.png",
            "Red Belt": "03-belt.png",
            "Green Belt": "04-belt.png",
            "Yellow Belt": "05-belt.png",
            "Purple Belt": "06-belt.png"
        },
        "weights": [
            1,
            2,
            4,
            3,
            2,
            2
        ]
    }
]

def get_variation_cnt():
    cnt = 1
    for l in layers:
        cnt *= len(l["weights"])
    return cnt
sk33z3r commented 2 years ago

I am testing a fix for #25 and noticed that even when I take out layer00 (Background Color), the weights still seem to be mismatched if I have layer04 in there. So it doesn't seem to have anything to do with the actual amount of layers. Still thinking there's an issue I am overlooking in my layer04 syntax, though.

Montspy commented 2 years ago

Dictionary keys need to be unique. You have 2 "Purple belts" in your layer04 definition

        "filenames": {
            "Black Belt": "01-belt.png",
            "Purple Belt": "02-belt.png",
            "Red Belt": "03-belt.png",
            "Green Belt": "04-belt.png",
            "Yellow Belt": "05-belt.png",
            "Purple Belt": "06-belt.png"
        },
sk33z3r commented 2 years ago

Some times a second pair of eyes is all you need, closing