11
22import cv2
33import os
4+ import sys
45import numpy as np
56import pandas as pd
67from 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