Skip to content

Commit a9fbd44

Browse files
authored
Merge pull request #331 from fourMs/copilot/fix-cropping-function-issue
Fix cropping window stalling on repeated use in the same session
2 parents 4401eb7 + eac2a4b commit a9fbd44

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

musicalgestures/_cropvideo.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -197,28 +197,37 @@ def mg_cropvideo_ffmpeg(
197197

198198
if crop_movement.lower() == 'manual':
199199
if not in_colab():
200-
201-
# scale_ratio = get_box_video_ratio(filename)
202-
# width, height = get_widthheight(filename)
203-
# scaled_width, scaled_height = [int(elem * scale_ratio) for elem in [width, height]]
204-
# first_frame_as_image = get_first_frame_as_image(filename, pict_format='.jpg')
205-
206-
# Cropping UI moved to another subprocess to avoid cv2.waitKey crashing Python with segmentation fault on Linux in Terminal
207-
import threading
208-
import queue
209-
210-
que = queue.Queue()
211-
t = threading.Thread(target=lambda q, arg1:q.put(cropping_window(arg1)), args=(que, filename))
212-
213-
t.start()
214-
t.join()
215-
216-
w, h, x, y = que.get()
217-
218-
# x = threading.Thread(target=run_cropping_window, args=(first_frame_as_image, scale_ratio, scaled_width, scaled_height))
219-
# run_cropping_window(first_frame_as_image, scale_ratio, scaled_width, scaled_height)
220-
# x.start()
221-
# x.join()
200+
import sys
201+
import subprocess
202+
import musicalgestures
203+
204+
scale_ratio = get_box_video_ratio(filename)
205+
width, height = get_widthheight(filename)
206+
scaled_width, scaled_height = [int(elem * scale_ratio) for elem in [width, height]]
207+
first_frame_as_image = get_first_frame_as_image(filename, pict_format='.jpg')
208+
209+
module_path = os.path.abspath(os.path.dirname(musicalgestures.__file__))
210+
pyfile = os.path.join(module_path, '_cropping_window.py')
211+
212+
result = subprocess.run(
213+
[sys.executable, pyfile, first_frame_as_image, str(scale_ratio), str(scaled_width), str(scaled_height)],
214+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
215+
)
216+
217+
os.remove(first_frame_as_image)
218+
219+
if result.returncode != 0:
220+
raise RuntimeError(
221+
f"Cropping window subprocess failed (exit code {result.returncode}):\n{result.stderr}"
222+
)
223+
224+
res = result.stdout.strip()
225+
res_array = res.split(' ')
226+
if len(res_array) != 4:
227+
raise RuntimeError(
228+
f"Unexpected output from cropping window: '{res}'"
229+
)
230+
w, h, x, y = [int(elem) for elem in res_array]
222231

223232
else:
224233
x, y, w, h = manual_text_input()
@@ -228,11 +237,6 @@ def mg_cropvideo_ffmpeg(
228237

229238
cropped_video = crop_ffmpeg(filename, w, h, x, y, target_name=target_name, overwrite=overwrite)
230239

231-
# if crop_movement.lower() == 'manual':
232-
# cv2.destroyAllWindows()
233-
# if not in_colab():
234-
# os.remove(first_frame_as_image)
235-
236240
return cropped_video
237241

238242

0 commit comments

Comments
 (0)