refactor(main.py): simplify memory usage calculation and streamline video safety checks
This commit is contained in:
25
main.py
25
main.py
@@ -489,34 +489,27 @@ class MediaGrader:
|
|||||||
if not self.is_video(self.media_files[self.current_index]):
|
if not self.is_video(self.media_files[self.current_index]):
|
||||||
return
|
return
|
||||||
|
|
||||||
# Safety check for huge videos
|
|
||||||
safe_frame_count = max(1, int(self.total_frames * 0.6))
|
|
||||||
|
|
||||||
# Calculate actual memory usage based on frame dimensions
|
# Calculate actual memory usage based on frame dimensions
|
||||||
frame_width = int(self.current_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
frame_width = int(self.current_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
||||||
frame_height = int(self.current_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
frame_height = int(self.current_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
||||||
bytes_per_frame = frame_width * frame_height * 3 # RGB (3 bytes per pixel)
|
total_mb = frame_width * frame_height * 3 / (1024 * 1024)
|
||||||
total_bytes = safe_frame_count * bytes_per_frame
|
|
||||||
total_mb = total_bytes / (1024 * 1024)
|
|
||||||
total_gb = total_mb / 1024
|
|
||||||
|
|
||||||
# Memory-based limits (not frame count)
|
# Memory-based limits (not frame count)
|
||||||
if total_gb > 8: # 8GB limit
|
if total_mb > 8000: # 8GB limit
|
||||||
print(f"Video too large for preloading!")
|
print(f"Video too large for preloading!")
|
||||||
print(f" Resolution: {frame_width}x{frame_height}")
|
print(f" Resolution: {frame_width}x{frame_height}")
|
||||||
print(f" Frames: {safe_frame_count} frames would use {total_gb:.1f}GB RAM")
|
print(f" Frames: {self.total_frames} frames would use {total_mb:.1f}GB RAM")
|
||||||
print(f"Multi-segment mode not available for videos requiring >8GB RAM")
|
print(f"Multi-segment mode not available for videos requiring >8GB RAM")
|
||||||
return
|
return
|
||||||
elif total_mb > 500: # 500MB warning
|
elif total_mb > 500: # 500MB warning
|
||||||
print(f"Large video detected:")
|
print(f"Large video detected:")
|
||||||
print(f" Resolution: {frame_width}x{frame_height}")
|
print(f" Resolution: {frame_width}x{frame_height}")
|
||||||
print(f" Memory: {safe_frame_count} frames will use {total_mb:.0f}MB RAM")
|
print(f" Memory: {self.total_frames} frames will use {total_mb:.0f}GB RAM")
|
||||||
print("Press any key to continue or 'q' to cancel...")
|
print("Press any key to continue or 'q' to cancel...")
|
||||||
# Note: In a real implementation, you'd want proper input handling here
|
# Note: In a real implementation, you'd want proper input handling here
|
||||||
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
print(f"Setting up {self.segment_count} segments with video preloading...")
|
print(f"Setting up {self.segment_count} segments with video preloading...")
|
||||||
print(f"Will preload {safe_frame_count} frames ({frame_width}x{frame_height}) = {total_mb:.0f}MB RAM")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
print("Cleaning up existing captures...")
|
print("Cleaning up existing captures...")
|
||||||
@@ -547,11 +540,11 @@ class MediaGrader:
|
|||||||
position_ratio = i / (self.segment_count - 1)
|
position_ratio = i / (self.segment_count - 1)
|
||||||
end_ratio = (i + 1) / (self.segment_count - 1) if i < self.segment_count - 1 else 1.0
|
end_ratio = (i + 1) / (self.segment_count - 1) if i < self.segment_count - 1 else 1.0
|
||||||
|
|
||||||
start_frame = int(position_ratio * (safe_frame_count - 1))
|
start_frame = int(position_ratio * (self.total_frames - 1))
|
||||||
end_frame = int(end_ratio * (safe_frame_count - 1))
|
end_frame = int(end_ratio * (self.total_frames - 1))
|
||||||
|
|
||||||
start_frame = max(0, min(start_frame, safe_frame_count - 1))
|
start_frame = max(0, min(start_frame, self.total_frames - 1))
|
||||||
end_frame = max(start_frame + 1, min(end_frame, safe_frame_count - 1)) # Ensure at least 1 frame per segment
|
end_frame = max(start_frame + 1, min(end_frame, self.total_frames - 1)) # Ensure at least 1 frame per segment
|
||||||
|
|
||||||
self.segment_positions.append(start_frame)
|
self.segment_positions.append(start_frame)
|
||||||
self.segment_end_positions.append(end_frame)
|
self.segment_end_positions.append(end_frame)
|
||||||
@@ -571,7 +564,7 @@ class MediaGrader:
|
|||||||
frames = []
|
frames = []
|
||||||
frame_count = 0
|
frame_count = 0
|
||||||
|
|
||||||
while frame_count < safe_frame_count:
|
while frame_count < self.total_frames:
|
||||||
ret, frame = self.current_cap.read()
|
ret, frame = self.current_cap.read()
|
||||||
if ret and frame is not None:
|
if ret and frame is not None:
|
||||||
frames.append(frame)
|
frames.append(frame)
|
||||||
|
Reference in New Issue
Block a user