agrobot_base/AgroBase/AgroBase/Forms/Atuador/frmAtuCamera.cs

218 lines
7.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AgroBase.Models;
using AgroBase.Services;
using CefSharp.WinForms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Newtonsoft.Json;
namespace AgroBase.Forms
{
public partial class frmAtuCamera : Form
{
private CameraService<CameraCoordenadasFrameModel> cameraService = new CameraService<CameraCoordenadasFrameModel>();
private ChromiumWebBrowser chromiumWebBrowser = null;
private Timer tmrLeitura;
private int MaxLeituras = 10;
private string VideoPorta = VariaveisPortas.CameraSolo.ToString();
private string VideoUrl = "green_objects";
private string SocketPorta = VariaveisPortas.SocketSolo.ToString();
private string ArquivoLeitura = "objetos.json";
public frmAtuCamera()
{
InitializeComponent();
}
private void frmCamera_Load(object sender, EventArgs e)
{
AtualizaListaCameras();
}
private void frmCamera_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
cameraService.socket.Disconnect();
cameraService.pythonProcess.Kill();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void TmrLeitura_Tick(object sender, EventArgs e)
{
ListarObjetosDetectados();
}
private void btnAtualizar_Click(object sender, EventArgs e)
{
AtualizaListaCameras();
}
private void btnSalvar_Click(object sender, EventArgs e)
{
if (btnSalvar.Text == "Editar")
{
btnSalvar.Text = "Salvar";
}
else
{
btnSalvar.Text = "Editar";
cameraService.selectedCamera = cmbCameras.SelectedIndex;
}
btnIniciar.Enabled = btnSalvar.Text == "Editar" && cmbCameras.SelectedIndex > 0;
cmbCameras.Enabled = btnSalvar.Text == "Salvar";
}
private void btnIniciar_Click(object sender, EventArgs e)
{
cameraService.ArquivoLeitura = ArquivoLeitura;
cameraService.IniciarCamera(
PythonService.ScriptWeedDetector,
new string[] {
MaxLeituras.ToString(),
VideoPorta,
VideoUrl,
ArquivoLeitura,
"1",
SocketPorta
},
VideoPorta,
VideoUrl,
true,
SocketPorta
);
tmrLeitura = new Timer() { Interval = 200 };
tmrLeitura.Tick += TmrLeitura_Tick;
tmrLeitura.Start();
}
private void AtualizaListaCameras()
{
cameraService.selectedCamera = cmbCameras.SelectedIndex > 0 ? cmbCameras.SelectedIndex : 0;
cmbCameras.Items.Clear();
cmbCameras.Items.AddRange(cameraService.AtualizaListaCameras().ToArray());
if (cmbCameras.Items.Count == 1)
{
cameraService.selectedCamera = 0;
}
cmbCameras.SelectedIndex = cameraService.selectedCamera;
}
private void ListarObjetosDetectados()
{
if (chromiumWebBrowser == null)
{
chromiumWebBrowser = new ChromiumWebBrowser(cameraService.URLcamera);
this.pnlCamera.Controls.Add(chromiumWebBrowser);
chromiumWebBrowser.Dock = DockStyle.Fill;
}
this.pnlLeituras.Controls.Clear();
int i = 0;
//var coordenadas = cameraService.AferirDadosCamera<CameraCoordenadasModel>();
var leituras = cameraService.socket.DadosRecebidos.ToList().OrderByDescending(x => x.timestamp).Take(MaxLeituras).ToList();
if (!leituras.Any())
{
return;
}
foreach (var objeto in leituras.FirstOrDefault().objetos)
{
GroupBox gpb = new GroupBox()
{
Name = "gpbLeitura" + i,
Size = new Size(120, 170),
Text = "Leitura " + (i + 1),
Location = new Point(3, 3 + (i * 190))
};
Label _lblID = new Label()
{
Name = "lblLeitura" + i + "ID",
Size = new Size(100, 20),
Text = "ID: " + objeto.id,
Location = new Point(6, 20)
};
Label _lblDescricao = new Label()
{
Name = "lblLeitura" + i + "Descricao",
Size = new Size(100, 20),
Text = "Descrição: " + objeto.descricao,
Location = new Point(6, 40)
};
Label _lblConfianca = new Label()
{
Name = "lblLeitura" + i + "Confianca",
Size = new Size(100, 20),
Text = "Confiança: " + (objeto.confianca * 100).ToString("0.00") + "%",
Location = new Point(6, 60)
};
Label _lblX = new Label()
{
Name = "lblLeitura" + i + "X",
Size = new Size(100, 20),
Text = "X: " + objeto.x,
Location = new Point(6, 80)
};
Label _lblY = new Label()
{
Name = "lblLeitura" + i + "Y",
Size = new Size(100, 20),
Text = "Y: " + objeto.y,
Location = new Point(6, 100)
};
Label _lblAltura = new Label()
{
Name = "lblLeitura" + i + "Altura",
Size = new Size(100, 20),
Text = "Altura: " + objeto.altura,
Location = new Point(6, 120)
};
Label _lblLargura = new Label()
{
Name = "lblLeitura" + i + "Largura",
Size = new Size(100, 20),
Text = "Largura: " + objeto.largura,
Location = new Point(6, 140)
};
gpb.Controls.Add(_lblID);
gpb.Controls.Add(_lblDescricao);
gpb.Controls.Add(_lblConfianca);
gpb.Controls.Add(_lblX);
gpb.Controls.Add(_lblY);
gpb.Controls.Add(_lblAltura);
gpb.Controls.Add(_lblLargura);
pnlLeituras.Controls.Add(gpb);
i++;
}
}
}
}