I have slight differences between obtaining features on the server and on the local machine.
0.0037348971236497164
0.0037349374033510685
model, preprocess = clip.load("ViT-B/32")
def extract_features(image_path):
# 如果是URL链接,下载图片
if isinstance(image_path, Image.Image):
img = image_path.convert('RGB')
elif image_path.startswith('http://') or image_path.startswith('https://'):
response = requests.get(image_path)
img = Image.open(BytesIO(response.content)).convert('RGB')
elif os.path.exists(image_path):
img = Image.open(image_path).convert('RGB')
else:
raise ValueError("Invalid image path or URL.")
img_tensor = preprocess(img).unsqueeze(0)
with torch.no_grad():
emb = model.encode_image(img_tensor)
emb = emb / emb.norm(dim=-1, keepdim=True)
features = emb[0].tolist()
return features