yolov5标注审核软件

opencv环境安装

在这里插入图片描述

using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 审核打标图像
{
    public partial class Form1 : Form
    {
        string Current_ImgPath = string.Empty;
        string Current_LabelPath = string.Empty;


        System.Drawing.Point mouseDownPoint = new System.Drawing.Point(); //记录拖拽过程鼠标位置
        bool isMove = false;    //判断鼠标在picturebox上移动时,是否处于拖拽过程(鼠标左键是否按下)
        int zoomStep = 160;      //缩放步长

        string ParamPath = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.pictureBox1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseWheel);

            // 列的设置
            this.dataGridView1.ColumnCount = 2;
            dataGridView1.Columns[0].Name = "Index";
            dataGridView1.Columns[1].Name = "图像路径";
            // 列宽自适应
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            }



            try
            {
                // 显示路径
                this.ParamPath = File.ReadAllText(@"./ParamPath.txt");
                textBox1.Text = this.ParamPath;
                // 显示内容
                DirectoryInfo imgDir = new DirectoryInfo(this.ParamPath);
                FileInfo[] filesInfo = imgDir.GetFiles();
                int count = 1;
                foreach (var item in filesInfo)
                {
                    dataGridView1.Rows.Add(count.ToString(), item.FullName);
                    count++;
                }

            }
            catch
            {

            }

        }


        private void button1_Click(object sender, EventArgs e)
        {


            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.Description = "选择目录";
            folder.SelectedPath = ParamPath;
            if (folder.ShowDialog() == DialogResult.OK)
            {
                //MessageBox.Show(folder.SelectedPath);
                textBox1.Text = folder.SelectedPath;
                ParamPath = folder.SelectedPath;
            }
            try
            {
                //如果文件不存在,则创建;存在则覆盖
                System.IO.File.WriteAllText(@"./ParamPath.txt", ParamPath, Encoding.UTF8);
            }
            catch (Exception exp)
            {
                MessageBox.Show("参数配置路径写入本地失败-失败原因:" + exp.Message);
            }


            // 清理

            dataGridView1.Rows.Clear();
            // 显示
            DirectoryInfo imgDir = new DirectoryInfo(folder.SelectedPath);
            FileInfo[] filesInfo = imgDir.GetFiles();
            int count = 1;
            foreach (var item in filesInfo)
            {
                dataGridView1.Rows.Add(count.ToString(), item.FullName);
                count++;
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("你确定移除图像和标签到“待保留文件夹”中吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                
            }
            else
            {
                return;
            }



            // 如果文件夹不存在,则创建
            DirectoryInfo path111 = Directory.GetParent(textBox1.Text.Trim());
            DirectoryInfo path112 = Directory.GetParent(path111.FullName);

            string dst_dir = Path.Combine(path112.FullName, "待保留图像和标签");

            if (!Directory.Exists(dst_dir))
            {
                Directory.CreateDirectory(dst_dir);
            }

            // 移动图像
            string NowTime1 = DateTime.Now.ToString("hhmmsss");

            string imgName = Path.GetFileName(Current_ImgPath);
            string dst_path_img = Path.Combine(dst_dir, NowTime1+"---"+imgName);
            File.Move(Current_ImgPath, dst_path_img);
            // 移动标签
            string labelName = Path.GetFileName(Current_LabelPath);
            string dst_path_label = Path.Combine(dst_dir, NowTime1 + "---" + labelName);
            File.Move(Current_LabelPath, dst_path_label);


            // 清除列表
            dataGridView1.Rows.Clear();
            // 显示内容
            DirectoryInfo imgDir = new DirectoryInfo(textBox1.Text.Trim());
            FileInfo[] filesInfo = imgDir.GetFiles();
            int count = 1;
            foreach (var item in filesInfo)
            {
                dataGridView1.Rows.Add(count.ToString(), item.FullName);
                count++;
            }

        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //获取单元格内容
            //获取单元格内容
            try
            {
                try
                {
                    // 原图路径
                    string imgPath = dataGridView1.CurrentCell.Value.ToString();
                    // 显示label1图像路径
                    label1.Text = imgPath;
                    // 重要赋值(***)
                    Current_ImgPath = imgPath;

                    // 读取图像
                    Mat img1 = new Mat(imgPath, ImreadModes.Color);
                    OpenCvSharp.Size imgSize = img1.Size();

                    int img1Width =  imgSize.Width;
                    int img1Height = imgSize.Height;
                    // 读取标签
                    string name = Path.GetFileName(imgPath).Split('.')[0];

                    string dirName = Path.GetDirectoryName(imgPath);
                    DirectoryInfo path001 = new DirectoryInfo(dirName); // 用于识别train还是val
                    DirectoryInfo path111 = Directory.GetParent(textBox1.Text.Trim());
                    DirectoryInfo path112 = Directory.GetParent(path111.FullName);
                    
                    string labelPath = Path.Combine(Path.Combine(path112.FullName,"labels", path001.Name), name + ".txt");

                    // 重要赋值(***)
                    Current_LabelPath = labelPath;

                    // 显示label路径
                    label2.Text = labelPath;
                    /*
                     *
                     * 核心:将标签框显示到图像进行显示
                     *
                     */
                    try
                    {

                        // 创建一个 StreamReader 的实例来读取文件 
                        // using 语句也能关闭 StreamReader
                        using (StreamReader sr = new StreamReader(labelPath))
                        {
                            string line;

                            // 从文件读取并显示行,直到文件的末尾 
                            while ((line = sr.ReadLine()) != null)
                            {
                                Console.WriteLine(line);
                                string[] res1 = line.Split(' ');

                                double center_x = img1Width * Convert.ToDouble(res1[1]);
                                int centerX = Convert.ToInt32(center_x);

                                //int leftUp1 = Convert.ToInt32(); 
                                double center_y = img1Height * Convert.ToDouble(res1[2]);
                                int centerY = Convert.ToInt32(center_y);

                                double wd = img1Height * Convert.ToDouble(res1[3]);
                                int Width = Convert.ToInt32(wd);

                                double hg = img1Height * Convert.ToDouble(res1[4]);
                                int Height = Convert.ToInt32(hg);


                                OpenCvSharp.Point truck_a = new OpenCvSharp.Point(centerX - Width/2, centerY-Height/2);
                                OpenCvSharp.Point truck_b = new OpenCvSharp.Point(centerX + Width/2, centerY + Height/2);
                                Cv2.Rectangle(img1, truck_a, truck_b, Scalar.Red, 15);
                            }

                            //Cv2.ImWrite(@"show.jpg", img1);
                            //把Mat格式的图片转换成Bitmap
                            Bitmap bitmap = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(img1);
                            //运行上面的代码需要安装新的库OpenCvSharp4.Extensions

                            //显示图片
                            pictureBox1.Image = bitmap;




                        }
                    
                    
                    }
                    catch (Exception exp)
                    {
                        MessageBox.Show(exp.Message);
                    }

                  //  pictureBox1.Image = Image.FromFile(@"show.jpg");
                    //  MessageBox.Show(content1);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }

        }



        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
                isMove = true;
                pictureBox1.Focus();
            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isMove = false;
            }

        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.Focus();
            if (isMove)
            {
                int x, y;
                int moveX, moveY;
                moveX = Cursor.Position.X - mouseDownPoint.X;
                moveY = Cursor.Position.Y - mouseDownPoint.Y;
                x = pictureBox1.Location.X + moveX;
                y = pictureBox1.Location.Y + moveY;
                pictureBox1.Location = new System.Drawing.Point(x, y);
                mouseDownPoint.X = Cursor.Position.X;
                mouseDownPoint.Y = Cursor.Position.Y;
            }
        }

               /// <summary>
        /// 滚轮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            int x = e.Location.X;
            int y = e.Location.Y;
            int ow = pictureBox1.Width;
            int oh = pictureBox1.Height;
            int VX, VY;

            // 放大
            if (e.Delta > 0)
            {
                pictureBox1.Width += zoomStep;
                pictureBox1.Height += zoomStep;

                PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                    BindingFlags.NonPublic);
                Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);

                pictureBox1.Width = rect.Width;
                pictureBox1.Height = rect.Height;
            }
            // 缩小
            if (e.Delta < 0)
            {
                // 如果picturebox的宽度小于图像的宽度十分之一,则直接返回
                if (pictureBox1.Width < pictureBox1.Image.Width / 5 || pictureBox1.Height < pictureBox1.Image.Height / 5)
                {
                    pictureBox1.Width = pictureBox1.Image.Width / 5;
                    pictureBox1.Height = pictureBox1.Image.Height / 5;
                    PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
    BindingFlags.NonPublic);
                    Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                    pictureBox1.Width = rect.Width;
                    pictureBox1.Height = rect.Height;

                    //PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                    //    BindingFlags.NonPublic);
                    //Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                    //pictureBox1.Width = rect.Width;
                    //pictureBox1.Height = rect.Height;
                }
                else
                {
                    pictureBox1.Width -= zoomStep;
                    pictureBox1.Height -= zoomStep;
                    PropertyInfo pInfo = pictureBox1.GetType().GetProperty("ImageRectangle", BindingFlags.Instance |
                        BindingFlags.NonPublic);
                    Rectangle rect = (Rectangle)pInfo.GetValue(pictureBox1, null);
                    pictureBox1.Width = rect.Width;
                    pictureBox1.Height = rect.Height;
                }
                    
                
            }

            VX = (int)((double)x * (ow - pictureBox1.Width) / ow);
            VY = (int)((double)y * (oh - pictureBox1.Height) / oh);
            pictureBox1.Location = new System.Drawing.Point(pictureBox1.Location.X + VX, pictureBox1.Location.Y + VY);
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值