goryhon commited on
Commit
c4aee39
·
verified ·
1 Parent(s): 5043bf0

Update web-demos/hugging_face/app.py

Browse files
Files changed (1) hide show
  1. web-demos/hugging_face/app.py +46 -47
web-demos/hugging_face/app.py CHANGED
@@ -61,52 +61,48 @@ def get_prompt(click_state, click_input):
61
  return prompt
62
 
63
  # extract frames from upload video
 
 
 
64
  def get_frames_from_video(video_input, video_state):
65
- """
66
- Args:
67
- video_path:str
68
- timestamp:float64
69
- Return
70
- [[0:nearest_frame], [nearest_frame:], nearest_frame]
71
- """
72
  video_path = video_input
73
  frames = []
74
  user_name = time.time()
75
  status_ok = True
76
  operation_log = [("[Must Do]", "Click image"), (": Video uploaded! Try to click the image shown in step2 to add masks.\n", None)]
77
  try:
78
- cap = cv2.VideoCapture(video_path)
79
- fps = cap.get(cv2.CAP_PROP_FPS)
80
- length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
81
-
82
- if length >= 500:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  operation_log = [("You uploaded a video with more than 500 frames. Stop the video extraction. Kindly lower the video frame rate to a value below 500. We highly recommend deploying the demo locally for long video processing.", "Error")]
84
- ret, frame = cap.read()
85
- if ret == True:
86
- original_h, original_w = frame.shape[:2]
87
- frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
88
  status_ok = False
89
- else:
90
- while cap.isOpened():
91
- ret, frame = cap.read()
92
- if ret == True:
93
- # resize input image
94
- original_h, original_w = frame.shape[:2]
95
- frames.append(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
96
- else:
97
- break
98
- t = len(frames)
99
- if t > 0:
100
- print(f'Inp video shape: t_{t}, s_{original_h}x{original_w}')
101
- else:
102
- print(f'Inp video shape: t_{t}, no input video!!!')
103
- except (OSError, TypeError, ValueError, KeyError, SyntaxError) as e:
104
  status_ok = False
105
  print("read_frame_source:{} error. {}\n".format(video_path, str(e)))
106
-
107
- # initialize video_state
108
- if frames[0].shape[0] > 720 or frames[0].shape[1] > 720:
109
- operation_log = [(f"Video uploaded! Try to click the image shown in step2 to add masks. (You uploaded a video with a size of {original_w}x{original_h}, and the length of its longest edge exceeds 720 pixels. We may resize the input video during processing.)", "Normal")]
110
 
111
  video_state = {
112
  "user_name": user_name,
@@ -117,18 +113,22 @@ def get_frames_from_video(video_input, video_state):
117
  "logits": [None]*len(frames),
118
  "select_frame_number": 0,
119
  "fps": fps
120
- }
121
- video_info = "Video Name: {},\nFPS: {},\nTotal Frames: {},\nImage Size:{}".format(video_state["video_name"], round(video_state["fps"], 0), length, (original_w, original_h))
122
- model.samcontroler.sam_controler.reset_image()
 
 
 
123
  model.samcontroler.sam_controler.set_image(video_state["origin_images"][0])
 
124
  return video_state, video_info, video_state["origin_images"][0], gr.update(visible=status_ok, maximum=len(frames), value=1), gr.update(visible=status_ok, maximum=len(frames), value=len(frames)), \
125
- gr.update(visible=status_ok), gr.update(visible=status_ok), \
126
- gr.update(visible=status_ok), gr.update(visible=status_ok),\
127
- gr.update(visible=status_ok), gr.update(visible=status_ok), \
128
- gr.update(visible=status_ok), gr.update(visible=status_ok), \
129
- gr.update(visible=status_ok), gr.update(visible=status_ok), \
130
- gr.update(visible=status_ok), gr.update(visible=status_ok, choices=[], value=[]), \
131
- gr.update(visible=True, value=operation_log), gr.update(visible=status_ok, value=operation_log)
132
 
133
  # get the select frame from gradio slider
134
  def select_template(image_selection_slider, video_state, interactive_state, mask_dropdown):
@@ -357,8 +357,7 @@ def inpaint_video(video_state, *_args):
357
  video_output = generate_video_from_frames(
358
  inpainted_all,
359
  output_path=output_path,
360
- fps=fps,
361
- bitrate="30M"
362
  )
363
 
364
  return video_output, operation_log, operation_log
 
61
  return prompt
62
 
63
  # extract frames from upload video
64
+ import tempfile
65
+ import ffmpeg
66
+ from PIL import Image
67
  def get_frames_from_video(video_input, video_state):
 
 
 
 
 
 
 
68
  video_path = video_input
69
  frames = []
70
  user_name = time.time()
71
  status_ok = True
72
  operation_log = [("[Must Do]", "Click image"), (": Video uploaded! Try to click the image shown in step2 to add masks.\n", None)]
73
  try:
74
+ # Получаем FPS из видео
75
+ probe = ffmpeg.probe(video_path)
76
+ video_streams = [stream for stream in probe['streams'] if stream['codec_type'] == 'video']
77
+ fps_str = video_streams[0]['r_frame_rate'] # Например: "25/1"
78
+ fps = eval(fps_str)
79
+
80
+ # Извлекаем кадры с максимальным качеством во временную папку
81
+ with tempfile.TemporaryDirectory() as tmpdir:
82
+ frame_pattern = os.path.join(tmpdir, 'frame_%05d.png')
83
+ (
84
+ ffmpeg
85
+ .input(video_path)
86
+ .output(frame_pattern, start_number=0, vsync=0, qscale=0)
87
+ .run(quiet=True)
88
+ )
89
+ extracted = sorted(os.listdir(tmpdir))
90
+ for file in extracted:
91
+ img = Image.open(os.path.join(tmpdir, file)).convert("RGB")
92
+ frames.append(np.array(img))
93
+
94
+ original_h, original_w = frames[0].shape[:2]
95
+
96
+ if len(frames) >= 500:
97
  operation_log = [("You uploaded a video with more than 500 frames. Stop the video extraction. Kindly lower the video frame rate to a value below 500. We highly recommend deploying the demo locally for long video processing.", "Error")]
 
 
 
 
98
  status_ok = False
99
+
100
+ except Exception as e:
 
 
 
 
 
 
 
 
 
 
 
 
 
101
  status_ok = False
102
  print("read_frame_source:{} error. {}\n".format(video_path, str(e)))
103
+
104
+ if frames and (frames[0].shape[0] > 720 or frames[0].shape[1] > 720):
105
+ operation_log = [(f"Video uploaded! Try to click the image shown in step2 to add masks. (You uploaded a video with a size of {original_w}x{original_h}, and the length of its longest edge exceeds 720 pixels. We may resize the input video during processing.)", "Normal")]
 
106
 
107
  video_state = {
108
  "user_name": user_name,
 
113
  "logits": [None]*len(frames),
114
  "select_frame_number": 0,
115
  "fps": fps
116
+ }
117
+
118
+ video_info = "Video Name: {},\nFPS: {},\nTotal Frames: {},\nImage Size:{}".format(
119
+ video_state["video_name"], round(video_state["fps"], 0), len(frames), (original_w, original_h)
120
+ )
121
+ model.samcontroler.sam_controler.reset_image()
122
  model.samcontroler.sam_controler.set_image(video_state["origin_images"][0])
123
+
124
  return video_state, video_info, video_state["origin_images"][0], gr.update(visible=status_ok, maximum=len(frames), value=1), gr.update(visible=status_ok, maximum=len(frames), value=len(frames)), \
125
+ gr.update(visible=status_ok), gr.update(visible=status_ok), \
126
+ gr.update(visible=status_ok), gr.update(visible=status_ok), \
127
+ gr.update(visible=status_ok), gr.update(visible=status_ok), \
128
+ gr.update(visible=status_ok), gr.update(visible=status_ok), \
129
+ gr.update(visible=status_ok), gr.update(visible=status_ok), \
130
+ gr.update(visible=status_ok, choices=[], value=[]), \
131
+ gr.update(visible=True, value=operation_log), gr.update(visible=status_ok, value=operation_log)
132
 
133
  # get the select frame from gradio slider
134
  def select_template(image_selection_slider, video_state, interactive_state, mask_dropdown):
 
357
  video_output = generate_video_from_frames(
358
  inpainted_all,
359
  output_path=output_path,
360
+ fps=fps
 
361
  )
362
 
363
  return video_output, operation_log, operation_log