File size: 1,002 Bytes
a8e2ab4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
import numpy as np
import cv2
from PIL import Image
import joblib
from scipy.cluster.vq import vq


class Bovw:
    def __init__(self):
        self.k, self.codebook = joblib.load("bovw-codebook.pkl")
        self.idf = np.load("idf.npy")
        self.sift = cv2.SIFT_create()

    def get_embedding(self, pil_image: Image.Image) -> np.ndarray:
        img_np = np.array(pil_image.convert("RGB"))
        img_np = cv2.resize(img_np, (224, 224))
        gray = cv2.cvtColor(img_np, cv2.COLOR_RGB2GRAY).astype('uint8')

        keypoints, descriptors = self.sift.detectAndCompute(gray, None)

        if descriptors is None or len(descriptors) == 0:
            return np.zeros(self.k)  # return zero-vector if no features found

        visual_words, _ = vq(descriptors, self.codebook)

        freq_vector = np.zeros(self.k)
        for word in visual_words:
            freq_vector[word] += 1

        tfidf_vector = freq_vector * self.idf

        return tfidf_vector