import image-plot the SentinelHub-py utils issue



我正在尝试使用SentinelHub-py绘制图像。这是我的代码的一部分:

from sentinelhub import SHConfig   
config = SHConfig()
config.sh_client_id = "76186bb6-a02e-4457-9a9d-126e4fffaed4"
config.sh_client_secret = "aTlX[s:39vzA8p}HA{k*Zp!fJNF~(c7e.u7r21V!"
config.save()
%reload_ext autoreload
%autoreload 2
%matplotlib inline
#Import them
import os
import datetime
import numpy as np
import matplotlib.pyplot as plt

from sentinelhub import SHConfig
from sentinelhub import MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, bbox_to_dimensions, DownloadRequest
from utils import plot_image
betsiboka_coords_wgs84 = [46.16, -16.15, 46.51, -15.58]
resolution = 60
betsiboka_bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
betsiboka_size = bbox_to_dimensions(betsiboka_bbox, resolution=resolution)
print(f'Image shape at {resolution} m resolution: {betsiboka_size} pixels')
"""
Utilities used by example notebooks
"""
import matplotlib.pyplot as plt
import numpy as np

def plot_image(image, factor=3.5/255, clip_range=(0,1)):
"""
Utility function for plotting RGB images.
"""
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
if clip_range is not None:
ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
else:
ax.imshow(image * factor, **kwargs)
ax.set_xticks([])
ax.set_yticks([])

给了我以下错误:

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_6632/1226496077.py in <module>
9 from sentinelhub import MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, bbox_to_dimensions, DownloadRequest
10 
---> 11 from utils import plot_image
12 
13 CLIENT_ID='76186bb6-a02e-4457-9a9d-126e4fffaed4'
ImportError: cannot import name 'plot_image' from 'utils' (c:xxxxxxxxxxxx.py)

有人对这个问题发表了评论:

"似乎你正在调用的utils包不是正确的。试着从examples文件夹中加载utils.py,或者您可以找到[这里][1](即将文件复制到笔记本的工作目录中)。">
[1]: https://github.com/sentinel-hub/sentinelhub-py

我把我的代码改成这样:

"""
Utilities used by example notebooks
"""
import matplotlib.pyplot as plt
import numpy as np

def plot_image(image, factor=3.5/255, clip_range=(0,1)):
"""
Utility function for plotting RGB images.
"""
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
if clip_range is not None:
ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
else:
ax.imshow(image * factor, **kwargs)
ax.set_xticks([])
ax.set_yticks([])

仍然没有绘制图像。

请问,有什么建议吗?

您必须导航到utils.py。在我的电脑里是:

(/图书馆/框架/Python.framework/版本/3.10/lib/python3.10/网站/跑龙套)。

之后,您必须复制并粘贴下一个脚本:

https://github.com/sentinel-hub/sentinelhub-py/blob/master/examples/utils.py

最后,您将能够在python脚本中输入:"from utils import plot_image">

最好的,何塞。

最新更新