-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.py
More file actions
185 lines (162 loc) · 7.08 KB
/
tensor.py
File metadata and controls
185 lines (162 loc) · 7.08 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import json
import os
import time
import uuid
import boto3
from PIL import Image
import numpy as np
from io import BytesIO
from concurrent.futures import ThreadPoolExecutor, as_completed
from supabase import create_client, Client
from dotenv import load_dotenv
# Deep learning imports
import tensorflow as tf
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2, preprocess_input
from tensorflow.keras.preprocessing.image import img_to_array
load_dotenv()
# Initialize S3 client and bucket name from environment variable
s3_client = boto3.client('s3')
BUCKET_NAME = os.environ.get("BUCKET_NAME", "oriane-contents")
# Initialize Supabase client using environment variables
SUPABASE_URL = os.environ.get("SUPABASE_URL")
SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
# Initialize MobileNetV2 for feature extraction
# We remove the top classifier and use global average pooling to get a feature vector.
model = MobileNetV2(weights="imagenet", include_top=False, pooling="avg")
def download_frame(shortcode, frame_number, platform, extension):
"""Download a single frame from S3."""
key = f"{platform}/{shortcode}/frames/{frame_number}.{extension}"
try:
response = s3_client.get_object(Bucket=BUCKET_NAME, Key=key)
return Image.open(BytesIO(response['Body'].read()))
except s3_client.exceptions.NoSuchKey:
return None
def get_all_frames(shortcode, platform, extension):
"""
Get all available frames for a given shortcode.
Uses a ThreadPoolExecutor to download frames concurrently.
Assumes a maximum of 100 frames; adjust as needed.
"""
frames = []
max_frames = 100 # adjust if necessary
with ThreadPoolExecutor() as executor:
future_to_frame = {
executor.submit(download_frame, shortcode, i, platform, extension): i
for i in range(max_frames)
}
for future in as_completed(future_to_frame):
frame_number = future_to_frame[future]
frame = future.result()
if frame:
frames.append((frame_number, frame))
# Sort frames by frame number to maintain order
frames.sort(key=lambda x: x[0])
return [frame for _, frame in frames]
def extract_features(image: Image.Image):
"""
Convert a PIL image to a feature vector using MobileNetV2.
Resizes image to 224x224, converts to RGB, preprocesses it, and extracts features.
"""
image = image.convert("RGB")
image = image.resize((224, 224))
image_array = img_to_array(image)
image_array = np.expand_dims(image_array, axis=0)
image_array = preprocess_input(image_array)
features = model.predict(image_array)
return np.squeeze(features)
def cosine_similarity(a, b):
"""Compute the cosine similarity between two vectors."""
a = np.array(a)
b = np.array(b)
dot_product = np.dot(a, b)
norm_a = np.linalg.norm(a)
norm_b = np.linalg.norm(b)
if norm_a == 0 or norm_b == 0:
return 0.0
return dot_product / (norm_a * norm_b)
def compare_frames(frame1, frame2):
"""
Compare two frames using deep learning features.
Extract features from both frames and compute the cosine similarity.
"""
feat1 = extract_features(frame1)
feat2 = extract_features(frame2)
similarity = cosine_similarity(feat1, feat2)
return similarity
def lambda_handler(event, context):
try:
# Check if a job_id is provided; if not, generate one and insert into ai_jobs.
job_id = event.get('job_id')
if not job_id:
job_id = str(uuid.uuid4())
job_insert_response = supabase.table("ai_jobs").insert({"job_id": job_id}).execute()
if job_insert_response.error:
raise Exception(f"Error inserting job: {job_insert_response.error}")
# Extract parameters from the event
monitored_shortcode = event.get('monitored_shortcode')
watched_shortcodes = event.get('watched_shortcodes', [])
platform = event.get('platform', 'instagram')
extension = event.get('extension', 'png')
if not monitored_shortcode or not watched_shortcodes:
return {
'statusCode': 400,
'body': json.dumps({'error': 'Missing required parameters: monitored_shortcode and watched_shortcodes'})
}
# Download frames for the monitored video
monitored_frames = get_all_frames(monitored_shortcode, platform, extension)
if not monitored_frames:
return {
'statusCode': 404,
'body': json.dumps({'error': f'No frames found for monitored shortcode: {monitored_shortcode}'})
}
records_to_insert = []
# Process each watched video
for watched_shortcode in watched_shortcodes:
start_time_video = time.time()
watched_frames = get_all_frames(watched_shortcode, platform, extension)
if not watched_frames:
record = {
"job_id": job_id,
"monitored_video": monitored_shortcode,
"watched_video": watched_shortcode,
"avg_similarity": None,
"processed_in_secs": time.time() - start_time_video,
"frame_results": [],
"max_similarity": None
}
records_to_insert.append(record)
continue
frame_comparisons = []
# Compare frames one-by-one
for i, (monitored_frame, watched_frame) in enumerate(zip(monitored_frames, watched_frames)):
similarity = compare_frames(monitored_frame, watched_frame)
frame_comparisons.append({'frame_number': i, 'similarity': float(similarity)})
similarities = [comp['similarity'] for comp in frame_comparisons]
avg_similarity = float(np.mean(similarities)) if similarities else None
max_similarity = float(max(similarities)) if similarities else None
processed_time = time.time() - start_time_video
record = {
"job_id": job_id,
"monitored_video": monitored_shortcode,
"watched_video": watched_shortcode,
"avg_similarity": avg_similarity,
"processed_in_secs": processed_time,
"frame_results": frame_comparisons,
"max_similarity": max_similarity
}
records_to_insert.append(record)
# Bulk insert the results into the ai_results table in Supabase.
supabase_response = supabase.table("ai_results").insert(records_to_insert).execute()
return {
'statusCode': 200,
'body': json.dumps({
'message': 'Analysis complete and stored in Supabase',
'supabase_response': supabase_response.data
})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'error': str(e)})
}