Skip to content

Commit 130ce14

Browse files
committed
Fixing errors
1 parent d75a326 commit 130ce14

3 files changed

Lines changed: 38 additions & 31 deletions

File tree

musicalgestures/_blurfaces.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def mg_blurfaces(self,
222222

223223
# Save warped video as blur_faces for parent MgVideo
224224
# we have to do this here since we are not using mg_blurfaces (that would normally save the result itself)
225-
self.blur_faces = musicalgestures.MgVideo(target_name, color=self.color, returned_by_process=True)
225+
self.blur_faces_video = musicalgestures.MgVideo(target_name, color=self.color, returned_by_process=True)
226226

227227
def save_txt(of, data, data_format, target_name=target_name, overwrite=overwrite):
228228
"""
@@ -302,7 +302,7 @@ def save_single_file(of, data, data_format, target_name=target_name, overwrite=o
302302

303303
if save_data:
304304
save_txt(of, data, data_format, target_name=target_name, overwrite=overwrite)
305-
return self.blur_faces
305+
return self.blur_faces_video
306306

307307
if draw_heatmap:
308308
target_name = os.path.splitext(target_name)[0] + '.png'
@@ -338,5 +338,5 @@ def save_single_file(of, data, data_format, target_name=target_name, overwrite=o
338338
return MgImage(target_name)
339339

340340
else:
341-
return self.blur_faces
341+
return self.blur_faces_video
342342

musicalgestures/_motionanalysis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def centroid(image, width, height):
2626
my = np.mean(image, axis=1)
2727

2828
if np.sum(mx) != 0 and np.sum(my) != 0:
29-
comx = x.reshape(1, width)@mx.reshape(width, 1)/np.sum(mx)
30-
comy = y.reshape(1, height)@my.reshape(height, 1)/np.sum(my)
29+
comx = np.dot(x, mx) / np.sum(mx)
30+
comy = np.dot(y, my) / np.sum(my)
3131
else:
3232
comx = 0
3333
comy = 0

musicalgestures/_pose.py

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import cv2
33
import os
4+
import sys
45
import numpy as np
56
import pandas as pd
67
from musicalgestures._utils import MgProgressbar, convert_to_avi, extract_wav, embed_audio_in_video, roundup, frame2ms, generate_outfilename, in_colab, ffmpeg_cmd
@@ -74,15 +75,25 @@ def pose(
7475

7576
# Check if .caffemodel file exists, download if necessary
7677
if not os.path.exists(weightsFile):
77-
print('Could not find weights file. Do you want to download it (~200MB)? (y/n)')
78-
answer = input()
79-
if answer.lower() == 'n':
80-
print('Ok. Exiting...')
81-
return musicalgestures.MgVideo(self.filename, color=self.color, returned_by_process=True)
82-
elif answer.lower() == 'y':
78+
print('Could not find weights file.')
79+
# Notebook/nbclient runs cannot satisfy input(), so auto-download in non-interactive mode.
80+
if not sys.stdin or not sys.stdin.isatty():
81+
print('Non-interactive session detected. Downloading model weights automatically (~200MB).')
8382
download_model(model)
8483
else:
85-
print(f'Unrecognized answer "{answer}". Exiting...')
84+
print('Do you want to download it (~200MB)? (y/n)')
85+
answer = input()
86+
if answer.lower() == 'n':
87+
print('Ok. Exiting...')
88+
return musicalgestures.MgVideo(self.filename, color=self.color, returned_by_process=True)
89+
elif answer.lower() == 'y':
90+
download_model(model)
91+
else:
92+
print(f'Unrecognized answer "{answer}". Exiting...')
93+
return musicalgestures.MgVideo(self.filename, color=self.color, returned_by_process=True)
94+
95+
if not os.path.exists(weightsFile):
96+
print('Model weights are still missing after download attempt. Exiting pose() call.')
8697
return musicalgestures.MgVideo(self.filename, color=self.color, returned_by_process=True)
8798

8899
# Read the network into Memory
@@ -92,6 +103,15 @@ def pose(
92103
if in_colab() and device == 'gpu':
93104
print('Sorry, OpenCV GPU acceleration is not supported in Colab. Switching to CPU.')
94105
device = 'cpu'
106+
elif device == 'gpu':
107+
cuda_devices = 0
108+
try:
109+
cuda_devices = cv2.cuda.getCudaEnabledDeviceCount()
110+
except Exception:
111+
cuda_devices = 0
112+
if cuda_devices <= 0:
113+
print('OpenCV CUDA backend is unavailable. Switching to CPU.')
114+
device = 'cpu'
95115

96116
if device == "cpu":
97117
net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU)
@@ -147,7 +167,7 @@ def pose(
147167
break
148168

149169
# Transform the bytes read into a numpy array
150-
frame = np.frombuffer(out, dtype=np.uint8).reshape([self.height, self.width, 3]) # height, width, channels
170+
frame = np.frombuffer(out, dtype=np.uint8).reshape([self.height, self.width, 3]).copy() # height, width, channels
151171

152172
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight), (0, 0, 0), swapRB=False, crop=False)
153173
net.setInput(inpBlob)
@@ -393,43 +413,30 @@ def download_model(modeltype):
393413
if the_system == 'Windows':
394414
command += f' {wget_win} {target_folder_mpi}'
395415
else:
396-
command = 'sudo -S bash ' + command
416+
command = 'bash ' + command
397417
command += f' {target_folder_mpi}'
398418
pb_prefix = 'Downloading MPI model:'
399419
elif modeltype.lower() == 'coco':
400420
command = coco_script
401421
if the_system == 'Windows':
402422
command += f' {wget_win} {target_folder_coco}'
403423
else:
404-
command = 'sudo -S bash ' + command
424+
command = 'bash ' + command
405425
command += f' {target_folder_coco}'
406426
pb_prefix = 'Downloading COCO model:'
407427
elif modeltype.lower() == 'body_25':
408428
command = body_25_script
409429
if the_system == 'Windows':
410430
command += f' {wget_win} {target_folder_body_25}'
411431
else:
412-
command = 'sudo -S bash ' + command
432+
command = 'bash ' + command
413433
command += f' {target_folder_body_25}'
414434
pb_prefix = 'Downloading BODY_25 model:'
415435

416436
pb = MgProgressbar(total=100, prefix=pb_prefix)
417437

418-
if the_system == 'Windows' or in_colab():
419-
process = subprocess.Popen(
420-
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=True)
421-
else:
422-
try:
423-
import getpass
424-
username = getpass.getuser()
425-
print(f'[sudo] password for {username}:')
426-
p = getpass.getpass()
427-
process = subprocess.Popen(
428-
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, universal_newlines=True, shell=True)
429-
process.stdin.write(p+'\n')
430-
process.stdin.flush()
431-
except Exception as error:
432-
print('ERROR', error)
438+
process = subprocess.Popen(
439+
command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True, shell=True)
433440

434441
try:
435442
i = 0

0 commit comments

Comments
 (0)