Enhance background preprocessing to handle Kubernetes CPU limits for rembg
Browse files- app_utils.py +6 -0
app_utils.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import rembg
|
| 2 |
import random
|
| 3 |
import torch
|
|
@@ -64,6 +65,11 @@ def remove_background(image: Image,
|
|
| 64 |
def background_preprocess(input_image, do_remove_background):
|
| 65 |
if input_image is None:
|
| 66 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
rembg_session = rembg.new_session() if do_remove_background else None
|
| 68 |
|
| 69 |
if do_remove_background:
|
|
|
|
| 1 |
+
import os
|
| 2 |
import rembg
|
| 3 |
import random
|
| 4 |
import torch
|
|
|
|
| 65 |
def background_preprocess(input_image, do_remove_background):
|
| 66 |
if input_image is None:
|
| 67 |
return None
|
| 68 |
+
if do_remove_background:
|
| 69 |
+
omp_num_threads = os.getenv("OMP_NUM_THREADS")
|
| 70 |
+
if omp_num_threads and omp_num_threads.endswith("m"):
|
| 71 |
+
# Kubernetes CPU limits may be expressed in millicores; rembg expects an int.
|
| 72 |
+
os.environ["OMP_NUM_THREADS"] = str(max(1, int(omp_num_threads[:-1]) // 1000))
|
| 73 |
rembg_session = rembg.new_session() if do_remove_background else None
|
| 74 |
|
| 75 |
if do_remove_background:
|