PGSCOM commited on
Commit
dbc6a2b
·
1 Parent(s): d6e7745

Versión para permitir video 4K

Browse files
Files changed (2) hide show
  1. Dockerfile +34 -0
  2. hugging_face/app.py +15 -13
Dockerfile ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official PyTorch image with CUDA support
2
+ FROM pytorch/pytorch:2.1.0-cuda12.1-cudnn8-runtime
3
+
4
+ # Set working directory
5
+ WORKDIR /app
6
+
7
+ # Install system dependencies required for OpenCV and FFmpeg
8
+ RUN apt-get update && apt-get install -y \
9
+ git \
10
+ ffmpeg \
11
+ libgl1-mesa-glx \
12
+ libglib2.0-0 \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ # Copy requirements file
16
+ COPY requirements.txt .
17
+
18
+ # Install Python dependencies
19
+ # Note: PySide6 and pyqtdarktheme are excluded/ignored if they fail,
20
+ # as they are for desktop GUI and not needed for this web demo.
21
+ RUN pip install --no-cache-dir -r requirements.txt || true
22
+
23
+ # Copy the rest of the application code
24
+ COPY . .
25
+
26
+ # Expose the port defined in app.py (default 8000)
27
+ EXPOSE 8000
28
+
29
+ # Set environment variables
30
+ ENV GRADIO_SERVER_NAME="0.0.0.0"
31
+
32
+ # Command to run the application
33
+ # We use the --port argument to match the EXPOSE instruction
34
+ CMD ["python", "hugging_face/app.py", "--port", "8000"]
hugging_face/app.py CHANGED
@@ -132,7 +132,7 @@ def get_frames_from_video(video_input, video_state):
132
  if ret == True:
133
  current_memory_usage = psutil.virtual_memory().percent
134
  frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
135
- if current_memory_usage > 90:
136
  break
137
  else:
138
  break
@@ -141,14 +141,14 @@ def get_frames_from_video(video_input, video_state):
141
  image_size = (frames[0].shape[0],frames[0].shape[1])
142
 
143
  # resize if resolution too big
144
- if image_size[0]>=1280 and image_size[0]>=1280:
145
- scale = 1080 / min(image_size)
146
- new_w = int(image_size[1] * scale)
147
- new_h = int(image_size[0] * scale)
148
- # update frames
149
- frames = [cv2.resize(f, (new_w, new_h), interpolation=cv2.INTER_AREA) for f in frames]
150
- # update image_size
151
- image_size = (frames[0].shape[0],frames[0].shape[1])
152
 
153
  # initialize video_state
154
  video_state = {
@@ -363,8 +363,8 @@ def generate_video_from_frames(frames, output_path, fps=30, gray2rgb=False, audi
363
  video_temp_path = output_path.replace(".mp4", "_temp.mp4")
364
 
365
  # resize back to ensure input resolution
366
- imageio.mimwrite(video_temp_path, frames, fps=fps, quality=7,
367
- codec='libx264', ffmpeg_params=["-vf", f"scale={w}:{h}"])
368
 
369
  # add audio to video if audio path exists
370
  if audio_path != "" and os.path.exists(audio_path):
@@ -425,6 +425,8 @@ from matanyone.model.matanyone import MatAnyone
425
  matanyone_model = MatAnyone.from_pretrained("PeiqingYang/MatAnyone")
426
 
427
  matanyone_model = matanyone_model.to(args.device).eval()
 
 
428
  matanyone_processor = InferenceCore(matanyone_model, cfg=matanyone_model.cfg)
429
 
430
  # download test samples
@@ -450,7 +452,7 @@ description = r"""
450
  🔥 MatAnyone is a practical human video matting framework supporting target assignment 🎯.<br>
451
  🎪 Try to drop your video/image, assign the target masks with a few clicks, and get the the matting results 🤡!<br>
452
 
453
- *Note: Due to the online GPU memory constraints, any input with too big resolution will be resized to 1080p.<br>*
454
  🚀 <b> If you encounter any issue (e.g., frozen video output) or wish to run on higher resolution inputs, please consider <u>duplicating this space</u> or
455
  <u>launching the <a href='https://github.com/pq-yang/MatAnyone?tab=readme-ov-file#-interactive-demo' target='_blank'>demo</a> locally</u> following the GitHub instructions.</b>
456
  """
@@ -978,4 +980,4 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=my_custom_css) as demo:
978
  gr.Markdown(article)
979
 
980
  demo.queue()
981
- demo.launch(debug=True)
 
132
  if ret == True:
133
  current_memory_usage = psutil.virtual_memory().percent
134
  frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
135
+ if current_memory_usage > 98:
136
  break
137
  else:
138
  break
 
141
  image_size = (frames[0].shape[0],frames[0].shape[1])
142
 
143
  # resize if resolution too big
144
+ # if image_size[0]>=1280 and image_size[0]>=1280:
145
+ # scale = 1080 / min(image_size)
146
+ # new_w = int(image_size[1] * scale)
147
+ # new_h = int(image_size[0] * scale)
148
+ # # update frames
149
+ # frames = [cv2.resize(f, (new_w, new_h), interpolation=cv2.INTER_AREA) for f in frames]
150
+ # # update image_size
151
+ # image_size = (frames[0].shape[0],frames[0].shape[1])
152
 
153
  # initialize video_state
154
  video_state = {
 
363
  video_temp_path = output_path.replace(".mp4", "_temp.mp4")
364
 
365
  # resize back to ensure input resolution
366
+ imageio.mimwrite(video_temp_path, frames, fps=fps,
367
+ codec='libx264', ffmpeg_params=["-crf", "18", "-preset", "slow", "-vf", f"scale={w}:{h}"])
368
 
369
  # add audio to video if audio path exists
370
  if audio_path != "" and os.path.exists(audio_path):
 
425
  matanyone_model = MatAnyone.from_pretrained("PeiqingYang/MatAnyone")
426
 
427
  matanyone_model = matanyone_model.to(args.device).eval()
428
+ # Force no internal resizing for high quality
429
+ matanyone_model.cfg.max_internal_size = -1
430
  matanyone_processor = InferenceCore(matanyone_model, cfg=matanyone_model.cfg)
431
 
432
  # download test samples
 
452
  🔥 MatAnyone is a practical human video matting framework supporting target assignment 🎯.<br>
453
  🎪 Try to drop your video/image, assign the target masks with a few clicks, and get the the matting results 🤡!<br>
454
 
455
+ *Note: High resolution inputs (4K) are supported but require significant RAM and VRAM.<br>*
456
  🚀 <b> If you encounter any issue (e.g., frozen video output) or wish to run on higher resolution inputs, please consider <u>duplicating this space</u> or
457
  <u>launching the <a href='https://github.com/pq-yang/MatAnyone?tab=readme-ov-file#-interactive-demo' target='_blank'>demo</a> locally</u> following the GitHub instructions.</b>
458
  """
 
980
  gr.Markdown(article)
981
 
982
  demo.queue()
983
+ demo.launch(debug=True, server_name="0.0.0.0", server_port=args.port)