Skip to content

Modified the official source code of OpenAI to enable clip not only to support PIL, but also to support opencv Python

License

Notifications You must be signed in to change notification settings

qsd-github/opencv-clip

Repository files navigation

clip-opencv-python

Modified the official source code of OpenAI to enable clip not only to support PIL, but also to support opencv-python

usage

import clip
import torch
import cv2 as cv

device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("./ViT-B-32.pt", device=device)

image = preprocess(cv.imread("./images/dog.jpg")).unsqueeze(0).to(device) 
text = clip.tokenize(["a diagram", "a dog", "a cat"]).to(device)

with torch.no_grad():
    image_features = model.encode_image(image)
    text_features = model.encode_text(text)

    logits_per_image, logits_per_text = model(image, text)
    probs = logits_per_image.softmax(dim=-1).cpu().numpy()
print(probs)

About

Modified the official source code of OpenAI to enable clip not only to support PIL, but also to support opencv Python

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages