import cv2
import numpy as np
import torch
import matplotlib.pyplot as plt
from PIL import Image
image1 = cv2.imread('100000726.jpg')
#image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image1= Image.fromarray(cv2.cvtColor(image1,cv2.COLOR_BGR2RGB))
调用segment-anything 模型masks = mask_generator.generate(img_array)
报错“ ‘Image’ object has no attribute ‘shape’”
改成:
from PIL import Image
import matplotlib.pyplot as plt
# 图片路径
image_path = '100000726.jpg'
# 读取图片
image = Image.open(image_path)
# 使用matplotlib展示图片
plt.imshow(image)
plt.axis('off') # 不显示坐标轴
plt.show()
调用segment-anything 模型masks = mask_generator.generate(image)
也报错:
AttributeError: ‘JpegImageFile’ object has no attribute ‘shape’
解决办法:
img_array = np.array(image)
转换为numpy即可输入mask_generator.generate(ima_array)