Spatial Analyst's SegmentMeanShift tool fails in standalone script

2922
12
10-18-2016 12:45 PM
EvanBlaisdell
New Contributor III

In ArcMap 10.4, I can run the SegmentMeanShift tool (Segment Mean Shift—Help | ArcGIS for Desktop ) in the Python window, or from the ArcToolbox, just fine. 

However, when trying to run a standalone script incorporating the tool, I receive the ever-useful "Unexpected Error." 

I am using the standalone tool template as provided in the link above. My Spatial Analyst licenses are valid. I am not using the optional band indexes parameter.

# Import system modules
import arcpy
from arcpy.sa import *

# Set local variables
inRaster = r"Path:\blah\blah\master.img"
spectral_detail = "15"
spatial_detail = "15"
min_segment_size = "10"

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# Execute
seg_raster = SegmentMeanShift(inRaster, spectral_detail, spatial_detail, min_segment_size)

# Save the output
seg_raster.save(r"Path:\blah\blah\seg_test.img")

This is the error: 

File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Functions.py", line 7783, in SegmentMeanShift
band_indexes)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\sa\Functions.py", line 7776, in Wrapper
band_indexes)
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 999998: Unexpected Error.
Failed to execute (SegmentMeanShift).

Again, it works fine within ArcMap. I should say I have gotten it to work in a standalone script before, with different spectral, spatial, and min_size parameters. If it's a problem with the inputs I don't know why it would work within ArcMap.

Any thoughts? Thanks.

0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

If you do it within arcmap, what do the results look like in the Results window?  Export it as a python snippet from there for comparison.  The only obvious difference between your example and the help topic example is the 4th option isn't specified and you used raw formatting for the paths and they used foreward slashes.  I am assuming that Path:\blah\blah doesn't contain spaces or other evil characters in it

EvanBlaisdell
New Contributor III

The paths are fine, and the 4th parameter is optional, but just for giggles I tried specifying that parameter and experienced the same behavior. 

This is how it looks in the Results window, after having run successfully within ArcMap:

It also works in the Python window within ArcMap:

arcpy.gp.SegmentMeanShift_sa("D:/REDACTED/master.img", "D:/REDACTED/segments.img", "20", "15", "10", "1 2 3")

But not in a standalone script:

Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 2016.2.3\helpers\pydev\_pydevd_bundle\pydevd_exec.py", line 3, in Exec
exec exp in global_vars, local_vars
File "<input>", line 1, in <module>
File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999998: Unexpected Error.
Failed to execute (SegmentMeanShift).

I have tried two different syntaxes in the script:

segments = SegmentMeanShift(in_raster, 20.0, 15, 10, '1 2 3')

and

arcpy.gp.SegmentMeanShift_sa(in_raster, segments, 20.0, 15, 10, "1 2 3")
0 Kudos
DanPatterson_Retired
MVP Emeritus

so now you have narrowed down to the pycharm environment... hmmmm 

you keep saying the 'script'

do you mean you did it the same way in the arcmap python window and compared it to doing it inside of pycharm s command line?  line by line regardless of the IDE should work whether it is arcmaps' ide, pythonwin, pycharm, spyder whatever.  If everything works on a line by line basis, then that is good. unfortunately back to you running your full script

so try your arcpy.gp.Segm.... in pycharm after import arcpy etc

EvanBlaisdell
New Contributor III

Well, new wrinkle. I adjusted the parameters and it works for some combinations but not others. It seems that spectral_detail is the culprit, with the tool failing for anything 18.0 or higher. The supposed maximum is 20.0. Of course, still, when I run it in ArcMap it works fine. Whether this is a problem with my particular data or a bug in the tool is a mystery to me.

0 Kudos
DanPatterson_Retired
MVP Emeritus

That would require testing with a different set of data... but that is pretty well it, since I see nothing on the bug list

Esri Support Search-Results 

If you remove the 'bug' filter... you get to see your post plus some help file topics and other posts

Esri Support Search-Results 

0 Kudos
EvanBlaisdell
New Contributor III

Similar behavior with a different dataset, but the maximum functioning value of the spectral_detail parameter was a bit different.

Perhaps more importantly, I tried my script on a machine that was still on 10.3, and it works fine. Same script on 10.4, no dice.

0 Kudos
DanPatterson_Retired
MVP Emeritus

then time to pass it up the food chain to tech support.. it could be a bug.  I don't imagine that there is a whole crowd using that tool in the world so it may have gone largely unnoticed, especially since you indicate it only fails in a standalone script and/or a non-arcmap python IDE.  Worth a shot... anyone can file a bug in Support

0 Kudos
YiZou1
by
New Contributor
arcpy.gp.SegmentMeanShift_sa(in_raster,r"C:\test\seg_test.img", 20.0, 15, 10, "1 2 3")

Can you try this again?

0 Kudos
NilsRatnaweera1
New Contributor II

I'm experiancing the same problem as described by Dan Patterso but I can't solve it by reducing spectral detail.

Your suggestion

arcpy.gp.SegmentMeanShift_sa(in_raster,r"C:\test\seg_test.img", 20.0, 15, 10, "1 2 3")

returns this error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: SegmentMeanShift() takes at most 5 arguments (6 given)
0 Kudos