remove opencv from webcam options

notify^2
sleevezipper 4 years ago
parent 5173c798f5
commit f91f28a269

@ -58,14 +58,11 @@ This sensor exposes the name of the currently focused window.
### WebcamActive ### WebcamActive
This sensor shows if the webcam is currently being used. It has two detection modes: This sensor shows if the webcam is currently being used. It uses the Windows registry to check will work from Windows 10 version 1903 and higher.
- Registry - this is the preferred method. This will work from Windows 10 version 1903 and higher.
- OpenCV - this method tries to access the webcam and if that fails, it assumes it is currently in use. This will flash the webcam activity light at every update interval. It also uses more CPU cycles and memory.
### MicrophoneActive ### MicrophoneActive
This sensor shows if the microphone is currently being used. It uses the Windows registry to check and wil work from Windows 10 version 1903 and higher. This sensor shows if the microphone is currently being used. It uses the Windows registry to check and will work from Windows 10 version 1903 and higher.
### CPULoad ### CPULoad

@ -9,7 +9,6 @@ namespace UserInterface.ViewModels
public class AddSensorViewModel : ViewModelBase public class AddSensorViewModel : ViewModelBase
{ {
private AvailableSensors selectedType; private AvailableSensors selectedType;
private WebcamDetectionMode selectedDetectionMode;
private string description; private string description;
private bool showQueryInput; private bool showQueryInput;
@ -32,7 +31,6 @@ namespace UserInterface.ViewModels
public AvailableSensors SelectedType { get => selectedType; set => this.RaiseAndSetIfChanged(ref selectedType, value); } public AvailableSensors SelectedType { get => selectedType; set => this.RaiseAndSetIfChanged(ref selectedType, value); }
public WebcamDetectionMode SelectedDetectionMode { get => selectedDetectionMode; set => this.RaiseAndSetIfChanged(ref selectedDetectionMode, value); }
public string Name { get; set; } public string Name { get; set; }
public string Query { get; set; } public string Query { get; set; }

@ -26,8 +26,6 @@
<ContentControl IsVisible="{Binding ShowWindowNameInput}" Margin="0 20 0 5">Window name</ContentControl> <ContentControl IsVisible="{Binding ShowWindowNameInput}" Margin="0 20 0 5">Window name</ContentControl>
<TextBlock TextWrapping="Wrap" MaxWidth="300" FontStyle="Italic" IsVisible="{Binding ShowWindowNameInput}" Margin="0 0 0 10">This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium".</TextBlock> <TextBlock TextWrapping="Wrap" MaxWidth="300" FontStyle="Italic" IsVisible="{Binding ShowWindowNameInput}" Margin="0 0 0 10">This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium".</TextBlock>
<TextBox IsVisible="{Binding ShowWindowNameInput}" Text="{Binding WindowName}" Watermark="Visual Studio Code" HorizontalAlignment="Left" MinWidth="300"/> <TextBox IsVisible="{Binding ShowWindowNameInput}" Text="{Binding WindowName}" Watermark="Visual Studio Code" HorizontalAlignment="Left" MinWidth="300"/>
<ContentControl IsVisible="{Binding ShowDetectionModeOptions}" Margin="0 20 0 10">Detection mode</ContentControl> <Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Save">Save</Button>
<ComboBox IsVisible="{Binding ShowDetectionModeOptions}" x:Name="DetectionModeComboBox" SelectedItem="{Binding SelectedDetectionMode}" MinHeight="27"></ComboBox>
<Button Width="75" HorizontalAlignment="Right" Margin="0 40 0 10" Click="Save">Save</Button>
</StackPanel> </StackPanel>
</Window> </Window>

@ -29,9 +29,6 @@ namespace UserInterface.Views
this.comboBox = this.FindControl<ComboBox>("ComboBox"); this.comboBox = this.FindControl<ComboBox>("ComboBox");
this.comboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast<AvailableSensors>(); this.comboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast<AvailableSensors>();
this.comboBox = this.FindControl<ComboBox>("DetectionModeComboBox");
this.comboBox.Items = Enum.GetValues(typeof(WebcamDetectionMode)).Cast<WebcamDetectionMode>();
// register IPC clients // register IPC clients
ServiceProvider serviceProvider = new ServiceCollection() ServiceProvider serviceProvider = new ServiceCollection()
.AddNamedPipeIpcClient<ServiceContractInterfaces>("addsensor", pipeName: "pipeinternal") .AddNamedPipeIpcClient<ServiceContractInterfaces>("addsensor", pipeName: "pipeinternal")
@ -51,7 +48,7 @@ namespace UserInterface.Views
public async void Save(object sender, RoutedEventArgs args) public async void Save(object sender, RoutedEventArgs args)
{ {
var item = ((AddSensorViewModel)this.DataContext); var item = ((AddSensorViewModel)this.DataContext);
dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName, DetectionMode = item.SelectedDetectionMode }; dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName};
string json = JsonSerializer.Serialize(model); string json = JsonSerializer.Serialize(model);
await this.client.InvokeAsync(x => x.AddSensor(item.SelectedType, json)); await this.client.InvokeAsync(x => x.AddSensor(item.SelectedType, json));
Close(); Close();

@ -1,4 +1,4 @@
using hass_workstation_service.Communication.InterProcesCommunication.Models; using hass_workstation_service.Communication.InterProcesCommunication.Models;
using hass_workstation_service.Communication.NamedPipe; using hass_workstation_service.Communication.NamedPipe;
using hass_workstation_service.Communication.Util; using hass_workstation_service.Communication.Util;
using hass_workstation_service.Data; using hass_workstation_service.Data;
@ -107,20 +107,7 @@ namespace hass_workstation_service.Communication.InterProcesCommunication
sensorToCreate = new ActiveWindowSensor(this._publisher, (int)model.UpdateInterval, model.Name); sensorToCreate = new ActiveWindowSensor(this._publisher, (int)model.UpdateInterval, model.Name);
break; break;
case AvailableSensors.WebcamActiveSensor: case AvailableSensors.WebcamActiveSensor:
DetectionMode detectionMode; sensorToCreate = new WebcamActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name);
switch ((WebcamDetectionMode)model.DetectionMode)
{
case WebcamDetectionMode.Registry:
detectionMode = DetectionMode.Registry;
break;
case WebcamDetectionMode.OpenCV:
detectionMode = DetectionMode.OpenCV;
break;
default:
detectionMode = DetectionMode.Registry;
break;
}
sensorToCreate = new WebcamActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name, detectionMode);
break; break;
case AvailableSensors.MicrophoneActiveSensor: case AvailableSensors.MicrophoneActiveSensor:
sensorToCreate = new MicrophoneActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name); sensorToCreate = new MicrophoneActiveSensor(this._publisher, (int)model.UpdateInterval, model.Name);

@ -1,4 +1,4 @@
using hass_workstation_service.Domain.Sensors; using hass_workstation_service.Domain.Sensors;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@ -42,10 +42,4 @@ namespace hass_workstation_service.Communication.InterProcesCommunication.Models
NamedWindowSensor, NamedWindowSensor,
IdleTimeSensor IdleTimeSensor
} }
public enum WebcamDetectionMode
{
Registry,
OpenCV
}
} }

@ -100,7 +100,7 @@ namespace hass_workstation_service.Data
sensor = new IdleTimeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); sensor = new IdleTimeSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break; break;
case "WebcamActiveSensor": case "WebcamActiveSensor":
sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.DetectionMode, configuredSensor.Id); sensor = new WebcamActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);
break; break;
case "MicrophoneActiveSensor": case "MicrophoneActiveSensor":
sensor = new MicrophoneActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id); sensor = new MicrophoneActiveSensor(publisher, configuredSensor.UpdateInterval, configuredSensor.Name, configuredSensor.Id);

@ -11,6 +11,5 @@ namespace hass_workstation_service.Data
public string Query { get; set; } public string Query { get; set; }
public int? UpdateInterval { get; set; } public int? UpdateInterval { get; set; }
public string WindowName { get; set; } public string WindowName { get; set; }
public DetectionMode DetectionMode { get; set; }
} }
} }

@ -1,6 +1,5 @@
using hass_workstation_service.Communication; using hass_workstation_service.Communication;
using Microsoft.Win32; using Microsoft.Win32;
using OpenCvSharp;
using System; using System;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -8,35 +7,20 @@ using System.Runtime.Versioning;
namespace hass_workstation_service.Domain.Sensors namespace hass_workstation_service.Domain.Sensors
{ {
public enum DetectionMode
{
Registry,
OpenCV
}
public class WebcamActiveSensor : AbstractSensor public class WebcamActiveSensor : AbstractSensor
{ {
public DetectionMode DetectionMode { get; private set; } public WebcamActiveSensor(MqttPublisher publisher, int? updateInterval = null, string name = "WebcamActive", Guid id = default) : base(publisher, name, updateInterval ?? 10, id)
public WebcamActiveSensor(MqttPublisher publisher, int? updateInterval = null, string name = "WebcamActive", DetectionMode detectionMode = DetectionMode.Registry, Guid id = default(Guid)) : base(publisher, name, updateInterval ?? 10, id)
{ {
this.DetectionMode = detectionMode;
} }
public override string GetState() public override string GetState()
{ {
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{ {
switch (this.DetectionMode) return IsWebCamInUseRegistry() ? "True" : "False";
{
case DetectionMode.Registry:
return IsWebCamInUseRegistry() ? "True" : "False";
case DetectionMode.OpenCV:
return IsWebCamInUseOpenCV() ? "True" : "False";
default:
return "Error";
}
} }
else else
{ {
return "unsopported"; return "unsupported";
} }
} }
public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig() public override AutoDiscoveryConfigModel GetAutoDiscoveryConfig()
@ -51,35 +35,6 @@ namespace hass_workstation_service.Domain.Sensors
}); });
} }
private bool IsWebCamInUseOpenCV()
{
try
{
VideoCapture capture = new VideoCapture(0);
OutputArray image = OutputArray.Create(new Mat());
// capture.Read() return false if it doesn't succeed in capturing
if (capture.Read(image))
{
capture.Release();
capture.Dispose();
return false;
}
else
{
capture.Release();
capture.Dispose();
return true;
}
}
catch (Exception)
{
return false;
}
}
[SupportedOSPlatform("windows")] [SupportedOSPlatform("windows")]
private bool IsWebCamInUseRegistry() private bool IsWebCamInUseRegistry()
{ {

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Worker"> <Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net5.0</TargetFramework>
@ -39,7 +39,6 @@
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" /> <PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="MQTTnet" Version="3.0.13" /> <PackageReference Include="MQTTnet" Version="3.0.13" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.5.1.20201229" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" /> <PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="System.Management" Version="5.0.0" /> <PackageReference Include="System.Management" Version="5.0.0" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save