AI-Naga commited on
Commit
a6033b1
·
1 Parent(s): d5d8055

Upload 6 files

Browse files
Files changed (6) hide show
  1. 1.jpg +0 -0
  2. a2.jpg +0 -0
  3. app.py +173 -0
  4. c1.jpg +0 -0
  5. requirements.txt +8 -0
  6. truck1.JPG +0 -0
1.jpg ADDED
a2.jpg ADDED
app.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%
2
+ # pip install torch torchvision torchaudio yolov5
3
+ import gradio as gr
4
+ import cv2
5
+ import requests
6
+ import os
7
+ import torch
8
+ import numpy as np
9
+ from ultralytics import YOLO
10
+ import yolov5
11
+ import easyocr
12
+ from paddleocr import PaddleOCR
13
+ import keras_ocr
14
+ import matplotlib.pyplot as plt
15
+ from paddleocr import draw_ocr
16
+ import pandas as pd
17
+
18
+ # %%
19
+ # Loading Yolo V5 model
20
+ model = yolov5.load('License_Plate_Model_Y5.pt')#, device="cpu")
21
+
22
+ # %%
23
+ def get_LicencePlate(frame, conf_threshold: gr.inputs.Slider = 0.1):
24
+
25
+ lst_plate_xyxy = []
26
+ # Setting model configuration
27
+ model.conf = conf_threshold
28
+
29
+ results = model(frame)
30
+ for index, row in results.pandas().xyxy[0].iterrows():
31
+ xmin = int(row['xmin'])
32
+ ymin = int(row['ymin'])
33
+ xmax = int(row['xmax'])
34
+ ymax = int(row['ymax'])
35
+ class_name=(row['name'])
36
+ if (class_name == 'license-plate'):
37
+ lst_plate_xyxy.append((xmin,ymin,xmax,ymax, class_name))
38
+
39
+ return lst_plate_xyxy
40
+
41
+ # %%
42
+ def extract_License_Number_PaddleOCR(image_path, conf_threshold):
43
+ image = cv2.imread(image_path)
44
+ # image = cv2.resize(image,(1020,800))
45
+ results = get_LicencePlate(image, conf_threshold)
46
+ PaddleOCR_output = []
47
+ # words = []
48
+ # boxes = []
49
+ # scores = []
50
+ for index in results:
51
+ x1 = index[0]
52
+ y1 = index[1]
53
+ x2 = index[2]
54
+ y2 = index[3]
55
+ class_name=(index[4])
56
+
57
+ cropped_img = image[y1:y2, x1:x2]
58
+
59
+ # processed_img = cropped_img
60
+ processed_img = cropped_img
61
+
62
+ # Perform OCR using PaddleOCR
63
+ paddle_ocr = PaddleOCR()
64
+ paddle_ocr_result = paddle_ocr.ocr(processed_img)
65
+
66
+ PaddleOCR_output.append(paddle_ocr_result)
67
+
68
+ print('\nPaddleOCR:')
69
+ print(''.join([text[1][0] + ' ' for text in paddle_ocr_result[0]]))
70
+
71
+ # Extract the words and bounding boxes from the OCR results
72
+ words = []
73
+ boxes = []
74
+ scores = []
75
+ for line in paddle_ocr_result:
76
+ for bbox in line:
77
+ words.append(bbox[1][0])
78
+ scores.append(bbox[1][1])
79
+ boxes.append(bbox[0])
80
+
81
+ output_image = cv2.rectangle(image,(x1,y1),(x2,y2),(0,0,255),2)
82
+ cv2.putText(output_image,str(words),(x1,y1),cv2.FONT_HERSHEY_PLAIN,2,(255,0,255),3)
83
+
84
+ return words, boxes, scores, output_image
85
+
86
+ # %%
87
+ def extract_License_Number_EasyOCR(image_path,conf_threshold ):
88
+ image = cv2.imread(image_path)
89
+ # image = cv2.resize(image,(1020,800))
90
+ results = get_LicencePlate(image ,conf_threshold=0.1 )
91
+ easyOCR_output = []
92
+ paddle_output = []
93
+
94
+ # print("results************************", results)
95
+ for index in results:
96
+ x1 = index[0]
97
+ y1 = index[1]
98
+ x2 = index[2]
99
+ y2 = index[3]
100
+ class_name=(index[4])
101
+
102
+ cropped_img = image[y1:y2, x1:x2]
103
+ processed_img = cropped_img
104
+
105
+ easyocr_reader = easyocr.Reader(['en'], verbose=False)
106
+ easyocr_result = easyocr_reader.readtext(processed_img)
107
+
108
+ easyOCR_output.append(easyocr_result)
109
+
110
+ print('\nEasyOCR:')
111
+ print(''.join([text[1] + ' ' for text in easyocr_result]))
112
+
113
+ # Extract the words and bounding boxes from the OCR results
114
+ words = []
115
+ boxes = []
116
+ scores = []
117
+ for line in easyocr_result:
118
+ words.append(line[1])
119
+ scores.append(line[2])
120
+ boxes.append(line[0])
121
+ output_image = cv2.rectangle(image,(x1,y1),(x2,y2),(0,0,255),2)
122
+ cv2.putText(output_image,str(words),(x1,y1),cv2.FONT_HERSHEY_PLAIN,1,(255,0,255),2)
123
+
124
+ return words, boxes, scores, output_image
125
+
126
+ # %%
127
+ def createDataframe(words, boxes, scores):
128
+ df = pd.DataFrame(list(zip(words, boxes, scores)), columns=['words', 'boxes', 'scores'])
129
+ return df
130
+
131
+ # %%
132
+ def initiate_Extract(image_path, conf_threshold: gr.inputs.Slider = 0.10, ocr_type="PaddleOCR"):
133
+
134
+ if ocr_type == "PaddleOCR":
135
+ words, boxes, scores, output_img = extract_License_Number_PaddleOCR(image_path, conf_threshold)
136
+ elif ocr_type == "EasyOCR":
137
+ words, boxes, scores, output_img = extract_License_Number_EasyOCR(image_path, conf_threshold)
138
+ else:
139
+ words, boxes, scores, output_img = extract_License_Number_PaddleOCR(image_path ,conf_threshold )
140
+
141
+ dataframe = createDataframe(words, boxes, scores)
142
+ return cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB), dataframe
143
+
144
+
145
+ # %%
146
+ import numpy as np
147
+ title = "License Plate Number Recognition using YOLO & Paddle OCR - EasyOCR"
148
+ description = ""
149
+
150
+ css = """.output_image, .input_image {height: 600px !important}"""
151
+ examples = [['truck1.jpg'],['c1.jpg']]
152
+ iface = gr.Interface(fn=initiate_Extract,
153
+ inputs=[
154
+ gr.inputs.Image(type="filepath", label="Input Image"),
155
+ gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.10, step=0.05, label="Confidence Threshold"),
156
+ gr.inputs.Dropdown(label="Select the OCR",default="PaddleOCR", choices=["PaddleOCR", "EasyOCR"]),
157
+ ],
158
+ outputs=[gr.outputs.Image(type="pil", label="annotated image"),"dataframe"] ,
159
+ title=title,
160
+ description=description,
161
+ examples=examples,
162
+ css=css,
163
+ analytics_enabled = True, enable_queue=True)
164
+
165
+ iface.launch(inline=False , debug=True)
166
+
167
+ # %%
168
+
169
+
170
+ # %%
171
+
172
+
173
+
c1.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio==3.4.0
2
+ opencv-python
3
+ numpy<1.24
4
+ ultralytics
5
+ yolov5
6
+ paddleocr
7
+ easyocr
8
+ torch
truck1.JPG ADDED