Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception: 'NoneType' object has no attribute 'crop' #191

Open
hrezvan opened this issue Sep 3, 2023 · 8 comments
Open

Exception: 'NoneType' object has no attribute 'crop' #191

hrezvan opened this issue Sep 3, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@hrezvan
Copy link

hrezvan commented Sep 3, 2023

I am trying to implement FastSAM in a for loop. I have a couple of bboxes in a list like below:
bbox[4:7] [[58.925381, 31.177402, 58.940381, 31.167402],
[58.940381, 31.167402, 58.955381, 31.157402],
[58.955381, 31.157402, 58.970381, 31.147402]]
in each loop I want to have a segmentation. every things is OK until the sixth loop which I get the Exception: 'NoneType' object has no attribute 'crop' error message for the tms_to_geotiff function. how can I solve this issue?
BTW here is my for loop code:

for j in range(len(bbox)):
  tms_to_geotiff(output='Image.tif', bbox=bbox[j], zoom=19, source="Satellite", overwrite=True)
  sam.set_image('Image.tif')
  output_mask_name = 'mask_'+str(j)+'.tif'
  sam.everything_prompt(output=output_mask_name)
  output_geojson_name = 'mask_'+str(j)+'.geojson'
  sam.raster_to_vector(output_mask_name, output_geojson_name)

In addition to the for loop, sometimes when I want to segment objects according to the FastSAM notebook, I get this error.

@hrezvan hrezvan added the bug Something isn't working label Sep 3, 2023
@giswqs
Copy link
Member

giswqs commented Sep 6, 2023

Please provide complete source code for debugging.

@hrezvan
Copy link
Author

hrezvan commented Sep 6, 2023

Please provide complete source code for debugging.

%pip install segment-geospatial segment-anything-fast

import leafmap
from samgeo import tms_to_geotiff
from samgeo.fast_sam import SamGeo
m = leafmap.Map(center=[27.371051, 62.359569], zoom=16, height="800px")
m.add_basemap("SATELLITE")
m
import numpy as np
x_range = np.arange(58.865381, 63.425381, 0.015)
y_range = np.arange(31.217403, 24.967403, -0.01)
x = x_range.tolist()
y = y_range.tolist()
bbox = []
for i in range(len(x)-1):
  for j in range(len(y)-1):
    box = [x[i], y[i], x[i+1], y[i+1]]
    bbox.append(box)
from samgeo.fast_sam import SamGeo
sam = SamGeo(model="FastSAM-x.pt")
for k in range(len(bbox)):
  tms_to_geotiff(output='Image.tif', bbox=bbox[k], zoom=16, source="Satellite", overwrite=True)
  sam.set_image('Image.tif')
  output_mask_name = 'mask_'+str(k)+'.tif'
  sam.everything_prompt(output=output_mask_name)
  output_geojson_name = 'mask_'+str(k)+'.geojson'
  sam.raster_to_vector(output_mask_name, output_geojson_name)

@rsousa95
Copy link

I got the same issue. Did you manage to fix it?

@hrezvan
Copy link
Author

hrezvan commented Nov 27, 2023

unfortunately not!

@krishnaglodha
Copy link

You guys found any solution ?

@chad-fisher
Copy link

It's because the bounding box needed is not in the same form as expected. The x and y need to be switched!

@agupta01
Copy link

agupta01 commented Jan 15, 2025

I ran into the same issue and discovered the root cause was that Google Maps Tile Server does not have tiles at my requested zoom level. SamGeo just obfuscates this error by returning a None for the non-existent tile instead of bubbling up the 404.

You can confirm this by cloning the source and replacing get_tile() in common.py with the following:

def get_tile(url):
    print(f"Fetching url: {url}")
    retry = 3
    while 1:
        try:
            r = SESSION.get(url, timeout=60)
            break
        except Exception as e:
            print(e)
            retry -= 1
            if not retry:
                raise
    if r.status_code == 404:
        print(r.text)
        return None
    elif not r.content:
        return None
    r.raise_for_status()
    return r.content

When you run tms_to_geotiff() again, if you see a bunch of 404's printed out, your requested zoom level is too high. Try lowering it until you stop seeing the error.

Longer term, segment-geospatial should fall back to lower zoom levels when this error occurs as a part of its retry policy (and surface the error as a warning). However, this probably would require a recalculation of the tiles for an image so it's not an easy fix. I can try to pick it up in the future but no promises lol.

FYI: I ran the provided coded as-is and it returned images just fine. Perhaps Google updated the images :).

@tahamukhtar20
Copy link

I faced the same issue in another library called leafmap, It worked when I ran the same code with Python 3.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants