Update src/license_plate_detector_ocr/infer.py
Browse files
src/license_plate_detector_ocr/infer.py
CHANGED
@@ -1,59 +1,63 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
import logging
|
4 |
-
import traceback
|
5 |
-
from inference.image_video_processor import process_image, process_video
|
6 |
-
|
7 |
-
# Append the current directory to sys.path
|
8 |
-
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
|
9 |
-
|
10 |
-
def is_image_file(file_path):
|
11 |
-
"""Check if the file is an image based on its extension."""
|
12 |
-
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff'}
|
13 |
-
return os.path.splitext(file_path)[1].lower() in image_extensions
|
14 |
-
|
15 |
-
def infer(input_path, output_path=None):
|
16 |
-
"""Process an image or video for license plate detection and OCR."""
|
17 |
-
model_path = "ckpts/yolo/finetune/runs/license_plate_detector/weights/best.pt"
|
18 |
-
|
19 |
-
logging.debug(f"Starting inference for {input_path} with output {output_path}")
|
20 |
-
|
21 |
-
if not os.path.exists(model_path):
|
22 |
-
error_msg = f"Error: Model file not found at {model_path}"
|
23 |
-
logging.error(error_msg)
|
24 |
-
print(error_msg)
|
25 |
-
return None, None
|
26 |
-
|
27 |
-
if not os.path.exists(input_path):
|
28 |
-
error_msg = f"Error: Input file not found at {input_path}"
|
29 |
-
logging.error(error_msg)
|
30 |
-
print(error_msg)
|
31 |
-
return None, None
|
32 |
-
|
33 |
-
try:
|
34 |
-
if is_image_file(input_path):
|
35 |
-
result_array, plate_texts = process_image(model_path, input_path, output_path)
|
36 |
-
else:
|
37 |
-
result_array, plate_texts = process_video(model_path, input_path, output_path)
|
38 |
-
|
39 |
-
if result_array is None:
|
40 |
-
error_msg = f"Error: Processing failed in {'process_image' if is_image_file(input_path) else 'process_video'} for {input_path}"
|
41 |
-
logging.error(error_msg)
|
42 |
-
print(error_msg)
|
43 |
-
return None, None
|
44 |
-
|
45 |
-
logging.debug(f"Inference successful: {len(plate_texts)} plates detected")
|
46 |
-
return result_array, plate_texts
|
47 |
-
except Exception as e:
|
48 |
-
error_msg = f"Error during inference for {input_path}: {str(e)}\n{traceback.format_exc()}"
|
49 |
-
logging.error(error_msg)
|
50 |
-
print(error_msg)
|
51 |
-
return None, None
|
52 |
-
|
53 |
-
if __name__ == "__main__":
|
54 |
-
import argparse
|
55 |
-
parser = argparse.ArgumentParser(description="Detect and read license plates in an image or video.")
|
56 |
-
parser.add_argument("--
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import logging
|
4 |
+
import traceback
|
5 |
+
from inference.image_video_processor import process_image, process_video
|
6 |
+
|
7 |
+
# Append the current directory to sys.path
|
8 |
+
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
|
9 |
+
|
10 |
+
def is_image_file(file_path):
|
11 |
+
"""Check if the file is an image based on its extension."""
|
12 |
+
image_extensions = {'.jpg', '.jpeg', '.png', '.bmp', '.tiff'}
|
13 |
+
return os.path.splitext(file_path)[1].lower() in image_extensions
|
14 |
+
|
15 |
+
def infer(model_path, input_path, output_path=None):
|
16 |
+
"""Process an image or video for license plate detection and OCR."""
|
17 |
+
# model_path = "ckpts/yolo/finetune/runs/license_plate_detector/weights/best.pt"
|
18 |
+
|
19 |
+
logging.debug(f"Starting inference for {input_path} with output {output_path}")
|
20 |
+
|
21 |
+
if not os.path.exists(model_path):
|
22 |
+
error_msg = f"Error: Model file not found at {model_path}"
|
23 |
+
logging.error(error_msg)
|
24 |
+
print(error_msg)
|
25 |
+
return None, None
|
26 |
+
|
27 |
+
if not os.path.exists(input_path):
|
28 |
+
error_msg = f"Error: Input file not found at {input_path}"
|
29 |
+
logging.error(error_msg)
|
30 |
+
print(error_msg)
|
31 |
+
return None, None
|
32 |
+
|
33 |
+
try:
|
34 |
+
if is_image_file(input_path):
|
35 |
+
result_array, plate_texts = process_image(model_path, input_path, output_path)
|
36 |
+
else:
|
37 |
+
result_array, plate_texts = process_video(model_path, input_path, output_path)
|
38 |
+
|
39 |
+
if result_array is None:
|
40 |
+
error_msg = f"Error: Processing failed in {'process_image' if is_image_file(input_path) else 'process_video'} for {input_path}"
|
41 |
+
logging.error(error_msg)
|
42 |
+
print(error_msg)
|
43 |
+
return None, None
|
44 |
+
|
45 |
+
logging.debug(f"Inference successful: {len(plate_texts)} plates detected")
|
46 |
+
return result_array, plate_texts
|
47 |
+
except Exception as e:
|
48 |
+
error_msg = f"Error during inference for {input_path}: {str(e)}\n{traceback.format_exc()}"
|
49 |
+
logging.error(error_msg)
|
50 |
+
print(error_msg)
|
51 |
+
return None, None
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
import argparse
|
55 |
+
parser = argparse.ArgumentParser(description="Detect and read license plates in an image or video.")
|
56 |
+
parser.add_argument("--model_path", type=str, default="ckpts/yolo/finetune/runs/license_plate_detector/weights/best.pt",
|
57 |
+
help="Path to the model file")
|
58 |
+
parser.add_argument("--input_path", type=str, required=True, help="Path to the input image or video file")
|
59 |
+
parser.add_argument("--output_path", type=str, default=None, help="Path to save the output file (optional)")
|
60 |
+
args = parser.parse_args()
|
61 |
+
|
62 |
+
result_array, plate_texts = infer(args.model_path, args.input_path, args.output_path)
|
63 |
+
|