public async Task <IActionResult> EnrollSpeaker([FromForm] string userId,
                                                        [FromForm] string projectId,
                                                        [FromForm] string speakerId,
                                                        [FromForm] IEnumerable <IFormFile> files)
        {
            // check if speaker has already been enrolled
            var query = await(from s in _context.SpeakerProfiles
                              where s.ProjectId.Equals(projectId)
                              select s).FirstOrDefaultAsync();

            string modelPath = query?.ModelPath ?? Path.GetTempFileName();

            // TODO: fill in speaker enrollment method
            try
            {
                foreach (var f in files)
                {
                    // train model
                    // await AnalysisController.EnrollSpeaker(speakerId, modelPath, f)
                }
            }
            catch (Exception e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError,
                                  new { message = $"Error enrolling speaker {speakerId}!\nError:\n{e}" }));
            }

            // if this speaker hasn't been enrolled, enroll them once training is complete
            if (query == null)
            {
                _context.Add(new SpeakerProfile
                {
                    SpeakerId = speakerId,
                    ProjectId = projectId,
                    UserId    = userId,
                    ModelPath = modelPath
                });
                _context.SaveChanges();
            }

            return(Ok());
        }
Exemplo n.º 2
0
        public IActionResult Delete(string clipID)
        {
            var currentUserId        = _userManager.GetUserId(HttpContext.User);
            var removeClipAssignment = _montageContext.ClipAssignments.FirstOrDefault(x => x.ClipId == clipID && x.UserId == currentUserId);

            if (removeClipAssignment != null)
            {
                _montageContext.ClipAssignments.Remove(removeClipAssignment);
            }

            var removeClip = _montageContext.AdobeClips.FirstOrDefault(x => x.ClipId == clipID);

            if (removeClip != null)
            {
                _montageContext.AdobeClips.Remove(removeClip);
            }

            _montageContext.SaveChanges();


            return(View("~/Views/Home/Account.cshtml", _montageContext.AdobeClips.Where(y => _montageContext.ClipAssignments.Where(x => x.UserId == currentUserId).Any(z => z.ClipId == y.ClipId)).ToList()));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Given a new clip which either hasn't been seen or was processed incorrectly
        /// Direct its file to the AnalysisController for analysis
        /// Save the results in the database
        /// Return the results to the caller
        /// </summary>
        private async Task <IActionResult> ProcessNewClip(IFormFile file, string projectId, string clipId, string userId, string footagePath)
        {
            if (file is null)
            {
                return(StatusCode(StatusCodes.Status400BadRequest,
                                  new
                {
                    message = $"Unable to find processed footage with id {projectId}/{clipId} for user {userId}\n" +
                              "(did you mean to send a file?)"
                }));
            }

            HttpRequest currentRequest = HttpContext.Request;

            // initialize empty response
            AnalysisResult response = new AnalysisResult();

            response.ClipId      = clipId;
            response.FootagePath = footagePath;

            // process file in bg
            await Task.Run(() =>
            {
                var fileStream = file.OpenReadStream();

                // if an audio file was sent, return transcript
                if (file.ContentType.StartsWith("audio/"))
                {
                    try
                    {
                        if (!(file.ContentType.EndsWith("/wav") || file.ContentType.EndsWith("/wave")))
                        {
                            fileStream = ConversionController.ConvertToWav(fileStream, file.ContentType);
                        }
                        //var buf = ((MemoryStream)fileStream).GetBuffer();
                        //var f = System.IO.File.Create(@"C:\data\audio\converted.wav", buf.Length);
                        //f.Write(buf);
                    }
                    finally
                    {
                        // analyze converted audio
                        // AnalysisController.TranscribeAudio(ref response, file);
                        AnalysisController.AnalyzeAudio(ref response, fileStream);
                        fileStream.Close();
                    }
                }
                else if (file.ContentType.StartsWith("video/"))
                {
                    try
                    {
                        var vidPt     = Path.GetTempFileName();
                        var preAudPt  = Path.GetTempFileName(); //@"C:\data\pre_tmp.wav";
                        var postAudPt = Path.GetTempFileName(); //@"C:\data\tmp.wav";
                        ConversionController.WriteStreamToDisk(fileStream, vidPt);
                        fileStream = System.IO.File.OpenRead(vidPt);

                        var splitStreams = ConversionController.SeparateAudioVideo(fileStream, file.ContentType);
                        fileStream       = splitStreams.audioStream;

                        ConversionController.WriteStreamToDisk(fileStream, preAudPt);

                        fileStream = ConversionController.ConvertToWavFiles(preAudPt, "");

                        ConversionController.WriteStreamToDisk(fileStream, postAudPt);

                        fileStream = System.IO.File.OpenRead(postAudPt);
                    }
                    finally
                    {
                        AnalysisController.AnalyzeAudio(ref response, fileStream);
                        fileStream.Close();
                    }
                }
            });

            // load or make corresponding clip
            var clip = FindClip(clipId);

            if (clip == null)
            {
                clip = new AdobeClip
                {
                    ClipId               = clipId,
                    FootagePath          = footagePath,
                    AnalysisResultString = response.Serialize()
                };
                _montageContext.AdobeClips.Add(clip);
                _montageContext.SaveChanges();
            }

            // load or make corresponding project
            var project = FindProject(projectId);

            if (project == null)
            {
                project = new AdobeProject
                {
                    ProjectId = projectId,
                    UserId    = userId
                };
                _montageContext.AdobeProjects.Add(project);
                _montageContext.SaveChanges();
            }

            // load or make corresponding clip assignment
            var assignment = FindAssignment(projectId, clipId);

            if (assignment == null)
            {
                assignment = new ClipAssignment
                {
                    ProjectId = projectId,
                    UserId    = project.UserId,
                    ClipId    = clip.ClipId
                };
                _montageContext.ClipAssignments.Add(assignment);
                _montageContext.SaveChanges();
            }

            await _montageContext.SaveChangesAsync();


            response.ClipId      = clipId;
            response.FootagePath = footagePath;
            return(Ok(response));
        }