static void DogsVsCats() { NeuralNetwork neuralNetwork = new NeuralNetwork(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); neuralNetwork.LoadH5($@"{AssetsPath}DogsVsCats.h5"); stopwatch.Stop(); long loadTime = stopwatch.ElapsedMilliseconds; long predictTotalMilliseconds = 0; neuralNetwork.Summary(); Predict($@"{AssetsPath}\Dog.jpg", "狗"); Predict($@"{AssetsPath}\Cat.jpg", "猫"); Console.WriteLine($"加载耗时:{loadTime} 预测耗时:{predictTotalMilliseconds} 总耗时:{loadTime + predictTotalMilliseconds}"); void Predict(string path, string actual) { float[,,] inputs = NeuralNetworkInput.FromRGBImage(new Bitmap(Image.FromFile(path), 150, 150)); stopwatch.Restart(); Console.Write($"实际:{actual} 预测:{Classification (neuralNetwork.Predict (inputs))} "); stopwatch.Stop(); predictTotalMilliseconds += stopwatch.ElapsedMilliseconds; Console.WriteLine($"耗时:{stopwatch.ElapsedMilliseconds}"); } string Classification(float value) { if (value >= 0.5F) { return("狗"); } return("猫"); } }
static void Yolo() { NeuralNetwork neuralNetwork = new NeuralNetwork(); neuralNetwork.LoadH5(@"D:\keras-yolo3-master\model_data\yolo.h5"); neuralNetwork.Summary(); string[] labels = { "person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" }; Bitmap image = new Bitmap($@"{AssetsPath}\Test.png"); float[,,] inputs = NeuralNetworkInput.FromRGBImage(new Bitmap(image, 416, 416)); List <NeuralNetworkYoloResult> results = neuralNetwork.Yolo(inputs, image.Width, image.Height, 0.6F, 0.5F); Graphics graphics = Graphics.FromImage(image); NeuralNetworkOutput.DrawYoloResult(graphics, results, labels); image.Save("Temp.jpg"); Process.Start("Temp.jpg"); }
void SelectMode() { PictureBox.Click += (sender, e) => { if (openFileDialog1.ShowDialog() == DialogResult.OK) { PictureBox.Image = Image.FromFile(openFileDialog1.FileName); float value = NeuralNetwork.Predict(NeuralNetworkInput.FromRGBImage(new Bitmap(PictureBox.Image, 150, 150))); Label.Text = $"{value} = {(value >= 0.5F ? "狗" : "猫")}"; } }; }
private void pictureBox1_Click(object sender, EventArgs e) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { Image image = Image.FromFile(openFileDialog1.FileName); pictureBox1.Image = image; float[,,] inputs = NeuralNetworkInput.FromRGBImage(new Bitmap(pictureBox1.Image, 416, 416)); List <NeuralNetworkYoloResult> results = NeuralNetwork.Yolo(inputs, pictureBox1.Image.Width, pictureBox1.Image.Height, 0.6F, 0.5F); Graphics graphics = Graphics.FromImage(image); NeuralNetworkOutput.DrawYoloResult(graphics, results, Labels); graphics.Dispose(); pictureBox1.Image = image; } }
void CaptureMode() { PictureBox.BackColor = Color.Lime; TopMost = true; SetWindowLong(Handle, GWL_EXSTYLE, WS_EX_LAYERED); SetLayeredWindowAttributes(Handle, 65280, 255, LWA_COLORKEY); Bitmap bitmap = null; float value = 0; Action capture = () => { Point point = PointToScreen(PictureBox.Location); Graphics.FromImage(bitmap).CopyFromScreen(point.X, point.Y, 0, 0, PictureBox.Size); }; Action show = () => { Label.Text = $"{value} = {(value >= 0.5F ? "狗" : "猫")}"; }; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); long time = 0, timeDelta, refreshTime = 0; Thread = new Thread(() => { while (true) { try { timeDelta = stopwatch.ElapsedMilliseconds - time; if (WindowState == FormWindowState.Minimized) { return; } if (time >= refreshTime) { refreshTime = time + 1000; bitmap = new Bitmap(PictureBox.Width, PictureBox.Height); Invoke(capture); value = NeuralNetwork.Predict(NeuralNetworkInput.FromRGBImage(new Bitmap(bitmap, 150, 150))); Invoke(show); } time += timeDelta; Thread.Sleep(1); } catch { } } }) { IsBackground = true }; Thread.Start(); }