116 lines
3.9 KiB
C#
116 lines
3.9 KiB
C#
using AForge.Video.DirectShow;
|
|
using AgroBase.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Management;
|
|
using System.Threading.Tasks;
|
|
using static MqttService;
|
|
|
|
namespace AgroBase.Services
|
|
{
|
|
public class CameraService
|
|
{
|
|
public static Dictionary<string, string> CamerasConectadas = new Dictionary<string, string>();
|
|
|
|
public Process pythonProcess;
|
|
public readonly object processLock = new object();
|
|
public DateTime? IniciadoEm = null;
|
|
public DateTime? ProntoEm = null;
|
|
public DateTime? FinalizadoEm = null;
|
|
public MqttTopicosModel _mqttTopico;
|
|
|
|
private string Endereco = "localhost";
|
|
public string ArquivoLeitura = "leitura.json";
|
|
public int cameraIndex = 0;
|
|
private double conf_threshold = 0.5;
|
|
private double nms_threshold = 0.4;
|
|
public List<string> URLcamera = new List<string>();
|
|
public bool InverterDadosLeitura = false;
|
|
|
|
|
|
public void IniciarCamera(string Script, string[] Argumentos, string FlaskPorta, List<string> FlaskUrl, string MqttTopico, Func<MqttTopicosMensagensModel, Task> mqttCallback = null)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
_mqttTopico = await Variaveis.MqttServiceLocal.AdicionarNovoTopico(MqttTopico, true, 2, mqttCallback);
|
|
|
|
lock (processLock)
|
|
{
|
|
pythonProcess = new Process();
|
|
pythonProcess = PythonService.RunScript(Script, Argumentos);
|
|
}
|
|
IniciadoEm = DateTime.Now;
|
|
|
|
foreach (string url in FlaskUrl)
|
|
{
|
|
string _url = "http://" + Endereco + ":" + FlaskPorta + "/" + url +
|
|
"?conf_threshold=" + conf_threshold.ToString().Replace(",", ".") +
|
|
"&nms_threshold=" + nms_threshold.ToString().Replace(",", ".") +
|
|
"&camera_index=" + cameraIndex;
|
|
URLcamera.Add(_url);
|
|
|
|
Console.WriteLine(_url);
|
|
}
|
|
});
|
|
}
|
|
|
|
private void PythonProcess_Exited(object sender, EventArgs e)
|
|
{
|
|
FinalizadoEm = DateTime.Now;
|
|
}
|
|
|
|
public static Dictionary<string, string> AtualizaListaCameras()
|
|
{
|
|
try
|
|
{
|
|
CamerasConectadas.Clear();
|
|
CamerasConectadas.Add("Sem vídeo", "Sem video");
|
|
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Camera')")) // PNPClass = 'Image' OR PNPClass = 'Camera')
|
|
{
|
|
int i = 0;
|
|
foreach (var device in searcher.Get())
|
|
{
|
|
string cameraDesc = i.ToString() + " - " + device["Caption"].ToString();
|
|
CamerasConectadas.Add(cameraDesc, device["DeviceID"].ToString());
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
catch (ApplicationException)
|
|
{
|
|
|
|
}
|
|
|
|
return CamerasConectadas;
|
|
}
|
|
|
|
public static Dictionary<string, string> ObterListaCameras()
|
|
{
|
|
var cameras = new Dictionary<string, string>();
|
|
try
|
|
{
|
|
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
|
|
|
|
cameras.Add("Sem vídeo", "Sem video");
|
|
int index = 0;
|
|
|
|
foreach (FilterInfo device in videoDevices)
|
|
{
|
|
string cameraDesc = $"{index} - {device.Name}";
|
|
cameras.Add(cameraDesc, device.MonikerString);
|
|
index++;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Erro ao listar câmeras: {ex.Message}");
|
|
}
|
|
|
|
return cameras;
|
|
}
|
|
|
|
|
|
}
|
|
}
|