-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.py
More file actions
34 lines (25 loc) · 1.14 KB
/
Engine.py
File metadata and controls
34 lines (25 loc) · 1.14 KB
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
#prende un video e lo confronta con i top 3 scorer tramite la cosine similiarity
from embedd import VideoEmbedding
from user import User
import os
from numpy.linalg import norm
import numpy as np
class Engine:
def __init__(self,user):
self.user=user
def video_search(self, dir):
files={f_name:0 for f_name in os.listdir(dir) if f_name.endswith('.mp4')}
system=VideoEmbedding()
for file in files.keys():
embedding=system.video_segmentation(f'{dir}/{file}')
scores=[]
for e,_,_ in self.user.embeddings[:3]:
score=self.score(embedding,e)
scores.append(score)
combined = np.array(scores) * np.array([e[2] for e in self.user.embeddings[:3]])
weights = np.exp(combined) / np.sum(np.exp(combined))
files[file] = np.dot(weights, combined)
return sorted(files, key=lambda video: files[video], reverse=True)[0]
def score(self, input_embedding, user_embedding):
cosine = np.dot(input_embedding, user_embedding) / (norm(input_embedding) * norm(user_embedding))
return cosine