-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict_cli.py
More file actions
58 lines (48 loc) · 1.72 KB
/
predict_cli.py
File metadata and controls
58 lines (48 loc) · 1.72 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
"""
Command-line interface for fish species prediction.
"""
import argparse
import json
import os
import sys
from predict import FishPredictor
def main():
"""
CLI for predicting fish species from an image.
Outputs prediction results as a JSON string.
"""
# Suppress model loading print statements for cleaner JSON output
original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
parser = argparse.ArgumentParser(description='Predict fish species from an image.')
parser.add_argument('image_path', help='Path to the image file.')
args = parser.parse_args()
# Restore stdout
sys.stdout.close()
sys.stdout = original_stdout
if not os.path.exists(args.image_path):
print(json.dumps({'error': f'Image not found at {args.image_path}'}))
return
try:
# Construct the model path relative to this script's location
script_dir = os.path.dirname(os.path.abspath(__file__))
model_path = os.path.join(script_dir, 'models', 'best_fish_model.pth')
if not os.path.exists(model_path):
print(json.dumps({'error': f'Model not found at {model_path}'}))
return
predictor = FishPredictor(model_path)
# Get only the top prediction
predictions = predictor.predict(args.image_path, top_k=1)
if predictions:
species, confidence = predictions[0]
result = {
'species': species,
'confidence': confidence
}
print(json.dumps(result))
else:
print(json.dumps({'error': 'Prediction failed.'}))
except Exception as e:
print(json.dumps({'error': str(e)}))
if __name__ == '__main__':
main()