fix
Browse files- __pycache__/perf_tuning.cpython-313.pyc +0 -0
- models/__init__.py +66 -25
__pycache__/perf_tuning.cpython-313.pyc
CHANGED
|
Binary files a/__pycache__/perf_tuning.cpython-313.pyc and b/__pycache__/perf_tuning.cpython-313.pyc differ
|
|
|
models/__init__.py
CHANGED
|
@@ -592,33 +592,74 @@ def matany_worker():
|
|
| 592 |
try:
|
| 593 |
logger.info("MatAnyone worker thread started")
|
| 594 |
|
| 595 |
-
#
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
|
| 600 |
-
progress_msg = f" MatAnyone processing frame {frame}/{total_frames} ({frame/total_frames*100:.1f}%)"
|
| 601 |
-
try:
|
| 602 |
-
progress_callback(progress_msg)
|
| 603 |
-
except:
|
| 604 |
-
logger.info(progress_msg)
|
| 605 |
-
progress_container[0] = frame
|
| 606 |
-
time.sleep(0.1) # Simulate processing time
|
| 607 |
|
| 608 |
-
|
| 609 |
-
|
| 610 |
-
progress_thread = threading.Thread(target=mock_progress)
|
| 611 |
-
progress_thread.daemon = True
|
| 612 |
-
progress_thread.start()
|
| 613 |
|
| 614 |
-
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
|
| 618 |
-
|
| 619 |
-
|
| 620 |
-
|
| 621 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
except Exception as e:
|
| 623 |
logger.error(f"MatAnyone worker thread failed: {e}")
|
| 624 |
exception_container[0] = e
|
|
|
|
| 592 |
try:
|
| 593 |
logger.info("MatAnyone worker thread started")
|
| 594 |
|
| 595 |
+
# Read video frames and mask
|
| 596 |
+
import cv2
|
| 597 |
+
import numpy as np
|
| 598 |
+
cap = cv2.VideoCapture(str(video_path))
|
| 599 |
+
mask_img = cv2.imread(str(mask_path), cv2.IMREAD_GRAYSCALE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
|
| 601 |
+
if mask_img is None:
|
| 602 |
+
raise ValueError(f"Could not read mask image: {mask_path}")
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
+
# Get video properties
|
| 605 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 606 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 607 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 608 |
+
|
| 609 |
+
# Resize mask to match video dimensions
|
| 610 |
+
mask_resized = cv2.resize(mask_img, (width, height))
|
| 611 |
+
seed_mask_hw = mask_resized.astype('float32') / 255.0
|
| 612 |
+
|
| 613 |
+
# Prepare output video writers
|
| 614 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 615 |
+
fg_writer = cv2.VideoWriter(str(fg_path), fourcc, fps, (width, height))
|
| 616 |
+
alpha_writer = cv2.VideoWriter(str(alpha_path), fourcc, fps, (width, height))
|
| 617 |
+
|
| 618 |
+
# Process frames using MatAnyone frame-by-frame API
|
| 619 |
+
frame_count = 0
|
| 620 |
+
frames = []
|
| 621 |
+
|
| 622 |
+
# Read all frames first
|
| 623 |
+
while True:
|
| 624 |
+
ret, frame = cap.read()
|
| 625 |
+
if not ret:
|
| 626 |
+
break
|
| 627 |
+
frames.append(frame)
|
| 628 |
+
|
| 629 |
+
cap.release()
|
| 630 |
+
|
| 631 |
+
# Process frames through MatAnyone
|
| 632 |
+
for i, alpha_result in enumerate(session.process_video(frames, seed_mask_hw, every=10)):
|
| 633 |
+
frame_count += 1
|
| 634 |
+
|
| 635 |
+
# Update progress
|
| 636 |
+
if progress_callback:
|
| 637 |
+
progress_msg = f"MatAnyone processing frame {frame_count}/{total_frames} ({frame_count/total_frames*100:.1f}%)"
|
| 638 |
+
try:
|
| 639 |
+
progress_callback(progress_msg)
|
| 640 |
+
except:
|
| 641 |
+
logger.info(progress_msg)
|
| 642 |
+
|
| 643 |
+
# Get current frame
|
| 644 |
+
current_frame = frames[i]
|
| 645 |
+
|
| 646 |
+
# Convert alpha to 3-channel for video writing
|
| 647 |
+
alpha_3ch = cv2.cvtColor((alpha_result * 255).astype('uint8'), cv2.COLOR_GRAY2BGR)
|
| 648 |
+
|
| 649 |
+
# Create foreground by applying alpha mask
|
| 650 |
+
alpha_norm = alpha_result[:, :, np.newaxis]
|
| 651 |
+
fg_frame = (current_frame.astype('float32') * alpha_norm).astype('uint8')
|
| 652 |
+
|
| 653 |
+
# Write frames
|
| 654 |
+
fg_writer.write(fg_frame)
|
| 655 |
+
alpha_writer.write(alpha_3ch)
|
| 656 |
+
|
| 657 |
+
# Clean up
|
| 658 |
+
fg_writer.release()
|
| 659 |
+
alpha_writer.release()
|
| 660 |
+
|
| 661 |
+
result_container[0] = True
|
| 662 |
+
logger.info(f"MatAnyone worker thread completed successfully - processed {frame_count} frames")
|
| 663 |
except Exception as e:
|
| 664 |
logger.error(f"MatAnyone worker thread failed: {e}")
|
| 665 |
exception_container[0] = e
|