Untitled

mail@pastecode.io avatar
unknown
csharp
a month ago
2.4 kB
1
Indexable
Never
using System;
using System.Diagnostics;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;

class Program
{
    static void Main(string[] args)
    {
        string pythonScript = "face_detection.py";
        string imagePath = "path_to_your_image.jpg"; // Replace with the actual path to your image
        string outputImagePath = "output_image.jpg";

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "python";
        startInfo.Arguments = $"{pythonScript} \"{imagePath}\"";
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardOutput = true;

        using (Process process = Process.Start(startInfo))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd().Trim();
                string[] coordinates = result.Split(',');

                if (coordinates.Length == 4)
                {
                    int x = int.Parse(coordinates[0]);
                    int y = int.Parse(coordinates[1]);
                    int w = int.Parse(coordinates[2]);
                    int h = int.Parse(coordinates[3]);

                    // Load the original image
                    using (Image<Rgba32> originalImage = Image.Load<Rgba32>(imagePath))
                    {
                        // Create a new image with the same dimensions as the original image
                        using (Image<Rgba32> outputImage = new Image<Rgba32>(originalImage.Width, originalImage.Height))
                        {
                            // Draw the rectangle onto the new image
                            outputImage.Mutate(ctx => ctx.DrawRectangle(Color.Red, 2, x, y, w, h));

                            // Save the new image with the drawn rectangle
                            outputImage.Save(outputImagePath);
                        }
                    }

                    Console.WriteLine($"Face found at (x:{x}, y:{y}), width:{w}, height:{h}");
                    Console.WriteLine($"Output image saved to {outputImagePath}");
                }
                else
                {
                    Console.WriteLine("No face found");
                }
            }
        }
    }
}
Leave a Comment