Anonymous
Installing Tesseract On Unity 3D
Сообщение
Anonymous » 25 янв 2025, 23:11
У меня возникла проблема с Tesseract, и я думаю, она связана с его неправильным импортом.
Я использовал менеджер пакетов NuGet в Visual Studios для установки Tesseract, а затем скопировал файлы в Папка плагина находится внутри папки Assets в Unity, а также eng.tessData. Однако каждый раз, когда я запускаю свой код и делаю снимок для его чтения, я получаю эту ошибку OCR Error: Целью вызова было создано исключение. Любой совет хорош.
Вот мой код
Код: Выделить всё
for the example.
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using Newtonsoft.Json.Linq;
using Tesseract;
public class CaptureCards : MonoBehaviour
{
public RawImage cameraFeed;
public Button captureButton;
public Text cardDetailsText;
private WebCamTexture webCamTexture;
private HttpClient httpClient;
// Start is called before the first frame update
void Start()
{
StartCameraFeed();
httpClient = new HttpClient();
captureButton.onClick.AddListener(CaptureAndProcess);
}
void StartCameraFeed()
{
WebCamDevice[] devices = WebCamTexture.devices;
if (devices.Length == 0)
{
Debug.LogError("No camera devices found.");
return;
}
// Log available cameras
Debug.Log("Available cameras:");
foreach (var device in devices)
{
Debug.Log(device.name);
}
string selectedCamera = devices[0].name;
Debug.Log("Using camera: " + selectedCamera);
webCamTexture = new WebCamTexture(selectedCamera);
cameraFeed.texture = webCamTexture;
cameraFeed.material.mainTexture = webCamTexture;
webCamTexture.Play();
}
async void CaptureAndProcess()
{
if (webCamTexture == null || !webCamTexture.isPlaying)
{
cardDetailsText.text = "Camera feed not available.";
return;
}
Texture2D captureImage = new Texture2D(webCamTexture.width, webCamTexture.height);
captureImage.SetPixels(webCamTexture.GetPixels());
captureImage.Apply();
string tempPath = Path.Combine(Application.persistentDataPath, "captureCard.png");
Texture2D preprocessedImage = PreprocessImage(captureImage);
File.WriteAllBytes(tempPath, preprocessedImage.EncodeToPNG());
Debug.Log("Image saved at: " + tempPath);
string cardName = await RecognizeText(tempPath);
if (!string.IsNullOrEmpty(cardName))
{
JObject cardData = await FetchCardDetails(cardName);
if (cardData != null)
{
DisplayCardDetails(cardData);
SaveCardDetails(cardData);
}
else
{
cardDetailsText.text = "Card not Found";
}
}
else
{
cardDetailsText.text = "Failed to recognize card name";
}
}
Texture2D PreprocessImage(Texture2D original)
{
int targetWidth = 800; // Resize for better OCR accuracy
int targetHeight = (int)(original.height * (800.0f / original.width));
Texture2D resizedImage = new Texture2D(targetWidth, targetHeight);
for (int y = 0; y < targetHeight; y++)
{
for (int x = 0; x < targetWidth; x++)
{
Color pixel = original.GetPixelBilinear((float)x / targetWidth, (float)y / targetHeight);
resizedImage.SetPixel(x, y, pixel);
}
}
resizedImage.Apply();
// Convert to grayscale
Color[] pixels = resizedImage.GetPixels();
for (int i = 0; i < pixels.Length; i++)
{
float gray = pixels[i].grayscale;
pixels[i] = new Color(gray, gray, gray);
}
resizedImage.SetPixels(pixels);
resizedImage.Apply();
return resizedImage;
}
async Task RecognizeText(string imagePath)
{
string resultText = string.Empty;
string tessDataPath = Path.Combine(Application.streamingAssetsPath, "tessdata");
Debug.Log($"StreamingAssets Path: {Application.streamingAssetsPath}");
Debug.Log($"File Exists: {File.Exists(Path.Combine(Application.streamingAssetsPath, "tessdata", "eng.traineddata"))}");
await Task.Run(() =>
{
try
{
if (!Directory.Exists(tessDataPath))
throw new DirectoryNotFoundException($"Tesseract data directory not found: {tessDataPath}");
if (!File.Exists(Path.Combine(tessDataPath, "eng.traineddata")))
throw new FileNotFoundException("Tesseract trained data file not found (eng.traineddata).");
if (!File.Exists(imagePath))
throw new FileNotFoundException($"Image file not found: {imagePath}");
using (var engine = new TesseractEngine(tessDataPath, "eng", EngineMode.Default))
{
using (var img = Pix.LoadFromFile(imagePath))
{
using (var page = engine.Process(img, PageSegMode.SingleBlock))
{
resultText = page.GetText().Trim();
}
}
}
}
catch (Exception ex)
{
Debug.LogError($"OCR Error: {ex.Message}\n{ex.StackTrace}");
}
});
return resultText;
}
async Task FetchCardDetails(string cardName)
{
try
{
string apiUrl = $"https://api.scryfall.com/cards/named?exact={UnityWebRequest.EscapeURL(cardName)}";
string response = await httpClient.GetStringAsync(apiUrl);
return JObject.Parse(response);
}
catch
{
return null;
}
}
void DisplayCardDetails(JObject cardData)
{
string name = cardData["name"].ToString();
string type = cardData["type_line"].ToString();
string manaCost = cardData["mana_cost"].ToString();
string description = cardData["oracle_text"].ToString();
cardDetailsText.text = $"Name: {name}\nType: {type}\nMana Cost: {manaCost}\nDescription: {description}";
}
void SaveCardDetails(JObject cardData)
{
string savePath = Path.Combine(Application.persistentDataPath, "savedCards.json");
JArray savedCards = File.Exists(savePath)
? JArray.Parse(File.ReadAllText(savePath))
: new JArray();
savedCards.Add(cardData);
File.WriteAllText(savePath, savedCards.ToString());
}
void OnDestroy()
{
// Clean up resources
webCamTexture.Stop();
httpClient.Dispose();
}
}
Чтобы исправить ошибку, из-за которой тессеракт не работает должным образом, чтобы я мог получить текст из изображения.
Подробнее здесь:
https://stackoverflow.com/questions/793 ... n-unity-3d
1737835906
Anonymous
У меня возникла проблема с Tesseract, и я думаю, она связана с его неправильным импортом. Я использовал менеджер пакетов NuGet в Visual Studios для установки Tesseract, а затем скопировал файлы в Папка плагина находится внутри папки Assets в Unity, а также eng.tessData. Однако каждый раз, когда я запускаю свой код и делаю снимок для его чтения, я получаю эту ошибку OCR Error: Целью вызова было создано исключение. Любой совет хорош. Вот мой код [code] for the example. using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; using Newtonsoft.Json.Linq; using Tesseract; public class CaptureCards : MonoBehaviour { public RawImage cameraFeed; public Button captureButton; public Text cardDetailsText; private WebCamTexture webCamTexture; private HttpClient httpClient; // Start is called before the first frame update void Start() { StartCameraFeed(); httpClient = new HttpClient(); captureButton.onClick.AddListener(CaptureAndProcess); } void StartCameraFeed() { WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length == 0) { Debug.LogError("No camera devices found."); return; } // Log available cameras Debug.Log("Available cameras:"); foreach (var device in devices) { Debug.Log(device.name); } string selectedCamera = devices[0].name; Debug.Log("Using camera: " + selectedCamera); webCamTexture = new WebCamTexture(selectedCamera); cameraFeed.texture = webCamTexture; cameraFeed.material.mainTexture = webCamTexture; webCamTexture.Play(); } async void CaptureAndProcess() { if (webCamTexture == null || !webCamTexture.isPlaying) { cardDetailsText.text = "Camera feed not available."; return; } Texture2D captureImage = new Texture2D(webCamTexture.width, webCamTexture.height); captureImage.SetPixels(webCamTexture.GetPixels()); captureImage.Apply(); string tempPath = Path.Combine(Application.persistentDataPath, "captureCard.png"); Texture2D preprocessedImage = PreprocessImage(captureImage); File.WriteAllBytes(tempPath, preprocessedImage.EncodeToPNG()); Debug.Log("Image saved at: " + tempPath); string cardName = await RecognizeText(tempPath); if (!string.IsNullOrEmpty(cardName)) { JObject cardData = await FetchCardDetails(cardName); if (cardData != null) { DisplayCardDetails(cardData); SaveCardDetails(cardData); } else { cardDetailsText.text = "Card not Found"; } } else { cardDetailsText.text = "Failed to recognize card name"; } } Texture2D PreprocessImage(Texture2D original) { int targetWidth = 800; // Resize for better OCR accuracy int targetHeight = (int)(original.height * (800.0f / original.width)); Texture2D resizedImage = new Texture2D(targetWidth, targetHeight); for (int y = 0; y < targetHeight; y++) { for (int x = 0; x < targetWidth; x++) { Color pixel = original.GetPixelBilinear((float)x / targetWidth, (float)y / targetHeight); resizedImage.SetPixel(x, y, pixel); } } resizedImage.Apply(); // Convert to grayscale Color[] pixels = resizedImage.GetPixels(); for (int i = 0; i < pixels.Length; i++) { float gray = pixels[i].grayscale; pixels[i] = new Color(gray, gray, gray); } resizedImage.SetPixels(pixels); resizedImage.Apply(); return resizedImage; } async Task RecognizeText(string imagePath) { string resultText = string.Empty; string tessDataPath = Path.Combine(Application.streamingAssetsPath, "tessdata"); Debug.Log($"StreamingAssets Path: {Application.streamingAssetsPath}"); Debug.Log($"File Exists: {File.Exists(Path.Combine(Application.streamingAssetsPath, "tessdata", "eng.traineddata"))}"); await Task.Run(() => { try { if (!Directory.Exists(tessDataPath)) throw new DirectoryNotFoundException($"Tesseract data directory not found: {tessDataPath}"); if (!File.Exists(Path.Combine(tessDataPath, "eng.traineddata"))) throw new FileNotFoundException("Tesseract trained data file not found (eng.traineddata)."); if (!File.Exists(imagePath)) throw new FileNotFoundException($"Image file not found: {imagePath}"); using (var engine = new TesseractEngine(tessDataPath, "eng", EngineMode.Default)) { using (var img = Pix.LoadFromFile(imagePath)) { using (var page = engine.Process(img, PageSegMode.SingleBlock)) { resultText = page.GetText().Trim(); } } } } catch (Exception ex) { Debug.LogError($"OCR Error: {ex.Message}\n{ex.StackTrace}"); } }); return resultText; } async Task FetchCardDetails(string cardName) { try { string apiUrl = $"https://api.scryfall.com/cards/named?exact={UnityWebRequest.EscapeURL(cardName)}"; string response = await httpClient.GetStringAsync(apiUrl); return JObject.Parse(response); } catch { return null; } } void DisplayCardDetails(JObject cardData) { string name = cardData["name"].ToString(); string type = cardData["type_line"].ToString(); string manaCost = cardData["mana_cost"].ToString(); string description = cardData["oracle_text"].ToString(); cardDetailsText.text = $"Name: {name}\nType: {type}\nMana Cost: {manaCost}\nDescription: {description}"; } void SaveCardDetails(JObject cardData) { string savePath = Path.Combine(Application.persistentDataPath, "savedCards.json"); JArray savedCards = File.Exists(savePath) ? JArray.Parse(File.ReadAllText(savePath)) : new JArray(); savedCards.Add(cardData); File.WriteAllText(savePath, savedCards.ToString()); } void OnDestroy() { // Clean up resources webCamTexture.Stop(); httpClient.Dispose(); } } [/code] Чтобы исправить ошибку, из-за которой тессеракт не работает должным образом, чтобы я мог получить текст из изображения. Подробнее здесь: [url]https://stackoverflow.com/questions/79387438/installing-tesseract-on-unity-3d[/url]