Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
eshiled-new
/
my_flask_web
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 1e8314d6
authored
Jan 23, 2024
by
杨林龙
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
优化OCR识别效率
1 parent
14ef8b0f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
149 additions
and
8 deletions
Identify_vinandsn/main04.py
Identify_vinandsn/main05.py
Identify_vinandsn/main04.py
View file @
1e8314d
...
@@ -318,7 +318,7 @@ for index, row in enumerate(result):
...
@@ -318,7 +318,7 @@ for index, row in enumerate(result):
conn_dev
.
commit
()
conn_dev
.
commit
()
else
:
else
:
# 如果识别不了 直接再放大图片
# 如果识别不了 直接再放大图片
scale_factor
=
10
scale_factor
=
5
dst
=
original_image
.
resize
(
dst
=
original_image
.
resize
(
(
(
original_image
.
width
*
scale_factor
,
original_image
.
width
*
scale_factor
,
...
...
Identify_vinandsn/main05.py
View file @
1e8314d
...
@@ -12,6 +12,7 @@ import pymysql
...
@@ -12,6 +12,7 @@ import pymysql
import
requests
import
requests
from
paddleocr
import
PaddleOCR
,
draw_ocr
from
paddleocr
import
PaddleOCR
,
draw_ocr
from
PIL
import
Image
from
PIL
import
Image
from
PIL
import
ImageEnhance
from
pyzbar.pyzbar
import
decode
from
pyzbar.pyzbar
import
decode
# 连接database
# 连接database
...
@@ -75,8 +76,8 @@ ocr = PaddleOCR(
...
@@ -75,8 +76,8 @@ ocr = PaddleOCR(
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL
=
"""
SQL
=
"""
SELECT b.* FROM sys_vin_check_ocr_0
1
a LEFT JOIN sys_order_file_ocr b ON b.id = a.file_id
SELECT b.* FROM sys_vin_check_ocr_0
6
a LEFT JOIN sys_order_file_ocr b ON b.id = a.file_id
WHERE
a.ocr_vin IS NULL
;
WHERE
db_vin NOT LIKE '
%*%
' AND ocr_vin != db_vin OR ocr_vin IS NULL
;
"""
"""
# 执行SQL语句
# 执行SQL语句
cursor_dev
.
execute
(
SQL
)
cursor_dev
.
execute
(
SQL
)
...
@@ -139,16 +140,39 @@ def validate_vin(item):
...
@@ -139,16 +140,39 @@ def validate_vin(item):
def
extract_vin
(
string
):
def
extract_vin
(
string
):
string
=
re
.
sub
(
r"O"
,
"0"
,
string
)
string
=
re
.
sub
(
r"O"
,
"0"
,
string
)
string
=
re
.
sub
(
r"I"
,
"1"
,
string
)
string
=
re
.
sub
(
r"I"
,
"1"
,
string
)
# pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})"
pattern
=
r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z]{17})"
pattern
=
r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z\s\u4e00-\u9fa5]{17})"
# pattern = r"(?=.*[0-9A-HJ-NPR-Z])(?!(.*[IOQ]))([0-9A-HJ-NPR-Z\s\u4e00-\u9fa5]{17})"
# pattern = r"[0-9A-HJ-NPR-Z]{17}"
# pattern = r"^[A-HJ-NPR-Z\d]{17}$"
match
=
re
.
search
(
pattern
,
string
)
match
=
re
.
search
(
pattern
,
string
)
if
match
:
if
match
:
vin
=
match
.
group
()
vin
=
match
.
group
()
pattern01
=
r"^\d+$"
match
=
re
.
match
(
pattern01
,
vin
)
# 如果全是数字 则不是车架号
if
bool
(
match
):
return
None
else
:
return
vin
return
vin
else
:
else
:
return
None
return
None
def
flip_image_90
(
image
):
# 逆时针旋转90度
print
(
"逆时针旋转90度"
)
# rotated_image = cv2.transpose(image)
# rotated_image = cv2.flip(rotated_image, 0)
rotated_image
=
image
.
rotate
(
90
)
return
rotated_image
def
flip_image_180
(
image
):
# 旋转180度
rotated_image
=
cv2
.
flip
(
image
,
-
1
)
return
rotated_image
# 下载图片数据
# 下载图片数据
for
index
,
row
in
enumerate
(
result
):
for
index
,
row
in
enumerate
(
result
):
print
(
row
)
print
(
row
)
...
@@ -172,6 +196,13 @@ for index, row in enumerate(result):
...
@@ -172,6 +196,13 @@ for index, row in enumerate(result):
original_image
=
Image
.
open
(
path
)
original_image
=
Image
.
open
(
path
)
# 创建对比度增强器
# enhancer = ImageEnhance.Contrast(original_image)
# 增强对比度(将对比度增强为原来的2倍)
# factor = 2.0
# original_image = enhancer.enhance(factor)
# 放大图片
# 放大图片
scale_factor
=
3
# 放大倍数
scale_factor
=
3
# 放大倍数
# scaled_image = original_image.resize(
# scaled_image = original_image.resize(
...
@@ -190,7 +221,7 @@ for index, row in enumerate(result):
...
@@ -190,7 +221,7 @@ for index, row in enumerate(result):
result_list
=
flatten_list
(
result
)
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log
08
.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
with
open
(
"./log
11
.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
else
:
...
@@ -210,7 +241,117 @@ for index, row in enumerate(result):
...
@@ -210,7 +241,117 @@ for index, row in enumerate(result):
print
(
vin_str
)
print
(
vin_str
)
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL01
=
f
"""
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_03 (order_id,file_id,db_vin,ocr_vin,create_time)
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 旋转90度宠重新识别
dst
=
flip_image_90
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log11.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
dst
=
flip_image_90
(
dst
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log11.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
# 执行SQL语句
print
(
SQL01
)
cursor_dev
.
execute
(
SQL01
)
conn_dev
.
commit
()
else
:
# 如果识别不了 直接再放大图片
scale_factor
=
5
dst
=
original_image
.
resize
(
(
original_image
.
width
*
scale_factor
,
original_image
.
height
*
scale_factor
,
)
)
result
=
ocr
.
ocr
(
np
.
asarray
(
dst
),
cls
=
True
)
# result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
for
line
in
result
:
print
(
line
)
# print(line[0][1][0])
result_list
=
flatten_list
(
result
)
print
(
"result_list"
,
result_list
)
with
open
(
"./log11.txt"
,
"a+"
,
encoding
=
"utf-8"
)
as
file
:
if
result_list
is
not
None
:
file
.
write
(
path
+
"----"
+
str
(
result_list
)
+
"
\n
"
)
else
:
file
.
write
(
path
+
"----"
+
"NULL"
+
"
\n
"
)
vin_str
=
""
for
item
in
result_list
:
print
(
item
)
vin_check
=
extract_vin
(
item
)
if
vin_check
is
not
None
:
vin_str
=
vin_check
break
if
vin_str
:
print
(
str
(
row
))
print
(
vin_str
)
# 定义要执行的SQL语句
SQL01
=
f
"""
INSERT INTO sys_vin_check_ocr_12 (order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
VALUES ({row[1]},{row[0]},'{row[9]}','{vin_str}',sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
...
@@ -220,7 +361,7 @@ for index, row in enumerate(result):
...
@@ -220,7 +361,7 @@ for index, row in enumerate(result):
else
:
else
:
# 定义要执行的SQL语句
# 定义要执行的SQL语句
SQL02
=
f
"""
SQL02
=
f
"""
INSERT INTO sys_vin_check_ocr_03
(order_id,file_id,db_vin,ocr_vin,create_time)
INSERT INTO sys_vin_check_ocr_12
(order_id,file_id,db_vin,ocr_vin,create_time)
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
VALUES ({row[1]},{row[0]},'{row[9]}',null,sysdate())
"""
"""
# 执行SQL语句
# 执行SQL语句
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment