Repository navigation
Expand file tree
/
Copy pathFireDetectionEngine.cs
More file actions
136 lines (119 loc) · 5.46 KB
/
Copy pathFireDetectionEngine.cs
File metadata and controls
136 lines (119 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
namespace GenetecEdwardsBridge
{
public class FireDetectionEngine
{
// Adjustable parameters for site tuning
private const double TriggerPercentage = 1.5; // Trigger alarm if more than 1.5% of the frame contains fire
// Path tracking to the NJIT / Multicore / CUDA Python processor script
private readonly string _pythonScriptPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "fire_cuda_engine.py");
/// <summary>
/// Extracts raw pixel matrices and streams them into the CUDA-accelerated Numba Python pipeline.
/// Returns true if a fire trigger threshold condition is mathematically satisfied.
/// </summary>
public unsafe bool AnalyzeFrameForFire(Bitmap frame, out double flameDensity)
{
flameDensity = 0.0;
if (frame == null) return false;
int width = frame.Width;
int height = frame.Height;
// 1. Lock bitmap bits into system RAM for swift array translation
BitmapData bitmapData = frame.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadOnly,
PixelFormat.Format24bppRgb
);
// Calculate exact size and extract raw unmanaged memory bytes into a managed array container
int totalByteCount = bitmapData.Stride * height;
byte[] rawImageBytes = new byte[totalByteCount];
Marshal.Copy(bitmapData.Scan0, rawImageBytes, 0, totalByteCount);
// Crucial: Unlock bits immediately to maintain strict life-safety memory efficiency and prevent OS leaks
frame.UnlockBits(bitmapData);
try
{
// 2. Configure an ultra-low overhead Python process wrapper
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "python.exe",
// Pass dimensions as command line flags so the Python side knows exactly how to slice the incoming byte stream
Arguments = $"`"{_pythonScriptPath}`" --width {width} --height {height}",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process process = Process.Start(startInfo))
{
if (process != null)
{
// 3. Directly stream the binary byte matrix over the pipeline stdin channel to bypass overhead constraints
using (BinaryWriter streamWriter = new BinaryWriter(process.StandardInput.BaseStream))
{
streamWriter.Write(rawImageBytes);
streamWriter.Flush();
}
// 4. Capture the calculated density scalar passed back by Numba/CUDA via stdout
string outputResult = process.StandardOutput.ReadLine();
if (!string.IsNullOrWhiteSpace(outputResult) && double.TryParse(outputResult, out double parsedDensity))
{
flameDensity = parsedDensity;
}
else
{
// Capture standard execution error logs if Python outputs execution structural issues
string operationalErrors = process.StandardError.ReadToEnd();
if (!string.IsNullOrWhiteSpace(operationalErrors))
{
Trace.WriteLine($"CUDA Python processing loop stderr warning: {operationalErrors}");
}
}
}
}
}
catch (Exception ex)
{
Trace.WriteLine($"Critical hardware acceleration handoff pipeline failure: {ex.Message}");
return false;
}
// 5. Evaluate if density percentage crosses the plant engineering trigger threshold boundaries
return flameDensity >= TriggerPercentage;
}
}
}
using System.Collections.Generic;
namespace GenetecEdwardsBridge
{
public class FireWatchConfig
{
public GenetecSettings GenetecConfig { get; set; }
public EdwardsSettings EdwardsConfig { get; set; }
public List<CameraMapping> HospitalMap { get; set; }
}
public class GenetecSettings
{
public string DirectoryServer { get; set; }
public string ServiceUser { get; set; }
public string ServicePassword { get; set; }
public string KiwiFireEventGuid { get; set; }
}
public class EdwardsSettings
{
public string ReceiverIp { get; set; }
public int ReceiverPort { get; set; }
public int HeartbeatIntervalSeconds { get; set; }
}
public class CameraMapping
{
public string CameraGuid { get; set; }
public string GenetecName { get; set; }
public string EdwardsNode { get; set; }
public string EdwardsZone { get; set; }
public string PhysicalRoom { get; set; }
}
}