Delete inference.py
Browse files- inference.py +0 -90
inference.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Simple inference script for background removal using HuggingFace Inference API
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
import sys
|
| 8 |
-
from pathlib import Path
|
| 9 |
-
from huggingface_hub import InferenceClient
|
| 10 |
-
from PIL import Image
|
| 11 |
-
|
| 12 |
-
def remove_background(input_path, output_path, model_id=None, hf_token=None):
|
| 13 |
-
"""
|
| 14 |
-
Remove background from an image using HuggingFace Inference API
|
| 15 |
-
|
| 16 |
-
Args:
|
| 17 |
-
input_path (str): Path to input image
|
| 18 |
-
output_path (str): Path to save output image
|
| 19 |
-
model_id (str): HuggingFace model ID
|
| 20 |
-
hf_token (str): HuggingFace API token
|
| 21 |
-
"""
|
| 22 |
-
|
| 23 |
-
# Get token and model from environment if not provided
|
| 24 |
-
if not hf_token:
|
| 25 |
-
hf_token = os.getenv("HF_TOKEN")
|
| 26 |
-
if not hf_token:
|
| 27 |
-
raise ValueError("HF_TOKEN is required. Set it as environment variable or pass as argument.")
|
| 28 |
-
|
| 29 |
-
if not model_id:
|
| 30 |
-
model_id = os.getenv("MODEL_ID", "briaai/RMBG-1.4")
|
| 31 |
-
|
| 32 |
-
# Initialize client
|
| 33 |
-
client = InferenceClient(token=hf_token)
|
| 34 |
-
|
| 35 |
-
# Load image
|
| 36 |
-
if not os.path.exists(input_path):
|
| 37 |
-
raise FileNotFoundError(f"Input image not found: {input_path}")
|
| 38 |
-
|
| 39 |
-
print(f"Loading image: {input_path}")
|
| 40 |
-
image = Image.open(input_path)
|
| 41 |
-
|
| 42 |
-
print(f"Processing with model: {model_id}")
|
| 43 |
-
try:
|
| 44 |
-
# Process image
|
| 45 |
-
result = client.image_segmentation(image, model=model_id)
|
| 46 |
-
|
| 47 |
-
# Handle different output formats
|
| 48 |
-
if isinstance(result, list) and len(result) > 0:
|
| 49 |
-
# If model returns segmentation masks
|
| 50 |
-
output_image = result[0]['mask']
|
| 51 |
-
else:
|
| 52 |
-
# If model returns direct image
|
| 53 |
-
output_image = result
|
| 54 |
-
|
| 55 |
-
# Save result
|
| 56 |
-
if hasattr(output_image, 'save'):
|
| 57 |
-
output_image.save(output_path)
|
| 58 |
-
print(f"Background removed image saved: {output_path}")
|
| 59 |
-
else:
|
| 60 |
-
print("Unexpected output format from model")
|
| 61 |
-
return False
|
| 62 |
-
|
| 63 |
-
return True
|
| 64 |
-
|
| 65 |
-
except Exception as e:
|
| 66 |
-
print(f"Error processing image: {str(e)}")
|
| 67 |
-
return False
|
| 68 |
-
|
| 69 |
-
def main():
|
| 70 |
-
"""Command line interface"""
|
| 71 |
-
if len(sys.argv) < 3:
|
| 72 |
-
print("Usage: python inference.py <input_image> <output_image> [model_id]")
|
| 73 |
-
print("Example: python inference.py input.jpg output.png")
|
| 74 |
-
print("Example: python inference.py input.jpg output.png briaai/RMBG-1.4")
|
| 75 |
-
sys.exit(1)
|
| 76 |
-
|
| 77 |
-
input_path = sys.argv[1]
|
| 78 |
-
output_path = sys.argv[2]
|
| 79 |
-
model_id = sys.argv[3] if len(sys.argv) > 3 else None
|
| 80 |
-
|
| 81 |
-
success = remove_background(input_path, output_path, model_id)
|
| 82 |
-
|
| 83 |
-
if success:
|
| 84 |
-
print("✅ Background removal completed successfully!")
|
| 85 |
-
else:
|
| 86 |
-
print("❌ Background removal failed!")
|
| 87 |
-
sys.exit(1)
|
| 88 |
-
|
| 89 |
-
if __name__ == "__main__":
|
| 90 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|