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

updated model and added logs

parent 52481e7d
......@@ -11,4 +11,4 @@ host=localhost
port=8200
[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
ultralytics
paddleocr==2.7.0.3
paddlepaddle==2.5.2
#loguru==0.7.2
......@@ -3,103 +3,113 @@ from ultralytics import YOLO
import torch
import cv2
from scripts.config.app_configurations import *
from scripts.logging.application_logging import logger
device = 'cuda' if torch.cuda.is_available() else 'cpu'
# print(f'Using device: {device}')
def load_ocr_model():
"""
Load paddle OCR model
"""
ocr = PaddleOCR(use_angle_cls=True, lang='en')
logger.info("Loaded OCR model")
return ocr
def load_yolo_model():
"""
Load license plate detection model
"""
model = YOLO(MODEL_PATH).to(device)
logger.info("Loaded Yolo model")
return model
def predict(img_path, ocr, yolo_model):
img = cv2.imread(img_path)
results = yolo_model.predict(img, iou=0.7)
if results:
r1_txt = None
r2_txt = None
for r in results:
# annotator = Annotator(img)
boxes = r.boxes
for box in boxes:
b = box.xyxy[0] # get box coordinates in (top, left, bottom, right) format
# print("bb: ",b.tolist())
x1 = int(b[0])
y1 = int(b[1])
x2 = int(b[2])
y2 = int(b[3])
w = abs(x2 - x1)
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]
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:
result = ocr.ocr(lp, cls=True)
for idx in range(len(result)):
res = result[idx]
text_x = 10
text_y = 30
if res:
for ind, line in enumerate(res):
# print("line")
# print(line)
text_tuple = line[1]
if r1_txt is None:
r1_txt = text_tuple[0]
print("R1 TEXT: ", r1_txt)
text_conf = text_tuple[1]
elif int(c) == 1:
result = ocr.ocr(lp, cls=True)
for idx in range(len(result)):
res = result[idx]
text_x = 10
text_y = 30
if res:
for ind, line in enumerate(res):
# print("line")
# print(line)
text_tuple = line[1]
if r2_txt is None:
r2_txt = text_tuple[0]
print("R2 TEXT: ", r2_txt)
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.imshow('detected LPR', lp)
# cv2.waitKey(0)
#
if r1_txt is not None and r2_txt is not None:
LP = f"{r1_txt} {r2_txt}"
# print("LP: ", LP)
return LP
return "Image Not Clear"
# result = ocr.ocr(img_path, cls=False,det=True,rec=True)[0]
# txts = [line[1][0] for line in result]
try:
img = cv2.imread(img_path)
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:
r1_txt = None
r2_txt = None
for r in results:
boxes = r.boxes
for box in boxes:
b = box.xyxy[0] # get box coordinates in (top, left, bottom, right) format
# print("bb: ",b.tolist())
x1 = int(b[0])
y1 = int(b[1])
x2 = int(b[2])
y2 = int(b[3])
w = abs(x2 - x1)
h = abs(y2 - y1)
lp = img[y1:y1 + h, x1:x1 + w]
c = box.cls
if int(c) == 0:
#extracting text from r1
# cv2.imwrite("LP.png", lp)
result = ocr.ocr(lp, cls=True)
for idx in range(len(result)):
res = result[idx]
if res:
for ind, line in enumerate(res):
text_tuple = line[1]
if r1_txt is None:
r1_txt = text_tuple[0]
logger.info(f"r1 text : {r1_txt}")
text_conf = text_tuple[1]
elif int(c) == 1:
#extracting text from r2
result = ocr.ocr(lp, cls=True)
for idx in range(len(result)):
res = result[idx]
if res:
for ind, line in enumerate(res):
text_tuple = line[1]
if r2_txt is None:
r2_txt = text_tuple[0]
logger.info(f"r2 text : {r2_txt}")
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.imshow('detected LPR', lp)
# cv2.waitKey(0)
#
if r1_txt is not None and r2_txt is not None:
LP = f"{r1_txt} {r2_txt}"
logger.info(f"License plate reading : {LP}")
return LP
return "Image Not Clear"
except Exception as e:
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
from scripts.logging.application_logging import logger
from scripts.config.app_constants import TEXT_EXTRACT
ocr = load_ocr_model()
yolo_model = load_yolo_model()
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