Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
22_23-J-84
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
KSH.Mohamed
22_23-J-84
Commits
3d44300b
Commit
3d44300b
authored
May 26, 2023
by
Bogahawatta L.B.G.D.P.K
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
9d41b95a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
192 additions
and
0 deletions
+192
-0
DetectPlates.py
DetectPlates.py
+192
-0
No files found.
DetectPlates.py
0 → 100644
View file @
3d44300b
# DetectPlates.py
import
cv2
import
numpy
as
np
import
math
import
Main
import
random
import
Preprocess
import
DetectChars
import
PossiblePlate
import
PossibleChar
PLATE_WIDTH_PADDING_FACTOR
=
1.3
PLATE_HEIGHT_PADDING_FACTOR
=
1.5
def
detectPlatesInScene
(
imgOriginalScene
):
listOfPossiblePlates
=
[]
height
,
width
,
numChannels
=
imgOriginalScene
.
shape
imgGrayscaleScene
=
np
.
zeros
((
height
,
width
,
1
),
np
.
uint8
)
imgThreshScene
=
np
.
zeros
((
height
,
width
,
1
),
np
.
uint8
)
imgContours
=
np
.
zeros
((
height
,
width
,
3
),
np
.
uint8
)
cv2
.
destroyAllWindows
()
if
Main
.
showSteps
==
True
:
cv2
.
imshow
(
"0"
,
imgOriginalScene
)
imgGrayscaleScene
,
imgThreshScene
=
Preprocess
.
preprocess
(
imgOriginalScene
)
if
Main
.
showSteps
==
True
:
cv2
.
imshow
(
"1a"
,
imgGrayscaleScene
)
cv2
.
imshow
(
"1b"
,
imgThreshScene
)
listOfPossibleCharsInScene
=
findPossibleCharsInScene
(
imgThreshScene
)
if
Main
.
showSteps
==
True
:
print
(
"step 2 - len(listOfPossibleCharsInScene) = "
+
str
(
len
(
listOfPossibleCharsInScene
)))
imgContours
=
np
.
zeros
((
height
,
width
,
3
),
np
.
uint8
)
contours
=
[]
for
possibleChar
in
listOfPossibleCharsInScene
:
contours
.
append
(
possibleChar
.
contour
)
cv2
.
drawContours
(
imgContours
,
contours
,
-
1
,
Main
.
SCALAR_WHITE
)
cv2
.
imshow
(
"2b"
,
imgContours
)
listOfListsOfMatchingCharsInScene
=
DetectChars
.
findListOfListsOfMatchingChars
(
listOfPossibleCharsInScene
)
if
Main
.
showSteps
==
True
:
print
(
"step 3 - listOfListsOfMatchingCharsInScene.Count = "
+
str
(
len
(
listOfListsOfMatchingCharsInScene
)))
imgContours
=
np
.
zeros
((
height
,
width
,
3
),
np
.
uint8
)
for
listOfMatchingChars
in
listOfListsOfMatchingCharsInScene
:
intRandomBlue
=
random
.
randint
(
0
,
255
)
intRandomGreen
=
random
.
randint
(
0
,
255
)
intRandomRed
=
random
.
randint
(
0
,
255
)
contours
=
[]
for
matchingChar
in
listOfMatchingChars
:
contours
.
append
(
matchingChar
.
contour
)
cv2
.
drawContours
(
imgContours
,
contours
,
-
1
,
(
intRandomBlue
,
intRandomGreen
,
intRandomRed
))
cv2
.
imshow
(
"3"
,
imgContours
)
for
listOfMatchingChars
in
listOfListsOfMatchingCharsInScene
:
possiblePlate
=
extractPlate
(
imgOriginalScene
,
listOfMatchingChars
)
if
possiblePlate
.
imgPlate
is
not
None
:
listOfPossiblePlates
.
append
(
possiblePlate
)
print
(
"
\n
"
+
str
(
len
(
listOfPossiblePlates
))
+
" possible plates found"
)
if
Main
.
showSteps
==
True
:
print
(
"
\n
"
)
cv2
.
imshow
(
"4a"
,
imgContours
)
for
i
in
range
(
0
,
len
(
listOfPossiblePlates
)):
p2fRectPoints
=
cv2
.
boxPoints
(
listOfPossiblePlates
[
i
]
.
rrLocationOfPlateInScene
)
cv2
.
line
(
imgContours
,
tuple
(
p2fRectPoints
[
0
]),
tuple
(
p2fRectPoints
[
1
]),
Main
.
SCALAR_RED
,
2
)
cv2
.
line
(
imgContours
,
tuple
(
p2fRectPoints
[
1
]),
tuple
(
p2fRectPoints
[
2
]),
Main
.
SCALAR_RED
,
2
)
cv2
.
line
(
imgContours
,
tuple
(
p2fRectPoints
[
2
]),
tuple
(
p2fRectPoints
[
3
]),
Main
.
SCALAR_RED
,
2
)
cv2
.
line
(
imgContours
,
tuple
(
p2fRectPoints
[
3
]),
tuple
(
p2fRectPoints
[
0
]),
Main
.
SCALAR_RED
,
2
)
cv2
.
imshow
(
"4a"
,
imgContours
)
print
(
"possible plate "
+
str
(
i
)
+
", click on any image and press a key to continue . . ."
)
cv2
.
imshow
(
"4b"
,
listOfPossiblePlates
[
i
]
.
imgPlate
)
cv2
.
waitKey
(
0
)
print
(
"
\n
plate detection complete, click on any image and press a key to begin char recognition . . .
\n
"
)
cv2
.
waitKey
(
0
)
return
listOfPossiblePlates
def
findPossibleCharsInScene
(
imgThresh
):
listOfPossibleChars
=
[]
intCountOfPossibleChars
=
0
imgThreshCopy
=
imgThresh
.
copy
()
contours
,
npaHierarchy
=
cv2
.
findContours
(
imgThreshCopy
,
cv2
.
RETR_LIST
,
cv2
.
CHAIN_APPROX_SIMPLE
)
height
,
width
=
imgThresh
.
shape
imgContours
=
np
.
zeros
((
height
,
width
,
3
),
np
.
uint8
)
for
i
in
range
(
0
,
len
(
contours
)):
if
Main
.
showSteps
==
True
:
cv2
.
drawContours
(
imgContours
,
contours
,
i
,
Main
.
SCALAR_WHITE
)
possibleChar
=
PossibleChar
.
PossibleChar
(
contours
[
i
])
if
DetectChars
.
checkIfPossibleChar
(
possibleChar
):
intCountOfPossibleChars
=
intCountOfPossibleChars
+
1
listOfPossibleChars
.
append
(
possibleChar
)
if
Main
.
showSteps
==
True
:
print
(
"
\n
step 2 - len(contours) = "
+
str
(
len
(
contours
)))
print
(
"step 2 - intCountOfPossibleChars = "
+
str
(
intCountOfPossibleChars
))
cv2
.
imshow
(
"2a"
,
imgContours
)
return
listOfPossibleChars
def
extractPlate
(
imgOriginal
,
listOfMatchingChars
):
possiblePlate
=
PossiblePlate
.
PossiblePlate
()
listOfMatchingChars
.
sort
(
key
=
lambda
matchingChar
:
matchingChar
.
intCenterX
)
fltPlateCenterX
=
(
listOfMatchingChars
[
0
]
.
intCenterX
+
listOfMatchingChars
[
len
(
listOfMatchingChars
)
-
1
]
.
intCenterX
)
/
2.0
fltPlateCenterY
=
(
listOfMatchingChars
[
0
]
.
intCenterY
+
listOfMatchingChars
[
len
(
listOfMatchingChars
)
-
1
]
.
intCenterY
)
/
2.0
ptPlateCenter
=
fltPlateCenterX
,
fltPlateCenterY
intPlateWidth
=
int
((
listOfMatchingChars
[
len
(
listOfMatchingChars
)
-
1
]
.
intBoundingRectX
+
listOfMatchingChars
[
len
(
listOfMatchingChars
)
-
1
]
.
intBoundingRectWidth
-
listOfMatchingChars
[
0
]
.
intBoundingRectX
)
*
PLATE_WIDTH_PADDING_FACTOR
)
intTotalOfCharHeights
=
0
for
matchingChar
in
listOfMatchingChars
:
intTotalOfCharHeights
=
intTotalOfCharHeights
+
matchingChar
.
intBoundingRectHeight
fltAverageCharHeight
=
intTotalOfCharHeights
/
len
(
listOfMatchingChars
)
intPlateHeight
=
int
(
fltAverageCharHeight
*
PLATE_HEIGHT_PADDING_FACTOR
)
fltOpposite
=
listOfMatchingChars
[
len
(
listOfMatchingChars
)
-
1
]
.
intCenterY
-
listOfMatchingChars
[
0
]
.
intCenterY
fltHypotenuse
=
DetectChars
.
distanceBetweenChars
(
listOfMatchingChars
[
0
],
listOfMatchingChars
[
len
(
listOfMatchingChars
)
-
1
])
fltCorrectionAngleInRad
=
math
.
asin
(
fltOpposite
/
fltHypotenuse
)
fltCorrectionAngleInDeg
=
fltCorrectionAngleInRad
*
(
180.0
/
math
.
pi
)
possiblePlate
.
rrLocationOfPlateInScene
=
(
tuple
(
ptPlateCenter
),
(
intPlateWidth
,
intPlateHeight
),
fltCorrectionAngleInDeg
)
rotationMatrix
=
cv2
.
getRotationMatrix2D
(
tuple
(
ptPlateCenter
),
fltCorrectionAngleInDeg
,
1.0
)
height
,
width
,
numChannels
=
imgOriginal
.
shape
imgRotated
=
cv2
.
warpAffine
(
imgOriginal
,
rotationMatrix
,
(
width
,
height
))
imgCropped
=
cv2
.
getRectSubPix
(
imgRotated
,
(
intPlateWidth
,
intPlateHeight
),
tuple
(
ptPlateCenter
))
possiblePlate
.
imgPlate
=
imgCropped
return
possiblePlate
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment