-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_hieroglyph_analyzer.py
More file actions
executable file
·129 lines (109 loc) · 5.31 KB
/
test_hieroglyph_analyzer.py
File metadata and controls
executable file
·129 lines (109 loc) · 5.31 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
import os
import argparse
import time
from hieroglyph_analyzer import HieroglyphAnalyzer
def main():
"""Test the HieroglyphAnalyzer class"""
parser = argparse.ArgumentParser(description="Test HieroglyphAnalyzer")
parser.add_argument("--image", type=str, help="Path to a single test image")
parser.add_argument("--dir", type=str, default="./test_images", help="Directory of test images")
parser.add_argument("--model", type=str, default="./advanced_output/app_ready_model.h5",
help="Path to model file")
parser.add_argument("--output", type=str, default="./analyzer_output", help="Output directory")
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
args = parser.parse_args()
# Create output directory if needed
os.makedirs(args.output, exist_ok=True)
# Initialize the analyzer
print(f"Initializing HieroglyphAnalyzer with model: {args.model}")
try:
analyzer = HieroglyphAnalyzer(
model_path=args.model,
class_map_path="./advanced_output/class_mapping.json",
enable_debug=args.debug
)
print("Successfully initialized analyzer")
except Exception as e:
print(f"Failed to initialize analyzer: {str(e)}")
return
# Process a single image if provided
if args.image:
if not os.path.exists(args.image):
print(f"Error: Image file not found at {args.image}")
return
try:
print(f"Analyzing single image: {args.image}")
start_time = time.time()
results = analyzer.analyze_image(args.image, visualize=True)
elapsed = time.time() - start_time
print(f"Analysis completed in {elapsed:.2f} seconds")
print(f"Detected {results['detection']['count']} potential hieroglyphs")
print(f"Successfully recognized {results['recognition']['count']} hieroglyphs")
# Print recognized hieroglyphs
if results['results']:
print("\nRecognized Hieroglyphs:")
for i, result in enumerate(results['results']):
if result['predictions']:
top_pred = result['predictions'][0]
print(f" {i+1}. {top_pred['class_name']} - {top_pred['confidence']:.2f}")
print(f" {top_pred['info']['description']}")
# Save results
base_name = os.path.splitext(os.path.basename(args.image))[0]
# Save visualization
if 'visualization' in results:
import cv2
vis_path = os.path.join(args.output, f"{base_name}_analyzed.jpg")
# Convert RGB to BGR for OpenCV
vis_bgr = cv2.cvtColor(results['visualization'], cv2.COLOR_RGB2BGR)
cv2.imwrite(vis_path, vis_bgr)
print(f"Saved visualization to {vis_path}")
# Display the image if possible
try:
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 8))
plt.imshow(results['visualization'])
plt.title(f"Analyzed: {base_name} - Found {len(results['results'])} hieroglyphs")
# Add text about detection parameters
params_text = (
f"Detection Parameters:\n"
f"Window Sizes: {analyzer.detection_params['window_sizes']}\n"
f"Stride Factor: {analyzer.detection_params['stride_factor']}\n"
f"IoU Threshold: {analyzer.detection_params['iou_threshold']}\n"
f"Max Detections: {analyzer.detection_params['max_detections']}"
)
plt.figtext(0.02, 0.02, params_text,
bbox=dict(facecolor='white', alpha=0.8),
fontsize=9)
plt.axis('off')
plt.tight_layout()
plt.show()
except Exception as e:
print(f"Could not display image: {str(e)}")
except Exception as e:
print(f"Error analyzing image: {str(e)}")
import traceback
traceback.print_exc()
# Process a directory of images
elif args.dir and os.path.isdir(args.dir):
try:
print(f"Processing images in directory: {args.dir}")
start_time = time.time()
results = analyzer.batch_process(
args.dir,
args.output,
save_visualizations=True,
save_json=True
)
elapsed = time.time() - start_time
print(f"Processed {len(results)} images in {elapsed:.2f} seconds")
print(f"Results saved to {args.output}")
except Exception as e:
print(f"Error processing directory: {str(e)}")
import traceback
traceback.print_exc()
else:
print("Please provide either an image file with --image or a directory with --dir")
parser.print_help()
if __name__ == "__main__":
main()