-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processor.py
More file actions
27 lines (21 loc) · 948 Bytes
/
image_processor.py
File metadata and controls
27 lines (21 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from transformers import CLIPProcessor, CLIPModel
import torch
from PIL import Image
import torch
from transformers import CLIPProcessor, CLIPModel
class ImageProcessor:
def __init__(self, model_name="openai/clip-vit-base-patch32"):
self.model = CLIPModel.from_pretrained(model_name)
self.processor = CLIPProcessor.from_pretrained(model_name)
self.model.eval()
def process_image(self, image_file):
try:
image = Image.open(image_file).convert("RGB")
inputs = self.processor(images=image, return_tensors="pt")
with torch.no_grad():
outputs = self.model.get_image_features(**inputs)
embedding = outputs / outputs.norm(p=2, dim=-1, keepdim=True) # L2 정규화
return embedding.squeeze().tolist()
except Exception as e:
print(f"Error in process_image: {e}")
return None