Skip to content

Commit

Permalink
错误兜底 截图方法允许直接修正截图范围
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Dec 31, 2024
1 parent c6bedbb commit 4e1d1b4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
7 changes: 5 additions & 2 deletions Core.Window/ScreenCapture/CaptureTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ static float LogNormalize(float value, float maxHDR, float k = 1)
if (value < 0) value = 0;
return (float)(Math.Log(1 + k * value) / Math.Log(1 + k * maxHDR));
}
public static unsafe byte[] GetBytesSpan(MappedSubresource mappedSubresource, OutputDesc1 outputDesc,ScreenCaptureInfo screenCaptureInfo)
public static unsafe byte[] GetBytesSpan(MappedSubresource mappedSubresource, OutputDesc1 outputDesc,ref ScreenCaptureInfo screenCaptureInfo)
{
int startX = Math.Clamp(screenCaptureInfo.X, 0, outputDesc.DesktopCoordinates.Size.X - 1);
int startY = Math.Clamp(screenCaptureInfo.Y, 0, outputDesc.DesktopCoordinates.Size.Y - 1);
int endX = Math.Clamp(screenCaptureInfo.X + screenCaptureInfo.Width, 0, outputDesc.DesktopCoordinates.Size.X);
int endY = Math.Clamp(screenCaptureInfo.Y + screenCaptureInfo.Height, 0, outputDesc.DesktopCoordinates.Size.Y);

screenCaptureInfo.Height = endY - startY;
screenCaptureInfo.Width = endX - startX;
screenCaptureInfo.X = startX;
screenCaptureInfo.Y = startY;
// 结果数组:区域宽 * 区域高 * 4(RGBA)
int regionWidth = endX - startX;
int regionHeight = endY - startY;
Expand Down
2 changes: 1 addition & 1 deletion Core.Window/ScreenCapture/ScreenCaptureByDx11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public unsafe Stack<ScreenCaptureResult> CaptureAllScreenBytes()
X = desc.DesktopCoordinates.Min.X,
Y = desc.DesktopCoordinates.Min.Y
};
var re = CaptureTool.GetBytesSpan(mappedSubresource, outputDesc,screenCaptureInfo);
var re = CaptureTool.GetBytesSpan(mappedSubresource, outputDesc,ref screenCaptureInfo);
intPtr.immediateContext->Unmap(stagingResource, 0);
outputDuplication->ReleaseFrame();

Expand Down
2 changes: 1 addition & 1 deletion Core.Window/ScreenCapture/ScreenCaptureByWGC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ IDirect3DDevice CreateDirect3DDeviceFromSharpDXDevice(ID3D11Device* d3dDevice)
throw new Exception("Failed to map staging texture");


var bytesSpan = CaptureTool.GetBytesSpan(mappedSubresource, outputDesc,screenCaptureInfo);
var bytesSpan = CaptureTool.GetBytesSpan(mappedSubresource, outputDesc,ref screenCaptureInfo);
return new ScreenCaptureResult()
{
Info = screenCaptureInfo,
Expand Down
2 changes: 1 addition & 1 deletion Core/SDKs/CustomScenario/CustomScenarioManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,6 @@ public static void ReCheck(bool onlyError = true)
if (configF.Exists) Load(configF);
}

LoadAll();
LoadAll();
}
}
21 changes: 9 additions & 12 deletions KitopiaEx/Ocr/Ocr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ public IEnumerable<OcrResult> OcrImg(ScreenCaptureResult dResult, CancellationTo
var ocrResults = new List<OcrResult>();
var callingAssembly = Assembly.GetExecutingAssembly().Location;
callingAssembly = callingAssembly.Remove(callingAssembly.LastIndexOf("\\"));

TextDetector _textDetector=new TextDetector($"{callingAssembly}\\Ocr\\ocr_det.onnx");
TextRecognizer _textRecognizer= new TextRecognizer($"{callingAssembly}\\Ocr\\ocr_rec.onnx",$"{callingAssembly}\\Ocr\\rec_word_dict.txt");
Mat img = Mat.FromPixelData(dResult.Info.Height,dResult.Info.Width,MatType.CV_8UC4,dResult.Bytes);

using TextDetector _textDetector = new TextDetector($"{callingAssembly}\\Ocr\\ocr_det.onnx");
using TextRecognizer _textRecognizer = new TextRecognizer($"{callingAssembly}\\Ocr\\ocr_rec.onnx",
$"{callingAssembly}\\Ocr\\rec_word_dict.txt");
using Mat img = Mat.FromPixelData(dResult.Info.Height, dResult.Info.Width, MatType.CV_8UC4, dResult.Bytes);
//img.SaveImage($"{callingAssembly}\\1.png");
var detect = _textDetector.Detect(img);
var textDetectorDstImg = _textDetector.dstImg;
using var textDetectorDstImg = _textDetector.dstImg;
foreach (var point2Fse in detect)
{
(Mat, Rect) textimg = _textDetector.GetRotateCropImage(textDetectorDstImg, point2Fse);
Expand All @@ -50,20 +51,16 @@ public IEnumerable<OcrResult> OcrImg(ScreenCaptureResult dResult, CancellationTo
}

Rect rect = textimg.Item2;

ocrResults.Add(new OcrResult()
{
SPoint = new Point(rect.Left,rect.Top),
EPoint = new Point(rect.Left+rect.Width,rect.Top+rect.Height),
SPoint = new Point(rect.Left, rect.Top),
EPoint = new Point(rect.Left + rect.Width, rect.Top + rect.Height),
Text = predictText
});
//Console.WriteLine(predictText);
}

img.Dispose();
textDetectorDstImg.Dispose();
_textDetector.Dispose();
_textRecognizer.Dispose();
return ocrResults;
}

Expand Down

0 comments on commit 4e1d1b4

Please sign in to comment.