Updated Readme
This commit is contained in:
@@ -24,7 +24,7 @@ pip install -r requirements(3.12).txt
|
|||||||
|
|
||||||
## Usage:
|
## Usage:
|
||||||
```
|
```
|
||||||
python .\rembg_video.py [-h] [--help] [-o] [--model] [--workers] [--smooth] [--read-ahead] [--write-buffer] input
|
python .\rembg_video.py [-h] [--help] [-o] [--model] [--workers] [--smooth] [--smooth-workers] [--buffer-size] input
|
||||||
```
|
```
|
||||||
<style>
|
<style>
|
||||||
table {
|
table {
|
||||||
@@ -52,20 +52,17 @@ th:nth-child(2) {
|
|||||||
<div style="display: flex; gap: 20px; align-items: flex-start;">
|
<div style="display: flex; gap: 20px; align-items: flex-start;">
|
||||||
<table>
|
<table>
|
||||||
<tr><th colspan="2">Optional Arguments:</th></tr>
|
<tr><th colspan="2">Optional Arguments:</th></tr>
|
||||||
<tr><td>-o</td><td>Output Path</td></tr>
|
<tr><td>-o</td><td>Output path</td></tr>
|
||||||
<tr><td>-h --help</td><td>Show Help</td></tr>
|
<tr><td>-h --help</td><td>Show help</td></tr>
|
||||||
<tr><td>--model</td><td>Choose model for rembg</td></tr>
|
<tr><td>--model</td><td>Choose model for rembg</td></tr>
|
||||||
|
<tr><td>--workers</td><td>Number of concurrent process workers</td></tr>
|
||||||
|
<tr><td>--smooth</td><td>Size of window for temporal smoothing</td></tr>
|
||||||
|
<tr><td>--smooth-workers</td><td>Number of CPU threads for temporal smoothing</td></tr>
|
||||||
|
<tr><td>--buffer-size</td><td>Set size of processing buffer</td></tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<table>
|
<table>
|
||||||
<tr><th colspan="2">Positional Arguments:</th></tr>
|
<tr><th colspan="2">Positional Arguments:</th></tr>
|
||||||
<tr><td>input</td><td>Input Video</td></tr>
|
<tr><td>input</td><td>Input Video</td></tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
optional arguments:
|
|
||||||
-h, --help show this help message and exit
|
|
||||||
-o Set output path (Default: export/output.mov)
|
|
||||||
-
|
|
||||||
|
|
||||||
Tip: [Alpha matting can be used to refine the results](https://github.com/danielgatis/rembg#advance-usage)
|
|
||||||
+11
-17
@@ -41,16 +41,10 @@ parser.add_argument(
|
|||||||
help="Temporal mask smoothing window size in frames (default: 3, 0 to disable)",
|
help="Temporal mask smoothing window size in frames (default: 3, 0 to disable)",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--read-ahead",
|
"--buffer-size",
|
||||||
type=int,
|
type=int,
|
||||||
default=8,
|
default=8,
|
||||||
help="Number of frames to read ahead into buffer (default: 8)",
|
help="Set maximum number of frames in buffer (default: 8)",
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--write-buffer",
|
|
||||||
type=int,
|
|
||||||
default=8,
|
|
||||||
help="Number of processed frames to buffer before writing (default: 8)",
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--smooth-workers",
|
"--smooth-workers",
|
||||||
@@ -75,9 +69,9 @@ def is_oom_error(exc):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def image_to_png_bytes(image):
|
def image_to_bytes(image):
|
||||||
with io.BytesIO() as buffer:
|
with io.BytesIO() as buffer:
|
||||||
image.save(buffer, format="PNG")
|
image.save(buffer, format="BMP")
|
||||||
return buffer.getvalue()
|
return buffer.getvalue()
|
||||||
|
|
||||||
|
|
||||||
@@ -99,7 +93,7 @@ def remove_with_fallback(image_bytes, session, scales=(1.0, 0.8, 0.6, 0.4)):
|
|||||||
Image.Resampling.LANCZOS,
|
Image.Resampling.LANCZOS,
|
||||||
)
|
)
|
||||||
with io.BytesIO() as buffer:
|
with io.BytesIO() as buffer:
|
||||||
resized.save(buffer, format="PNG")
|
resized.save(buffer, format="BMP")
|
||||||
scaled_bytes = buffer.getvalue()
|
scaled_bytes = buffer.getvalue()
|
||||||
|
|
||||||
scaled_output = remove(scaled_bytes, session=session)
|
scaled_output = remove(scaled_bytes, session=session)
|
||||||
@@ -107,11 +101,11 @@ def remove_with_fallback(image_bytes, session, scales=(1.0, 0.8, 0.6, 0.4)):
|
|||||||
scaled_output_bytes = bytes(scaled_output)
|
scaled_output_bytes = bytes(scaled_output)
|
||||||
elif isinstance(scaled_output, np.ndarray):
|
elif isinstance(scaled_output, np.ndarray):
|
||||||
with io.BytesIO() as buffer:
|
with io.BytesIO() as buffer:
|
||||||
Image.fromarray(scaled_output).save(buffer, format="PNG")
|
Image.fromarray(scaled_output).save(buffer, format="BMP")
|
||||||
scaled_output_bytes = buffer.getvalue()
|
scaled_output_bytes = buffer.getvalue()
|
||||||
elif isinstance(scaled_output, Image.Image):
|
elif isinstance(scaled_output, Image.Image):
|
||||||
with io.BytesIO() as buffer:
|
with io.BytesIO() as buffer:
|
||||||
scaled_output.save(buffer, format="PNG")
|
scaled_output.save(buffer, format="BMP")
|
||||||
scaled_output_bytes = buffer.getvalue()
|
scaled_output_bytes = buffer.getvalue()
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
@@ -126,7 +120,7 @@ def remove_with_fallback(image_bytes, session, scales=(1.0, 0.8, 0.6, 0.4)):
|
|||||||
|
|
||||||
output_full = original.copy()
|
output_full = original.copy()
|
||||||
output_full.putalpha(alpha)
|
output_full.putalpha(alpha)
|
||||||
return image_to_png_bytes(output_full)
|
return image_to_bytes(output_full)
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
last_exc = exc
|
last_exc = exc
|
||||||
@@ -185,8 +179,8 @@ try:
|
|||||||
args.model, providers=["CUDAExecutionProvider", "CPUExecutionProvider"]
|
args.model, providers=["CUDAExecutionProvider", "CPUExecutionProvider"]
|
||||||
)
|
)
|
||||||
|
|
||||||
read_queue = Queue(maxsize=args.read_ahead)
|
read_queue = Queue(maxsize=args.buffer_size)
|
||||||
write_queue = Queue(maxsize=args.write_buffer)
|
write_queue = Queue(maxsize=args.buffer_size)
|
||||||
errors = []
|
errors = []
|
||||||
active_processors = [
|
active_processors = [
|
||||||
args.workers
|
args.workers
|
||||||
@@ -280,7 +274,7 @@ try:
|
|||||||
# overwriting processed_dir in place. Overlapping windows mean a
|
# overwriting processed_dir in place. Overlapping windows mean a
|
||||||
# frame can be a *read* dependency for several write_idx tasks;
|
# frame can be a *read* dependency for several write_idx tasks;
|
||||||
# writing in place risked one thread reading a file while another
|
# writing in place risked one thread reading a file while another
|
||||||
# was mid-save on it (truncated/corrupt PNG -> shape errors).
|
# was mid-save on it (truncated/corrupt BMP -> shape errors).
|
||||||
smoothed_dir = processed_dir + "_smoothed"
|
smoothed_dir = processed_dir + "_smoothed"
|
||||||
if not os.path.isdir(smoothed_dir):
|
if not os.path.isdir(smoothed_dir):
|
||||||
os.mkdir(smoothed_dir)
|
os.mkdir(smoothed_dir)
|
||||||
|
|||||||
Reference in New Issue
Block a user