-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
117 lines (102 loc) · 4.22 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from config_manager import load_config
from window_manager import find_window
from file_manager import read_first_line, list_images
from image_processing import find_image_on_screen, extract_text_from_image
from result_saver import save_results
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def process_image(img_file, folder, ocr_lang):
img_path = Path(folder) / img_file
logging.info(f"Processing image: {img_path}")
try:
location = find_image_on_screen(str(img_path))
if location:
try:
text = extract_text_from_image(str(img_path), language=ocr_lang)
logging.info(f"Extracted Text from {img_file}:\n{text}")
return (img_file, str(location), text)
except Exception as e:
logging.error(f"Error extracting text from image {img_file}: {e}")
else:
logging.warning(f"Image not found on screen: {img_file}")
except Exception as e:
logging.error(f"Error processing image {img_file}: {e}")
return None
def select_images(images):
print("Select images to search (separate numbers with commas, or enter 0 for all):")
for idx, img in enumerate(images, start=1):
print(f"{idx}: {img}")
selection = input("Enter your choice: ")
if selection == "0":
return images
try:
indices = list(map(int, selection.split(',')))
selected_images = [images[i - 1] for i in indices if 0 < i <= len(images)]
if not selected_images:
raise ValueError("No valid selections made.")
return selected_images
except (ValueError, IndexError) as e:
logging.error(f"Invalid selection: {e}")
return []
def select_ocr_language(default_lang):
print(f"Select OCR Language (default is '{default_lang}'):")
print("1: English")
print("2: Thai")
print("3: English + Thai")
lang_choice = input("Enter your choice (default is '3'): ").strip() or "3"
lang_map = {"1": "eng", "2": "tha", "3": "eng+tha"}
return lang_map.get(lang_choice, default_lang)
def main():
try:
config = load_config()
if config is None:
logging.error("Failed to load configuration.")
return
image_folder = config.get('Settings', 'image_folder')
default_ocr_language = config.get('Settings', 'ocr_language')
config_file = config.get('Settings', 'config_file')
window_title = read_first_line(config_file)
if not window_title:
logging.error("No window title found in config file.")
return
window_handle = find_window(window_title)
if not window_handle:
logging.error(f"Failed to find window with title: {window_title}")
return
images = list_images(image_folder)
if not images:
logging.error("No images found in the folder.")
return
selected_images = select_images(images)
if not selected_images:
logging.info("No images selected for processing.")
return
ocr_lang = select_ocr_language(default_ocr_language)
results = []
with ThreadPoolExecutor(max_workers=4) as executor:
futures = [executor.submit(process_image, img_file, image_folder, ocr_lang) for img_file in selected_images]
for future in futures:
result = future.result()
if result:
results.append(result)
if results:
save_results(results)
logging.info("Results saved successfully.")
else:
logging.info("No results to save.")
except Exception as e:
logging.error(f"An error occurred: {e}")
if __name__ == "__main__":
while True:
print("1: Start Program")
print("0: Exit Program")
choice = input("Enter your choice: ")
if choice == "1":
main()
elif choice == "0":
logging.info("Exiting program.")
break
else:
logging.warning("Invalid choice. Please enter 1 to start or 0 to exit.")