Wi-zz commited on
Commit
3cd611d
Β·
verified Β·
1 Parent(s): 42ce57b

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +199 -0
README.md ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Magic Crop ✨πŸͺ„
2
+
3
+ A powerful AI-powered image cropping tool that intelligently crops images to preserve maximum pixels while removing unwanted elements like watermarks. Uses the Florence-2-large vision-language model for accurate object detection and smart cropping decisions.
4
+
5
+ ## 🌟 Features
6
+
7
+ - **AI-Powered Detection**: Uses Florence-2-large model for accurate object detection
8
+ - **Smart Cropping**: Automatically determines the best crop direction (above, below, left, or right)
9
+ - **Object-Aware Cropping**: Optional feature to avoid cropping other important objects
10
+ - **Batch Processing**: Process multiple images efficiently with configurable batch sizes
11
+ - **Recursive Folder Processing**: Handle entire directory structures
12
+ - **Customizable Prompts**: Detect any object type, not just watermarks
13
+ - **Crop Threshold Protection**: Skip images that would require excessive cropping
14
+ - **Error Handling**: Robust error handling with optional file organization
15
+ - **GPU Acceleration**: Automatic CUDA detection for faster processing
16
+
17
+
18
+ ## πŸ“‹ Requirements
19
+
20
+ - Python 3.7+
21
+ - PyTorch
22
+ - Transformers
23
+ - PIL (Pillow)
24
+ - tqdm
25
+ - Florence-2-large model (local model provided)
26
+
27
+
28
+ ## πŸ› οΈ Installation
29
+
30
+ 1. **Install Python dependencies**:
31
+ ```bash
32
+ pip install requirements.txt
33
+ ```
34
+
35
+ 2. **Download Florence-2-large model**:
36
+ - Place the Florence-2-large model files in a `./Florence-2-large/` directory relative to the script (automatically downloaded with huggingface-cli)
37
+ - The script expects local model files (`local_files_only=True`)
38
+
39
+ ## πŸš€ Usage
40
+
41
+ ### Basic Usage
42
+
43
+ ```bash
44
+ python crop.py input_image.jpg --prompt "watermark"
45
+ ```
46
+
47
+
48
+ ### Process Multiple Images
49
+
50
+ ```bash
51
+ python crop.py image1.jpg image2.jpg image3.jpg -o output_folder
52
+ ```
53
+
54
+
55
+ ### Process Folders Recursively
56
+
57
+ ```bash
58
+ python crop.py /path/to/images/ -r -o /path/to/output/
59
+ ```
60
+
61
+
62
+ ### Advanced Usage with Object-Aware Cropping
63
+
64
+ ```bash
65
+ python crop.py /path/to/images/ -r -o output/ --object-aware --crop-threshold 15 --prompt "logo"
66
+ ```
67
+
68
+
69
+ ## πŸ“ Command-Line Arguments
70
+
71
+ | Argument | Type | Default | Description |
72
+ | :-- | :-- | :-- | :-- |
73
+ | `input_paths` | str+ | Required | Paths to input images or folders |
74
+ | `-r, --recursive` | flag | False | Process folders recursively |
75
+ | `-o, --output_folder` | str | Input path | Output folder path |
76
+ | `--bs` | int | 1 | Batch size for processing |
77
+ | `--prompt` | str | "Watermark" | Object detection prompt |
78
+ | `--object-aware` | flag | False | Enable object-aware cropping |
79
+ | `--crop-threshold` | float | 20.0 | Max crop area percentage threshold |
80
+ | `--move-skipped` | flag | False | Copy skipped files to '_Skipped_' folder |
81
+ | `--move-errored` | flag | False | Copy errored files to '_Errored_' folder |
82
+ | `--debug` | flag | False | Enable debug output |
83
+
84
+ ## 🎯 How It Works
85
+
86
+ ### 1. Object Detection
87
+
88
+ The script uses Florence-2-large to detect objects matching your prompt in the image.
89
+
90
+ ### 2. Smart Cropping Logic
91
+
92
+ - **Corner Distance Priority**: Prefers objects closer to image corners
93
+ - **Size Consideration**: Among corner objects, prefers smaller ones
94
+ - **Crop Direction**: Calculates crop area for all four directions (above, below, left, right)
95
+ - **Maximum Preservation**: Chooses the direction that preserves the most pixels
96
+
97
+
98
+ ### 3. Object-Aware Mode
99
+
100
+ When enabled, the script:
101
+
102
+ - Detects all objects in the image
103
+ - Calculates how many object pixels would be lost with each crop direction
104
+ - Chooses the crop that minimizes object pixel loss
105
+
106
+
107
+ ### 4. Safety Thresholds
108
+
109
+ - Skips images where cropping would remove more than the threshold percentage
110
+ - Default threshold: 20% of total image area
111
+
112
+
113
+ ## πŸ“ Output Structure
114
+
115
+ ```
116
+ output_folder/
117
+ β”œβ”€β”€ original_image_crop_above.jpg
118
+ β”œβ”€β”€ another_image_crop_left.jpg
119
+ β”œβ”€β”€ _Skipped_/ # (if --move-skipped enabled)
120
+ β”‚ └── skipped_files...
121
+ └── _Errored_/ # (if --move-errored enabled)
122
+ └── errored_files...
123
+ ```
124
+
125
+
126
+ ## πŸ’‘ Examples
127
+
128
+ ### Remove Watermarks
129
+
130
+ ```bash
131
+ python crop.py watermarked_images/ -r -o clean_images/ --prompt "watermark"
132
+ ```
133
+
134
+
135
+ ### Remove Logos with Object Protection
136
+
137
+ ```bash
138
+ python crop.py branded_images/ -r --object-aware --prompt "logo" --crop-threshold 10
139
+ ```
140
+
141
+
142
+ ### Process with Error Organization
143
+
144
+ ```bash
145
+ python crop.py images/ -r --move-skipped --move-errored --debug
146
+ ```
147
+
148
+
149
+ ### High-Performance Batch Processing
150
+
151
+ ```bash
152
+ python crop.py large_dataset/ -r --bs 4 -o processed/ --crop-threshold 25
153
+ ```
154
+
155
+
156
+ ## ⚠️ Important Notes
157
+
158
+ - **Model Requirement**: Ensure Florence-2-large model is properly installed in `./Florence-2-large/`
159
+ - **Memory Usage**: Larger batch sizes require more GPU/CPU memory
160
+ - **Quality**: Output images are saved as JPEG with 98% quality
161
+ - **File Formats**: Supports PNG, JPG, JPEG, GIF, BMP input formats
162
+ - **GPU Recommended**: CUDA-capable GPU significantly speeds up processing
163
+
164
+
165
+ ## πŸ› Troubleshooting
166
+
167
+ ### Common Issues
168
+
169
+ 1. **Model Loading Error**: Ensure Florence-2-large is in the correct directory
170
+ 2. **CUDA Out of Memory**: Reduce batch size (`--bs 1`)
171
+ 3. **No Objects Detected**: Try different prompts or check image quality
172
+ 4. **Large Crop Areas**: Adjust `--crop-threshold` value
173
+
174
+ ### Debug Mode
175
+
176
+ Use `--debug` flag to see:
177
+
178
+ - Detailed object detection results
179
+ - Crop calculations
180
+ - Processing decisions
181
+
182
+
183
+ ## πŸ“Š Performance Tips
184
+
185
+ - Use GPU for faster processing
186
+ - Increase batch size for bulk processing (if memory allows)
187
+ - Enable `--object-aware` only when necessary (slower but more accurate)
188
+ - Use appropriate crop thresholds to avoid processing unsuitable images
189
+
190
+
191
+ ## 🀝 Contributing
192
+
193
+ Feel free to submit issues, feature requests, or pull requests to improve this tool!
194
+
195
+ ## πŸ“„ License
196
+
197
+ This project uses the Florence-2-large model. Please check the model's license terms for usage restrictions.
198
+
199
+ <div style="text-align: center">⁂</div>