| 161 | return tracks |
| 162 | |
| 163 | def crop_video(args, track, cropFile): |
| 164 | # CPU: crop the face clips |
| 165 | flist = glob.glob(os.path.join(args.pyframesPath, '*.jpg')) # Read the frames |
| 166 | flist.sort() |
| 167 | vOut = cv2.VideoWriter(cropFile + 't.avi', cv2.VideoWriter_fourcc(*'XVID'), 25, (224,224))# Write video |
| 168 | dets = {'x':[], 'y':[], 's':[]} |
| 169 | for det in track['bbox']: # Read the tracks |
| 170 | dets['s'].append(max((det[3]-det[1]), (det[2]-det[0]))/2) |
| 171 | dets['y'].append((det[1]+det[3])/2) # crop center x |
| 172 | dets['x'].append((det[0]+det[2])/2) # crop center y |
| 173 | dets['s'] = signal.medfilt(dets['s'], kernel_size=13) # Smooth detections |
| 174 | dets['x'] = signal.medfilt(dets['x'], kernel_size=13) |
| 175 | dets['y'] = signal.medfilt(dets['y'], kernel_size=13) |
| 176 | for fidx, frame in enumerate(track['frame']): |
| 177 | cs = args.cropScale |
| 178 | bs = dets['s'][fidx] # Detection box size |
| 179 | bsi = int(bs * (1 + 2 * cs)) # Pad videos by this amount |
| 180 | image = cv2.imread(flist[frame]) |
| 181 | frame = numpy.pad(image, ((bsi,bsi), (bsi,bsi), (0, 0)), 'constant', constant_values=(110, 110)) |
| 182 | my = dets['y'][fidx] + bsi # BBox center Y |
| 183 | mx = dets['x'][fidx] + bsi # BBox center X |
| 184 | face = frame[int(my-bs):int(my+bs*(1+2*cs)),int(mx-bs*(1+cs)):int(mx+bs*(1+cs))] |
| 185 | vOut.write(cv2.resize(face, (224, 224))) |
| 186 | audioTmp = cropFile + '.wav' |
| 187 | audioStart = (track['frame'][0]) / 25 |
| 188 | audioEnd = (track['frame'][-1]+1) / 25 |
| 189 | vOut.release() |
| 190 | command = ("ffmpeg -y -i %s -async 1 -ac 1 -vn -acodec pcm_s16le -ar 16000 -threads %d -ss %.3f -to %.3f %s -loglevel panic" % \ |
| 191 | (args.audioFilePath, args.nDataLoaderThread, audioStart, audioEnd, audioTmp)) |
| 192 | output = subprocess.call(command, shell=True, stdout=None) # Crop audio file |
| 193 | _, audio = wavfile.read(audioTmp) |
| 194 | command = ("ffmpeg -y -i %st.avi -i %s -threads %d -c:v copy -c:a copy %s.avi -loglevel panic" % \ |
| 195 | (cropFile, audioTmp, args.nDataLoaderThread, cropFile)) # Combine audio and video file |
| 196 | output = subprocess.call(command, shell=True, stdout=None) |
| 197 | os.remove(cropFile + 't.avi') |
| 198 | return {'track':track, 'proc_track':dets} |
| 199 | |
| 200 | def extract_MFCC(file, outPath): |
| 201 | # CPU: extract mfcc |