Commit a0bf954f authored by Bruce's avatar Bruce

add README

parent 03270e42
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
import cv2
import os
import hashlib
def verticalMappingToFolder(image):
name = hashlib.md5(image.data).hexdigest()[:8]
print(name)
cv2.imwrite("./cache/finemapping/"+name+".png",image)
#coding=utf-8
import cv2
import numpy as np
from . import niblack_thresholding as nt
from . import deskew
def fitLine_ransac(pts,zero_add = 0 ,col=300):
if len(pts)>=2:
[vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
# righty = int(((136- x) * vy / vx) + y)
righty = int(((col- x) * vy / vx) + y)
return lefty+30+zero_add,righty+30+zero_add
return 0,0
#精定位算法
def findContoursAndDrawBoundingBox(image_rgb,col=300):
# print('col',col)
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
# gray_image = cv2.equalizeHist(gray_image)
# for k in np.linspace(-1.5, -0.2,10):
for k in np.linspace(-50, 0, 15):
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
# cv2.imshow("image1",binary_niblack)
# cv2.waitKey(0)
contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
# if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200):
if (bdbox[3]/float(bdbox[2])>0.355 and bdbox[3]*bdbox[2]>630 and bdbox[3]*bdbox[2]<1700):
# cv2.rectangle(image_rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
# print('bdbox[3]=',bdbox[3],'bdbox[2]=',bdbox[2])
# line_upper.append([bdbox[0]-15,bdbox[1]-15])
# line_lower.append([bdbox[0]+bdbox[2]+10,bdbox[1]+bdbox[3]+10])
# line_upper.append([bdbox[0]-20,bdbox[1]-20])
# line_lower.append([bdbox[0]+bdbox[2]+15,bdbox[1]+bdbox[3]+15])
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),3,col)
# leftyA, rightyA = np.array(line_lower)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3,col)
# leftyB, rightyB = np.array(line_upper)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[300,60],[0,60],[300,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
# cv2.imshow("image_rgb",image_rgb)
image = cv2.warpPerspective(rgb,mat,(300,60),flags=cv2.INTER_CUBIC)
# gray_image = cv2.bitwise_not(image)
image,M = deskew.fastDeskew(image)
return image
#多级
def findContoursAndDrawBoundingBox2(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
for k in np.linspace(-1.6, -0.2,10):
# for k in np.linspace(-15, 0, 15):
# #
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = nt.niBlackThreshold(gray_image,19,k)
# cv2.imshow("binary_niblack_opencv",binary_niblack_)
# cv2.imshow("binary_niblack_skimage", binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
#size 5 22 area 110 rate 4.4
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[190,76],[0,76],[190,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(190,76),flags=cv2.INTER_CUBIC)
image,M= deskew.fastDeskew(image)
return image
#coding=utf-8
import cv2
import numpy as np
from . import niblack_thresholding as nt
from . import deskew
def fitLine_ransac(pts,zero_add = 0 ,col=300):
if len(pts)>=2:
[vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
# righty = int(((136- x) * vy / vx) + y)
righty = int(((col- x) * vy / vx) + y)
return lefty+30+zero_add,righty+30+zero_add
return 0,0
#精定位算法
def findContoursAndDrawBoundingBox(image_rgb,col=300):
# print('col',col)
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
# gray_image = cv2.equalizeHist(gray_image)
# for k in np.linspace(-1.5, -0.2,10):
for k in np.linspace(-50, 0, 15):
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
# cv2.imshow("image1",binary_niblack)
# cv2.waitKey(0)
contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
# if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200):
if (bdbox[3]/float(bdbox[2])>0.355 and bdbox[3]*bdbox[2]>630 and bdbox[3]*bdbox[2]<1700):
# cv2.rectangle(image_rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
# print('bdbox[3]=',bdbox[3],'bdbox[2]=',bdbox[2])
# line_upper.append([bdbox[0]-15,bdbox[1]-15])
# line_lower.append([bdbox[0]+bdbox[2]+10,bdbox[1]+bdbox[3]+10])
# line_upper.append([bdbox[0]-20,bdbox[1]-20])
# line_lower.append([bdbox[0]+bdbox[2]+15,bdbox[1]+bdbox[3]+15])
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),3,col)
# leftyA, rightyA = np.array(line_lower)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3,col)
# leftyB, rightyB = np.array(line_upper)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[300,60],[0,60],[300,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
# cv2.imshow("image_rgb",image_rgb)
image = cv2.warpPerspective(rgb,mat,(300,60),flags=cv2.INTER_CUBIC)
# print('image.shape:',image.shape)
# gray_image = cv2.bitwise_not(image)
image,M = deskew.fastDeskew(image)
return image
#多级
def findContoursAndDrawBoundingBox2(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
for k in np.linspace(-1.6, -0.2,10):
# for k in np.linspace(-15, 0, 15):
# #
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = nt.niBlackThreshold(gray_image,19,k)
# cv2.imshow("binary_niblack_opencv",binary_niblack_)
# cv2.imshow("binary_niblack_skimage", binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[190,76],[0,76],[190,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(190,76),flags=cv2.INTER_CUBIC)
image,M= deskew.fastDeskew(image)
return image
# -- coding: UTF-8
import cv2
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import os
boundaries = [
([100,80,0],[240,220,110]), # yellow
([0,40,50],[110,180,250]), # blue
([0,60,0],[60,160,70]), # green
]
color_attr = ["黄牌","蓝牌",'绿牌','白牌','黑牌']
threhold_green = 13
threhold_blue = 13
threhold_yellow1 = 50
threhold_yellow2 = 70
# plt.figure()
# plt.axis("off")
# plt.imshow(image)
# plt.show()
import numpy as np
def centroid_histogram(clt):
numLabels = np.arange(0, len(np.unique(clt.labels_)) + 1)
(hist, _) = np.histogram(clt.labels_, bins=numLabels)
# normalize the histogram, such that it sums to one
hist = hist.astype("float")
hist /= hist.sum()
# return the histogram
return hist
def plot_colors(hist, centroids):
bar = np.zeros((50, 300, 3), dtype="uint8")
startX = 0
for (percent, color) in zip(hist, centroids):
endX = startX + (percent * 300)
cv2.rectangle(bar, (int(startX), 0), (int(endX), 50),
color.astype("uint8").tolist(), -1)
startX = endX
# return the bar chart
return bar
def search_boundaries(color):
for i,color_bound in enumerate(boundaries):
if np.all(color >= color_bound[0]) and np.all(color <= color_bound[1]):
return i
return -1
def judge_color(color):
r = color[0]
g = color[1]
b = color[2]
if g - r >= threhold_green and g - b >= threhold_green:
return 2
if b - r >= threhold_blue and b - g >= threhold_blue:
return 1
if r- b > threhold_yellow2 and g - b > threhold_yellow2:
return 0
if r > 200 and b > 200 and g > 200:
return 3
if r < 50 and b < 50 and g < 50:
return 4
return -1
def judge_plate_color(img):
image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = image.reshape((image.shape[0] * image.shape[1], 3))
clt = KMeans(n_clusters=2)
clt.fit(image)
hist = centroid_histogram(clt)
index = np.argmax(hist)
#print clt.cluster_centers_[index]
#color_index = search_boundaries(clt.cluster_centers_[index])
color_index = judge_color(clt.cluster_centers_[index])
if color_index == -1:
if index == 0:
secound_index = 1
else:
secound_index = 0
color_index = judge_color(clt.cluster_centers_[secound_index])
if color_index == -1:
print(clt.cluster_centers_)
bar = plot_colors(hist, clt.cluster_centers_)
# show our color bart
plt.figure()
plt.axis("off")
plt.imshow(bar)
plt.show()
if color_index != -1:
return color_attr[color_index],clt.cluster_centers_[index]
else:
return None,clt.cluster_centers_[index]
\ No newline at end of file
import json
with open("/Users/universe/ProgramUniverse/zeusees/HyperLPR/config.json") as f:
configuration = json.load(f)
import cv2
import numpy as np
#import matplotlib.pyplot as plt
import time
t0=time.time()
def words(image):
#自動閥值二值化
ret,thresh1 = cv2.threshold(image,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
h=thresh1.shape[1]
w=thresh1.shape[0]
# plt.figure()
# plt.imshow(thresh1)
#取Y方向波
y_thresh1=np.zeros([36]).astype('int')
count_y=0
for i in range(w):
for j in range(h):
if thresh1[i,j]==255:
y_thresh1[i]+=1
count_y+=1
#投影 Y 軸影像
y_fig=np.zeros([36,136])
for i in range(w):
for j in range(h-y_thresh1[i],h):
y_fig[i,j]=1
# plt.figure()
# plt.imshow(y_fig)
#Y方向找點
Q1_y=136-(count_y//36)//2
def find_ypoint():
pos=[w,0]
bestP=[[],[]]
for i in range(w//2,0,-1):
if y_fig[i,Q1_y]==0: #StartPos 找波型於第 Q1_y rol 上
bestP[0].append(i) #之 pixel為 0 的位置
if y_fig[w-i,Q1_y]==0 : #EndPos 找波型於第 Q1_y rol 上
bestP[1].append(w-i) #之 pixel為 0 的位置
if bestP[0] and bestP[1]:
pos[1]=bestP[0][0]
pos[0]=bestP[1][0]
break;
elif bestP[0]:
pos[1]=bestP[0][0]
elif bestP[1]:
pos[0]=bestP[1][0]
return pos[1],pos[0]
#上下精定位車牌位置
s,e = find_ypoint()
print("上下定位擷取cols",s,"~",e)
newImg=np.zeros([(e-s)+1,136])
for i in range(s,e):
for j in range(newImg.shape[1]):
newImg[i-s,j]=thresh1[i,j]
# plt.figure()
# plt.imshow(newImg)
#取X方向波
h=newImg.shape[1]
w=newImg.shape[0]
x_thresh1=np.zeros([136]).astype('int')
for i in range(h):
for j in range(w):
if newImg[j,i]==255:
x_thresh1[i]+=1
#投影 X 軸影像
x_fig=np.zeros([w,h])
for i in range(h):
for j in range(w-x_thresh1[i],w):
x_fig[j,i]=1
if i == 0 or i==h: #前後預留白邊
x_fig[j,i]=1
# 形態 開 運算
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(6, 6))
x_fig = cv2.morphologyEx(x_fig, cv2.MORPH_OPEN, kernel)
#plt.figure()
#plt.imshow(x_fig)
#左右精定位車牌位置
def findXrange(x_fig):
bestP=[[],[]]
w=x_fig.shape[0]
for i in range(h):
if x_fig[w-1,i]==0:
bestP[0].append(i)
if x_fig[w-1,h-i-1]==0:
bestP[1].append(h-i-1)
if bestP[0] and bestP[1]:
return bestP[0][0],bestP[1][0]
leftP,rightP=findXrange(x_fig) #找到左右起始點
print("左右定位擷取 rols",leftP,"~",rightP)
x_fig1=np.zeros([(e-s)+1,(rightP-leftP)+1]) #精定位後的重新投影
for i in range(leftP,rightP):
for j in range(x_fig1.shape[0]):
x_fig1[j,i-leftP]=x_fig[j,i]
#plt.figure()
#plt.imshow(x_fig1)
#取 x_fig1 值
w=x_fig1.shape[0]
h=x_fig1.shape[1]
#print(w,h)
x_thresh2=np.zeros([h]).astype('int')
for i in range(h-1):
for j in range(w-1):
if x_fig1[j,i]==1:
x_thresh2[i]+=1
if i <= 1 or i>=134: # 預留黑邊 2 pixle
x_thresh2[i]=0
# X方向找點
point=0
pos=np.array([]).astype('int')
temp1=0
for j in range(h-1,0,-1):
if x_thresh2[j]<temp1 and x_thresh2[j]==0 :
point+=1
pos=np.append(pos,j)
elif x_thresh2[j]>temp1 and temp1==0 :
point+=1
pos=np.append(pos,j)
temp1=x_thresh2[j]
test=np.zeros([h])
test.fill(w)
pos=pos.astype('int32')
pos.sort()
for k,j in enumerate(pos,0):
test[j]=0
#結果
print("point",point,",共",(point//2),"字元") #扣除車牌中的 '點' *
print(pos)
print(time.time()-t0,'s')
return point//2,pos,e,s,rightP,leftP
def CutImg(image2,pos,e,s,rightP,leftP):
newImg=np.zeros([e-s,rightP-leftP])
# newImg=np.zeros([34-2,132-4])
for i in range(newImg.shape[0]):
for j in range(newImg.shape[1]):
newImg[i,j]=image2[i+s,j+leftP]
count=1
for Q in range(0,len(pos)-1,2):
cutImg1=np.zeros([e-s,pos[Q+1]-pos[Q]])
for i in range (cutImg1.shape[0]):
for j in range (cutImg1.shape[1]):
cutImg1[i,j]=newImg[i,j+pos[Q]]
#print(len(pos))
#print(cutImg1.shape[1])
if cutImg1.shape[1]<10 and Q<len(pos)-2:
judge=(23-cutImg1.shape[1])//2
resImg1=np.zeros([e-s,23])
for i in range (resImg1.shape[0]):
for j in range (resImg1.shape[1]):
resImg1[i,j]=newImg[i,j+pos[Q]-judge]
count+=1
cv2.imwrite("cut"+str(count)+".png", resImg1)
continue
count+=1
cv2.imwrite("cut"+str(count)+".png", cutImg1)
#coding=utf-8
import numpy as np
import cv2
import time
from matplotlib import pyplot as plt
import math
from scipy.ndimage import filters
#
# def strokeFiter():
# pass;
def angle(x,y):
return int(math.atan2(float(y),float(x))*180.0/3.1415)
def h_rot(src, angle, scale=1.0):
w = src.shape[1]
h = src.shape[0]
rangle = np.deg2rad(angle)
nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale
nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale
rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)
rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))
rot_mat[0,2] += rot_move[0]
rot_mat[1,2] += rot_move[1]
return cv2.warpAffine(src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4)
pass
def v_rot(img, angel, shape, max_angel):
size_o = [shape[1],shape[0]]
size = (shape[1]+ int(shape[0]*np.cos((float(max_angel )/180) * 3.14)),shape[0])
interval = abs( int( np.sin((float(angel) /180) * 3.14)* shape[0]))
pts1 = np.float32([[0,0],[0,size_o[1]],[size_o[0],0],[size_o[0],size_o[1]]])
if(angel>0):
pts2 = np.float32([[interval,0],[0,size[1] ],[size[0],0 ],[size[0]-interval,size_o[1]]])
else:
pts2 = np.float32([[0,0],[interval,size[1] ],[size[0]-interval,0 ],[size[0],size_o[1]]])
M = cv2.getPerspectiveTransform(pts1,pts2)
dst = cv2.warpPerspective(img,M,size)
return dst,M
def skew_detection(image_gray):
h, w = image_gray.shape[:2]
eigen = cv2.cornerEigenValsAndVecs(image_gray,12, 5)
angle_sur = np.zeros(180,np.uint)
eigen = eigen.reshape(h, w, 3, 2)
flow = eigen[:,:,2]
vis = image_gray.copy()
vis[:] = (192 + np.uint32(vis)) / 2
d = 12
points = np.dstack( np.mgrid[d/2:w:d, d/2:h:d] ).reshape(-1, 2)
for x, y in points:
vx, vy = np.int32(flow[int(y), int(x)]*d)
# cv2.line(rgb, (x-vx, y-vy), (x+vx, y+vy), (0, 355, 0), 1, cv2.LINE_AA)
ang = angle(vx,vy)
angle_sur[(ang+180)%180] +=1
# torr_bin = 30
angle_sur = angle_sur.astype(np.float)
angle_sur = (angle_sur-angle_sur.min())/(angle_sur.max()-angle_sur.min())
angle_sur = filters.gaussian_filter1d(angle_sur,5)
skew_v_val = angle_sur[20:180-20].max()
skew_v = angle_sur[30:180-30].argmax() + 30
skew_h_A = angle_sur[0:30].max()
skew_h_B = angle_sur[150:180].max()
skew_h = 0
if (skew_h_A > skew_v_val*0.3 or skew_h_B > skew_v_val*0.3):
if skew_h_A>=skew_h_B:
skew_h = angle_sur[0:20].argmax()
else:
skew_h = - angle_sur[160:180].argmax()
return skew_h,skew_v
def fastDeskew(image):
image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# image_gray = cv2.bitwise_not(image_gray)
skew_h,skew_v = skew_detection(image_gray)
# print("校正角度 h ",skew_h,"v",skew_v)
deskew,M = v_rot(image,int((90-skew_v)*1.5),image.shape,60)
return deskew,M
if __name__ == '__main__':
fn = './dataset/0.jpg'
img = cv2.imread(fn)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
skew_h,skew_v = skew_detection(gray)
img = v_rot(img,(90-skew_v ),img.shape,60)
# img = h_rot(img,skew_h)
# if img.shape[0]>img.shape[1]:
# img = h_rot(img, -90)
plt.show()
cv2.waitKey()
import cv2
import numpy as np
watch_cascade = cv2.CascadeClassifier('./model/cascade_haar_tw2.xml')
#watch_cascade = cv2.CascadeClassifier('./model/cascade_lbp_tw4.xml')
def computeSafeRegion(shape,bounding_rect):
top = bounding_rect[1] # y
bottom = bounding_rect[1] + bounding_rect[3] # y + h
left = bounding_rect[0] # x
right = bounding_rect[0] + bounding_rect[2] # x + w
min_top = 0
max_bottom = shape[0]
min_left = 0
max_right = shape[1]
# print "computeSateRegion input shape",shape
if top < min_top:
top = min_top
# print "tap top 0"
if left < min_left:
left = min_left
# print "tap left 0"
if bottom > max_bottom:
bottom = max_bottom
#print "tap max_bottom max"
if right > max_right:
right = max_right
#print "tap max_right max"
# print "corr",left,top,right,bottom
return [left,top,right-left,bottom-top]
def cropped_from_image(image,rect):
x, y, w, h = computeSafeRegion(image.shape,rect)
return image[y:y+h,x:x+w]
def detectPlateRough(image_gray,resize_h = 1000,en_scale =1.1 ,top_bottom_padding_rate = 0):
# print(image_gray.shape)
# if top_bottom_padding_rate>0.2:
# print("error:top_bottom_padding_rate > 0.2:",top_bottom_padding_rate)
# exit(1)
height = image_gray.shape[0]
padding = int(height*top_bottom_padding_rate)
scale = image_gray.shape[1]/float(image_gray.shape[0])
image = cv2.resize(image_gray, (int(scale*resize_h), resize_h))
image_color_cropped = image[padding:resize_h,0:image_gray.shape[1]]
# image_color_cropped = image
image_gray = cv2.cvtColor(image_color_cropped,cv2.COLOR_RGB2GRAY)
# image_gray = cv2.bitwise_not(image_gray)
watches = watch_cascade.detectMultiScale(image_gray, en_scale, 2, minSize=(31, 13),maxSize=(500,210))
cropped_images = []
for (x, y, w, h) in watches:
# 0329 測試擴增
# y -= h * 0.1
# h += h * 0.2;
cropped_origin = cropped_from_image(image_color_cropped, (int(x), int(y), int(w), int(h)))
# x -= w * 0.18
# w += w * 0.36
# 縮減
# x += w * 0.05
# w -= w * 0.1;
# correct
# x -= w * 0.12
# w += w * 0.24
# cropped = cropped_from_image(image_color_cropped, (int(x), int(y), int(w), int(h)))
# y -= h * 0.18
# h += h * 0.36;
# flagcrop = cropped_from_image(image_color_cropped, (int(x), int(y), int(w), int(h)))
# cropped = cropped_from_image(image_color_cropped, (int(x), int(y), int(w), int(h)))
cropped=[]
flagcrop=[]
cropped_images.append([cropped,[x, y+padding, w, h],cropped_origin,flagcrop])
# cropped_images.append([cropped,[x, y+padding, w, h],cropped_origin])
# cropped_images.append([cropped, [x, y, w,h],cropped_origin])
return cropped_images
#coding=utf-8
from keras import backend as K
from keras.models import load_model
from keras.layers import *
import numpy as np
import random
import string
import cv2
from . import e2emodel as model
chars = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
"琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z","港","学","使","警","澳","挂","军","北","南","广","沈","兰","成","济","海","民","航","空"
];
pred_model = model.construct_model("./model/ocr_plate_all_w_rnn_2.h5",)
import time
def fastdecode(y_pred):
results = ""
confidence = 0.0
table_pred = y_pred.reshape(-1, len(chars)+1)
res = table_pred.argmax(axis=1)
for i,one in enumerate(res):
if one<len(chars) and (i==0 or (one!=res[i-1])):
results+= chars[one]
confidence+=table_pred[i][one]
confidence/= len(results)
return results,confidence
def recognizeOne(src):
# x_tempx= cv2.imread(src)
x_tempx = src
# x_tempx = cv2.bitwise_not(x_tempx)
x_temp = cv2.resize(x_tempx,( 160,40))
x_temp = x_temp.transpose(1, 0, 2)
t0 = time.time()
y_pred = pred_model.predict(np.array([x_temp]))
y_pred = y_pred[:,2:,:]
# plt.imshow(y_pred.reshape(16,66))
# plt.show()
#
# cv2.imshow("x_temp",x_tempx)
# cv2.waitKey(0)
return fastdecode(y_pred)
#
#
# import os
#
# path = "/Users/yujinke/PycharmProjects/HyperLPR_Python_web/cache/finemapping"
# for filename in os.listdir(path):
# if filename.endswith(".png") or filename.endswith(".jpg") or filename.endswith(".bmp"):
# x = os.path.join(path,filename)
# recognizeOne(x)
# # print time.time() - t0
#
# # cv2.imshow("x",x)
# # cv2.waitKey()
from keras import backend as K
from keras.models import *
from keras.layers import *
from . import e2e
def ctc_lambda_func(args):
y_pred, labels, input_length, label_length = args
y_pred = y_pred[:, 2:, :]
return K.ctc_batch_cost(labels, y_pred, input_length, label_length)
def construct_model(model_path):
input_tensor = Input((None, 40, 3))
x = input_tensor
base_conv = 32
for i in range(3):
x = Conv2D(base_conv * (2 ** (i)), (3, 3),padding="same")(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Conv2D(256, (5, 5))(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(1024, (1, 1))(x)
x = BatchNormalization()(x)
x = Activation('relu')(x)
x = Conv2D(len(e2e.chars)+1, (1, 1))(x)
x = Activation('softmax')(x)
base_model = Model(inputs=input_tensor, outputs=x)
base_model.load_weights(model_path)
return base_model
#coding=utf-8
import cv2
import numpy as np
from . import niblack_thresholding as nt
from . import deskew
def fitLine_ransac(pts,zero_add = 0 ):
if len(pts)>=2:
[vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((136- x) * vy / vx) + y)
return lefty+30+zero_add,righty+30+zero_add
return 0,0
#精定位算法
def findContoursAndDrawBoundingBox(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
# for k in np.linspace(-1.5, -0.2,10):
for k in np.linspace(-50, 0, 15):
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
# cv2.imshow("image1",binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
# pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
# pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
# mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
# image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[190,76],[0,76],[190,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(190,76),flags=cv2.INTER_CUBIC)
image,M = deskew.fastDeskew(image)
return image
#多级
def findContoursAndDrawBoundingBox2(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
for k in np.linspace(-1.6, -0.2,10):
# for k in np.linspace(-15, 0, 15):
# #
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = nt.niBlackThreshold(gray_image,19,k)
# cv2.imshow("binary_niblack_opencv",binary_niblack_)
# cv2.imshow("binary_niblack_skimage", binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
image,M= deskew.fastDeskew(image)
return image
\ No newline at end of file
#coding=utf-8
import cv2
import numpy as np
from . import niblack_thresholding as nt
from . import deskew
def fitLine_ransac(pts,zero_add = 0 ):
if len(pts)>=2:
[vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
# righty = int(((136- x) * vy / vx) + y)
righty = int(((136- x) * vy / vx) + y)
return lefty+30+zero_add,righty+30+zero_add
return 0,0
#精定位算法
def findContoursAndDrawBoundingBox(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
# gray_image = cv2.equalizeHist(gray_image)
# for k in np.linspace(-1.5, -0.2,10):
for k in np.linspace(-50, 0, 15):
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
# cv2.imshow("image1",binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
# print('bdbox[3]=',bdbox[3],'bdbox[2]=',bdbox[2])
# line_upper.append([bdbox[0]-15,bdbox[1]-15])
# line_lower.append([bdbox[0]+bdbox[2]+10,bdbox[1]+bdbox[3]+10])
# line_upper.append([bdbox[0]-20,bdbox[1]-20])
# line_lower.append([bdbox[0]+bdbox[2]+15,bdbox[1]+bdbox[3]+15])
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[230,76],[0,76],[230,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(230,76),flags=cv2.INTER_CUBIC)
# gray_image = cv2.bitwise_not(image)
image,M = deskew.fastDeskew(image)
return image
#多级
def findContoursAndDrawBoundingBox2(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
for k in np.linspace(-1.6, -0.2,10):
# for k in np.linspace(-15, 0, 15):
# #
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = nt.niBlackThreshold(gray_image,19,k)
# cv2.imshow("binary_niblack_opencv",binary_niblack_)
# cv2.imshow("binary_niblack_skimage", binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[190,76],[0,76],[190,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(190,76),flags=cv2.INTER_CUBIC)
image,M= deskew.fastDeskew(image)
return image
#coding=utf-8
import cv2
import numpy as np
from . import niblack_thresholding as nt
from . import deskew
def fitLine_ransac(pts,zero_add = 0 ):
if len(pts)>=2:
[vx, vy, x, y] = cv2.fitLine(pts, cv2.DIST_HUBER, 0, 0.01, 0.01)
lefty = int((-x * vy / vx) + y)
righty = int(((136- x) * vy / vx) + y)
return lefty+30+zero_add,righty+30+zero_add
return 0,0
#精定位算法
def findContoursAndDrawBoundingBox(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
# for k in np.linspace(-1.5, -0.2,10):
for k in np.linspace(-50, 0, 15):
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = cv2.adaptiveThreshold(gray_image,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,17,k)
# cv2.imshow("image1",binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1200) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0]-15,bdbox[1]-15])
line_lower.append([bdbox[0]+bdbox[2]+10,bdbox[1]+bdbox[3]+10])
line_experiment.append([bdbox[0]-20,bdbox[1]-20])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-3)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[160,76],[0,76],[160,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(160,76),flags=cv2.INTER_CUBIC)
image,M = deskew.fastDeskew(image)
return image
#多级
def findContoursAndDrawBoundingBox2(image_rgb):
line_upper = [];
line_lower = [];
line_experiment = []
grouped_rects = []
gray_image = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
for k in np.linspace(-1.6, -0.2,10):
# for k in np.linspace(-15, 0, 15):
# #
# thresh_niblack = threshold_niblack(gray_image, window_size=21, k=k)
# binary_niblack = gray_image > thresh_niblack
# binary_niblack = binary_niblack.astype(np.uint8) * 255
binary_niblack = nt.niBlackThreshold(gray_image,19,k)
# cv2.imshow("binary_niblack_opencv",binary_niblack_)
# cv2.imshow("binary_niblack_skimage", binary_niblack)
# cv2.waitKey(0)
imagex, contours, hierarchy = cv2.findContours(binary_niblack.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
bdbox = cv2.boundingRect(contour)
if (bdbox[3]/float(bdbox[2])>0.7 and bdbox[3]*bdbox[2]>100 and bdbox[3]*bdbox[2]<1000) or (bdbox[3]/float(bdbox[2])>3 and bdbox[3]*bdbox[2]<100):
# cv2.rectangle(rgb,(bdbox[0],bdbox[1]),(bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]),(255,0,0),1)
line_upper.append([bdbox[0],bdbox[1]])
line_lower.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
line_experiment.append([bdbox[0],bdbox[1]])
line_experiment.append([bdbox[0]+bdbox[2],bdbox[1]+bdbox[3]])
# grouped_rects.append(bdbox)
rgb = cv2.copyMakeBorder(image_rgb,30,30,0,0,cv2.BORDER_REPLICATE)
leftyA, rightyA = fitLine_ransac(np.array(line_lower),2)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyA), (0, leftyA), (0, 0, 255), 1,cv2.LINE_AA)
leftyB, rightyB = fitLine_ransac(np.array(line_upper),-4)
rows,cols = rgb.shape[:2]
# rgb = cv2.line(rgb, (cols - 1, rightyB), (0, leftyB), (0,255, 0), 1,cv2.LINE_AA)
pts_map1 = np.float32([[cols - 1, rightyA], [0, leftyA],[cols - 1, rightyB], [0, leftyB]])
pts_map2 = np.float32([[136,36],[0,36],[136,0],[0,0]])
mat = cv2.getPerspectiveTransform(pts_map1,pts_map2)
image = cv2.warpPerspective(rgb,mat,(136,36),flags=cv2.INTER_CUBIC)
image,M= deskew.fastDeskew(image)
return image
#coding=utf-8
from keras.layers import Conv2D, Input,MaxPool2D, Reshape,Activation,Flatten, Dense
from keras.models import Model, Sequential
from keras.layers.advanced_activations import PReLU
from keras.optimizers import adam
import tensorflow as tf
import numpy as np
import cv2
def getModel():
# input = Input(shape=[17, 38, 3]) # change this shape to [None,None,3] to enable arbitraty shape input
input = Input(shape=[16, 66, 3]) # change this shape to [None,None,3] to enable arbitraty shape input
x = Conv2D(10, (3, 3), strides=1, padding='valid', name='conv1')(input)
x = Activation("relu", name='relu1')(x)
x = MaxPool2D(pool_size=2)(x)
x = Conv2D(16, (3, 3), strides=1, padding='valid', name='conv2')(x)
x = Activation("relu", name='relu2')(x)
x = Conv2D(32, (3, 3), strides=1, padding='valid', name='conv3')(x)
x = Activation("relu", name='relu3')(x)
x = Flatten()(x)
output = Dense(2,name = "dense")(x)
output = Activation("relu", name='relu4')(output)
model = Model([input], [output])
return model
def load_model():
global model
model.load_weights("./model/model12.h5")
# this is key : save the graph after loading the model
global graph
graph = tf.get_default_graph()
model = getModel()
load_model()
def getmodel():
return model
def gettest_model():
# input = Input(shape=[17, 38, 3]) # change this shape to [None,None,3] to enable arbitraty shape input
input = Input(shape=[16, 66, 3]) # change this shape to [None,None,3] to enable arbitraty shape input
A = Conv2D(10, (3, 3), strides=1, padding='valid', name='conv1')(input)
B = Activation("relu", name='relu1')(A)
C = MaxPool2D(pool_size=2)(B)
x = Conv2D(16, (3, 3), strides=1, padding='valid', name='conv2')(C)
x = Activation("relu", name='relu2')(x)
x = Conv2D(32, (3, 3), strides=1, padding='valid', name='conv3')(x)
K = Activation("relu", name='relu3')(x)
x = Flatten()(K)
dense = Dense(2,name = "dense")(x)
output = Activation("relu", name='relu4')(dense)
x = Model([input], [output])
x.load_weights("./model/model12.h5")
ok = Model([input], [dense])
for layer in ok.layers:
print(layer)
return ok
def finemappingVertical(image):
# resized = cv2.resize(image,(38,17))
resized = cv2.resize(image,(66,16))
resized = resized.astype(np.float)/255
with graph.as_default():
res= model.predict(np.array([resized]))[0]
# print("keras_predict",res)
res =res*image.shape[1]
res = res.astype(np.int)
H,T = res
H-=5
# H-=27
# print ('H=',H)
#3 79.86
#4 79.3
#5 79.5
#6 78.3
#T
#T+1 80.9
#T+2 81.75
#T+3 81.75
if H<0:
H=0
# T+=1;
T+=5;
if T>= image.shape[1]:
T= image.shape[1]
image = image[:,H:T]
# image = cv2.resize(image, (int(190), int(76)))
image = cv2.resize(image, (int(190), int(60)))
return image
\ No newline at end of file
import cv2
import numpy as np
def niBlackThreshold( src, blockSize, k, binarizationMethod= 0 ):
mean = cv2.boxFilter(src,cv2.CV_32F,(blockSize, blockSize),borderType=cv2.BORDER_REPLICATE)
sqmean = cv2.sqrBoxFilter(src, cv2.CV_32F, (blockSize, blockSize), borderType = cv2.BORDER_REPLICATE)
variance = sqmean - (mean*mean)
stddev = np.sqrt(variance)
thresh = mean + stddev * float(-k)
thresh = thresh.astype(src.dtype)
k = (src>thresh)*255
k = k.astype(np.uint8)
return k
# cv2.imshow()
\ No newline at end of file
This diff is collapsed.
#coding=utf-8
from . import detect
from . import finemapping as fm
from . import segmentation
import cv2
import time
import numpy as np
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import json
import sys
from . import typeDistinguish as td
import imp
imp.reload(sys)
fontC = ImageFont.truetype("./Font/platech.ttf", 14, 0);
from . import e2e
#寻找车牌左右边界
def find_edge(image):
sum_i = image.sum(axis=0)
sum_i = sum_i.astype(np.float)
sum_i/=image.shape[0]*255
# print sum_i
start= 0 ;
end = image.shape[1]-1
for i,one in enumerate(sum_i):
if one>0.4:
start = i;
if start-3<0:
start = 0
else:
start -=3
break;
for i,one in enumerate(sum_i[::-1]):
if one>0.4:
end = end - i;
if end+4>image.shape[1]-1:
end = image.shape[1]-1
else:
end+=4
break
return start,end
#垂直边缘检测
def verticalEdgeDetection(image):
image_sobel = cv2.Sobel(image.copy(),cv2.CV_8U,1,0)
# image = auto_canny(image_sobel)
# img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT
# canny_image = auto_canny(image)
flag,thres = cv2.threshold(image_sobel,0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY)
print(flag)
flag,thres = cv2.threshold(image_sobel,int(flag*0.7),255,cv2.THRESH_BINARY)
# thres = simpleThres(image_sobel)
kernal = np.ones(shape=(3,15))
thres = cv2.morphologyEx(thres,cv2.MORPH_CLOSE,kernal)
return thres
#确定粗略的左右边界
def horizontalSegmentation(image):
thres = verticalEdgeDetection(image)
# thres = thres*image
head,tail = find_edge(thres)
# print head,tail
# cv2.imshow("edge",thres)
tail = tail+5
if tail>135:
tail = 135
image = image[0:35,head:tail]
image = cv2.resize(image, (int(136), int(36)))
return image
#打上boundingbox和标签
def drawRectBox(image,rect,addText):
cv2.rectangle(image, (int(rect[0]), int(rect[1])), (int(rect[0] + rect[2]), int(rect[1] + rect[3])), (0,0, 255), 2, cv2.LINE_AA)
cv2.rectangle(image, (int(rect[0]-1), int(rect[1])-16), (int(rect[0] + 115), int(rect[1])), (0, 0, 255), -1, cv2.LINE_AA)
img = Image.fromarray(image)
draw = ImageDraw.Draw(img)
#draw.text((int(rect[0]+1), int(rect[1]-16)), addText.decode("utf-8"), (255, 255, 255), font=fontC)
draw.text((int(rect[0]+1), int(rect[1]-16)), addText, (255, 255, 255), font=fontC)
imagex = np.array(img)
return imagex
from . import cache
from . import finemapping_vertical as fv
def RecognizePlateJson(image):
images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
jsons = []
for j,plate in enumerate(images):
plate,rect,origin_plate =plate
res, confidence = e2e.recognizeOne(origin_plate)
print("res",res)
cv2.imwrite("./"+str(j)+"_rough.jpg",plate)
# print "车牌类型:",ptype
# plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
plate =cv2.resize(plate,(136,int(36*2.5)))
t1 = time.time()
ptype = td.SimplePredict(plate)
if ptype>0 and ptype<4:
plate = cv2.bitwise_not(plate)
# demo = verticalEdgeDetection(plate)
image_rgb = fm.findContoursAndDrawBoundingBox(plate)
image_rgb = fv.finemappingVertical(image_rgb)
cache.verticalMappingToFolder(image_rgb)
# print time.time() - t1,"校正"
print("e2e:",e2e.recognizeOne(image_rgb)[0])
image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_BGR2GRAY)
cv2.imwrite("./"+str(j)+".jpg",image_gray)
# image_gray = horizontalSegmentation(image_gray)
t2 = time.time()
res, confidence = e2e.recognizeOne(image_rgb)
res_json = {}
if confidence > 0.6:
res_json["Name"] = res
res_json["Type"] = td.plateType[ptype]
res_json["Confidence"] = confidence;
res_json["x"] = int(rect[0])
res_json["y"] = int(rect[1])
res_json["w"] = int(rect[2])
res_json["h"] = int(rect[3])
jsons.append(res_json)
print(json.dumps(jsons,ensure_ascii=False,encoding="gb2312"))
return json.dumps(jsons,ensure_ascii=False,encoding="gb2312")
def SimpleRecognizePlateByE2E(image):
t0 = time.time()
images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
res_set = []
for j,plate in enumerate(images):
plate, rect, origin_plate =plate
# plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
plate =cv2.resize(plate,(136,36*2))
res,confidence = e2e.recognizeOne(origin_plate)
print("res",res)
t1 = time.time()
ptype = td.SimplePredict(plate)
if ptype>0 and ptype<5:
# pass
plate = cv2.bitwise_not(plate)
image_rgb = fm.findContoursAndDrawBoundingBox(plate)
image_rgb = fv.finemappingVertical(image_rgb)
image_rgb = fv.finemappingVertical(image_rgb)
cache.verticalMappingToFolder(image_rgb)
cv2.imwrite("./"+str(j)+".jpg",image_rgb)
res,confidence = e2e.recognizeOne(image_rgb)
print(res,confidence)
res_set.append([[],res,confidence])
if confidence>0.7:
image = drawRectBox(image, rect, res+" "+str(round(confidence,3)))
return image,res_set
def SimpleRecognizePlate(image):
t0 = time.time()
images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
res_set = []
for j,plate in enumerate(images):
plate, rect, origin_plate =plate
# plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
plate =cv2.resize(plate,(136,36*2))
t1 = time.time()
ptype = td.SimplePredict(plate)
if ptype>0 and ptype<5:
plate = cv2.bitwise_not(plate)
image_rgb = fm.findContoursAndDrawBoundingBox(plate)
image_rgb = fv.finemappingVertical(image_rgb)
cache.verticalMappingToFolder(image_rgb)
print("e2e:", e2e.recognizeOne(image_rgb))
image_gray = cv2.cvtColor(image_rgb,cv2.COLOR_RGB2GRAY)
# image_gray = horizontalSegmentation(image_gray)
cv2.imshow("image_gray",image_gray)
# cv2.waitKey()
cv2.imwrite("./"+str(j)+".jpg",image_gray)
# cv2.imshow("image",image_gray)
# cv2.waitKey(0)
print("校正",time.time() - t1,"s")
# cv2.imshow("image,",image_gray)
# cv2.waitKey(0)
t2 = time.time()
val = segmentation.slidingWindowsEval(image_gray)
# print val
print("分割和识别",time.time() - t2,"s")
if len(val)==3:
blocks, res, confidence = val
if confidence>0.7:
image = drawRectBox(image,rect,res)
res_set.append(res)
for i,block in enumerate(blocks):
block_ = cv2.resize(block,(25,25))
block_ = cv2.cvtColor(block_,cv2.COLOR_GRAY2BGR)
image[j * 25:(j * 25) + 25, i * 25:(i * 25) + 25] = block_
if image[j*25:(j*25)+25,i*25:(i*25)+25].shape == block_.shape:
pass
if confidence>0:
print("车牌:",res,"置信度:",confidence/7)
else:
pass
# print "不确定的车牌:", res, "置信度:", confidence
print(time.time() - t0,"s")
return image,res_set
#coding=utf-8
from . import detect
from . import finemapping as fm
from . import segmentation
import cv2
import time
import numpy as np
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import json
import sys
from . import typeDistinguish as td
import imp
from . import cache
from . import finemapping_vertical as fv
imp.reload(sys)
fontC = ImageFont.truetype("./Font/platech.ttf", 14, 0);
from . import e2e
def find_edge(image):
sum_i = image.sum(axis=0)
sum_i = sum_i.astype(np.float)
sum_i/=image.shape[0]*255
# print sum_i
start= 0 ;
end = image.shape[1]-1
for i,one in enumerate(sum_i):
if one>0.4:
start = i;
if start-3<0:
start = 0
else:
start -=3
break;
for i,one in enumerate(sum_i[::-1]):
if one>0.4:
end = end - i;
if end+4>image.shape[1]-1:
end = image.shape[1]-1
else:
end+=4
break
return start,end
#垂直边缘检测
def verticalEdgeDetection(image):
image_sobel = cv2.Sobel(image.copy(),cv2.CV_8U,1,0)
# image = auto_canny(image_sobel)
# img_sobel, CV_8U, 1, 0, 3, 1, 0, BORDER_DEFAULT
# canny_image = auto_canny(image)
flag,thres = cv2.threshold(image_sobel,0,255,cv2.THRESH_OTSU|cv2.THRESH_BINARY)
print(flag)
flag,thres = cv2.threshold(image_sobel,int(flag*0.7),255,cv2.THRESH_BINARY)
# thres = simpleThres(image_sobel)
kernal = np.ones(shape=(3,15))
thres = cv2.morphologyEx(thres,cv2.MORPH_CLOSE,kernal)
return thres
#确定粗略的左右边界
def horizontalSegmentation(image):
thres = verticalEdgeDetection(image)
# thres = thres*image
head,tail = find_edge(thres)
# print head,tail
# cv2.imshow("edge",thres)
tail = tail+5
if tail>135:
tail = 135
image = image[0:35,head:tail]
image = cv2.resize(image, (int(136), int(36)))
return image
#打上boundingbox和标签
def drawRectBox(image,rect,addText):
cv2.rectangle(image, (int(rect[0]), int(rect[1])), (int(rect[0] + rect[2]), int(rect[1] + rect[3])), (0,0, 255), 2, cv2.LINE_AA)
cv2.rectangle(image, (int(rect[0]-1), int(rect[1])-16), (int(rect[0] + 115), int(rect[1])), (0, 0, 255), -1, cv2.LINE_AA)
img = Image.fromarray(image)
draw = ImageDraw.Draw(img)
#draw.text((int(rect[0]+1), int(rect[1]-16)), addText.decode("utf-8"), (255, 255, 255), font=fontC)
draw.text((int(rect[0]+1), int(rect[1]-16)), addText, (255, 255, 255), font=fontC)
imagex = np.array(img)
return imagex
def SimpleRecognizePlate(image):
t0 = time.time()
images = detect.detectPlateRough(image,image.shape[0],top_bottom_padding_rate=0.1)
res_set = []
for j,plate in enumerate(images):
plate, rect, origin_plate =plate
# plate = cv2.cvtColor(plate, cv2.COLOR_RGB2GRAY)
# plate =cv2.resize(plate,(136,36*2))
resize_h = 136
height = plate.shape[0]
scale = plate.shape[1]/float(plate.shape[0])
plate = cv2.resize(plate, (int(scale*resize_h), resize_h))
image_rgb = fm.findContoursAndDrawBoundingBox(plate)
image_rgb = fv.finemappingVertical(image_rgb)
cache.verticalMappingToFolder(image_rgb)
return image,res_set
\ No newline at end of file
#coding=utf-8
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D,MaxPool2D
from keras.optimizers import SGD
from keras import backend as K
K.set_image_dim_ordering('tf')
import tensorflow as tf
import cv2
import numpy as np
index = {"京": 0, "沪": 1, "津": 2, "渝": 3, "冀": 4, "晋": 5, "蒙": 6, "辽": 7, "吉": 8, "黑": 9, "苏": 10, "浙": 11, "皖": 12,
"闽": 13, "赣": 14, "鲁": 15, "豫": 16, "鄂": 17, "湘": 18, "粤": 19, "桂": 20, "琼": 21, "川": 22, "贵": 23, "云": 24,
"藏": 25, "陕": 26, "甘": 27, "青": 28, "宁": 29, "新": 30, "0": 31, "1": 32, "2": 33, "3": 34, "4": 35, "5": 36,
"6": 37, "7": 38, "8": 39, "9": 40, "A": 41, "B": 42, "C": 43, "D": 44, "E": 45, "F": 46, "G": 47, "H": 48,
"J": 49, "K": 50, "L": 51, "M": 52, "N": 53, "P": 54, "Q": 55, "R": 56, "S": 57, "T": 58, "U": 59, "V": 60,
"W": 61, "X": 62, "Y": 63, "Z": 64,"港":65,"学":66 ,"O":67 ,"使":68,"警":69,"澳":70,"挂":71};
chars = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
"琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P",
"Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z","港","学","O","使","警","澳","挂" ];
def Getmodel_tensorflow(nb_classes):
# nb_classes = len(charset)
img_rows, img_cols = 23, 23
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# x = np.load('x.npy')
# y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
# weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
# weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=(img_rows, img_cols,1)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(512, (3, 3)))
# model.add(Activation('relu'))
# model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
# model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
def Getmodel_ch(nb_classes):
# nb_classes = len(charset)
img_rows, img_cols = 23, 23
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# x = np.load('x.npy')
# y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
# weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
# weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=(img_rows, img_cols,1)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(512, (3, 3)))
# model.add(Activation('relu'))
# model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
# model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(756))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
def load_model():
global model
model.load_weights("./model/char_rec_QQ_3.h5")
# this is key : save the graph after loading the model
global graph
graph = tf.get_default_graph()
model = Getmodel_tensorflow(65)
#构建网络
# model_ch = Getmodel_ch(31)
# model_ch.load_weights("./model/char_chi_sim.h5")
# model_ch.save_weights("./model/char_chi_sim.h5")
# model.load_weights("./model/char_rec_QQ_3.h5")
load_model()
# model.save("./model/char_rec.h5")
def SimplePredict(image,pos,nums):
image = cv2.resize(image, (23, 23))
image = cv2.equalizeHist(image)
image = image.astype(np.float) / 255
image -= image.mean()
image = np.expand_dims(image, 3)
#cv2.imshow('image',image)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
zero_add = 0;
with graph.as_default():
res = np.array(model.predict(np.array([image]))[0])
res = res[31: 65]
zero_add = 31
'''
if nums==6:
if pos <=0: #第一個字為英文
res =res[31+10:65]
zero_add = 31+10;
else: #剩下5個為數字
res = res[31: 31+11]
zero_add = 31
else:
res = res[31: 65]
zero_add = 31
'''
'''
if pos!=0:
res = np.array(model.predict(np.array([image]))[0])
else:
res = np.array(model_ch.predict(np.array([image]))[0])
zero_add = 0 ;
if pos==0:
res = res[:31]
elif pos==1:
res = res[31+10:65]
zero_add = 31+10
else:
res = res[31:]
zero_add = 31
'''
max_id = res.argmax()
#print("res.max()=",res.max())
return res.max(),chars[max_id+zero_add],max_id+zero_add
#coding=utf-8
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D,MaxPool2D
from keras.optimizers import SGD
from keras import backend as K
K.set_image_dim_ordering('tf')
import cv2
import numpy as np
index = {"京": 0, "沪": 1, "津": 2, "渝": 3, "冀": 4, "晋": 5, "蒙": 6, "辽": 7, "吉": 8, "黑": 9, "苏": 10, "浙": 11, "皖": 12,
"闽": 13, "赣": 14, "鲁": 15, "豫": 16, "鄂": 17, "湘": 18, "粤": 19, "桂": 20, "琼": 21, "川": 22, "贵": 23, "云": 24,
"藏": 25, "陕": 26, "甘": 27, "青": 28, "宁": 29, "新": 30, "0": 31, "1": 32, "2": 33, "3": 34, "4": 35, "5": 36,
"6": 37, "7": 38, "8": 39, "9": 40, "A": 41, "B": 42, "C": 43, "D": 44, "E": 45, "F": 46, "G": 47, "H": 48,
"J": 49, "K": 50, "L": 51, "M": 52, "N": 53, "P": 54, "Q": 55, "R": 56, "S": 57, "T": 58, "U": 59, "V": 60,
"W": 61, "X": 62, "Y": 63, "Z": 64,"港":65,"学":66 ,"O":67 ,"使":68,"警":69,"澳":70,"挂":71};
#index = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5,"6": 6, "7": 7, "8": 8, "9": 9, "A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15, "G": 16, "H": 17,
# "J": 18, "K": 19, "L": 20, "M": 21, "N": 22, "P": 23, "Q": 24, "R": 25, "S": 26, "T": 27, "U": 28, "V": 29,
# "W": 30, "X": 31, "Y": 32, "Z": 33};
chars = ["京", "沪", "津", "渝", "冀", "晋", "蒙", "辽", "吉", "黑", "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "粤", "桂",
"琼", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",
"B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P",
"Q", "R", "S", "T", "U", "V", "W", "X",
"Y", "Z","港","学","O","使","警","澳","挂" ];
#chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A","B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P",
# "Q", "R", "S", "T", "U", "V", "W", "X","Y", "Z","O"];
def Getmodel_tensorflow(nb_classes):
# nb_classes = len(charset)
img_rows, img_cols = 23, 23
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# x = np.load('x.npy')
# y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
# weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
# weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=(img_rows, img_cols,1)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(512, (3, 3)))
# model.add(Activation('relu'))
# model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
# model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
def Getmodel_ch(nb_classes):
# nb_classes = len(charset)
img_rows, img_cols = 23, 23
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# x = np.load('x.npy')
# y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
# weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
# weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
model = Sequential()
model.add(Conv2D(32, (5, 5),input_shape=(img_rows, img_cols,1)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.25))
model.add(Conv2D(512, (3, 3)))
# model.add(Activation('relu'))
# model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
# model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(756))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
#model = Getmodel_tensorflow(65)
model = Getmodel_tensorflow(65)
#构建网络
model_ch = Getmodel_ch(31)
model_ch.load_weights("./model/char_chi_sim.h5")
#model_ch.load_weights("./model/char_rec.h5")
# model_ch.save_weights("./model/char_chi_sim.h5")
model.load_weights("./model/char_rec.h5")
# model.save("./model/char_rec.h5")
def SimplePredict(image,pos):
image = cv2.resize(image, (23, 23))
image = cv2.equalizeHist(image)
image = image.astype(np.float) / 255
image -= image.mean()
# cv2.imshow("image",image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
image = np.expand_dims(image, 3)
zero_add = 0 ;
res = np.array(model.predict(np.array([image]))[0])
if pos<=2:
zero_add = 41
res = res[31+10:65]
else:
zero_add = 31
res = res[31:42]
# res = np.array(model_ch.predict(np.array([image]))[0])
# if pos==0:
# res = res[:31]
# if pos==1:
# res = res[31+10:65]
# zero_add = 31+10
# else:
# res = res[31:]
max_id = res.argmax()
return res.max(),chars[max_id+zero_add],max_id+zero_add
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
import cv2
import os
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
import theano
import numpy as np
def norm(img,datashape=(13,55)):
img = cv2.resize(img, (datashape[1], datashape[0]));
#flag,img = cv2.threshold(img, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY)
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
cv2.THRESH_BINARY, 5, 0.5)
#
# cv2.imshow("imgx",img);
# cv2.waitKey(0)
# cv2.destroyAllWindows()
img = (img.astype(np.float32) / 255)
shape = img.shape
if len(shape) == 2:
img -= img.mean()
img = np.expand_dims(img, 2)
else:
img[:,:,1] -= img[:,:,1].mean()
return img
def loadData(pathT,pathF,datashape=(13,55)):
prepare_data =[]
prepare_label = [];
for parent,dirnames,filenames in os.walk(pathT):
for filename in filenames:
path = os.path.join(parent,filename)
if path.endswith(".jpg") or path.endswith(".png") or path.endswith(".bmp"):
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = norm(img,datashape)
prepare_data.append(img);
prepare_label.append([1,0]);
for parent, dirnames, filenames in os.walk(pathF):
for filename in filenames:
path = os.path.join(parent, filename)
if path.endswith(".jpg") or path.endswith(".png") or path.endswith(".bmp"):
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = norm(img,datashape)
prepare_data.append(img);
prepare_label.append([0,1]);
return np.array(prepare_data),np.array(prepare_label)
def arrangeData(data):
prepare_data, prepare_label = data;
rand = np.random.RandomState(321)
shuffle = rand.permutation(len(prepare_data))
shuffle = np.random.permutation(len(prepare_data));
digits, labels = prepare_data[shuffle], prepare_label[shuffle]
return digits,labels
def constructmodel(inputshape):
model = Sequential()
extract_conv1 = Convolution2D(8, 3, 3, border_mode='valid', input_shape=(inputshape))
model.add(extract_conv1)
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Convolution2D(16, 2, 2, border_mode='valid'))
model.add(Convolution2D(64, 1, 1, border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, init='normal'))
model.add(Activation('relu'))
model.add(Dropout(0.50))
model.add(Dense(32, init='normal'))
model.add(Activation('relu'))
model.add(Dense(2, init='normal'))
model.add(Activation('softmax'))
model.summary()
return model
def train(pathT,pathF):
model = constructmodel((13,55,1))
model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=["accuracy"])
data = loadData(pathT,pathF);
training_data,training_label = arrangeData(data)
print (training_label.shape,training_data.shape)
print (training_label)
# model.fit(training_data,training_label,nb_epoch=50,validation_split=0.1,show_accuracy=True)
model.fit(training_data,training_label,nb_epoch=10000,validation_split=0.1)
model.save("./forCNN_plate/judgeV3.h5")
#
#
#train("./forCNN_plate/trained_T","./forCNN_plate/trained_F")
#
\ No newline at end of file
#coding=utf-8
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPool2D
from keras.optimizers import SGD
from keras import backend as K
K.set_image_dim_ordering('tf')
import cv2
import numpy as np
plateType = ["蓝牌","单层黄牌","新能源车牌","白色","黑色-港澳"]
def Getmodel_tensorflow(nb_classes):
# nb_classes = len(charset)
img_rows, img_cols = 9, 34
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
# x = np.load('x.npy')
# y = np_utils.to_categorical(range(3062)*45*5*2, nb_classes)
# weight = ((type_class - np.arange(type_class)) / type_class + 1) ** 3
# weight = dict(zip(range(3063), weight / weight.mean())) # 调整权重,高频字优先
model = Sequential()
model.add(Conv2D(16, (5, 5),input_shape=(img_rows, img_cols,3)))
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=(nb_pool, nb_pool)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
model = Getmodel_tensorflow(5)
model.load_weights("./model/plate_type.h5")
model.save("./model/plate_type.h5")
def SimplePredict(image):
image = cv2.resize(image, (34, 9))
image = image.astype(np.float) / 255
res = np.array(model.predict(np.array([image]))[0])
return res.argmax()
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
File added
cascade_lbp_tw1 學長剪好的車牌訓練 1345 + 原生負樣本 4705 maxFalseAlarmRate 0.45
cascade_lbp_tw2 學長剪好的車牌訓練 1345 +自己剪的 339+ 原生負樣本 4705 maxFalseAlarmRate 0.45
cascade_lbp_tw3 學長剪好的車牌訓練 1095(扣掉沒剪好的)+自己剪的 339+自己剪的 382 maxFalseAlarmRate 0.4
cascade_lbp_tw4 學長剪好的車牌訓練 1095(扣掉沒剪好的)+自己剪的1717 負樣本原生4705+自己剪2649 pos 2777 samples train 2450 maxFalseAlarmRate 0.4
cascade_haar_tw 學長剪好的車牌訓練 1345 +自己剪的 382 + 原生負樣本 4705 maxFalseAlarmRate 0.45
cascade_haar_tw2 有留邊框車牌5565 + 網路上抓的車體負樣本8144 +自己從yolo切車出來再由粗定位將車牌遮掉9262 maxFalseAlarmRate 0.4
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment