1.身份证号码识别(结果:身份证号识别结果为:911124198108030024)
import cv2
import numpy as np
import paddlehub as hub
def get_text ( ) :
img = cv2. imread( "images1/images/shenfen03.jpg" )
gray_img = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
gs_img = cv2. GaussianBlur( gray_img, ( 9 , 9 ) , 0 )
ero_img = cv2. erode( gs_img, np. ones( ( 11 , 11 ) , np. uint8) )
cany_img = cv2. Canny( ero_img, 70 , 300 )
cv2. imshow( "Canny Image" , cany_img)
contours, _ = cv2. findContours( cany_img, cv2. RETR_EXTERNAL, cv2. CHAIN_APPROX_NONE)
contour_img = np. zeros_like( img)
cv2. drawContours( contour_img, contours, - 1 , ( 255 , 255 , 255 ) , 2 )
cv2. imshow( "Contours" , contour_img)
for contour in contours:
x, y, w, h = cv2. boundingRect( contour)
print ( w, h)
if w > 200 and h < 70 :
cv2. rectangle( img, ( x, y) , ( x + w, y + h) , ( 0 , 255 , 0 ) , 2 )
out_img = img[ y: y + h, x: x + w]
cv2. imshow( "title" , img)
cv2. imshow( "title" , out_img)
cv2. waitKey( 0 )
ocr = hub. Module( name= "chinese_ocr_db_crnn_server" )
results = ocr. recognize_text( images= [ out_img] )
for result in results:
data = result[ 'data' ]
for x in data:
print ( '文本: ' , x[ 'text' ] )
cv2. destroyAllWindows( )
if __name__ == "__main__" :
get_text( )
2.车牌识别
import cv2
import numpy as np
import paddlehub as hub
def get_text ( ) :
img = cv2. imread( "images1/images/car6.png" )
gray_img = cv2. cvtColor( img, cv2. COLOR_BGR2GRAY)
eroded = cv2. morphologyEx( gray_img, cv2. MORPH_TOPHAT, np. ones( ( 9 , 9 ) , np. uint8) )
gs_img = cv2. GaussianBlur( eroded, ( 9 , 9 ) , 2 )
cany_img = cv2. Canny( gs_img, 170 , 180 )
eroded2 = cv2. dilate( cany_img, np. ones( ( 17 , 17 ) , np. uint8) , iterations= 2 )
contours, _ = cv2. findContours( eroded2, cv2. RETR_EXTERNAL, cv2. CHAIN_APPROX_NONE)
contour_img = np. zeros_like( img)
cv2. drawContours( contour_img, contours, - 1 , ( 255 , 255 , 255 ) , 2 )
cv2. imshow( "Contours" , contour_img)
for contour in contours:
x, y, w, h = cv2. boundingRect( contour)
print ( w, h)
if w > 20 and h > 20 :
cv2. rectangle( img, ( x, y) , ( x + w, y + h) , ( 0 , 255 , 0 ) , 2 )
out_img = img[ y: y + h, x: x + w]
cv2. imshow( "title" , out_img)
cv2. waitKey( 0 )
cv2. destroyAllWindows( )
ocr = hub. Module( name= "chinese_ocr_db_crnn_server" )
results = ocr. recognize_text( images= [ out_img] )
for result in results:
data = result[ 'data' ]
for x in data:
print ( '文本: ' , x[ 'text' ] )
if __name__ == "__main__" :
get_text( )