[GH-ISSUE #511] Can I search images in Chinese? I used the following code to extract image features #430

Open
opened 2026-10-31 21:14:54 +00:00 by bsakaguchi · 0 comments
Owner

Originally created by @bbhxwl on GitHub (Aug 1, 2025).
Original GitHub issue: https://github.com/openai/CLIP/issues/511

model, preprocess = clip.load("ViT-B/32", device=device)

def extract_features(image_path):
    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
Originally created by @bbhxwl on GitHub (Aug 1, 2025). Original GitHub issue: https://github.com/openai/CLIP/issues/511 ``` model, preprocess = clip.load("ViT-B/32", device=device) def extract_features(image_path): 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 ```
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
openai/CLIP#430
No description provided.