fix(effi-ocr): improve file search and normalize language codes

- Better file finding (startswith + contains)
- Normalize language codes (French->fra, English->eng, etc.)
- List all files in directory when searching
This commit is contained in:
Folzo
2026-04-11 01:31:06 +02:00
parent 54aaea3df3
commit a8e9792412
+23 -18
View File
@@ -24,6 +24,16 @@ class Tools:
self.uploads_dir = "/app/backend/data/uploads" self.uploads_dir = "/app/backend/data/uploads"
self.host_uploads_dir = "/home/effi/effi/data/uploads" self.host_uploads_dir = "/home/effi/effi/data/uploads"
def _normalize_language(self, lang):
lang_map = {
"french": "fra", "francais": "fra", "fr": "fra",
"english": "eng", "eng": "eng",
"german": "deu", "de": "deu",
"spanish": "spa", "es": "spa",
"italian": "ita", "it": "ita"
}
return lang_map.get(lang.lower(), lang)
def extract_text_from_image(self, image_path, language="fra"): def extract_text_from_image(self, image_path, language="fra"):
try: try:
file_path = self._find_file(image_path) file_path = self._find_file(image_path)
@@ -32,8 +42,8 @@ class Tools:
return "[ERROR] File not found: " + image_path + ". Use list_recent_files() to see available files." return "[ERROR] File not found: " + image_path + ". Use list_recent_files() to see available files."
with open(file_path, 'rb') as f: with open(file_path, 'rb') as f:
files = {'file': f} files = {'file': (os.path.basename(file_path), f)}
data = {'lang': language, 'preprocess': 'true'} data = {'lang': self._normalize_language(language), 'preprocess': 'true'}
response = requests.post( response = requests.post(
self.ocr_url + "/ocr", self.ocr_url + "/ocr",
@@ -66,7 +76,10 @@ class Tools:
def list_recent_files(self, count=10): def list_recent_files(self, count=10):
try: try:
files = glob.glob(os.path.join(self.uploads_dir, "*")) files = []
if os.path.exists(self.uploads_dir):
files = glob.glob(os.path.join(self.uploads_dir, "*"))
if not files and os.path.exists(self.host_uploads_dir): if not files and os.path.exists(self.host_uploads_dir):
files = glob.glob(os.path.join(self.host_uploads_dir, "*")) files = glob.glob(os.path.join(self.host_uploads_dir, "*"))
@@ -104,22 +117,14 @@ class Tools:
if os.path.exists(path): if os.path.exists(path):
return path return path
for directory in [self.uploads_dir, self.host_uploads_dir]: search_dirs = [d for d in [self.uploads_dir, self.host_uploads_dir] if os.path.exists(d)]
if not os.path.exists(directory):
continue
pattern = os.path.join(directory, filename + "*") for directory in search_dirs:
matches = glob.glob(pattern) all_files = glob.glob(os.path.join(directory, "*"))
if matches: for f in all_files:
return matches[0] basename = os.path.basename(f)
if basename.startswith(filename) or filename in basename:
pattern = os.path.join(directory, "*" + filename + "*") return f
matches = glob.glob(pattern)
if matches:
for m in matches:
if filename in os.path.basename(m):
return m
return matches[0]
return None return None