JacobLinCool commited on
Commit
9ecb18c
Β·
verified Β·
1 Parent(s): 92a946e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -2
app.py CHANGED
@@ -54,6 +54,7 @@ def generate_image_embeddings(zip_file):
54
  try:
55
  # Extract images from zip
56
  images = []
 
57
  with zipfile.ZipFile(zip_file.name, "r") as zip_ref:
58
  for file_info in zip_ref.filelist:
59
  if file_info.filename.lower().endswith(
@@ -63,13 +64,17 @@ def generate_image_embeddings(zip_file):
63
  img = Image.open(io.BytesIO(img_file.read())).convert("RGB")
64
  images.append(img)
65
 
 
 
66
  if len(images) == 0:
67
  return None, "❌ No valid images found in the zip file"
68
 
69
  # Generate embeddings
70
  embeddings = []
 
71
  with torch.no_grad():
72
  for i, image in enumerate(images):
 
73
  image_input = image_processor(
74
  images=image,
75
  max_num_patches=determine_max_value(image),
@@ -84,6 +89,7 @@ def generate_image_embeddings(zip_file):
84
  embeddings.append(normalized_features.cpu().numpy())
85
 
86
  embeddings = np.vstack(embeddings)
 
87
 
88
  # Create JSON output
89
  result = json.dumps(
@@ -96,10 +102,13 @@ def generate_image_embeddings(zip_file):
96
  )
97
 
98
  message = f"βœ… Successfully generated embeddings for {len(images)} images\nShape: {embeddings.shape}"
 
99
  return result, message
100
 
101
  except Exception as e:
102
- return None, f"❌ Error: {str(e)}"
 
 
103
 
104
 
105
  def extract_frames(video_path: str, fps: int = 4):
@@ -151,15 +160,19 @@ def generate_video_embeddings(video_file, fps):
151
  """
152
  try:
153
  # Extract frames
 
154
  frames = extract_frames(video_file.name, fps)
 
155
 
156
  if len(frames) == 0:
157
  return None, "❌ No frames could be extracted from the video"
158
 
159
  # Generate embeddings
160
  embeddings = []
 
161
  with torch.no_grad():
162
  for i, frame in enumerate(frames):
 
163
  image_input = image_processor(
164
  images=frame,
165
  max_num_patches=determine_max_value(frame),
@@ -174,6 +187,7 @@ def generate_video_embeddings(video_file, fps):
174
  embeddings.append(normalized_features.cpu().numpy())
175
 
176
  embeddings = np.vstack(embeddings)
 
177
 
178
  # Create JSON output
179
  result = json.dumps(
@@ -187,10 +201,13 @@ def generate_video_embeddings(video_file, fps):
187
  )
188
 
189
  message = f"βœ… Successfully generated embeddings for {len(frames)} frames (extracted at {fps} fps)\nShape: {embeddings.shape}"
 
190
  return result, message
191
 
192
  except Exception as e:
193
- return None, f"❌ Error: {str(e)}"
 
 
194
 
195
 
196
  # Create Gradio interface
 
54
  try:
55
  # Extract images from zip
56
  images = []
57
+ print(f"Extracting images from zip file: {zip_file.name}")
58
  with zipfile.ZipFile(zip_file.name, "r") as zip_ref:
59
  for file_info in zip_ref.filelist:
60
  if file_info.filename.lower().endswith(
 
64
  img = Image.open(io.BytesIO(img_file.read())).convert("RGB")
65
  images.append(img)
66
 
67
+ print(f"Extracted {len(images)} images from zip file")
68
+
69
  if len(images) == 0:
70
  return None, "❌ No valid images found in the zip file"
71
 
72
  # Generate embeddings
73
  embeddings = []
74
+ print(f"Generating embeddings for {len(images)} images...")
75
  with torch.no_grad():
76
  for i, image in enumerate(images):
77
+ print(f"Processing image {i + 1}/{len(images)}")
78
  image_input = image_processor(
79
  images=image,
80
  max_num_patches=determine_max_value(image),
 
89
  embeddings.append(normalized_features.cpu().numpy())
90
 
91
  embeddings = np.vstack(embeddings)
92
+ print(f"Embeddings shape: {embeddings.shape}")
93
 
94
  # Create JSON output
95
  result = json.dumps(
 
102
  )
103
 
104
  message = f"βœ… Successfully generated embeddings for {len(images)} images\nShape: {embeddings.shape}"
105
+ print(message)
106
  return result, message
107
 
108
  except Exception as e:
109
+ error_msg = f"❌ Error: {str(e)}"
110
+ print(error_msg)
111
+ return None, error_msg
112
 
113
 
114
  def extract_frames(video_path: str, fps: int = 4):
 
160
  """
161
  try:
162
  # Extract frames
163
+ print(f"Extracting frames from video: {video_file.name} at {fps} fps")
164
  frames = extract_frames(video_file.name, fps)
165
+ print(f"Extracted {len(frames)} frames from video")
166
 
167
  if len(frames) == 0:
168
  return None, "❌ No frames could be extracted from the video"
169
 
170
  # Generate embeddings
171
  embeddings = []
172
+ print(f"Generating embeddings for {len(frames)} frames...")
173
  with torch.no_grad():
174
  for i, frame in enumerate(frames):
175
+ print(f"Processing frame {i + 1}/{len(frames)}")
176
  image_input = image_processor(
177
  images=frame,
178
  max_num_patches=determine_max_value(frame),
 
187
  embeddings.append(normalized_features.cpu().numpy())
188
 
189
  embeddings = np.vstack(embeddings)
190
+ print(f"Embeddings shape: {embeddings.shape}")
191
 
192
  # Create JSON output
193
  result = json.dumps(
 
201
  )
202
 
203
  message = f"βœ… Successfully generated embeddings for {len(frames)} frames (extracted at {fps} fps)\nShape: {embeddings.shape}"
204
+ print(message)
205
  return result, message
206
 
207
  except Exception as e:
208
+ error_msg = f"❌ Error: {str(e)}"
209
+ print(error_msg)
210
+ return None, error_msg
211
 
212
 
213
  # Create Gradio interface