-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraphtarget.py
103 lines (81 loc) · 3.06 KB
/
graphtarget.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import numpy as np
import pickle
from data_collection import load_collections
from define import define
from resources import resources_dirname
area = define.details_areas['graphtarget']['label']
filepath = os.path.join(resources_dirname, 'graphtarget.res')
if os.path.exists(filepath):
with open(filepath, 'rb') as f:
table = pickle.load(f)
else:
table = {}
def get_graphtarget(image_details):
if image_details.height != define.details_trimsize[1]:
return None
np_value = np.array(image_details.crop(area))
flattend = np_value.flatten()
unique, counts = np.unique(flattend, return_counts=True)
dark_count = np.count_nonzero(unique < 100)
mode = unique[np.argmax(counts[dark_count:])+dark_count]
if not mode in table.keys():
return None
count = np.count_nonzero(flattend==mode)
if not count in table[mode].keys():
return None
return table[mode][count]
def larning_graphtarget(targets):
global table
print('larning graphtarget')
print(f'count: {len(targets)}')
table = {}
keys = {}
values = []
for key, target in targets.items():
value = target['value']
np_value = target['np']
flattend = np_value.flatten()
unique, counts = np.unique(flattend, return_counts=True)
dark_count = np.count_nonzero(unique < 100)
mode = unique[np.argmax(counts[dark_count:])+dark_count]
if not mode in table.keys():
table[mode] = {}
keys[mode] = {}
count = np.count_nonzero(flattend==mode)
if count in table[mode].keys():
if table[mode][count] != value:
print(f'mode: {mode}')
print(f'count: {count}')
print(f'value: {value}')
print(f'key: {key}')
print(f'duplicate: {table[mode][count]}, {keys[mode][count]}')
print("NG")
return
else:
table[mode][count] = value
keys[mode][count] = key
values.append(value)
uniques, counts = np.unique(np.array(values), return_counts=True)
multicount_values = [value for value in np.where(counts>=2,uniques,None) if value is not None]
if len(multicount_values) != 0:
print(multicount_values)
print('NG')
return
with open(filepath, 'wb') as f:
pickle.dump(table, f)
return table
if __name__ == '__main__':
collections = load_collections()
targets = {}
for collection in collections:
label = collection.label
if collection.details is not None:
image = collection.details
if 'graphtarget' in label['details'].keys() and label['details']['graphtarget'] != '':
targets[collection.key] = {
'value': label['details']['graphtarget'],
'np': np.array(image.crop(area))
}
larning_graphtarget(targets)
print(table)