58 lines
2.2 KiB
C#
58 lines
2.2 KiB
C#
using FaceRecognitionApp.Hubs;
|
|
using FaceRecognitionApp.Services;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Регистрация сервисов
|
|
builder.Services.AddSingleton<DatabaseService>();
|
|
builder.Services.AddSingleton<FaceRecognitionService>();
|
|
builder.Services.AddSignalR();
|
|
|
|
var app = builder.Build();
|
|
|
|
// 1. Включаем поиск файлов по умолчанию (index.html, default.html и т.д.)
|
|
app.UseDefaultFiles();
|
|
|
|
// 2. Включаем раздачу статических файлов (css, js, картинки)
|
|
app.UseStaticFiles();
|
|
|
|
// 3. Подключаем SignalR хаб
|
|
app.MapHub<FaceHub>("/faceHub");
|
|
|
|
// Инициализация камеры и распознавания
|
|
var db = app.Services.GetRequiredService<DatabaseService>();
|
|
var faceService = app.Services.GetRequiredService<FaceRecognitionService>();
|
|
|
|
// URL камеры (замените на свой или используйте тестовый видеофайл)
|
|
var cameraUrl = Environment.GetEnvironmentVariable("CAMERA_URL") ?? "rtsp://admin:4NUDZhJ7@172.22.10.101:554/cam/realmonitor?channel=1&subtype=1";
|
|
|
|
try
|
|
{
|
|
Console.WriteLine($"Connecting to camera: {cameraUrl}...");
|
|
var camera = new CameraService(cameraUrl, faceService, async (face) => {
|
|
var hub = app.Services.GetRequiredService<IHubContext<FaceHub>>();
|
|
await hub.Clients.All.SendAsync("NewFace", new
|
|
{
|
|
image = Convert.ToBase64String(face.ImageData),
|
|
isRecognized = face.IsRecognized,
|
|
name = face.MatchedEmployee?.Name ?? "Unknown"
|
|
});
|
|
});
|
|
camera.Start();
|
|
Console.WriteLine("✅ Camera service started.");
|
|
|
|
// Корректное завершение работы камеры при остановке приложения
|
|
app.Lifetime.ApplicationStopping.Register(() => {
|
|
Console.WriteLine("Shutting down camera...");
|
|
camera.Dispose();
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"⚠️ Camera Error: {ex.Message}");
|
|
Console.WriteLine("Application will run without camera monitoring.");
|
|
}
|
|
|
|
Console.WriteLine("🚀 Application is ready. Open http://localhost:5290 in your browser.");
|
|
app.Run(); |