-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect_plate1.py
74 lines (59 loc) · 2.05 KB
/
detect_plate1.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from charloc3 import identify
import numpy as np
import random
import keras
from keras import datasets, layers, models, preprocessing
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw
import json
import os
# get plate cropping model
json_file = open('model_v3.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
crop_plate = models.model_from_json(loaded_model_json)
crop_plate.load_weights("model_v3_3.h5")
# get char detect model
json_file = open('charcollectedvgg.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
char_detect = models.model_from_json(loaded_model_json)
char_detect.load_weights("charcollectedvgg.h5")
def detectplate(img_path):
# prediction of the bounding box
im = Image.open(img_path)
im.thumbnail((400, 300))
ini = np.array([np.asarray(im)]) / 255
out = crop_plate.predict(ini)[0]
# coords of bounding box
x1, y1, x2, y2= out[0] * im.size[0], out[1] * im.size[1], out[2] * im.size[0], out[3] * im.size[1]
im_h = abs(y1 - y2)
y1 -= im_h; y2 += im_h
# cropping image (top left, bottom right)
cropped = im.crop((x1 - 10, y1 - 10, x2 + 10, y2 + 10))
# convert the cropped plate thingy into a open cv thingy
open_cv_image = np.array(cropped)
# Convert RGB to BGR
open_cv_image = open_cv_image[:, :, ::-1].copy()
# get the box of each char
lis = identify(open_cv_image)
res = ''
# detect individual char
for coord in lis:
crop_char = cropped.crop((coord[0][0], coord[0][1], coord[1][0], coord[1][1]))
crop_char = crop_char.resize((20, 30))
# get prediction
ini = np.array([np.asarray(crop_char)]) / 255
out = char_detect.predict(ini)[0]
# identify prediction
dicta = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ'
maxi = 0
idx = 0
for i in range(0, len(out)):
if i == 0:
maxi = out[i]
elif(out[i] > maxi):
maxi = out[i]
idx = i
res = res + dicta[idx]
return res