Spaces:
Running
Running
ovi054
commited on
Commit
·
85c2996
1
Parent(s):
e778da4
1st commit
Browse files- .gitattributes +1 -0
- Tensorflow/scripts/generate_tfrecord.py +168 -0
- Tensorflow/workspace/annotations/.gitkeep +0 -0
- Tensorflow/workspace/annotations/label_map.pbtxt +212 -0
- Tensorflow/workspace/images/test/.gitkeep +0 -0
- Tensorflow/workspace/images/test/1 (2).jpg +0 -0
- Tensorflow/workspace/images/test/1 (2).xml +50 -0
- Tensorflow/workspace/images/train/.gitkeep +0 -0
- Tensorflow/workspace/images/train/1 (1).jpg +0 -0
- Tensorflow/workspace/images/train/1 (1).xml +38 -0
- Tensorflow/workspace/models/.gitkeep +0 -0
- Tensorflow/workspace/models/my_ssd_mobnet/checkpoint +16 -0
- Tensorflow/workspace/models/my_ssd_mobnet/ckpt-51.data-00000-of-00001 +3 -0
- Tensorflow/workspace/models/my_ssd_mobnet/ckpt-51.index +0 -0
- Tensorflow/workspace/models/my_ssd_mobnet/pipeline.config +191 -0
- app.py +543 -0
- requirements.txt +0 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
Tensorflow/scripts/generate_tfrecord.py
ADDED
@@ -0,0 +1,168 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
""" Sample TensorFlow XML-to-TFRecord converter
|
2 |
+
|
3 |
+
usage: generate_tfrecord.py [-h] [-x XML_DIR] [-l LABELS_PATH] [-o OUTPUT_PATH] [-i IMAGE_DIR] [-c CSV_PATH]
|
4 |
+
|
5 |
+
optional arguments:
|
6 |
+
-h, --help show this help message and exit
|
7 |
+
-x XML_DIR, --xml_dir XML_DIR
|
8 |
+
Path to the folder where the input .xml files are stored.
|
9 |
+
-l LABELS_PATH, --labels_path LABELS_PATH
|
10 |
+
Path to the labels (.pbtxt) file.
|
11 |
+
-o OUTPUT_PATH, --output_path OUTPUT_PATH
|
12 |
+
Path of output TFRecord (.record) file.
|
13 |
+
-i IMAGE_DIR, --image_dir IMAGE_DIR
|
14 |
+
Path to the folder where the input image files are stored. Defaults to the same directory as XML_DIR.
|
15 |
+
-c CSV_PATH, --csv_path CSV_PATH
|
16 |
+
Path of output .csv file. If none provided, then no file will be written.
|
17 |
+
"""
|
18 |
+
|
19 |
+
import os
|
20 |
+
import glob
|
21 |
+
import pandas as pd
|
22 |
+
import io
|
23 |
+
import xml.etree.ElementTree as ET
|
24 |
+
import argparse
|
25 |
+
|
26 |
+
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Suppress TensorFlow logging (1)
|
27 |
+
import tensorflow.compat.v1 as tf
|
28 |
+
from PIL import Image
|
29 |
+
from object_detection.utils import dataset_util, label_map_util
|
30 |
+
from collections import namedtuple
|
31 |
+
|
32 |
+
# Initiate argument parser
|
33 |
+
parser = argparse.ArgumentParser(
|
34 |
+
description="Sample TensorFlow XML-to-TFRecord converter")
|
35 |
+
parser.add_argument("-x",
|
36 |
+
"--xml_dir",
|
37 |
+
help="Path to the folder where the input .xml files are stored.",
|
38 |
+
type=str)
|
39 |
+
parser.add_argument("-l",
|
40 |
+
"--labels_path",
|
41 |
+
help="Path to the labels (.pbtxt) file.", type=str)
|
42 |
+
parser.add_argument("-o",
|
43 |
+
"--output_path",
|
44 |
+
help="Path of output TFRecord (.record) file.", type=str)
|
45 |
+
parser.add_argument("-i",
|
46 |
+
"--image_dir",
|
47 |
+
help="Path to the folder where the input image files are stored. "
|
48 |
+
"Defaults to the same directory as XML_DIR.",
|
49 |
+
type=str, default=None)
|
50 |
+
parser.add_argument("-c",
|
51 |
+
"--csv_path",
|
52 |
+
help="Path of output .csv file. If none provided, then no file will be "
|
53 |
+
"written.",
|
54 |
+
type=str, default=None)
|
55 |
+
|
56 |
+
args = parser.parse_args()
|
57 |
+
|
58 |
+
if args.image_dir is None:
|
59 |
+
args.image_dir = args.xml_dir
|
60 |
+
|
61 |
+
label_map = label_map_util.load_labelmap(args.labels_path)
|
62 |
+
label_map_dict = label_map_util.get_label_map_dict(label_map)
|
63 |
+
|
64 |
+
|
65 |
+
def xml_to_csv(path):
|
66 |
+
"""Iterates through all .xml files (generated by labelImg) in a given directory and combines
|
67 |
+
them in a single Pandas dataframe.
|
68 |
+
|
69 |
+
Parameters:
|
70 |
+
----------
|
71 |
+
path : str
|
72 |
+
The path containing the .xml files
|
73 |
+
Returns
|
74 |
+
-------
|
75 |
+
Pandas DataFrame
|
76 |
+
The produced dataframe
|
77 |
+
"""
|
78 |
+
|
79 |
+
xml_list = []
|
80 |
+
for xml_file in glob.glob(path + '/*.xml'):
|
81 |
+
tree = ET.parse(xml_file)
|
82 |
+
root = tree.getroot()
|
83 |
+
for member in root.findall('object'):
|
84 |
+
value = (root.find('filename').text,
|
85 |
+
int(root.find('size')[0].text),
|
86 |
+
int(root.find('size')[1].text),
|
87 |
+
member[0].text,
|
88 |
+
int(member[4][0].text),
|
89 |
+
int(member[4][1].text),
|
90 |
+
int(member[4][2].text),
|
91 |
+
int(member[4][3].text)
|
92 |
+
)
|
93 |
+
xml_list.append(value)
|
94 |
+
column_name = ['filename', 'width', 'height',
|
95 |
+
'class', 'xmin', 'ymin', 'xmax', 'ymax']
|
96 |
+
xml_df = pd.DataFrame(xml_list, columns=column_name)
|
97 |
+
return xml_df
|
98 |
+
|
99 |
+
|
100 |
+
def class_text_to_int(row_label):
|
101 |
+
return label_map_dict[row_label]
|
102 |
+
|
103 |
+
|
104 |
+
def split(df, group):
|
105 |
+
data = namedtuple('data', ['filename', 'object'])
|
106 |
+
gb = df.groupby(group)
|
107 |
+
return [data(filename, gb.get_group(x)) for filename, x in zip(gb.groups.keys(), gb.groups)]
|
108 |
+
|
109 |
+
|
110 |
+
def create_tf_example(group, path):
|
111 |
+
with tf.gfile.GFile(os.path.join(path, '{}'.format(group.filename)), 'rb') as fid:
|
112 |
+
encoded_jpg = fid.read()
|
113 |
+
encoded_jpg_io = io.BytesIO(encoded_jpg)
|
114 |
+
image = Image.open(encoded_jpg_io)
|
115 |
+
width, height = image.size
|
116 |
+
|
117 |
+
filename = group.filename.encode('utf8')
|
118 |
+
image_format = b'jpg'
|
119 |
+
xmins = []
|
120 |
+
xmaxs = []
|
121 |
+
ymins = []
|
122 |
+
ymaxs = []
|
123 |
+
classes_text = []
|
124 |
+
classes = []
|
125 |
+
|
126 |
+
for index, row in group.object.iterrows():
|
127 |
+
xmins.append(row['xmin'] / width)
|
128 |
+
xmaxs.append(row['xmax'] / width)
|
129 |
+
ymins.append(row['ymin'] / height)
|
130 |
+
ymaxs.append(row['ymax'] / height)
|
131 |
+
classes_text.append(row['class'].encode('utf8'))
|
132 |
+
classes.append(class_text_to_int(row['class']))
|
133 |
+
|
134 |
+
tf_example = tf.train.Example(features=tf.train.Features(feature={
|
135 |
+
'image/height': dataset_util.int64_feature(height),
|
136 |
+
'image/width': dataset_util.int64_feature(width),
|
137 |
+
'image/filename': dataset_util.bytes_feature(filename),
|
138 |
+
'image/source_id': dataset_util.bytes_feature(filename),
|
139 |
+
'image/encoded': dataset_util.bytes_feature(encoded_jpg),
|
140 |
+
'image/format': dataset_util.bytes_feature(image_format),
|
141 |
+
'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
|
142 |
+
'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
|
143 |
+
'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
|
144 |
+
'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
|
145 |
+
'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
|
146 |
+
'image/object/class/label': dataset_util.int64_list_feature(classes),
|
147 |
+
}))
|
148 |
+
return tf_example
|
149 |
+
|
150 |
+
|
151 |
+
def main(_):
|
152 |
+
|
153 |
+
writer = tf.python_io.TFRecordWriter(args.output_path)
|
154 |
+
path = os.path.join(args.image_dir)
|
155 |
+
examples = xml_to_csv(args.xml_dir)
|
156 |
+
grouped = split(examples, 'filename')
|
157 |
+
for group in grouped:
|
158 |
+
tf_example = create_tf_example(group, path)
|
159 |
+
writer.write(tf_example.SerializeToString())
|
160 |
+
writer.close()
|
161 |
+
print('Successfully created the TFRecord file: {}'.format(args.output_path))
|
162 |
+
if args.csv_path is not None:
|
163 |
+
examples.to_csv(args.csv_path, index=None)
|
164 |
+
print('Successfully created the CSV file: {}'.format(args.csv_path))
|
165 |
+
|
166 |
+
|
167 |
+
if __name__ == '__main__':
|
168 |
+
tf.app.run()
|
Tensorflow/workspace/annotations/.gitkeep
ADDED
File without changes
|
Tensorflow/workspace/annotations/label_map.pbtxt
ADDED
@@ -0,0 +1,212 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
item {
|
2 |
+
name:'1'
|
3 |
+
id:1
|
4 |
+
}
|
5 |
+
item {
|
6 |
+
name:'2'
|
7 |
+
id:2
|
8 |
+
}
|
9 |
+
item {
|
10 |
+
name:'4'
|
11 |
+
id:3
|
12 |
+
}
|
13 |
+
item {
|
14 |
+
name:'7'
|
15 |
+
id:4
|
16 |
+
}
|
17 |
+
item {
|
18 |
+
name:'9'
|
19 |
+
id:5
|
20 |
+
}
|
21 |
+
item {
|
22 |
+
name:'11'
|
23 |
+
id:6
|
24 |
+
}
|
25 |
+
item {
|
26 |
+
name:'12'
|
27 |
+
id:7
|
28 |
+
}
|
29 |
+
item {
|
30 |
+
name:'13'
|
31 |
+
id:8
|
32 |
+
}
|
33 |
+
item {
|
34 |
+
name:'14'
|
35 |
+
id:9
|
36 |
+
}
|
37 |
+
item {
|
38 |
+
name:'16'
|
39 |
+
id:10
|
40 |
+
}
|
41 |
+
item {
|
42 |
+
name:'17'
|
43 |
+
id:11
|
44 |
+
}
|
45 |
+
item {
|
46 |
+
name:'18'
|
47 |
+
id:12
|
48 |
+
}
|
49 |
+
item {
|
50 |
+
name:'19'
|
51 |
+
id:13
|
52 |
+
}
|
53 |
+
item {
|
54 |
+
name:'21'
|
55 |
+
id:14
|
56 |
+
}
|
57 |
+
item {
|
58 |
+
name:'22'
|
59 |
+
id:15
|
60 |
+
}
|
61 |
+
item {
|
62 |
+
name:'23'
|
63 |
+
id:16
|
64 |
+
}
|
65 |
+
item {
|
66 |
+
name:'26'
|
67 |
+
id:17
|
68 |
+
}
|
69 |
+
item {
|
70 |
+
name:'27'
|
71 |
+
id:18
|
72 |
+
}
|
73 |
+
item {
|
74 |
+
name:'28'
|
75 |
+
id:19
|
76 |
+
}
|
77 |
+
item {
|
78 |
+
name:'29'
|
79 |
+
id:20
|
80 |
+
}
|
81 |
+
item {
|
82 |
+
name:'30'
|
83 |
+
id:21
|
84 |
+
}
|
85 |
+
item {
|
86 |
+
name:'31'
|
87 |
+
id:22
|
88 |
+
}
|
89 |
+
item {
|
90 |
+
name:'32'
|
91 |
+
id:23
|
92 |
+
}
|
93 |
+
item {
|
94 |
+
name:'33'
|
95 |
+
id:24
|
96 |
+
}
|
97 |
+
item {
|
98 |
+
name:'34'
|
99 |
+
id:25
|
100 |
+
}
|
101 |
+
item {
|
102 |
+
name:'35'
|
103 |
+
id:26
|
104 |
+
}
|
105 |
+
item {
|
106 |
+
name:'36'
|
107 |
+
id:27
|
108 |
+
}
|
109 |
+
item {
|
110 |
+
name:'37'
|
111 |
+
id:28
|
112 |
+
}
|
113 |
+
item {
|
114 |
+
name:'38'
|
115 |
+
id:29
|
116 |
+
}
|
117 |
+
item {
|
118 |
+
name:'39'
|
119 |
+
id:30
|
120 |
+
}
|
121 |
+
item {
|
122 |
+
name:'40'
|
123 |
+
id:31
|
124 |
+
}
|
125 |
+
item {
|
126 |
+
name:'41'
|
127 |
+
id:32
|
128 |
+
}
|
129 |
+
item {
|
130 |
+
name:'42'
|
131 |
+
id:33
|
132 |
+
}
|
133 |
+
item {
|
134 |
+
name:'43'
|
135 |
+
id:34
|
136 |
+
}
|
137 |
+
item {
|
138 |
+
name:'45'
|
139 |
+
id:35
|
140 |
+
}
|
141 |
+
item {
|
142 |
+
name:'46'
|
143 |
+
id:36
|
144 |
+
}
|
145 |
+
item {
|
146 |
+
name:'47'
|
147 |
+
id:37
|
148 |
+
}
|
149 |
+
item {
|
150 |
+
name:'49'
|
151 |
+
id:38
|
152 |
+
}
|
153 |
+
item {
|
154 |
+
name:'50'
|
155 |
+
id:39
|
156 |
+
}
|
157 |
+
item {
|
158 |
+
name:'51'
|
159 |
+
id:40
|
160 |
+
}
|
161 |
+
item {
|
162 |
+
name:'52'
|
163 |
+
id:41
|
164 |
+
}
|
165 |
+
item {
|
166 |
+
name:'53'
|
167 |
+
id:42
|
168 |
+
}
|
169 |
+
item {
|
170 |
+
name:'54'
|
171 |
+
id:43
|
172 |
+
}
|
173 |
+
item {
|
174 |
+
name:'55'
|
175 |
+
id:44
|
176 |
+
}
|
177 |
+
item {
|
178 |
+
name:'57'
|
179 |
+
id:45
|
180 |
+
}
|
181 |
+
item {
|
182 |
+
name:'58'
|
183 |
+
id:46
|
184 |
+
}
|
185 |
+
item {
|
186 |
+
name:'60'
|
187 |
+
id:47
|
188 |
+
}
|
189 |
+
item {
|
190 |
+
name:'61'
|
191 |
+
id:48
|
192 |
+
}
|
193 |
+
item {
|
194 |
+
name:'62'
|
195 |
+
id:49
|
196 |
+
}
|
197 |
+
item {
|
198 |
+
name:'63'
|
199 |
+
id:50
|
200 |
+
}
|
201 |
+
item {
|
202 |
+
name:'64'
|
203 |
+
id:51
|
204 |
+
}
|
205 |
+
item {
|
206 |
+
name:'66'
|
207 |
+
id:52
|
208 |
+
}
|
209 |
+
item {
|
210 |
+
name:'67'
|
211 |
+
id:53
|
212 |
+
}
|
Tensorflow/workspace/images/test/.gitkeep
ADDED
File without changes
|
Tensorflow/workspace/images/test/1 (2).jpg
ADDED
![]() |
Tensorflow/workspace/images/test/1 (2).xml
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<annotation>
|
2 |
+
<folder>BigDataset</folder>
|
3 |
+
<filename>1 (8).jpg</filename>
|
4 |
+
<path>F:\Thesis Files\BigDataset\1 (8).jpg</path>
|
5 |
+
<source>
|
6 |
+
<database>Unknown</database>
|
7 |
+
</source>
|
8 |
+
<size>
|
9 |
+
<width>159</width>
|
10 |
+
<height>110</height>
|
11 |
+
<depth>1</depth>
|
12 |
+
</size>
|
13 |
+
<segmented>0</segmented>
|
14 |
+
<object>
|
15 |
+
<name>30</name>
|
16 |
+
<pose>Unspecified</pose>
|
17 |
+
<truncated>1</truncated>
|
18 |
+
<difficult>0</difficult>
|
19 |
+
<bndbox>
|
20 |
+
<xmin>1</xmin>
|
21 |
+
<ymin>35</ymin>
|
22 |
+
<xmax>77</xmax>
|
23 |
+
<ymax>96</ymax>
|
24 |
+
</bndbox>
|
25 |
+
</object>
|
26 |
+
<object>
|
27 |
+
<name>28</name>
|
28 |
+
<pose>Unspecified</pose>
|
29 |
+
<truncated>0</truncated>
|
30 |
+
<difficult>0</difficult>
|
31 |
+
<bndbox>
|
32 |
+
<xmin>79</xmin>
|
33 |
+
<ymin>37</ymin>
|
34 |
+
<xmax>128</xmax>
|
35 |
+
<ymax>109</ymax>
|
36 |
+
</bndbox>
|
37 |
+
</object>
|
38 |
+
<object>
|
39 |
+
<name>62</name>
|
40 |
+
<pose>Unspecified</pose>
|
41 |
+
<truncated>0</truncated>
|
42 |
+
<difficult>0</difficult>
|
43 |
+
<bndbox>
|
44 |
+
<xmin>63</xmin>
|
45 |
+
<ymin>2</ymin>
|
46 |
+
<xmax>154</xmax>
|
47 |
+
<ymax>98</ymax>
|
48 |
+
</bndbox>
|
49 |
+
</object>
|
50 |
+
</annotation>
|
Tensorflow/workspace/images/train/.gitkeep
ADDED
File without changes
|
Tensorflow/workspace/images/train/1 (1).jpg
ADDED
![]() |
Tensorflow/workspace/images/train/1 (1).xml
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<annotation>
|
2 |
+
<folder>Last</folder>
|
3 |
+
<filename>1 (1).jpg</filename>
|
4 |
+
<path>F:\Thesis Files\Last\1 (1).jpg</path>
|
5 |
+
<source>
|
6 |
+
<database>Unknown</database>
|
7 |
+
</source>
|
8 |
+
<size>
|
9 |
+
<width>159</width>
|
10 |
+
<height>68</height>
|
11 |
+
<depth>1</depth>
|
12 |
+
</size>
|
13 |
+
<segmented>0</segmented>
|
14 |
+
<object>
|
15 |
+
<name>31</name>
|
16 |
+
<pose>Unspecified</pose>
|
17 |
+
<truncated>1</truncated>
|
18 |
+
<difficult>0</difficult>
|
19 |
+
<bndbox>
|
20 |
+
<xmin>1</xmin>
|
21 |
+
<ymin>3</ymin>
|
22 |
+
<xmax>70</xmax>
|
23 |
+
<ymax>68</ymax>
|
24 |
+
</bndbox>
|
25 |
+
</object>
|
26 |
+
<object>
|
27 |
+
<name>27</name>
|
28 |
+
<pose>Unspecified</pose>
|
29 |
+
<truncated>0</truncated>
|
30 |
+
<difficult>0</difficult>
|
31 |
+
<bndbox>
|
32 |
+
<xmin>72</xmin>
|
33 |
+
<ymin>3</ymin>
|
34 |
+
<xmax>153</xmax>
|
35 |
+
<ymax>64</ymax>
|
36 |
+
</bndbox>
|
37 |
+
</object>
|
38 |
+
</annotation>
|
Tensorflow/workspace/models/.gitkeep
ADDED
File without changes
|
Tensorflow/workspace/models/my_ssd_mobnet/checkpoint
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
model_checkpoint_path: "ckpt-51"
|
2 |
+
all_model_checkpoint_paths: "ckpt-45"
|
3 |
+
all_model_checkpoint_paths: "ckpt-46"
|
4 |
+
all_model_checkpoint_paths: "ckpt-47"
|
5 |
+
all_model_checkpoint_paths: "ckpt-48"
|
6 |
+
all_model_checkpoint_paths: "ckpt-49"
|
7 |
+
all_model_checkpoint_paths: "ckpt-50"
|
8 |
+
all_model_checkpoint_paths: "ckpt-51"
|
9 |
+
all_model_checkpoint_timestamps: 1647536347.1598632
|
10 |
+
all_model_checkpoint_timestamps: 1647537278.138767
|
11 |
+
all_model_checkpoint_timestamps: 1647538209.904142
|
12 |
+
all_model_checkpoint_timestamps: 1647539145.8142953
|
13 |
+
all_model_checkpoint_timestamps: 1647540079.101388
|
14 |
+
all_model_checkpoint_timestamps: 1647541013.3102336
|
15 |
+
all_model_checkpoint_timestamps: 1647541949.6900263
|
16 |
+
last_preserved_timestamp: 1647465050.9765189
|
Tensorflow/workspace/models/my_ssd_mobnet/ckpt-51.data-00000-of-00001
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f522d8067633f24a4948fa5f8b93e421ac681ad1ff01a5a86b1b3130546fcd60
|
3 |
+
size 21052821
|
Tensorflow/workspace/models/my_ssd_mobnet/ckpt-51.index
ADDED
Binary file (48 kB). View file
|
|
Tensorflow/workspace/models/my_ssd_mobnet/pipeline.config
ADDED
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
model {
|
2 |
+
ssd {
|
3 |
+
num_classes: 53
|
4 |
+
image_resizer {
|
5 |
+
fixed_shape_resizer {
|
6 |
+
height: 640
|
7 |
+
width: 640
|
8 |
+
}
|
9 |
+
}
|
10 |
+
feature_extractor {
|
11 |
+
type: "ssd_mobilenet_v2_fpn_keras"
|
12 |
+
depth_multiplier: 1.0
|
13 |
+
min_depth: 16
|
14 |
+
conv_hyperparams {
|
15 |
+
regularizer {
|
16 |
+
l2_regularizer {
|
17 |
+
weight: 4e-05
|
18 |
+
}
|
19 |
+
}
|
20 |
+
initializer {
|
21 |
+
random_normal_initializer {
|
22 |
+
mean: 0.0
|
23 |
+
stddev: 0.01
|
24 |
+
}
|
25 |
+
}
|
26 |
+
activation: RELU_6
|
27 |
+
batch_norm {
|
28 |
+
decay: 0.997
|
29 |
+
scale: true
|
30 |
+
epsilon: 0.001
|
31 |
+
}
|
32 |
+
}
|
33 |
+
use_depthwise: true
|
34 |
+
override_base_feature_extractor_hyperparams: true
|
35 |
+
fpn {
|
36 |
+
min_level: 3
|
37 |
+
max_level: 7
|
38 |
+
additional_layer_depth: 128
|
39 |
+
}
|
40 |
+
}
|
41 |
+
box_coder {
|
42 |
+
faster_rcnn_box_coder {
|
43 |
+
y_scale: 10.0
|
44 |
+
x_scale: 10.0
|
45 |
+
height_scale: 5.0
|
46 |
+
width_scale: 5.0
|
47 |
+
}
|
48 |
+
}
|
49 |
+
matcher {
|
50 |
+
argmax_matcher {
|
51 |
+
matched_threshold: 0.5
|
52 |
+
unmatched_threshold: 0.5
|
53 |
+
ignore_thresholds: false
|
54 |
+
negatives_lower_than_unmatched: true
|
55 |
+
force_match_for_each_row: true
|
56 |
+
use_matmul_gather: true
|
57 |
+
}
|
58 |
+
}
|
59 |
+
similarity_calculator {
|
60 |
+
iou_similarity {
|
61 |
+
}
|
62 |
+
}
|
63 |
+
box_predictor {
|
64 |
+
weight_shared_convolutional_box_predictor {
|
65 |
+
conv_hyperparams {
|
66 |
+
regularizer {
|
67 |
+
l2_regularizer {
|
68 |
+
weight: 4e-05
|
69 |
+
}
|
70 |
+
}
|
71 |
+
initializer {
|
72 |
+
random_normal_initializer {
|
73 |
+
mean: 0.0
|
74 |
+
stddev: 0.01
|
75 |
+
}
|
76 |
+
}
|
77 |
+
activation: RELU_6
|
78 |
+
batch_norm {
|
79 |
+
decay: 0.997
|
80 |
+
scale: true
|
81 |
+
epsilon: 0.001
|
82 |
+
}
|
83 |
+
}
|
84 |
+
depth: 128
|
85 |
+
num_layers_before_predictor: 4
|
86 |
+
kernel_size: 3
|
87 |
+
class_prediction_bias_init: -4.6
|
88 |
+
share_prediction_tower: true
|
89 |
+
use_depthwise: true
|
90 |
+
}
|
91 |
+
}
|
92 |
+
anchor_generator {
|
93 |
+
multiscale_anchor_generator {
|
94 |
+
min_level: 3
|
95 |
+
max_level: 7
|
96 |
+
anchor_scale: 4.0
|
97 |
+
aspect_ratios: 1.0
|
98 |
+
aspect_ratios: 2.0
|
99 |
+
aspect_ratios: 0.5
|
100 |
+
scales_per_octave: 2
|
101 |
+
}
|
102 |
+
}
|
103 |
+
post_processing {
|
104 |
+
batch_non_max_suppression {
|
105 |
+
score_threshold: 1e-08
|
106 |
+
iou_threshold: 0.6
|
107 |
+
max_detections_per_class: 100
|
108 |
+
max_total_detections: 100
|
109 |
+
use_static_shapes: false
|
110 |
+
}
|
111 |
+
score_converter: SIGMOID
|
112 |
+
}
|
113 |
+
normalize_loss_by_num_matches: true
|
114 |
+
loss {
|
115 |
+
localization_loss {
|
116 |
+
weighted_smooth_l1 {
|
117 |
+
}
|
118 |
+
}
|
119 |
+
classification_loss {
|
120 |
+
weighted_sigmoid_focal {
|
121 |
+
gamma: 2.0
|
122 |
+
alpha: 0.25
|
123 |
+
}
|
124 |
+
}
|
125 |
+
classification_weight: 1.0
|
126 |
+
localization_weight: 1.0
|
127 |
+
}
|
128 |
+
encode_background_as_zeros: true
|
129 |
+
normalize_loc_loss_by_codesize: true
|
130 |
+
inplace_batchnorm_update: true
|
131 |
+
freeze_batchnorm: false
|
132 |
+
}
|
133 |
+
}
|
134 |
+
train_config {
|
135 |
+
batch_size: 8
|
136 |
+
data_augmentation_options {
|
137 |
+
random_horizontal_flip {
|
138 |
+
}
|
139 |
+
}
|
140 |
+
data_augmentation_options {
|
141 |
+
random_crop_image {
|
142 |
+
min_object_covered: 0.0
|
143 |
+
min_aspect_ratio: 0.75
|
144 |
+
max_aspect_ratio: 3.0
|
145 |
+
min_area: 0.75
|
146 |
+
max_area: 1.0
|
147 |
+
overlap_thresh: 0.0
|
148 |
+
}
|
149 |
+
}
|
150 |
+
sync_replicas: true
|
151 |
+
optimizer {
|
152 |
+
momentum_optimizer {
|
153 |
+
learning_rate {
|
154 |
+
cosine_decay_learning_rate {
|
155 |
+
learning_rate_base: 0.08
|
156 |
+
total_steps: 50000
|
157 |
+
warmup_learning_rate: 0.026666
|
158 |
+
warmup_steps: 1000
|
159 |
+
}
|
160 |
+
}
|
161 |
+
momentum_optimizer_value: 0.9
|
162 |
+
}
|
163 |
+
use_moving_average: false
|
164 |
+
}
|
165 |
+
fine_tune_checkpoint: "Tensorflow/workspace/pre-trained-models/ssd_mobilenet_v2_fpnlite_640x640_coco17_tpu-8/checkpoint/ckpt-0"
|
166 |
+
num_steps: 50000
|
167 |
+
startup_delay_steps: 0.0
|
168 |
+
replicas_to_aggregate: 8
|
169 |
+
max_number_of_boxes: 100
|
170 |
+
unpad_groundtruth_tensors: false
|
171 |
+
fine_tune_checkpoint_type: "detection"
|
172 |
+
fine_tune_checkpoint_version: V2
|
173 |
+
}
|
174 |
+
train_input_reader {
|
175 |
+
label_map_path: "Tensorflow/workspace/annotations/label_map.pbtxt"
|
176 |
+
tf_record_input_reader {
|
177 |
+
input_path: "Tensorflow/workspace/annotations/train.record"
|
178 |
+
}
|
179 |
+
}
|
180 |
+
eval_config {
|
181 |
+
metrics_set: "coco_detection_metrics"
|
182 |
+
use_moving_averages: false
|
183 |
+
}
|
184 |
+
eval_input_reader {
|
185 |
+
label_map_path: "Tensorflow/workspace/annotations/label_map.pbtxt"
|
186 |
+
shuffle: false
|
187 |
+
num_epochs: 1
|
188 |
+
tf_record_input_reader {
|
189 |
+
input_path: "Tensorflow/workspace/annotations/test.record"
|
190 |
+
}
|
191 |
+
}
|
app.py
ADDED
@@ -0,0 +1,543 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from __future__ import division, print_function
|
2 |
+
# coding=utf-8
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
import glob
|
6 |
+
import re
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
# Keras
|
10 |
+
from tensorflow.keras.models import load_model
|
11 |
+
#from tensorflow.keras.preprocessing import image
|
12 |
+
|
13 |
+
# Flask utils
|
14 |
+
from flask import Flask, redirect, url_for, request, render_template
|
15 |
+
from werkzeug.utils import secure_filename
|
16 |
+
# from gevent.pywsgi import WSGIServer
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
|
21 |
+
|
22 |
+
#import everytnimg
|
23 |
+
# from skimage.io import imread, imshow
|
24 |
+
# from skimage.filters import gaussian, threshold_otsu
|
25 |
+
# from skimage.feature import canny
|
26 |
+
# from skimage.transform import probabilistic_hough_line, rotate
|
27 |
+
# from process_image import process_image
|
28 |
+
|
29 |
+
# import glob
|
30 |
+
# import math
|
31 |
+
|
32 |
+
import cv2
|
33 |
+
# import numpy as np
|
34 |
+
# from PIL import Image
|
35 |
+
# from matplotlib import pyplot as plt
|
36 |
+
# from matplotlib.patches import Rectangle
|
37 |
+
#%matplotlib inline
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
# from collections import OrderedDict
|
42 |
+
# from PIL import Image
|
43 |
+
|
44 |
+
# import pandas as pd
|
45 |
+
# import seaborn as sns
|
46 |
+
|
47 |
+
# import math
|
48 |
+
|
49 |
+
|
50 |
+
#import all from Hough transfrom cell
|
51 |
+
# from skimage.transform import hough_line, hough_line_peaks
|
52 |
+
# from skimage.transform import rotate
|
53 |
+
# from skimage.feature import canny
|
54 |
+
# from skimage.io import imread
|
55 |
+
# from skimage.color import rgb2gray
|
56 |
+
# import matplotlib.pyplot as plt
|
57 |
+
# from scipy.stats import mode as md
|
58 |
+
# from myhough import deskew, deskew2
|
59 |
+
|
60 |
+
# from segment_words import sortit,words,createk,hpf,bps,wps,baw
|
61 |
+
|
62 |
+
# from myverify import verify
|
63 |
+
#from detect_frame import detect_frame
|
64 |
+
import pathlib
|
65 |
+
|
66 |
+
|
67 |
+
#import more
|
68 |
+
import tensorflow as tf
|
69 |
+
from object_detection.utils import config_util
|
70 |
+
# from object_detection.protos import pipeline_pb2
|
71 |
+
# from google.protobuf import text_format
|
72 |
+
|
73 |
+
# import os
|
74 |
+
from object_detection.utils import label_map_util
|
75 |
+
# from object_detection.utils import visualization_utils as viz_utils
|
76 |
+
from object_detection.builders import model_builder
|
77 |
+
|
78 |
+
# Load pipeline config and build a detection model
|
79 |
+
WORKSPACE_PATH = 'Tensorflow/workspace'
|
80 |
+
# SCRIPTS_PATH = 'Tensorflow/scripts'
|
81 |
+
#APIMODEL_PATH = 'Tensorflow/models'
|
82 |
+
ANNOTATION_PATH = WORKSPACE_PATH+'/annotations'
|
83 |
+
# IMAGE_PATH = WORKSPACE_PATH+'/images'
|
84 |
+
MODEL_PATH = WORKSPACE_PATH+'/models'
|
85 |
+
PRETRAINED_MODEL_PATH = WORKSPACE_PATH+'/pre-trained-models'
|
86 |
+
CONFIG_PATH = MODEL_PATH+'/my_ssd_mobnet/pipeline.config'
|
87 |
+
CHECKPOINT_PATH = MODEL_PATH+'/my_ssd_mobnet/'
|
88 |
+
# INPUT_IMAGE_PATH = 'Tensorflow/myimages'
|
89 |
+
# MODEL_PATH = 'E:/RealTimeObjectDetection/model.best.hdf5'
|
90 |
+
|
91 |
+
configs = config_util.get_configs_from_pipeline_file(CONFIG_PATH)
|
92 |
+
detection_model = model_builder.build(model_config=configs['model'], is_training=False)
|
93 |
+
|
94 |
+
# Restore checkpoint
|
95 |
+
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
|
96 |
+
ckpt.restore(os.path.join(CHECKPOINT_PATH, 'ckpt-51')).expect_partial()
|
97 |
+
|
98 |
+
@tf.function
|
99 |
+
def detect_fn(image):
|
100 |
+
image, shapes = detection_model.preprocess(image)
|
101 |
+
prediction_dict = detection_model.predict(image, shapes)
|
102 |
+
detections = detection_model.postprocess(prediction_dict, shapes)
|
103 |
+
return detections
|
104 |
+
|
105 |
+
def detect_frame(frame,isRealTime = False):
|
106 |
+
image_np = np.array(frame)
|
107 |
+
cpimg = frame.copy()
|
108 |
+
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
|
109 |
+
detections = detect_fn(input_tensor)
|
110 |
+
print(len(detections))
|
111 |
+
num_detections = int(detections.pop('num_detections'))
|
112 |
+
#print("hello")
|
113 |
+
#print(num_detections)
|
114 |
+
|
115 |
+
#print(len(detections['detection_scores']))
|
116 |
+
detections = {key: value[0, :num_detections].numpy()
|
117 |
+
for key, value in detections.items()}
|
118 |
+
detections['num_detections'] = num_detections
|
119 |
+
row,col,dummy = image_np.shape
|
120 |
+
# detection_classes should be ints.
|
121 |
+
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
|
122 |
+
#print(detections['detection_classes'])
|
123 |
+
mark = [0]*15
|
124 |
+
myletters = []
|
125 |
+
for i in range(0,15):
|
126 |
+
curi=detections['detection_classes'][i]
|
127 |
+
classi=classes[curi]
|
128 |
+
print(classes[curi],end='-')
|
129 |
+
cur=detections['detection_scores'][i]
|
130 |
+
if(cur<0.2):
|
131 |
+
continue
|
132 |
+
print(cur,end=' ')
|
133 |
+
print(detections['detection_boxes'][i], end=' ')
|
134 |
+
x0=(detections['detection_boxes'][i][0])
|
135 |
+
y0=(detections['detection_boxes'][i][1])
|
136 |
+
x1=(detections['detection_boxes'][i][2])
|
137 |
+
y1=(detections['detection_boxes'][i][3])
|
138 |
+
curarea=(x1-x0)*(y1-y0)
|
139 |
+
ok=1
|
140 |
+
for j in range(0,i):
|
141 |
+
#print(mark[j])
|
142 |
+
if mark[j]==0:
|
143 |
+
continue
|
144 |
+
curj=detections['detection_classes'][j]
|
145 |
+
classj=classes[curj]
|
146 |
+
|
147 |
+
if classi=='ি' or classj=='ি':
|
148 |
+
if classi!=classj:
|
149 |
+
continue
|
150 |
+
if classi=='ী' or classj=='ী':
|
151 |
+
if classi!=classj:
|
152 |
+
continue
|
153 |
+
|
154 |
+
x2=(detections['detection_boxes'][j][0])
|
155 |
+
y2=(detections['detection_boxes'][j][1])
|
156 |
+
x3=(detections['detection_boxes'][j][2])
|
157 |
+
y3=(detections['detection_boxes'][j][3])
|
158 |
+
x4=max(x0,x2)
|
159 |
+
y4=max(y0,y2)
|
160 |
+
x5=min(x1,x3)
|
161 |
+
y5=min(y1,y3)
|
162 |
+
if x4>x5 or y4>y5:
|
163 |
+
continue
|
164 |
+
prevarea=(x3-x2)*(y3-y2)
|
165 |
+
commonarea=(x5-x4)*(y5-y4)
|
166 |
+
ins1=curarea/commonarea
|
167 |
+
ins2=prevarea/commonarea
|
168 |
+
ins=commonarea/(curarea+prevarea-commonarea)
|
169 |
+
print(ins1,end=' ')
|
170 |
+
if(ins>=0.5):
|
171 |
+
ok=0
|
172 |
+
cur=detections['detection_classes'][j]
|
173 |
+
print(classes[cur])
|
174 |
+
break
|
175 |
+
if ok==1:
|
176 |
+
mark[i]=1
|
177 |
+
cur=detections['detection_classes'][i]
|
178 |
+
#myletters.append(classes[cur])
|
179 |
+
print(ok)
|
180 |
+
#verification
|
181 |
+
for i in range(0,15):
|
182 |
+
if mark[i]==0 or avver==0:
|
183 |
+
continue
|
184 |
+
if detections['detection_classes'][i]>38:
|
185 |
+
continue
|
186 |
+
x0=int(detections['detection_boxes'][i][0]*row)
|
187 |
+
y0=int(detections['detection_boxes'][i][1]*col)
|
188 |
+
x1=int(detections['detection_boxes'][i][2]*row)
|
189 |
+
y1=int(detections['detection_boxes'][i][3]*col)
|
190 |
+
#print(y0,y1,x0,x1)
|
191 |
+
currImg = cpimg[x0:x1,y0:y1]
|
192 |
+
|
193 |
+
curscore = detections['detection_scores'][i]
|
194 |
+
curclass = detections['detection_classes'][i]
|
195 |
+
label,conf = verify(currImg)
|
196 |
+
#print(ulta[label],conf)
|
197 |
+
#print(curclass,curscore)
|
198 |
+
if conf>curscore and ulta[label]!=curclass and ulta[label]!=-1:
|
199 |
+
detections['detection_classes'][i]=ulta[label]
|
200 |
+
detections['detection_scores'][i]=conf
|
201 |
+
|
202 |
+
for i in range(0,15):
|
203 |
+
if(detections['detection_scores'][i]<0.2):
|
204 |
+
continue
|
205 |
+
if mark[i]==0:
|
206 |
+
continue
|
207 |
+
cur=detections['detection_classes'][i]
|
208 |
+
cur=classes[cur]
|
209 |
+
y0=(detections['detection_boxes'][i][1])
|
210 |
+
y1=(detections['detection_boxes'][i][3])
|
211 |
+
pair = (y0,cur,y1)
|
212 |
+
myletters.append(pair)
|
213 |
+
myletters.sort(key = lambda x: x[0])
|
214 |
+
#print(myletters)
|
215 |
+
for i in range(len(myletters)-1,-1,-1):
|
216 |
+
y0=myletters[i][0]
|
217 |
+
curr=myletters[i][1]
|
218 |
+
y1=myletters[i][2]
|
219 |
+
if curr=='ু' or curr=='্র':
|
220 |
+
mxarea=0
|
221 |
+
mxno=i-1
|
222 |
+
for j in range(0,len(myletters)):
|
223 |
+
if i==j:
|
224 |
+
continue
|
225 |
+
y2=myletters[j][0]
|
226 |
+
y3=myletters[j][2]
|
227 |
+
curcommon = min(y3,y1)-max(y0,y2)
|
228 |
+
if curcommon>mxarea:
|
229 |
+
mxarea = curcommon
|
230 |
+
mxno=j
|
231 |
+
if mxno!=(i-1):
|
232 |
+
myletters[i],myletters[i+1]=myletters[i+1],myletters[i]
|
233 |
+
|
234 |
+
res_list = [x[1] for x in myletters]
|
235 |
+
print(res_list)
|
236 |
+
|
237 |
+
|
238 |
+
for i in range(len(res_list)-2, -1, -1):
|
239 |
+
x=res_list[i]
|
240 |
+
y=res_list[i+1]
|
241 |
+
if x=='ে' or x=='ি':
|
242 |
+
res_list[i],res_list[i+1]=res_list[i+1],res_list[i]
|
243 |
+
for i in range(len(res_list)-2, -1, -1):
|
244 |
+
x=res_list[i]
|
245 |
+
y=res_list[i+1]
|
246 |
+
print(x,y)
|
247 |
+
if x=='অ' and y=='া':
|
248 |
+
print('yo')
|
249 |
+
res_list[i]='আ'
|
250 |
+
res_list.pop(i+1)
|
251 |
+
print(res_list)
|
252 |
+
for i in res_list:
|
253 |
+
print(i,end='')
|
254 |
+
|
255 |
+
print(' ')
|
256 |
+
return res_list
|
257 |
+
|
258 |
+
|
259 |
+
|
260 |
+
|
261 |
+
# Define a flask app
|
262 |
+
# app = Flask(__name__)
|
263 |
+
|
264 |
+
# Model saved with Keras model.save()
|
265 |
+
|
266 |
+
# Load your trained model
|
267 |
+
# model = load_model(MODEL_PATH)
|
268 |
+
#model._make_predict_function() # Necessary
|
269 |
+
# print('Model loaded. Start serving...')
|
270 |
+
|
271 |
+
# You can also use pretrained model from Keras
|
272 |
+
# Check https://keras.io/applications/
|
273 |
+
#from keras.applications.resnet50 import ResNet50
|
274 |
+
#model = ResNet50(weights='imagenet')
|
275 |
+
#model.save('')
|
276 |
+
# print('Model loaded. Check http://127.0.0.1:5000/')
|
277 |
+
avver=0
|
278 |
+
clicked=1
|
279 |
+
wp = None; bp = None;
|
280 |
+
|
281 |
+
category_index = label_map_util.create_category_index_from_labelmap(ANNOTATION_PATH+'/label_map.pbtxt')
|
282 |
+
classes=['অ','ই','উ','এ','ও','ক','খ','গ','ঘ','চ','ছ','জ','ঝ','ট','ঠ','ড','ত','থ','দ','ধ','ন','প','ফ','ব','ভ','ম','য','র','ল','শ','ষ','স','হ','ড়','য়','ৎ','ং','ঁ','০','১','২','৩','৪','৫','৭','৮','া','ি','ী','ে','ু','্র','্য']
|
283 |
+
labels=[1,2,4,7,9,11,12,13,14,16,17,18,19,21,22,23,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,45,46,47,49,50,51,52,53,54,55,57,58,60,61,62,63,64,66,67]
|
284 |
+
ulta=[0,-1,1,-1,2,-1,-1,3,-1,4,-1,5,6,7,8,-1,9,10,11,12,-1,13,14,15,-1,-1,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,-1,34,35,36,-1,37,38,39,40,41,42,43,-1,44,45,-1,46,47,48,49,50,-1,51,52]
|
285 |
+
|
286 |
+
|
287 |
+
def model_predict(word):
|
288 |
+
#img = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
|
289 |
+
'''
|
290 |
+
if clicked==1:
|
291 |
+
bp = 66
|
292 |
+
wp = 160
|
293 |
+
mode = "GCMODE"
|
294 |
+
if mode == "GCMODE":
|
295 |
+
img= hpf(img,kSize = 51)
|
296 |
+
wp = 127
|
297 |
+
img = wps(img,wp)
|
298 |
+
img = bps(img)
|
299 |
+
elif mode == "RMODE":
|
300 |
+
bps()
|
301 |
+
wps()
|
302 |
+
elif mode == "SMODE":
|
303 |
+
bps()
|
304 |
+
wps()
|
305 |
+
baw()
|
306 |
+
img = cv2.fastNlMeansDenoising(img, img, 50.0, 7, 21)
|
307 |
+
print("\ndone.")
|
308 |
+
xs=img.shape
|
309 |
+
if len(xs)==3:
|
310 |
+
img = img[:,:,0]
|
311 |
+
|
312 |
+
img = cv.adaptiveThreshold(img,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
|
313 |
+
angeel = deskew(img)
|
314 |
+
if angeel!=0:
|
315 |
+
img = deskew2(img,angeel)
|
316 |
+
ho,wo=img.shape
|
317 |
+
area=ho*wo
|
318 |
+
ara=words(img,25,11,7,area/5000)
|
319 |
+
ara.reverse()
|
320 |
+
#cv2.imshow('input image',img)
|
321 |
+
sz=len(ara)
|
322 |
+
for i in range(0,sz):
|
323 |
+
ara[i]=sorted(ara[i], key=lambda entry:entry[0][0])
|
324 |
+
cnt2=0
|
325 |
+
files = glob.glob('Tensorflow/myimages/*')
|
326 |
+
for f in files:
|
327 |
+
os.remove(f)
|
328 |
+
for i in range(0,sz):
|
329 |
+
#print(ara[i].shape)
|
330 |
+
tmp=ara[i]
|
331 |
+
sz2=len(tmp)
|
332 |
+
if i%10==0:
|
333 |
+
cnt2=cnt2+1
|
334 |
+
for j in range(0,sz2):
|
335 |
+
a,b=tmp[j]
|
336 |
+
b = cv2.adaptiveThreshold(b,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
|
337 |
+
if j<10:
|
338 |
+
cnt3=0
|
339 |
+
elif j<20:
|
340 |
+
cnt3=1
|
341 |
+
else:
|
342 |
+
cnt3=2
|
343 |
+
cv2.imwrite('Tensorflow/myimages/ocr %d%d%d%d.jpg' % (cnt2,i,cnt3,j), b)
|
344 |
+
#cv2.imshow('Crop %d%d' % (i,j), b)
|
345 |
+
cv2.waitKey(0)
|
346 |
+
|
347 |
+
PATH_TO_TEST_IMAGES_DIR = pathlib.Path('Tensorflow/myimages')
|
348 |
+
TEST_IMAGE_PATHS = (list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpg"))+list(PATH_TO_TEST_IMAGES_DIR.glob("*.jpeg"))) #+list(PATH_TO_TEST_IMAGES_DIR.glob("*.png"))
|
349 |
+
print(len(TEST_IMAGE_PATHS))
|
350 |
+
final = []
|
351 |
+
for image_path in TEST_IMAGE_PATHS:
|
352 |
+
print("ovi")
|
353 |
+
print(image_path)
|
354 |
+
frame = cv2.imread(str(image_path))
|
355 |
+
x=str(image_path)
|
356 |
+
print(x[25])
|
357 |
+
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
358 |
+
final.append((detect_frame(frame),x[25]))
|
359 |
+
'''
|
360 |
+
frame = cv2.fastNlMeansDenoising(word,word, 50.0, 7, 21)
|
361 |
+
xs = frame.shape
|
362 |
+
if(len(xs)==3):
|
363 |
+
frame = frame[:,:,0]
|
364 |
+
frame= cv2.adaptiveThreshold(frame,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
|
365 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
|
366 |
+
# x=str(img_path)
|
367 |
+
#print(x[25])
|
368 |
+
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
369 |
+
image_np = np.array(frame)
|
370 |
+
cpimg = frame.copy()
|
371 |
+
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
|
372 |
+
image_t, shapes = detection_model.preprocess(input_tensor)
|
373 |
+
prediction_dict = detection_model.predict(image_t, shapes)
|
374 |
+
detections = detection_model.postprocess(prediction_dict, shapes)
|
375 |
+
# print(len(detections))
|
376 |
+
num_detections = int(detections.pop('num_detections'))
|
377 |
+
#print("hello")
|
378 |
+
#print(num_detections)
|
379 |
+
|
380 |
+
#print(len(detections['detection_scores']))
|
381 |
+
detections = {key: value[0, :num_detections].numpy()
|
382 |
+
for key, value in detections.items()}
|
383 |
+
detections['num_detections'] = num_detections
|
384 |
+
row,col,dummy = image_np.shape
|
385 |
+
# detection_classes should be ints.
|
386 |
+
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
|
387 |
+
#print(detections['detection_classes'])
|
388 |
+
mark = [0]*15
|
389 |
+
myletters = []
|
390 |
+
for i in range(0,15):
|
391 |
+
curi=detections['detection_classes'][i]
|
392 |
+
classi=classes[curi]
|
393 |
+
# print(classes[curi],end='-')
|
394 |
+
cur=detections['detection_scores'][i]
|
395 |
+
if(cur<0.2):
|
396 |
+
continue
|
397 |
+
# print(cur,end=' ')
|
398 |
+
# print(detections['detection_boxes'][i], end=' ')
|
399 |
+
x0=(detections['detection_boxes'][i][0])
|
400 |
+
y0=(detections['detection_boxes'][i][1])
|
401 |
+
x1=(detections['detection_boxes'][i][2])
|
402 |
+
y1=(detections['detection_boxes'][i][3])
|
403 |
+
curarea=(x1-x0)*(y1-y0)
|
404 |
+
ok=1
|
405 |
+
for j in range(0,i):
|
406 |
+
#print(mark[j])
|
407 |
+
if mark[j]==0:
|
408 |
+
continue
|
409 |
+
curj=detections['detection_classes'][j]
|
410 |
+
classj=classes[curj]
|
411 |
+
|
412 |
+
if classi=='ি' or classj=='ি':
|
413 |
+
if classi!=classj:
|
414 |
+
continue
|
415 |
+
if classi=='ী' or classj=='ী':
|
416 |
+
if classi!=classj:
|
417 |
+
continue
|
418 |
+
|
419 |
+
x2=(detections['detection_boxes'][j][0])
|
420 |
+
y2=(detections['detection_boxes'][j][1])
|
421 |
+
x3=(detections['detection_boxes'][j][2])
|
422 |
+
y3=(detections['detection_boxes'][j][3])
|
423 |
+
x4=max(x0,x2)
|
424 |
+
y4=max(y0,y2)
|
425 |
+
x5=min(x1,x3)
|
426 |
+
y5=min(y1,y3)
|
427 |
+
if x4>x5 or y4>y5:
|
428 |
+
continue
|
429 |
+
prevarea=(x3-x2)*(y3-y2)
|
430 |
+
commonarea=(x5-x4)*(y5-y4)
|
431 |
+
ins1=curarea/commonarea
|
432 |
+
ins2=prevarea/commonarea
|
433 |
+
ins=commonarea/(curarea+prevarea-commonarea)
|
434 |
+
# print(ins1,end=' ')
|
435 |
+
if(ins>=0.5):
|
436 |
+
ok=0
|
437 |
+
cur=detections['detection_classes'][j]
|
438 |
+
# print(classes[cur])
|
439 |
+
break
|
440 |
+
if ok==1:
|
441 |
+
mark[i]=1
|
442 |
+
cur=detections['detection_classes'][i]
|
443 |
+
#myletters.append(classes[cur])
|
444 |
+
# print(ok)
|
445 |
+
#verification
|
446 |
+
for i in range(0,15):
|
447 |
+
if mark[i]==0 or avver==0:
|
448 |
+
continue
|
449 |
+
if detections['detection_classes'][i]>38:
|
450 |
+
continue
|
451 |
+
x0=int(detections['detection_boxes'][i][0]*row)
|
452 |
+
y0=int(detections['detection_boxes'][i][1]*col)
|
453 |
+
x1=int(detections['detection_boxes'][i][2]*row)
|
454 |
+
y1=int(detections['detection_boxes'][i][3]*col)
|
455 |
+
#print(y0,y1,x0,x1)
|
456 |
+
currImg = cpimg[x0:x1,y0:y1]
|
457 |
+
|
458 |
+
curscore = detections['detection_scores'][i]
|
459 |
+
curclass = detections['detection_classes'][i]
|
460 |
+
label,conf = verify(currImg)
|
461 |
+
#print(ulta[label],conf)
|
462 |
+
#print(curclass,curscore)
|
463 |
+
if conf>curscore and ulta[label]!=curclass and ulta[label]!=-1:
|
464 |
+
detections['detection_classes'][i]=ulta[label]
|
465 |
+
detections['detection_scores'][i]=conf
|
466 |
+
|
467 |
+
for i in range(0,15):
|
468 |
+
if(detections['detection_scores'][i]<0.2):
|
469 |
+
continue
|
470 |
+
if mark[i]==0:
|
471 |
+
continue
|
472 |
+
cur=detections['detection_classes'][i]
|
473 |
+
cur=classes[cur]
|
474 |
+
y0=(detections['detection_boxes'][i][1])
|
475 |
+
y1=(detections['detection_boxes'][i][3])
|
476 |
+
pair = (y0,cur,y1)
|
477 |
+
myletters.append(pair)
|
478 |
+
myletters.sort(key = lambda x: x[0])
|
479 |
+
#print(myletters)
|
480 |
+
for i in range(len(myletters)-1,-1,-1):
|
481 |
+
y0=myletters[i][0]
|
482 |
+
curr=myletters[i][1]
|
483 |
+
y1=myletters[i][2]
|
484 |
+
if curr=='ু' or curr=='্র':
|
485 |
+
mxarea=0
|
486 |
+
mxno=i-1
|
487 |
+
for j in range(0,len(myletters)):
|
488 |
+
if i==j:
|
489 |
+
continue
|
490 |
+
y2=myletters[j][0]
|
491 |
+
y3=myletters[j][2]
|
492 |
+
curcommon = min(y3,y1)-max(y0,y2)
|
493 |
+
if curcommon>mxarea:
|
494 |
+
mxarea = curcommon
|
495 |
+
mxno=j
|
496 |
+
if mxno!=(i-1):
|
497 |
+
myletters[i],myletters[i+1]=myletters[i+1],myletters[i]
|
498 |
+
|
499 |
+
res_list = [x[1] for x in myletters]
|
500 |
+
# print(res_list)
|
501 |
+
|
502 |
+
|
503 |
+
for i in range(len(res_list)-2, -1, -1):
|
504 |
+
x=res_list[i]
|
505 |
+
y=res_list[i+1]
|
506 |
+
if x=='ে' or x=='ি':
|
507 |
+
res_list[i],res_list[i+1]=res_list[i+1],res_list[i]
|
508 |
+
for i in range(len(res_list)-2, -1, -1):
|
509 |
+
x=res_list[i]
|
510 |
+
y=res_list[i+1]
|
511 |
+
# print(x,y)
|
512 |
+
if x=='অ' and y=='া':
|
513 |
+
# print('yo')
|
514 |
+
res_list[i]='আ'
|
515 |
+
res_list.pop(i+1)
|
516 |
+
# print(res_list)
|
517 |
+
output=''
|
518 |
+
for i in res_list:
|
519 |
+
output=output+i
|
520 |
+
|
521 |
+
# print(' ')
|
522 |
+
return output
|
523 |
+
'''
|
524 |
+
output=''
|
525 |
+
for i in range(0,len(final)):
|
526 |
+
ara=final[i][0]
|
527 |
+
numb=final[i][1]
|
528 |
+
if i>0 and numb!=final[i-1][1]:
|
529 |
+
output= output+'\n'
|
530 |
+
word = ''.join(ara)
|
531 |
+
#corrected_word = get_campaign(word)
|
532 |
+
output= output + word
|
533 |
+
#print(corrected_word,end='')
|
534 |
+
output = output + ' '
|
535 |
+
return output
|
536 |
+
'''
|
537 |
+
|
538 |
+
import gradio as gr
|
539 |
+
|
540 |
+
|
541 |
+
demo = gr.Interface(fn=model_predict, inputs= "paint", outputs="text")
|
542 |
+
|
543 |
+
demo.launch()
|
requirements.txt
ADDED
Binary file (5.1 kB). View file
|
|