Commit 530e3fc6 authored by sikhin.vc's avatar sikhin.vc

updated model and added logs

parent 52481e7d
...@@ -11,4 +11,4 @@ host=localhost ...@@ -11,4 +11,4 @@ host=localhost
port=8200 port=8200
[PATH] [PATH]
model_path = scripts/utility/model/yolov8l_r1_r2_lp_best.pt model_path = scripts/utility/model/r1_r2_lpr_yolov8s.pt
...@@ -55,4 +55,5 @@ uvicorn==0.19.0 ...@@ -55,4 +55,5 @@ uvicorn==0.19.0
ultralytics ultralytics
paddleocr==2.7.0.3 paddleocr==2.7.0.3
paddlepaddle==2.5.2 paddlepaddle==2.5.2
#loguru==0.7.2
...@@ -3,28 +3,44 @@ from ultralytics import YOLO ...@@ -3,28 +3,44 @@ from ultralytics import YOLO
import torch import torch
import cv2 import cv2
from scripts.config.app_configurations import * from scripts.config.app_configurations import *
from scripts.logging.application_logging import logger
device = 'cuda' if torch.cuda.is_available() else 'cpu' device = 'cuda' if torch.cuda.is_available() else 'cpu'
# print(f'Using device: {device}')
def load_ocr_model(): def load_ocr_model():
"""
Load paddle OCR model
"""
ocr = PaddleOCR(use_angle_cls=True, lang='en') ocr = PaddleOCR(use_angle_cls=True, lang='en')
logger.info("Loaded OCR model")
return ocr return ocr
def load_yolo_model(): def load_yolo_model():
"""
Load license plate detection model
"""
model = YOLO(MODEL_PATH).to(device) model = YOLO(MODEL_PATH).to(device)
logger.info("Loaded Yolo model")
return model return model
def predict(img_path, ocr, yolo_model): def predict(img_path, ocr, yolo_model):
try:
img = cv2.imread(img_path) img = cv2.imread(img_path)
results = yolo_model.predict(img, iou=0.7)
results = yolo_model.predict(img, iou=0.7) # predict r1, r2 and license plate using yolo model r1 - single letter region in license plate, r2 - four or five letter region in license plate
if results: if results:
r1_txt = None r1_txt = None
r2_txt = None r2_txt = None
for r in results: for r in results:
# annotator = Annotator(img)
boxes = r.boxes boxes = r.boxes
for box in boxes: for box in boxes:
...@@ -37,53 +53,45 @@ def predict(img_path, ocr, yolo_model): ...@@ -37,53 +53,45 @@ def predict(img_path, ocr, yolo_model):
w = abs(x2 - x1) w = abs(x2 - x1)
h = abs(y2 - y1) h = abs(y2 - y1)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 1)
# lp = img[max(y1 - 30, 0):min(y1+h + 30, img.shape[0]), max(x1 - 30, 0):min(x1+w + 30, img.shape[1])]
lp = img[y1:y1 + h, x1:x1 + w] lp = img[y1:y1 + h, x1:x1 + w]
c = box.cls c = box.cls
# print("class: ", c)
# annotator.box_label(b, model.names[int(c)])
# img = annotator.result()
# cv2.imshow('YOLO V8 Detection', cv2.resize(img, (900, 600)))
if int(c) == 0: if int(c) == 0:
#extracting text from r1
# cv2.imwrite("LP.png", lp)
result = ocr.ocr(lp, cls=True) result = ocr.ocr(lp, cls=True)
for idx in range(len(result)): for idx in range(len(result)):
res = result[idx] res = result[idx]
text_x = 10
text_y = 30
if res: if res:
for ind, line in enumerate(res): for ind, line in enumerate(res):
# print("line")
# print(line)
text_tuple = line[1] text_tuple = line[1]
if r1_txt is None: if r1_txt is None:
r1_txt = text_tuple[0] r1_txt = text_tuple[0]
print("R1 TEXT: ", r1_txt) logger.info(f"r1 text : {r1_txt}")
text_conf = text_tuple[1] text_conf = text_tuple[1]
elif int(c) == 1: elif int(c) == 1:
#extracting text from r2
result = ocr.ocr(lp, cls=True) result = ocr.ocr(lp, cls=True)
for idx in range(len(result)): for idx in range(len(result)):
res = result[idx] res = result[idx]
text_x = 10
text_y = 30
if res: if res:
for ind, line in enumerate(res): for ind, line in enumerate(res):
# print("line")
# print(line)
text_tuple = line[1] text_tuple = line[1]
if r2_txt is None: if r2_txt is None:
r2_txt = text_tuple[0] r2_txt = text_tuple[0]
print("R2 TEXT: ", r2_txt) logger.info(f"r2 text : {r2_txt}")
text_conf = text_tuple[1] text_conf = text_tuple[1]
# cv2.putText(lp, txt, (text_x , text_y + ind*50), cv2.FONT_HERSHEY_SIMPLEX , 1, (255, 0, 0), 2, cv2.LINE_AA) # cv2.putText(lp, txt, (text_x , text_y + ind*50), cv2.FONT_HERSHEY_SIMPLEX , 1, (255, 0, 0), 2, cv2.LINE_AA)
...@@ -94,12 +102,14 @@ def predict(img_path, ocr, yolo_model): ...@@ -94,12 +102,14 @@ def predict(img_path, ocr, yolo_model):
if r1_txt is not None and r2_txt is not None: if r1_txt is not None and r2_txt is not None:
LP = f"{r1_txt} {r2_txt}" LP = f"{r1_txt} {r2_txt}"
# print("LP: ", LP) logger.info(f"License plate reading : {LP}")
return LP return LP
return "Image Not Clear" return "Image Not Clear"
# result = ocr.ocr(img_path, cls=False,det=True,rec=True)[0] except Exception as e:
# txts = [line[1][0] for line in result] logger.info(f"Error while reading license plate : {e}")
return f"Error while reading license plate : {e}"
......
...@@ -8,6 +8,7 @@ from scripts.core.handler.Text_Extraction_Handler import load_ocr_model,load_yol ...@@ -8,6 +8,7 @@ from scripts.core.handler.Text_Extraction_Handler import load_ocr_model,load_yol
from scripts.logging.application_logging import logger from scripts.logging.application_logging import logger
from scripts.config.app_constants import TEXT_EXTRACT from scripts.config.app_constants import TEXT_EXTRACT
ocr = load_ocr_model() ocr = load_ocr_model()
yolo_model = load_yolo_model() yolo_model = load_yolo_model()
router = APIRouter() router = APIRouter()
......
test.jpg

550 KB | W: | H:

test.jpg

414 KB | W: | H:

test.jpg
test.jpg
test.jpg
test.jpg
  • 2-up
  • Swipe
  • Onion skin
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment