コード例 #1
0
        private void GetFileInfoCallback(FileInfoResponse fir)
        {
            if (fir.ok)
            {
                Console.WriteLine("Got File info.");

                if (fir.file.initial_comment.comment.Contains(client.MySelf.id) || fir.file.initial_comment.comment.Contains("faceswapperbot"))
                {
                    using (var webClient = new WebClient())
                    {
                        webClient.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + config.botToken);
                        webClient.DownloadFile(fir.file.url_private, "srcFile." + fir.file.filetype);

                        var src = new Image <Bgr, byte>("srcFile." + fir.file.filetype);

                        var graySrc = src.Convert <Gray, byte>();   //Convert to gray scale
                        graySrc._EqualizeHist();                    //Equalize histogram for better detection
                                                                    //graySrc = graySrc.SmoothGaussian(25);     //Smooth the image with a gaussian filter
                        var faces = haarCascade.DetectMultiScale(graySrc);

                        var dest = src.ToBitmap();

                        //create graphics from main image
                        using (Graphics g = Graphics.FromImage(dest))
                        {
                            //Draw each new face on top of the old ones
                            foreach (var face in faces)
                            {
                                int faceIdx = rand.Next(faceImages.Count);
                                var newFace = faceImages[faceIdx];

                                var ratio      = (float)newFace.Width / newFace.Height;
                                var h          = face.Height;
                                var w          = (int)(face.Height * ratio);
                                var difference = (face.Width - w) / 2f;

                                g.DrawImage(faceImages[faceIdx],
                                            face.X + difference - (w * (float)config.upScalingFactor / 2f),    //faces are fitted aacording to their height, so we need to center them according to width
                                            face.Y - (h * (float)config.upScalingFactor / 2f),                 //we also center them according to their up scaling
                                            w * (1f + (float)config.upScalingFactor),                          //we increase the width and height of the new faces according to their up scaling
                                            h * (1f + (float)config.upScalingFactor));
                            }

                            //Save modified image to memory stream and upload it
                            using (MemoryStream ms = new MemoryStream())
                            {
                                dest.Save(ms, ImageFormat.Png);

                                client.UploadFile(FileUploadCallback, ms.ToArray(), fir.file.id + "_swapped." + fir.file.filetype, myChannels.Select(c => c.id).ToArray());
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine(fir.error);
            }
        }
コード例 #2
0
        private async void LoadClick(object sender, RoutedEventArgs e)
        {
            string input = searchBox_Textbox.Text;

            if (ContainsSpecialChars(input))
            {
                MessageBox.Show("Input can only contain numbers & alphanumeric characters");
                return;
            }

            // Server request
            ImageAnimationController controller = ImageBehavior.GetAnimationController(loadingGif_Image);

            controller.Play();

            Connection connection = new Connection();

            status_Textblock.Text = "Connecting...";
            await connection.Connect(Settings.Default.ServerIP, Settings.Default.ServerPort);

            status_Textblock.Text = "Connected";
            FileInfoRequest request = new FileInfoRequest(input);

            status_Textblock.Text = "Checking for file...";
            await connection.SendPacket(request);

            FileInfoResponse response = await connection.ReceivePacket() as FileInfoResponse;

            connection.Close();
            await Task.Delay(1000);

            status_Textblock.Text = "Got response...";
            controller.Pause();

            if (response != null && response.Exists)
            {
                DownloadWindow window = new DownloadWindow(response.File);
                window.Owner = this;
                window.Show();
                window.Closing += DownloadWindowClosing;

                windows.Add(window);
            }
            else
            {
                status_Textblock.Text = "Invalid response!";
            }
        }
コード例 #3
0
ファイル: Connection.cs プロジェクト: Di0n/FileTransfer
        private static IPacket HandlePacket(dynamic jsonData)
        {
            string packetType = jsonData.packetType;

            switch (packetType)
            {
            case nameof(FileInfoResponse):
                return(FileInfoResponse.ToClass(jsonData));

            case nameof(DownloadID):
                return(DownloadID.ToClass(jsonData));

            default:
                return(null);
            }
        }
コード例 #4
0
ファイル: Server.cs プロジェクト: Di0n/FileTransfer
        /// <summary>
        /// Handelt een bestand info aanvraag af.
        /// </summary>
        /// <param name="request"></param>
        private void HandleFileInfoRequest(FileInfoRequest request, StateObject state)
        {
            string path = Settings.Default.FileFolder + request.ID;
            string fileWithExtension = NetworkUtils.GetAssociatedFile(Settings.Default.FileFolder, request.ID);
            bool   exists            = File.Exists(fileWithExtension);


            FileInfoResponse response = new FileInfoResponse();

            if (exists)
            {
                string      text = File.ReadAllText(path + ".json");
                dynamic     json = JsonConvert.DeserializeObject(text);
                NetworkFile file = Util.NetworkUtils.FromJson(json);
                FileInfo    fi   = new FileInfo(fileWithExtension);

                file.FileSize     = fi.Length;
                file.CreationDate = fi.CreationTimeUtc;
                response          = new FileInfoResponse(file);
            }
            //state.Client.Socket.BeginReceive(state.Buffer, 0, StateObject.BufferSize, 0, ReceiveMessageCallback, state);
            SendPacket(state.Client, response, SendCallback, FollowUpTask.RECEIVE_MSG);
        }
コード例 #5
0
 public static void ClassInitialize(TestContext context)
 {
     OkBaseResponse     = JsonLoader.LoadJson <BaseResponse>(@"Data/base.json");
     OkFileInfoResponse = JsonLoader.LoadJson <FileInfoResponse>(@"Files/Data/file_info.json");
     OkFileListResponse = JsonLoader.LoadJson <FileListResponse>(@"Files/Data/file_list.json");
 }