Skip to content

Commit

Permalink
v 1.3
Browse files Browse the repository at this point in the history
- При запуске переводчика, окна захвата и перевода теперь скрываются. Текст появляется только тогда когда он был распознан.
- Окна захвата и перевода во время работы не перекрывают интерфейс игры.
- Окно перевода теперь не захватывается, и можно накладывать окно захвата на окно перевода.
- Добавлена возможность настройки задержки пропадания текста.
- Сломался Google - переводчик, переводилась только первое предложение. Исправлено изменением способ перевода через Google.
  • Loading branch information
PavlikBender committed Dec 1, 2024
1 parent 241b0ac commit cba8972
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 120 deletions.
14 changes: 12 additions & 2 deletions Interfaces/IParametersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

public interface IParametersService
{

/// <summary>
/// Is in translation mode.
/// </summary>
bool IsStarted
{
get;
set;
}

/// <summary>
/// Current translated line.
/// </summary>
Expand All @@ -14,8 +24,8 @@ string TranslatedLine
/// <summary>
/// Border thickness of selection area.
/// </summary>
int SelectionBorderThickness
int SelectionBorderThickness
{
get;
get;
}
}
15 changes: 15 additions & 0 deletions Interfaces/IWindowService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ public interface IWindowService
/// </summary>
void Normalize(string windowName);

/// <summary>
/// Set window click thru style.
/// </summary>
void SetWindowClickThru(string windowName);

/// <summary>
/// Revert click thru style.
/// </summary>
void SetWindowClickable(string windowName);

/// <summary>
/// Exclude window from capture.
/// </summary>
void ExcludeFromCapture(string windowName);

/// <summary>
/// Установить владельца всех окон.
/// </summary>
Expand Down
29 changes: 21 additions & 8 deletions MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
x:Class="ScreTran.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ScreTran"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
d:DataContext="{d:DesignInstance Type=local:MainWindowModel}"
Width="600"
Height="530"
Height="600"
mc:Ignorable="d"
Title="ScreTran" Background="#FF313338" WindowStyle="None" ResizeMode="NoResize" Loaded="Window_Loaded"
UseLayoutRounding="True">
Expand All @@ -21,7 +22,14 @@
<Window.Resources>
<ResourceDictionary>
<BooleanToVisibilityConverter x:Key="BoolToVisConverter" />
<local:BoolToInvertedBoolConverter x:Key="BoolToInvertedBoolConverter"/>
<local:BoolToObjectConverter x:Key="BoolToInvertedBoolConverter">
<local:BoolToObjectConverter.TrueValue>
<s:Boolean>False</s:Boolean>
</local:BoolToObjectConverter.TrueValue>
<local:BoolToObjectConverter.FalseValue>
<s:Boolean>True</s:Boolean>
</local:BoolToObjectConverter.FalseValue>
</local:BoolToObjectConverter>
<Style TargetType="Label">
<Setter Property="Margin" Value="0,8,0,6"/>
<Setter Property="Foreground" Value="White"/>
Expand Down Expand Up @@ -143,7 +151,7 @@
Grid.Row="0"
Command="{Binding StopCommand}"
Content="СТОП" HorizontalAlignment="Stretch" Width="Auto" VerticalAlignment="Center" Style="{StaticResource StandardButtonStyle}"
Margin="12,8" Panel.ZIndex="10" Background="#FF7B3838" Visibility="{Binding IsStarted, Converter={StaticResource BoolToVisConverter}}"/>
Margin="12,8" Panel.ZIndex="10" Background="#FF7B3838" Visibility="{Binding Parameters.IsStarted, Converter={StaticResource BoolToVisConverter}}"/>
<Button
Grid.Row="0"
Command="{Binding StartCommand}"
Expand All @@ -160,6 +168,7 @@
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
Expand All @@ -179,14 +188,14 @@
</StackPanel>
<StackPanel Grid.Row="2" ToolTip="ЭКСПЕРИМЕНТАЛЬНАЯ НАСТРОЙКА!!!&#x0a;Модель используемая для распознавания текста с экрана.&#x0a;Недоступно для изменения если программа находится в процессе перевода.">
<Label Content="Модель"/>
<ComboBox Style="{DynamicResource ComboBoxStyle1}" SelectedItem="{Binding Settings.OcrModel, Mode=TwoWay}" ItemsSource="{Binding Models}" Background="#FF1E1F22" BorderBrush="{x:Null}" Foreground="White" FontSize="14" IsEnabled="{Binding IsStarted, Converter={StaticResource BoolToInvertedBoolConverter}}">
<ComboBox Style="{DynamicResource ComboBoxStyle1}" SelectedItem="{Binding Settings.OcrModel, Mode=TwoWay}" ItemsSource="{Binding Models}" Background="#FF1E1F22" BorderBrush="{x:Null}" Foreground="White" FontSize="14" IsEnabled="{Binding Parameters.IsStarted, Converter={StaticResource BoolToInvertedBoolConverter}}">
<ComboBox.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}" Color="#FF1E1F22" />
<SolidColorBrush x:Key="{x:Static SystemColors.WindowFrameBrushKey}"/>
</ComboBox.Resources>
</ComboBox>
</StackPanel>
<StackPanel Grid.Row="3" ToolTip="Горячая клавиша, при нажатии которой скрываются/восстанавливаются области распознавания текста и перевода.&#x0a;При скрытии областей перевод останавливается.&#x0a;При восстановлении областей автоматически запускается перевод.">
<StackPanel Grid.Row="3" ToolTip="Горячая клавиша, для включения/выключения переводчика.">
<Label Content="Горячая клавиша"/>
<Grid>
<Grid.ColumnDefinitions>
Expand All @@ -201,13 +210,17 @@
</StackPanel>
<StackPanel Grid.Row="4" ToolTip="Указывается частота в секундах с которой будет делаться захват экрана.&#x0a;Чем меньше частота, тем больше нагрузка на процессор.&#x0a;Если сделать частоту слишком большой - может происходить пропуск реплик.&#x0a;Недоступно для изменения если программа находится в процессе перевода.">
<Label Content="Частота захвата экрана (с.)"/>
<xctk:DecimalUpDown Value="{Binding Settings.Period}" Increment=".1" Maximum="10" Minimum="0.3" Background="#FF1E1F22" BorderBrush="{x:Null}" Foreground="White" FontSize="14" IsEnabled="{Binding IsStarted, Converter={StaticResource BoolToInvertedBoolConverter}}"/>
<xctk:DecimalUpDown Value="{Binding Settings.Period}" Increment=".1" Maximum="10" Minimum="0.3" Background="#FF1E1F22" BorderBrush="{x:Null}" Foreground="White" FontSize="14" IsEnabled="{Binding Parameters.IsStarted, Converter={StaticResource BoolToInvertedBoolConverter}}"/>
</StackPanel>
<StackPanel Grid.Row="5" ToolTip="Указывается количество итераций захвата экрана, после которого будет скрываться окно перевода, если в окне захвата не обнаружен текст.&#x0a;Например: частота захвата - 1с. количество итераций - 2, то при отсутствии текста для перевода, окно перевода скроется через 2с.">
<Label Content="Итераций для скрытия"/>
<xctk:DecimalUpDown Value="{Binding Settings.HideInterval}" Increment="1" Maximum="10" Minimum="1" Background="#FF1E1F22" BorderBrush="{x:Null}" Foreground="White" FontSize="14" IsEnabled="{Binding Parameters.IsStarted, Converter={StaticResource BoolToInvertedBoolConverter}}"/>
</StackPanel>
<StackPanel Grid.Row="5" Margin="12" Orientation="Horizontal" HorizontalAlignment="Center">
<StackPanel Grid.Row="6" Margin="12" Orientation="Horizontal" HorizontalAlignment="Center">
<Button x:Name="ResetSettingsToDefault" Margin="0,0,8,0" Content="Вернуть по умолчанию" Command="{Binding ResetSettingsToDefaultCommand}" Style="{StaticResource StandardButtonStyle}" ToolTip="Вернуть настройки по умолчанию."/>
<Button x:Name="ResetWindowPosition" Content="Сбросить положение окон" Command="{Binding ResetWindowsPositionCommand}" Style="{StaticResource StandardButtonStyle}"/>
</StackPanel>
<TextBlock Grid.Row="6" Margin="0,0,0,8" FontSize="12" HorizontalAlignment="Center">
<TextBlock Grid.Row="7" Margin="0,0,0,8" FontSize="12" HorizontalAlignment="Center">
<Hyperlink x:Name="Link" NavigateUri="https://github.com/PavlikBender/ScreTran" Foreground="#FFAFAFAF">О проекте</Hyperlink>
</TextBlock>
</Grid>
Expand Down
7 changes: 7 additions & 0 deletions Models/SettingsModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public partial class SettingsModel : ObservableObject
[ObservableProperty]
private float _period;

// TODO Не должно быть меньше _period
/// <summary>
/// Hide interval.
/// </summary>
[ObservableProperty]
private int _hideInterval;

/// <summary>
/// Translator.
Expand Down Expand Up @@ -78,6 +84,7 @@ public void ResetToDefault()
FontSize = 21;
Key = new Key(0x7B);
Period = 1.0f;
HideInterval = 2;
Translator = Enumerations.Translator.Google;
OcrModel = Enumerations.Model.English;
}
Expand Down
14 changes: 7 additions & 7 deletions ScreTran.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<ApplicationIcon>ScreTranLogo.ico</ApplicationIcon>
<AssemblyVersion>1.2.6</AssemblyVersion>
<AssemblyVersion>1.3</AssemblyVersion>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

Expand All @@ -20,18 +20,18 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.3.2" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.6.1" />
<PackageReference Include="GTranslate" Version="2.2.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.122" />
<PackageReference Include="GTranslate" Version="2.2.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.135" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.10.0.20240616" />
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.10.0.20241108" />
<PackageReference Include="Sdcb.PaddleInference" Version="2.5.0.1" />
<PackageReference Include="Sdcb.PaddleInference.runtime.win64.mkl" Version="2.6.1" />
<PackageReference Include="Sdcb.PaddleOCR" Version="2.7.0.3" />
<PackageReference Include="Sdcb.PaddleOCR.Models.Local" Version="2.7.0" />
<PackageReference Include="System.Drawing.Common" Version="8.0.8" />
<PackageReference Include="System.Drawing.Common" Version="9.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 18 additions & 0 deletions Services/ExecutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ExecutionService : IExecutionService

private Task _lastTask;
private string _lastLine;
private float _timeWithoutUpdate;

private Enumerations.Model _currentModel;

Expand All @@ -44,6 +45,10 @@ public ExecutionService(ISettingsService settingsService, IParametersService par
/// </summary>
public void Start()
{
_timeWithoutUpdate = 0;
_parameters.TranslatedLine = string.Empty;
_lastLine = string.Empty;

var period = (int)(1000 * _settings.Period);
_timer = new Timer(ProccessByTimerCommands, null, 0, period);

Expand Down Expand Up @@ -181,7 +186,20 @@ private void RecognizeTextAndTranslate()
var line = PaddleOCRRecognize(GetImage());

if (string.IsNullOrWhiteSpace(line))
{
_timeWithoutUpdate++;

if (_timeWithoutUpdate > _settings.HideInterval)
{
_timeWithoutUpdate = 0;
_parameters.TranslatedLine = string.Empty;
_lastLine = string.Empty;
}

return;
}

_timeWithoutUpdate = 0;

line = line.Trim()
// Для корректной обработки переводчиком нужно убрать переводы строк
Expand Down
7 changes: 7 additions & 0 deletions Services/ParametersService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ namespace ScreTran;

public partial class ParametersService : ObservableObject, IParametersService
{
/// <summary>
/// Is in translation mode.
/// </summary>
[ObservableProperty]
private bool _isStarted;

/// <summary>
/// Translated line.
/// </summary>
Expand All @@ -20,5 +26,6 @@ public ParametersService()
{
SelectionBorderThickness = 5;
TranslatedLine = "<Окно перевода>";
IsStarted = false;
}
}
24 changes: 4 additions & 20 deletions Services/TranslationService.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using System.Net.Http;
using System.Web;
using GTranslate.Translators;
using Newtonsoft.Json.Linq;
using GTranslate.Translators;

namespace ScreTran;

public class TranslationService : ITranslationService
{
private readonly YandexTranslator _yandexTranslator;
private readonly BingTranslator _bingTranslator;
private readonly GoogleTranslator _googleTranslator;

public TranslationService()
{
_yandexTranslator = new YandexTranslator();
_bingTranslator = new BingTranslator();
_googleTranslator = new GoogleTranslator();
}

/// <summary>
Expand All @@ -36,29 +35,14 @@ public string Translate(string input, Enumerations.Translator translator)
private async Task<string> TranslateAsync(string input, Enumerations.Translator translator)
{
if (translator == Enumerations.Translator.Google)
return await TranslateGoogleAsync(input);
return (await _googleTranslator.TranslateAsync(input, "ru")).Translation;
if (translator == Enumerations.Translator.Yandex)
return (await _yandexTranslator.TranslateAsync(input, "ru")).Translation;
if (translator == Enumerations.Translator.Bing)
return (await _bingTranslator.TranslateAsync(input, "ru")).Translation;

return input;
}

/// <summary>
/// Translate input from english to russian GoogleTranslate asynchroniously.
/// </summary>
/// <param name="input">input text.</param>
/// <returns>Translated text.</returns>
public async Task<string> TranslateGoogleAsync(string input)
{
var to = "ru";
var url = $"https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl={to}&dt=t&q={HttpUtility.UrlEncode(input)}";

using var client = new HttpClient();
var response = await client.GetStringAsync(url).ConfigureAwait(false);
return (string)JArray.Parse(response)[0][0][0];
}
}


Loading

0 comments on commit cba8972

Please sign in to comment.