| 8 | |
| 9 | |
| 10 | class ContentDetector: |
| 11 | def __init__(self): |
| 12 | |
| 13 | model_name = "openai/clip-vit-large-patch14" |
| 14 | |
| 15 | self.model = CLIPModel.from_pretrained(model_name, cache_dir=annotator_ckpts_path).cuda().eval() |
| 16 | self.processor = AutoProcessor.from_pretrained(model_name, cache_dir=annotator_ckpts_path) |
| 17 | |
| 18 | def __call__(self, img): |
| 19 | assert img.ndim == 3 |
| 20 | with torch.no_grad(): |
| 21 | img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) |
| 22 | inputs = self.processor(images=img, return_tensors="pt").to('cuda') |
| 23 | image_features = self.model.get_image_features(**inputs) |
| 24 | image_feature = image_features[0].detach().cpu().numpy() |
| 25 | return image_feature |