Example #1
0
 public void UploadUserPhoto(Stream stream, Rect rect)
 {
     if (this.IsSaving)
     {
         return;
     }
     this.IsSaving = true;
     this.SetInProgress(true, "");
     ImagePreprocessor.PreprocessImage(stream, VKConstants.ResizedImageSize, true, (Action <ImagePreprocessResult>)(pres =>
     {
         Stream stream1  = pres.Stream;
         byte[] numArray = (byte[])new byte[stream1.Length];
         stream1.Read(numArray, 0, (int)stream1.Length);
         stream1.Close();
         UsersService.Instance.SaveProfilePhoto(ImagePreprocessor.GetThumbnailRect((double)pres.Width, (double)pres.Height, rect), numArray, (Action <BackendResult <ProfilePhoto, ResultCode> >)(res =>
         {
             this.IsSaving = false;
             this.SetInProgress(false, "");
             if (res.ResultCode == ResultCode.Succeeded)
             {
                 BaseDataManager.Instance.NeedRefreshBaseData = true;
                 BaseDataManager.Instance.RefreshBaseDataIfNeeded();
             }
             else
             {
                 ExtendedMessageBox.ShowSafe(CommonResources.Error);
             }
         }));
     }));
 }
 public void UpdatePhoto(Stream photoStream, Rect crop)
 {
     this.IsPhotoChanging = true;
     this.SetInProgress(true, "");
     ImagePreprocessor.PreprocessImage(photoStream, VKConstants.ResizedImageSize, true, (Action<ImagePreprocessResult>)(resized =>
     {
         Stream stream = resized.Stream;
         byte[] photoData = ImagePreprocessor.ReadFully(stream);
         stream.Close();
         BackendServices.MessagesService.UpdateChatPhoto(this.ChatId, photoData, ImagePreprocessor.GetThumbnailRect((double)resized.Width, (double)resized.Height, crop), (Action<BackendResult<ChatInfoWithMessageId, ResultCode>>)(result => Execute.ExecuteOnUIThread((Action)(() =>
         {
             if (result.ResultCode == ResultCode.Succeeded)
             {
                 this._chatInformation.chat.photo_200 = result.ResultData.chat.photo_200;
                 this.NotifyPropertyChanged<string>((Expression<Func<string>>)(() => this.Photo));
                 this.NotifyPropertyChanged<Visibility>((Expression<Func<Visibility>>)(() => this.PhotoPlaceholderVisibility));
                 this.NotifyPropertyChanged<bool>((Expression<Func<bool>>)(() => this.IsPhotoMenuEnabled));
             }
             else
                 GenericInfoUC.ShowBasedOnResult(result.ResultCode, "", (VKRequestsDispatcher.Error)null);
             this.SetInProgress(false, "");
             this.IsPhotoChanging = false;
         }))));
     }));
 }
Example #3
0
        public void Dispose()
        {
            PixelDeltaAlgorithm.Dispose();
            ImagePreprocessor.Dispose();

            GC.SuppressFinalize(this);
        }
Example #4
0
        private void mnistDesktopButton_Click(object sender, EventArgs e)
        {
            MNistEnvironment env = new MNistEnvironment(this);

            env.Path        = Application.StartupPath + "\\..\\..\\..\\..\\..\\Mnist\\";
            env.ItemsToLoad = 5;
            _agents.Add(env);
            ImagePreprocessor preprocessor = new ImagePreprocessor(this);

            _agents.Add(preprocessor);
            HTMBuilder htmBuilder = new HTMBuilder(this);

            _agents.Add(htmBuilder);
            preprocessor.InputAgent = env;          // link the preprocessor to the environment
            htmBuilder.InputAgent   = preprocessor; // link the htm builder to the preprocessor

            // Set the layout
            preprocessor.MdiParent = this;
            preprocessor.Location  = new Point(0, 0);
            preprocessor.Show();
            htmBuilder.MdiParent = this;
            htmBuilder.Location  = new Point(0, preprocessor.Height);
            htmBuilder.Show();
            env.MdiParent = this;
            env.Location  = new Point(preprocessor.Width, 0);
            env.Show();

            //
            this.Log("Image desktop created.");
            initializeButton.Enabled   = true;
            imageDesktopButton.Enabled = false;
            mnistDesktopButton.Enabled = false;
            dataDesktopButton.Enabled  = false;
        }
        private void OnClassifyImage(object sender, EventArgs e)
        {
            try
            {
                _loggingService.Log("Classify drawing has started");

                var imagePreprocessor = new ImagePreprocessor();

                double[] pixels = imagePreprocessor.Preprocess(_uploadImageView.Image);

                IPredictionModel predictionModel = Global.PredictionModel;

                double[] prediction = predictionModel.Predict(pixels);

                _uploadImageView.ProcessPrediction(prediction);

                _loggingService.Log("Classify drawing has completed");
            }
            catch (NullReferenceException exception)
            {
                _loggingService.Log(exception);

                _messageService.ShowMessage("No image was uploaded. Please upload an image and try again.", "Upload error", icon: MessageBoxIcon.Information);
            }
            catch (Exception exception)
            {
                _loggingService.Log(exception);

                _messageService.ShowMessage("An error ocurred while classyfing the drawing. Please try again.", "Classification error", icon: MessageBoxIcon.Information);
            }
        }
Example #6
0
        public async Task <Prediction> PredictAsync(Image <Rgba32> image)
        {
            var preprocessor = new ImagePreprocessor(Rgba32.White, Rgba32.Black);

            image = preprocessor.Preprocess(image);

            var inputs = PrepareMLStudioInput(image);

            using (var client = new HttpClient())
            {
                var requestContent = new StringContent(inputs);
                requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _apiKey);

                var response = await client.PostAsync(_apiUrl, requestContent);

                var responseContent = response.Content is HttpContent c ? await c.ReadAsStringAsync() : null;

                var prediction = JsonConvert.DeserializeObject <MLStudioResponseObject>(responseContent);

                var tag = prediction.results.WebServiceOutput0.FirstOrDefault()?["Scored Labels"];

                return(new Prediction
                {
                    Tag = Convert.ToInt32(tag),
                    Probability = 1
                });
            }
        }
Example #7
0
 public void UploadPhoto(Stream photoDataStream, Action <BackendResult <Photo, ResultCode> > callback)
 {
     ImagePreprocessor.PreprocessImage(photoDataStream, VKConstants.ResizedImageSize, true, (Action <ImagePreprocessResult>)(pres =>
     {
         Stream stream   = pres.Stream;
         byte[] numArray = new byte[stream.Length];
         stream.Read(numArray, 0, (int)stream.Length);
         stream.Close();
         this.SetInProgress(true, "");
         PhotosService.Current.UploadPhotoToAlbum(this._albumId, this._isGroup ? this._userOrGroupId : 0, numArray, (Action <BackendResult <Photo, ResultCode> >)(res =>
         {
             this.SetInProgress(false, "");
             if (res.ResultCode == ResultCode.Succeeded)
             {
                 Execute.ExecuteOnUIThread((Action)(() =>
                 {
                     Photo resultData1 = res.ResultData;
                     EventAggregator current = EventAggregator.Current;
                     PhotoUploadedToAlbum photoUploadedToAlbum = new PhotoUploadedToAlbum();
                     photoUploadedToAlbum.photo = resultData1;
                     long pid = resultData1.pid;
                     photoUploadedToAlbum.pid = pid;
                     string str = resultData1.aid.ToString();
                     photoUploadedToAlbum.aid = str;
                     current.Publish((object)photoUploadedToAlbum);
                     if (string.IsNullOrEmpty(this.ThumbSrc))
                     {
                         EventAggregator.Current.Publish((object)new PhotoSetAsAlbumCover()
                         {
                             aid = this._albumId,
                             pid = resultData1.pid,
                             Photo = resultData1
                         });
                     }
                     else
                     {
                         if (this._albumPhotos.Count <= 1)
                         {
                             return;
                         }
                         Photo photo = this._albumPhotos[1].Photo;
                         if (!(this.ThumbSrc == photo.photo_75) && !(this.ThumbSrc == photo.photo_130) && (!(this.ThumbSrc == photo.photo_604) && !(this.ThumbSrc == photo.photo_807)) && (!(this.ThumbSrc == photo.photo_1280) && !(this.ThumbSrc == photo.photo_2560) && (!(this.ThumbSrc == photo.src) && !(this.ThumbSrc == photo.src_small))) && (!(this.ThumbSrc == photo.src_big) && !(this.ThumbSrc == photo.src_xbig) && !(this.ThumbSrc == photo.src_xxbig)))
                         {
                             return;
                         }
                         Photo resultData2 = res.ResultData;
                         EventAggregator.Current.Publish((object)new PhotoSetAsAlbumCover()
                         {
                             aid = this._albumId,
                             Photo = resultData2,
                             pid = resultData2.pid
                         });
                     }
                 }));
             }
             callback(res);
         }));
     }));
 }
Example #8
0
        public Stream GetImageStream(string albumId, int seqNo, bool preview = false)
        {
            ImageEffectsInfo imageEffectsInfo1 = this._sessionEffectsInfo.GetImageEffectsInfo(albumId, seqNo);

            if (!preview && imageEffectsInfo1.AppliedAny)
            {
                string pathForEffects = this.GetPathForEffects(albumId, seqNo);
                using (IsolatedStorageFile storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream storageFileStream = storeForApplication.OpenFile(pathForEffects, (FileMode)3, (FileAccess)1))
                    {
                        MemoryStream exifStream = new MemoryStream();
                        if (imageEffectsInfo1.Exif != null)
                        {
                            exifStream = new MemoryStream(this.ResetOrientation(imageEffectsInfo1.ParsedExif.OrientationOffset, imageEffectsInfo1.Exif, imageEffectsInfo1.ParsedExif.LittleEndian));
                        }
                        MemoryStream memoryStream = ImagePreprocessor.MergeExif((Stream)storageFileStream, exifStream);
                        exifStream.Close();
                        return((Stream)memoryStream);
                    }
                }
            }
            else
            {
                Stopwatch stopwatch    = Stopwatch.StartNew();
                Picture   galleryImage = this.GetGalleryImage(albumId, seqNo);
                if (preview)
                {
                    Stream thumbnail = galleryImage.GetThumbnail();
                    galleryImage.Dispose();
                    stopwatch.Stop();
                    return(thumbnail);
                }
                ImageEffectsInfo imageEffectsInfo2 = this.GetImageEffectsInfo(albumId, seqNo);
                Stream           image             = galleryImage.GetImage();
                if (imageEffectsInfo2.Exif == null && !this.SuppressParseEXIF)
                {
                    Stopwatch.StartNew();
                    MemoryStream exifStream;
                    ImagePreprocessor.PatchAwayExif(image, out exifStream);
                    image.Position         = 0L;
                    imageEffectsInfo2.Exif = new byte[exifStream.Length];
                    exifStream.Read(imageEffectsInfo2.Exif, 0, (int)exifStream.Length);
                    JpegInfo info = new ExifReader(image).info;
                    imageEffectsInfo2.ParsedExif = info;
                    image.Position = 0L;
                }
                stopwatch.Stop();
                galleryImage.Dispose();
                return(image);
            }
        }
Example #9
0
        /// <summary>
        /// When an image changes, this method updates all images
        /// </summary>
        private void RefreshDisplayedImages()
        {
            currImageI    = new Image <Bgr, byte>(curr.Title);
            nextImageI    = new Image <Bgr, byte>(next.Title);
            _preprocessor = new ImagePreprocessor(curr, next);

            int currFacesCount = _preprocessor.FacesListCurr.Count;
            int nextFacesCount = _preprocessor.FacesListNext.Count;

            if (currFacesCount > 0 && nextFacesCount > 0)
            {
                facesDetected = true;
            }
            if (!_preprocessor.MorphEnabled)
            {
                InitializeComponent();
                //morphBtn.IsEnabled = false;
                mySlider.IsEnabled = false;
                displayMorphOk     = false;
            }
            else
            {
                InitializeComponent();
                //morphBtn.IsEnabled = true;
                mySlider.IsEnabled = true;
                displayMorphOk     = true;
            }

            // Display errors and number of found faces:
            facesCountCurr.Content   = $"{_preprocessor.FacesListCurr.Count}";
            facesCountNext.Content   = $"{_preprocessor.FacesListNext.Count}";
            errorMessageCurr.Content = "";
            errorMessageNext.Content = "";
            if (_preprocessor.FacesListCurr.Count == 0)
            {
                errorMessageCurr.Content = "No faces were found";
            }
            errorMessageCurr.Foreground = System.Windows.Media.Brushes.Red;

            if (_preprocessor.FacesListNext.Count == 0)
            {
                errorMessageNext.Content = "No faces were found";
            }
            errorMessageNext.Foreground = System.Windows.Media.Brushes.Red;
            // ---------------------------------------------

            this.currImageI = _preprocessor.CurrImageI;
            this.nextImageI = _preprocessor.NextImageI;
        }
Example #10
0
        private void OnClassifyDrawing(object sender, EventArgs e)
        {
            try
            {
                _loggingService.Log("Classify drawing has started");

                var imagePreprocessor = new ImagePreprocessor();

                IPredictionModel predictionModel = Global.PredictionModel;

                Image img = _slidingWindowView.Drawing;

                foreach (Size windowSize in WindowSizes)
                {
                    foreach (BoundingBox boundingBox in ImageUtilities.SlidingWindow(img, windowSize, 112))
                    {
                        try
                        {
                            double[] pixels = imagePreprocessor.Preprocess(boundingBox.Image);

                            double[] prediction = predictionModel.Predict(pixels);

                            // If classification is over 99% draw a bounding box at this location
                            int    predicted         = prediction.ArgMax();
                            double predictedAccuracy = prediction[prediction.ArgMax()];

                            if (predictedAccuracy >= 0.95)
                            {
                                _slidingWindowView.DrawBoundingBox(boundingBox, predicted, predictedAccuracy);
                            }
                        }
                        catch (Exception exception)
                        {
                            _loggingService.Log(exception);
                        }
                    }
                }

                _loggingService.Log("Classify drawing has completed");
            }
            catch (Exception exception)
            {
                _loggingService.Log(exception);

                _messageService.ShowMessage("An error ocurred while classyfing the drawing. Please try again.", "Classification error", icon: MessageBoxIcon.Information);
            }
        }
        private void InitCamera()
        {
            //  ADD CAMERA SUPPORT
            _imagePreprocessor = new ImagePreprocessor();
            _cameraHandler     = CameraHandler.Instance;
            Handler threadLooper = new Handler(MainLooper);


            _cameraHandler.InitializeCamera(this, threadLooper, new ImageAvailableListener()
            {
                OnImageAvailableAction = (imageReader) =>
                {
                    Bitmap bitmap = _imagePreprocessor.PreprocessImage(imageReader.AcquireNextImage());
                    OnPhotoReady(bitmap);
                }
            });
        }
Example #12
0
        internal static async Task Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Source directory needed.");
                return;
            }
            string res = args[0];
            string sourceDirectory = $"Resources/{res}";
            string destTempDirectory = $"temp/{res}_preprocessed";

            bool skipPreprocessing = false;
            if (args.Length > 1 && bool.Parse(args[1]))
                skipPreprocessing = true;
            if (!skipPreprocessing)
            {
                IImageProvider rawImageProvider = new DirectoryImageProvider();
                IImagePreprocessor preprocessor = new ImagePreprocessor();
                preprocessor.ProcessImages(rawImageProvider.LoadImages(sourceDirectory), destTempDirectory);
            }
            int NUM_LAYERS = 8;
            Network network = new Network(NUM_LAYERS);
            var pictureDirectories = Directory.EnumerateDirectories(destTempDirectory);
            Directory.CreateDirectory("results");
            Directory.CreateDirectory(destTempDirectory);
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();

            int NUM_THREADS = 4;
            for (int i = 0; i < NUM_LAYERS; i += NUM_THREADS)
            {
                Task[] splitTask = new Task[NUM_THREADS];
                for(int t = 0; t < NUM_THREADS; t++)
                {
                    int layerIndex = t + i;
                    splitTask[t] = Task.Run(() => DoAnalysis(layerIndex, pictureDirectories, ref network));
                }
                await Task.WhenAll(splitTask);
            }

        }
Example #13
0
 internal void SetUserPhoto(Stream stream, Rect rect)
 {
     this._photoCropRect = rect;
     ImagePreprocessor.PreprocessImage(stream, 1500000, false, (Action <ImagePreprocessResult>)(imPR => Execute.ExecuteOnUIThread((Action)(() =>
     {
         BitmapImage bitmapImage = new BitmapImage();
         Stream stream1 = imPR.Stream;
         bitmapImage.SetSource(stream1);
         WriteableBitmap bmp = new WriteableBitmap((BitmapSource)bitmapImage);
         WriteableBitmap bitmap = bmp.Crop(new Rect()
         {
             Width = (double)bmp.PixelWidth * rect.Width,
             Height = (double)bmp.PixelHeight * rect.Height,
             Y = rect.Top * (double)bmp.PixelHeight,
             X = rect.Left * (double)bmp.PixelWidth
         });
         try
         {
             if (!string.IsNullOrWhiteSpace(this._fullAvatarUri))
             {
                 File.Delete(this._fullAvatarUri);
             }
             using (IsolatedStorageFile storeForApplication = IsolatedStorageFile.GetUserStoreForApplication())
             {
                 MemoryStream memoryStream = new MemoryStream();
                 bitmap.SaveJpeg((Stream)memoryStream, bitmap.PixelWidth, bitmap.PixelHeight, 0, 90);
                 memoryStream.Position = 0L;
                 this._fullAvatarUri = Guid.NewGuid().ToString();
                 ImageCache.Current.TrySetImageForUri("cropped" + this._fullAvatarUri, (Stream)memoryStream);
                 stream.Position = 0L;
                 using (IsolatedStorageFileStream storageFileStream = storeForApplication.OpenFile(this._fullAvatarUri, FileMode.Create, FileAccess.Write))
                     StreamUtils.CopyStream(stream, (Stream)storageFileStream, (Action <double>)null, (Cancellation)null, 0L);
                 this.HavePhoto = true;
             }
         }
         catch
         {
         }
     }))));
 }
Example #14
0
        public override void Upload(Action completionCallback, Action <double> progressCallback = null)
        {
            Action action;

            Execute.ExecuteOnUIThread((Action)(() =>
            {
                if (this.UploadState == OutboundAttachmentUploadState.Uploading)
                {
                    return;
                }
                if (this.UploadState == OutboundAttachmentUploadState.Completed)
                {
                    completionCallback();
                }
                else
                {
                    this.UploadState = OutboundAttachmentUploadState.Uploading;
                    Stream stream = ImageCache.Current.GetCachedImageStream(this.LocalUrlBig);
                    ImagePreprocessor.PreprocessImage(stream, VKConstants.ResizedImageSize, true, (Action <ImagePreprocessResult>)(pres =>
                    {
                        Stream stream2 = pres.Stream;
                        stream.Close();
                        byte[] bytes = ImagePreprocessor.ReadFully(stream2);
                        stream2.Close();
                        this.UploadImpl(bytes, (action = (Action)(() =>
                        {
                            if (this._uploadState == OutboundAttachmentUploadState.Completed && this._photo != null)
                            {
                                this.CleanupCache();
                                this._localUri = this._photo.src_big;
                            }
                            completionCallback();
                        })), progressCallback);
                    }));
                }
            }));
        }
Example #15
0
        /// <summary>
        /// Generates full video
        /// </summary>
        /// <param name="images"></param>
        public VideoGenerator(ObservableCollection <ImageDetails> images, int fpsUser, float alphaUser, string path)
        {
            this.images          = images;
            this.destinationPath = path;
            ImagePreprocessor ip1;

            System.Drawing.Size tmpSize = new System.Drawing.Size(0, 0);

            for (int i = 0; i < images.Count - 1; i++)
            {
                ip1 = new ImagePreprocessor(images.ElementAt(i), images.ElementAt(i + 1));
                if (ip1.FacesListCurr.Count == 0)
                {
                    images.RemoveAt(i);
                }
                if (ip1.FacesListNext.Count == 0)
                {
                    images.RemoveAt(i + 1);
                }
                else
                {
                    if (ip1.curr.ResizedImage.Width > tmpSize.Width)
                    {
                        tmpSize.Width = ip1.curr.ResizedImage.Width;
                    }
                    if (ip1.curr.ResizedImage.Height > tmpSize.Height)
                    {
                        tmpSize.Height = ip1.curr.ResizedImage.Width;
                    }
                }
            }
            if (tmpSize.Width > 1280)
            {
                tmpSize.Width  = 1280;
                tmpSize.Height = 720;
            }

            MorphImage        m;
            ImagePreprocessor ip;

            try
            {
                //videoWriter = new VideoWriter(fileName: destinationPath, fps: 30, size: tmpSize, isColor: true);
                videoWriter = new VideoWriter(fileName: destinationPath, compressionCode: VideoWriter.Fourcc('M', 'P', '4', 'V'), fps: fpsUser, size: tmpSize, isColor: true);


                List <Mat> frames = new List <Mat>();

                for (int i = 0; i < images.Count - 1; i++)
                {
                    ip = new ImagePreprocessor(images.ElementAt(i), images.ElementAt(i + 1), tmpSize);

                    float alpha = 0.0f;
                    while (alpha < 1.0f)
                    {
                        m = new MorphImage(ip.curr, ip.next, ip.ffpCurr, ip.ffpNext, alpha);
                        Image <Bgr, byte> morphedImage = m.GetMorphedImageI();

                        frames.Add(morphedImage.Mat);
                        //videoWriter.Write(morphedImage.Mat);
                        alpha += alphaUser;
                        //morphedImage.Dispose();
                    }
                }
                int img1 = 0;
                foreach (Mat mat in frames)
                {
                    img1 += 1;
                    videoWriter.Write(mat);
                }
                if (videoWriter.IsOpened)
                {
                    videoWriter.Dispose();
                }
                MessageBox.Show($"Completed");
            }
            catch (Exception)
            {
                MessageBox.Show("The program has run out of memory. Try to use fewer images, a larger alpha value (0.05 - 0.1) or a lower FPS count (25)");
            }
        }
 public BlobHandler()
 {
     imgProcessor = new ImagePreprocessor();
 }
Example #17
0
 private byte[] ResetOrientation(long p, byte[] exifData, bool littleEndian)
 {
     return(ImagePreprocessor.ResetOrientation(p, exifData, littleEndian));
 }