ChronoStellar
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import joblib
|
4 |
+
from skimage.feature import hog
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
MODEL_PATH = 'models/hog_lreg_model_4.pkl'
|
8 |
+
clf = joblib.load(MODEL_PATH)
|
9 |
+
|
10 |
+
def ocr(pil_image):
|
11 |
+
# Convert PIL image to OpenCV format
|
12 |
+
im = np.array(pil_image)
|
13 |
+
im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
|
14 |
+
|
15 |
+
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
|
16 |
+
ret, im_th = cv2.threshold(im_gray, 120, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
17 |
+
ctrs, hier = cv2.findContours(im_th, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
|
18 |
+
bboxes = [cv2.boundingRect(c) for c in ctrs]
|
19 |
+
sorted_bboxes = sorted(bboxes, key=lambda b: b[0]) # Sort by x-coordinate
|
20 |
+
|
21 |
+
plate_char = []
|
22 |
+
image_height, image_width = im.shape[:2]
|
23 |
+
height_threshold = image_height * 0.3
|
24 |
+
width_threshold = image_width * 0.3
|
25 |
+
|
26 |
+
for num, i_bboxes in enumerate(sorted_bboxes):
|
27 |
+
[x, y, w, h] = i_bboxes
|
28 |
+
if h > height_threshold and w < width_threshold:
|
29 |
+
roi = im_gray[y:y + h, x:x + w]
|
30 |
+
roi = cv2.resize(roi, (64, 128), interpolation=cv2.INTER_AREA)
|
31 |
+
roi_hog_fd = hog(roi, orientations=9, pixels_per_cell=(8, 8), cells_per_block=(1, 1))
|
32 |
+
nbr = clf.predict(np.array([roi_hog_fd]))
|
33 |
+
plate_char.append(str(nbr[0]))
|
34 |
+
|
35 |
+
return ''.join(plate_char)
|
36 |
+
|
37 |
+
# Create Gradio interface
|
38 |
+
interface = gr.Interface(
|
39 |
+
fn=ocr,
|
40 |
+
inputs=gr.Image(type="pil", label="Upload License Plate Image"),
|
41 |
+
outputs=gr.Textbox(label="Predicted License Plate"),
|
42 |
+
title="Automatic License Plate Recognition",
|
43 |
+
description="Upload an image of a license plate, and the system will predict the text on it.",
|
44 |
+
)
|
45 |
+
|
46 |
+
# Launch the Gradio app
|
47 |
+
interface.launch()
|