Upload 10 files
Browse files- .gitattributes +5 -0
- app.py +39 -3
- model2.py +61 -0
- models/large_LP_YOLOm_best.pt +3 -0
.gitattributes
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
models/charrec.pt filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
models/large_LP_YOLOm_best.pt filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
models/yolov8n_lp_det.pt filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
models/yolov8n_lpchar_det.pt filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
models/yolov8n.pt filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
|
@@ -1,18 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import model1 as m1
|
|
|
|
| 3 |
|
| 4 |
cars = []
|
| 5 |
lps = []
|
| 6 |
lp_texts = []
|
| 7 |
counter = 0
|
| 8 |
|
| 9 |
-
# this is the main function that passes the images to the model
|
| 10 |
-
def
|
| 11 |
global cars, lps, lp_texts, counter
|
| 12 |
(cars, lps, lp_texts) = m1.run([image])
|
| 13 |
counter = 0
|
| 14 |
return cars[0], lps[0], lp_texts[0]
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# function to go to next detected car licence plate
|
| 17 |
def next_img():
|
| 18 |
global counter
|
|
@@ -28,6 +36,20 @@ def prev_img():
|
|
| 28 |
return cars[index], lps[index], lp_texts[index]
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
# this code is responcible for the front end part of the page
|
| 32 |
with gr.Blocks() as demo:
|
| 33 |
gr.Markdown("## ANPR Project")
|
|
@@ -46,7 +68,7 @@ with gr.Blocks() as demo:
|
|
| 46 |
next = gr.Button(value="next")
|
| 47 |
prev = gr.Button(value="prev")
|
| 48 |
|
| 49 |
-
submit.click(
|
| 50 |
next.click(next_img, outputs=[car, lp, lp_text])
|
| 51 |
prev.click(prev_img, outputs=[car, lp, lp_text])
|
| 52 |
|
|
@@ -54,5 +76,19 @@ with gr.Blocks() as demo:
|
|
| 54 |
gr.Markdown("Using 2 different ML models")
|
| 55 |
gr.Markdown("YOLOv8m for car dection + easy ocr for text detection")
|
| 56 |
gr.Markdown("YOLOv8m for car dection is trained on a large dataset of 25K training images")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
demo.launch(share=False)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import model1 as m1
|
| 3 |
+
import model2 as m2
|
| 4 |
|
| 5 |
cars = []
|
| 6 |
lps = []
|
| 7 |
lp_texts = []
|
| 8 |
counter = 0
|
| 9 |
|
| 10 |
+
# this is the main function that passes the images to the model 1
|
| 11 |
+
def model1(image):
|
| 12 |
global cars, lps, lp_texts, counter
|
| 13 |
(cars, lps, lp_texts) = m1.run([image])
|
| 14 |
counter = 0
|
| 15 |
return cars[0], lps[0], lp_texts[0]
|
| 16 |
|
| 17 |
+
# this is the main function that passes the images to the model 1
|
| 18 |
+
def model2(image):
|
| 19 |
+
global lps, lp_texts, counter
|
| 20 |
+
(lps, lp_texts) = m2.run([image])
|
| 21 |
+
counter = 0
|
| 22 |
+
return lps[0], lp_texts[0]
|
| 23 |
+
|
| 24 |
# function to go to next detected car licence plate
|
| 25 |
def next_img():
|
| 26 |
global counter
|
|
|
|
| 36 |
return cars[index], lps[index], lp_texts[index]
|
| 37 |
|
| 38 |
|
| 39 |
+
# function to go to next detected licence plate
|
| 40 |
+
def next_img_lp():
|
| 41 |
+
global counter
|
| 42 |
+
counter += 1
|
| 43 |
+
index = int(counter % len(lps))
|
| 44 |
+
return lps[index], lp_texts[index]
|
| 45 |
+
|
| 46 |
+
# function to go to prev detected licence plate
|
| 47 |
+
def prev_img_lp():
|
| 48 |
+
global counter
|
| 49 |
+
counter -= 1
|
| 50 |
+
index = int(counter % len(lps))
|
| 51 |
+
return lps[index], lp_texts[index]
|
| 52 |
+
|
| 53 |
# this code is responcible for the front end part of the page
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
gr.Markdown("## ANPR Project")
|
|
|
|
| 68 |
next = gr.Button(value="next")
|
| 69 |
prev = gr.Button(value="prev")
|
| 70 |
|
| 71 |
+
submit.click(model1, inputs=[img], outputs=[car, lp, lp_text])
|
| 72 |
next.click(next_img, outputs=[car, lp, lp_text])
|
| 73 |
prev.click(prev_img, outputs=[car, lp, lp_text])
|
| 74 |
|
|
|
|
| 76 |
gr.Markdown("Using 2 different ML models")
|
| 77 |
gr.Markdown("YOLOv8m for car dection + easy ocr for text detection")
|
| 78 |
gr.Markdown("YOLOv8m for car dection is trained on a large dataset of 25K training images")
|
| 79 |
+
img2 = gr.Image(label="Input")
|
| 80 |
+
submit2 = gr.Button(value="submit")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
lp2 = gr.Image(label="Licence Plate")
|
| 84 |
+
lp_text2 = gr.Text(label="Plate Number")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
next2 = gr.Button(value="next")
|
| 88 |
+
prev2 = gr.Button(value="prev")
|
| 89 |
+
|
| 90 |
+
submit2.click(model2, inputs=[img2], outputs=[lp2, lp_text2])
|
| 91 |
+
next2.click(next_img_lp, outputs=[lp2, lp_text2])
|
| 92 |
+
prev2.click(prev_img_lp, outputs=[lp2, lp_text2])
|
| 93 |
|
| 94 |
demo.launch(share=False)
|
model2.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from model1 import reader, np, YOLO
|
| 2 |
+
|
| 3 |
+
lp_detection = YOLO("models/large_LP_YOLOm_best.pt")
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
# function to detect licence plates in the given car images
|
| 7 |
+
def detect_lp(inputs):
|
| 8 |
+
lps = []
|
| 9 |
+
# running the license plate detection model with 50% confidence threshold
|
| 10 |
+
lp_results = lp_detection.predict(source=inputs, conf=0.5, verbose=False)
|
| 11 |
+
# iterating through each output (num of outputs will be same as num of inputs)
|
| 12 |
+
for lp_result in lp_results:
|
| 13 |
+
# finding the bounding boxes of the license plate detected
|
| 14 |
+
lp_boxes = lp_result.boxes.xyxy.tolist()
|
| 15 |
+
# iterating through each license plate detected
|
| 16 |
+
for lp_box in lp_boxes:
|
| 17 |
+
# cropping license plate image from the car image
|
| 18 |
+
lp = lp_result.orig_img[int(lp_box[1]):int(lp_box[3]), int(lp_box[0]):int(lp_box[2])]
|
| 19 |
+
lps.append(lp)
|
| 20 |
+
# breaking as we only want to detect one licence plate per car
|
| 21 |
+
break
|
| 22 |
+
|
| 23 |
+
# if no licence plate is detected then we are adding a black image
|
| 24 |
+
if len(lp_boxes) == 0:
|
| 25 |
+
lps.append(np.zeros((100,100,3), np.uint8))
|
| 26 |
+
|
| 27 |
+
return lps
|
| 28 |
+
|
| 29 |
+
# function to detect licence plate number in the given licence plate images
|
| 30 |
+
def detect_lp_text(inputs):
|
| 31 |
+
plate_number = []
|
| 32 |
+
# iterating through each licence plate
|
| 33 |
+
for input in inputs:
|
| 34 |
+
# finding the number/text in licence plate
|
| 35 |
+
result = reader.readtext(input)
|
| 36 |
+
|
| 37 |
+
# if no text is found in the licence plate, then adding a default text not found
|
| 38 |
+
if len(result) == 0:
|
| 39 |
+
plate_number.append("not found")
|
| 40 |
+
else:
|
| 41 |
+
# adding the licence plate number to a list
|
| 42 |
+
plate_number.append(result[0][1])
|
| 43 |
+
|
| 44 |
+
return plate_number
|
| 45 |
+
|
| 46 |
+
def run(inputs):
|
| 47 |
+
|
| 48 |
+
# for future, to handle multiple inputs
|
| 49 |
+
# currently using just one input
|
| 50 |
+
inputs = inputs[0]
|
| 51 |
+
|
| 52 |
+
# detecting licence plates from the input images
|
| 53 |
+
# returns licence plate images, if it cant find a license plate a black image is returned
|
| 54 |
+
lps = detect_lp(inputs)
|
| 55 |
+
|
| 56 |
+
# detecting licence plate number from licence plate images
|
| 57 |
+
# returns text from the licence plate images, if none is detected "not found" text is returned
|
| 58 |
+
lp_text = detect_lp_text(lps)
|
| 59 |
+
|
| 60 |
+
return lps, lp_text
|
| 61 |
+
|
models/large_LP_YOLOm_best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b9600657c811e290bf3c92f0270c8c182d3ff57fa961a5e5bb82e6136ca1cb1b
|
| 3 |
+
size 207513151
|