File size: 2,977 Bytes
72260ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Fix: Mask Dimension Mismatch Error

## Problem
Error during video generation:
```
ValueError: could not broadcast input array from shape (1012,1024) into shape (1000,1024)
```

## Root Cause
The mask dimensions (1012Γ—1024) exceeded the canvas bounds (1000Γ—1024) at line 1081:
```python
mask_full[h_min:h_min + mask.shape[0], w_min:w_min + mask.shape[1]] = mask
```

This happened when:
1. Template bounding box (bbox) calculation positioned the mask near canvas edges
2. Mask size + position exceeded canvas dimensions
3. NumPy couldn't broadcast larger array into smaller space

## Solution
Added **bounds checking and clipping** before mask assignment:

```python
# Before (BROKEN):
mask_full[h_min:h_min + mask.shape[0], w_min:w_min + mask.shape[1]] = mask

# After (FIXED):
# Clip mask to fit within canvas bounds
canvas_h, canvas_w = mask_full.shape
mask_h, mask_w = mask.shape

# Calculate actual region that fits
h_end = min(h_min + mask_h, canvas_h)
w_end = min(w_min + mask_w, canvas_w)

# Clip mask if it exceeds bounds
actual_h = h_end - h_min
actual_w = w_end - w_min

mask_full[h_min:h_end, w_min:w_end] = mask[:actual_h, :actual_w]
```

## How It Works

### Example: Mask Exceeds Bottom/Right Bounds
```
Canvas: 1000Γ—1024 (hΓ—w)
Mask: 1012Γ—1024
Position: h_min=0, w_min=0

Before Fix:
  Tries to assign mask[0:1012, 0:1024] β†’ canvas[0:1012, 0:1024]
  ERROR: canvas only has 1000 rows!

After Fix:
  h_end = min(0 + 1012, 1000) = 1000
  w_end = min(0 + 1024, 1024) = 1024
  actual_h = 1000 - 0 = 1000
  actual_w = 1024 - 0 = 1024

  Assigns mask[0:1000, 0:1024] β†’ canvas[0:1000, 0:1024]
  βœ… SUCCESS: Clips bottom 12 rows of mask to fit
```

### Example: Mask Exceeds All Bounds
```
Canvas: 1000Γ—1024
Mask: 520Γ—530
Position: h_min=500, w_min=500

Before Fix:
  Tries: canvas[500:1020, 500:1030] = mask
  ERROR: Canvas ends at row 1000, column 1024!

After Fix:
  h_end = min(500 + 520, 1000) = 1000
  w_end = min(500 + 530, 1024) = 1024
  actual_h = 1000 - 500 = 500
  actual_w = 1024 - 500 = 524

  Assigns: canvas[500:1000, 500:1024] = mask[0:500, 0:524]
  βœ… SUCCESS: Clips mask to fit remaining canvas space
```

## Changed Files
- `app_hf_spaces.py` (line ~1077-1094)

## Testing
This fix handles:
- βœ… Masks larger than canvas
- βœ… Masks positioned near edges
- βœ… Masks that exceed multiple bounds
- βœ… Normal cases (no clipping needed)

## Impact
- βœ… Prevents crash during video generation
- βœ… Gracefully clips oversized masks
- βœ… No visual quality loss (excess mask area is outside canvas anyway)
- βœ… Works with all template sizes and aspect ratios

## Deploy
```bash
# Commit the fix
git add app_hf_spaces.py
git commit -m "Fix mask dimension mismatch error with bounds checking"

# Push to HuggingFace Space
git push hf deploy-clean-v3:main

# Wait for Space to rebuild (~2 minutes)
```

## Expected Result
Video generation should complete successfully without the broadcast error, even when masks extend beyond canvas bounds.