|
1 | | -import keras |
2 | | -from tensorflow.keras.models import load_model |
3 | | - |
4 | | -import tensorflow as tf |
5 | | -from PIL import Image |
6 | | -import numpy as np |
7 | | -import glob |
8 | | -import os |
9 | | -import cv2 |
10 | | -import configparser |
11 | | -import math |
12 | | -import time |
13 | | -from shutil import copyfile |
14 | | - |
15 | | -class ReadDigitalDigit: |
16 | | - def __init__(self): |
17 | | - config = configparser.ConfigParser() |
18 | | - config.read('./config/config.ini') |
19 | | - |
20 | | - self.log_Image = '' |
21 | | - self.LogNames = '' |
22 | | - |
23 | | - self.model_file = config['Digital_Digit']['Modelfile'] |
24 | | - if config.has_option('Digital_Digit', 'LogImageLocation'): |
25 | | - self.log_Image = config['Digital_Digit']['LogImageLocation'] |
26 | | - self.CheckAndLoadDefaultConfig() |
27 | | - |
28 | | - if config.has_option('Digital_Digit', 'LogImageLocation'): |
29 | | - if (os.path.exists(self.log_Image)): |
30 | | - for i in range(10): |
31 | | - pfad = self.log_Image + '/' + str(i) |
32 | | - if not os.path.exists(pfad): |
33 | | - os.makedirs(pfad) |
34 | | - pfad = self.log_Image + '/NaN' |
35 | | - if not os.path.exists(pfad): |
36 | | - os.makedirs(pfad) |
37 | | - |
38 | | - if config.has_option('Digital_Digit', 'LogNames'): |
39 | | - zw_LogNames = config.get('Digital_Digit', 'LogNames').split(',') |
40 | | - self.LogNames = [] |
41 | | - for nm in zw_LogNames: |
42 | | - self.LogNames.append(nm.strip()) |
43 | | - |
44 | | - self.model_file = config['Digital_Digit']['Modelfile'] |
45 | | - self.model = load_model(self.model_file) |
46 | | - |
47 | | - def CheckAndLoadDefaultConfig(self): |
48 | | - defaultdir = "./config_default/" |
49 | | - targetdir = './config/' |
50 | | - if not os.path.exists(self.model_file): |
51 | | - zerlegt = self.model_file.split('/') |
52 | | - pfad = zerlegt[0] |
53 | | - for i in range(1, len(zerlegt)-1): |
54 | | - pfad = pfad + '/' + zerlegt[i] |
55 | | - if not os.path.exists(pfad): |
56 | | - os.makedirs(pfad) |
57 | | - defaultmodel = self.model_file.replace(targetdir, defaultdir) |
58 | | - copyfile(defaultmodel, self.model_file) |
59 | | - if len(self.log_Image) > 0: |
60 | | - if not os.path.exists(self.log_Image): |
61 | | - zerlegt = self.log_Image.split('/') |
62 | | - pfad = zerlegt[0] |
63 | | - for i in range(1, len(zerlegt)): |
64 | | - pfad = pfad + '/' + zerlegt[i] |
65 | | - if not os.path.exists(pfad): |
66 | | - os.makedirs(pfad) |
67 | | - |
68 | | - def Readout(self, PictureList, logtime): |
69 | | - self.result = [] |
70 | | - for image in PictureList: |
71 | | - value = self.ReadoutSingleImage(image[1]) |
72 | | - if len(self.log_Image) > 0: |
73 | | - self.saveLogImage(image, value, logtime) |
74 | | - self.result.append(value) |
75 | | - return self.result |
76 | | - |
77 | | - def ReadoutSingleImage(self, image): |
78 | | - test_image = cv2.resize(image,(20,32), interpolation = cv2.INTER_CUBIC) |
79 | | - cv2.imwrite('./image_tmp/resize.jpg', test_image) |
80 | | - test_image = np.array(test_image, dtype="float32") |
81 | | -# test_image/=255. |
82 | | - img = np.reshape(test_image,[1,32,20,3]) |
83 | | - result = self.model.predict_classes(img) |
84 | | - if result == 10: |
85 | | - result = "NaN" |
86 | | - else: |
87 | | - result = result[0] |
88 | | - return result |
89 | | - |
90 | | - def saveLogImage(self, image, value, logtime): |
91 | | - if (len(self.LogNames) > 0) and (not image[0] in self.LogNames): |
92 | | - return |
93 | | - speichername = image[0] + '_' + logtime + '.jpg' |
94 | | - speichername = self.log_Image + '/' + str(value) + '/' + speichername |
95 | | - cv2.imwrite(speichername, image[1]) |
| 1 | +import keras |
| 2 | +from tensorflow.keras.models import load_model |
| 3 | + |
| 4 | +import tensorflow as tf |
| 5 | +from PIL import Image |
| 6 | +import numpy as np |
| 7 | +import glob |
| 8 | +import os |
| 9 | +import cv2 |
| 10 | +import configparser |
| 11 | +import math |
| 12 | +import time |
| 13 | +from shutil import copyfile |
| 14 | + |
| 15 | +class ReadDigitalDigit: |
| 16 | + def __init__(self): |
| 17 | + config = configparser.ConfigParser() |
| 18 | + config.read('./config/config.ini') |
| 19 | + |
| 20 | + self.log_Image = '' |
| 21 | + self.LogNames = '' |
| 22 | + |
| 23 | + self.model_file = config['Digital_Digit']['Modelfile'] |
| 24 | + if config.has_option('Digital_Digit', 'LogImageLocation'): |
| 25 | + self.log_Image = config['Digital_Digit']['LogImageLocation'] |
| 26 | + self.CheckAndLoadDefaultConfig() |
| 27 | + |
| 28 | + if config.has_option('Digital_Digit', 'LogImageLocation'): |
| 29 | + if (os.path.exists(self.log_Image)): |
| 30 | + for i in range(10): |
| 31 | + pfad = self.log_Image + '/' + str(i) |
| 32 | + if not os.path.exists(pfad): |
| 33 | + os.makedirs(pfad) |
| 34 | + pfad = self.log_Image + '/NaN' |
| 35 | + if not os.path.exists(pfad): |
| 36 | + os.makedirs(pfad) |
| 37 | + |
| 38 | + if config.has_option('Digital_Digit', 'LogNames'): |
| 39 | + zw_LogNames = config.get('Digital_Digit', 'LogNames').split(',') |
| 40 | + self.LogNames = [] |
| 41 | + for nm in zw_LogNames: |
| 42 | + self.LogNames.append(nm.strip()) |
| 43 | + |
| 44 | + self.model_file = config['Digital_Digit']['Modelfile'] |
| 45 | + self.model = load_model(self.model_file) |
| 46 | + |
| 47 | + def CheckAndLoadDefaultConfig(self): |
| 48 | + defaultdir = "./config_default/" |
| 49 | + targetdir = './config/' |
| 50 | + if not os.path.exists(self.model_file): |
| 51 | + zerlegt = self.model_file.split('/') |
| 52 | + pfad = zerlegt[0] |
| 53 | + for i in range(1, len(zerlegt)-1): |
| 54 | + pfad = pfad + '/' + zerlegt[i] |
| 55 | + if not os.path.exists(pfad): |
| 56 | + os.makedirs(pfad) |
| 57 | + defaultmodel = self.model_file.replace(targetdir, defaultdir) |
| 58 | + copyfile(defaultmodel, self.model_file) |
| 59 | + if len(self.log_Image) > 0: |
| 60 | + if not os.path.exists(self.log_Image): |
| 61 | + zerlegt = self.log_Image.split('/') |
| 62 | + pfad = zerlegt[0] |
| 63 | + for i in range(1, len(zerlegt)): |
| 64 | + pfad = pfad + '/' + zerlegt[i] |
| 65 | + if not os.path.exists(pfad): |
| 66 | + os.makedirs(pfad) |
| 67 | + |
| 68 | + def Readout(self, PictureList, logtime): |
| 69 | + self.result = [] |
| 70 | + for image in PictureList: |
| 71 | + value = self.ReadoutSingleImage(image[1]) |
| 72 | + if len(self.log_Image) > 0: |
| 73 | + self.saveLogImage(image, value, logtime) |
| 74 | + self.result.append(value) |
| 75 | + return self.result |
| 76 | + |
| 77 | + def ReadoutSingleImage(self, image): |
| 78 | + test_image = cv2.resize(image,(20,32), interpolation = cv2.INTER_CUBIC) |
| 79 | + cv2.imwrite('./image_tmp/resize.jpg', test_image) |
| 80 | + test_image = np.array(test_image, dtype="float32") |
| 81 | +# test_image/=255. |
| 82 | + img = np.reshape(test_image,[1,32,20,3]) |
| 83 | + result = self.model.predict_classes(img) |
| 84 | + if result == 10: |
| 85 | + result = "NaN" |
| 86 | + else: |
| 87 | + result = result[0] |
| 88 | + return result |
| 89 | + |
| 90 | + def saveLogImage(self, image, value, logtime): |
| 91 | + if (len(self.LogNames) > 0) and (not image[0] in self.LogNames): |
| 92 | + return |
| 93 | + speichername = image[0] + '_' + logtime + '.jpg' |
| 94 | + speichername = self.log_Image + '/' + str(value) + '/' + speichername |
| 95 | + cv2.imwrite(speichername, image[1]) |
0 commit comments