RemuNus commited on
Commit
aeb6a70
1 Parent(s): 28d01e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -13,17 +13,17 @@ from google.oauth2 import service_account
13
  array = []
14
  # アイテムの類似率
15
  item_similarity = 0.9
16
- ocr_similarity = 0.4
17
  # 2値化のためのしきい値を設定
18
  thresh_value = 128
19
  # スタックしないアイテムの最大カウント
20
  unique_item_count = 5
21
 
22
- def mask(img):
23
- mask = img[:,:,3]
24
  mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
25
  mask = mask.astype(np.float32) / 255.0
26
- rgb = img[:,:,0:3]
27
  rgb = rgb.astype(np.float32) * mask
28
  rgb = np.round(rgb).astype(np.uint8)
29
  return rgb
@@ -35,23 +35,28 @@ def gray_binary(img):
35
 
36
  def Scan(input_image, progress=gr.Progress()):
37
  global array
 
38
  # BGRからRGBに変換
39
  input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
40
 
41
- # 一時ファイルに画像を保存してパスを取得
42
  temp_image_path = os.path.join(tempfile.gettempdir(), "temp_image.jpg")
43
  cv2.imwrite(temp_image_path, input_image)
44
-
45
- # 画像を読み込む(RGBA形式)
46
  source_image = cv2.imread(temp_image_path , cv2.IMREAD_UNCHANGED)
47
-
48
- # 一時ファイルを削除
49
  os.remove(temp_image_path)
50
 
51
  # 検出用画像の入ったフォルダパス指定
52
  item_folder_path = './Image/ItemImage/'
53
  unique_item_folder_path = './Image/UniqueItemImage/'
54
  ocr_folder_path = './Image/Number/'
 
 
 
 
 
 
 
 
55
 
56
  # スタックするアイテムをスキャン
57
  # フォルダ内の画像ファイル名をすべて取得
@@ -74,7 +79,7 @@ def Scan(input_image, progress=gr.Progress()):
74
  target_image_top_half = target_image[:half_height, :]
75
 
76
  # 透過を考慮してマスクを作成し適用
77
- target_image_top_half_rgb = mask(target_image_top_half)
78
 
79
  # マッチングを行う
80
  result = cv2.matchTemplate(source_image, target_image_top_half_rgb, cv2.TM_CCOEFF_NORMED)
@@ -103,12 +108,10 @@ def Scan(input_image, progress=gr.Progress()):
103
  number = 0
104
 
105
  # マッチした画像の下にある数字を切り取る(数字1文字7*13)
106
- x1, y1, x2, y2 = bottom_right[0] - width + 34 - num * 7, bottom_right[1] + 2, bottom_right[0] - 1 - num * 7, bottom_right[1] + half_height - 6
 
107
  region = source_image[y1:y2, x1:x2]
108
 
109
- # グレースケール+2値化
110
- region_gray_binary = gray_binary(region)
111
-
112
  # フォルダ内の画像ファイル名を取得
113
  number_image_files = [f for f in os.listdir(ocr_folder_path) if os.path.isfile(os.path.join(ocr_folder_path, f))]
114
 
@@ -117,13 +120,17 @@ def Scan(input_image, progress=gr.Progress()):
117
  if not number_image_files.endswith('.webp'):
118
  continue
119
 
120
- # 画像読込 マスク適用 グレースケール+2値化
121
  number_image = cv2.imread(os.path.join(ocr_folder_path, number_image_files), cv2.IMREAD_UNCHANGED)
122
- number_image = mask(number_image)
123
- nnumber_image_gray_binary = gray_binary(number_image)
 
 
 
 
124
 
125
  # マッチングを行う
126
- number_result = cv2.matchTemplate(region_gray_binary, nnumber_image_gray_binary, cv2.TM_CCOEFF_NORMED)
127
 
128
  # 類似度が最大の位置を取得
129
  min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(number_result)
@@ -144,7 +151,7 @@ def Scan(input_image, progress=gr.Progress()):
144
  array.append([filename_without_extension, item_count])
145
 
146
  # マッチした箇所を四角で囲む
147
- cv2.rectangle(source_image, top_left, (top_left[0] + width, top_left[1] + height), (0, 255, 0), 2)
148
 
149
  # スタックしないアイテムをスキャン
150
  # フォルダ内の画像ファイル名をすべて取得
@@ -167,7 +174,7 @@ def Scan(input_image, progress=gr.Progress()):
167
  target_image_top_half = target_image[:half_height, :]
168
 
169
  # 透過を考慮してマスクを作成し適用
170
- target_image_top_half_rgb = mask(target_image_top_half)
171
 
172
  # 最大(unique_item_count)個まで検出
173
  for num in range(unique_item_count):
@@ -196,7 +203,7 @@ def Scan(input_image, progress=gr.Progress()):
196
  array[index][1] += 1
197
 
198
  # マッチした箇所を四角で囲む
199
- cv2.rectangle(source_image, top_left, (top_left[0] + width, top_left[1] + height), (0, 255, 0), 2)
200
 
201
  # RGBからBGRに変換
202
  output_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2BGR)
 
13
  array = []
14
  # アイテムの類似率
15
  item_similarity = 0.9
16
+ ocr_similarity = 0.9
17
  # 2値化のためのしきい値を設定
18
  thresh_value = 128
19
  # スタックしないアイテムの最大カウント
20
  unique_item_count = 5
21
 
22
+ def mask(mask, rgb):
23
+ mask = mask[:,:,3]
24
  mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
25
  mask = mask.astype(np.float32) / 255.0
26
+ rgb = rgb[:,:,0:3]
27
  rgb = rgb.astype(np.float32) * mask
28
  rgb = np.round(rgb).astype(np.uint8)
29
  return rgb
 
35
 
36
  def Scan(input_image, progress=gr.Progress()):
37
  global array
38
+
39
  # BGRからRGBに変換
40
  input_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
41
 
42
+ # 一時ファイルに画像を保存して読み込む(RGBA形式)
43
  temp_image_path = os.path.join(tempfile.gettempdir(), "temp_image.jpg")
44
  cv2.imwrite(temp_image_path, input_image)
 
 
45
  source_image = cv2.imread(temp_image_path , cv2.IMREAD_UNCHANGED)
 
 
46
  os.remove(temp_image_path)
47
 
48
  # 検出用画像の入ったフォルダパス指定
49
  item_folder_path = './Image/ItemImage/'
50
  unique_item_folder_path = './Image/UniqueItemImage/'
51
  ocr_folder_path = './Image/Number/'
52
+ Inventory_img = cv2.imread('./Image/Inventory.png')
53
+
54
+ # 画像からイベントリ部分を抽出
55
+ result = cv2.matchTemplate(source_image, Inventory_img, cv2.TM_CCOEFF_NORMED)
56
+ min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
57
+ x1, y1 = max_loc[0] - 13, max_loc[1] - 11
58
+ x2, y2 = x1 + 454 , y1 + 804
59
+ source_image = source_image[y1:y2, x1:x2]
60
 
61
  # スタックするアイテムをスキャン
62
  # フォルダ内の画像ファイル名をすべて取得
 
79
  target_image_top_half = target_image[:half_height, :]
80
 
81
  # 透過を考慮してマスクを作成し適用
82
+ target_image_top_half_rgb = mask(target_image_top_half, target_image_top_half)
83
 
84
  # マッチングを行う
85
  result = cv2.matchTemplate(source_image, target_image_top_half_rgb, cv2.TM_CCOEFF_NORMED)
 
108
  number = 0
109
 
110
  # マッチした画像の下にある数字を切り取る(数字1文字7*13)
111
+ x1, y1 = bottom_right[0] - width + 34 - num * 7, bottom_right[1] + 2
112
+ x2, y2 = x1 + 7 , y1 + 13
113
  region = source_image[y1:y2, x1:x2]
114
 
 
 
 
115
  # フォルダ内の画像ファイル名を取得
116
  number_image_files = [f for f in os.listdir(ocr_folder_path) if os.path.isfile(os.path.join(ocr_folder_path, f))]
117
 
 
120
  if not number_image_files.endswith('.webp'):
121
  continue
122
 
123
+ # 比較画像読込 マスク適用 グレースケール+2値化
124
  number_image = cv2.imread(os.path.join(ocr_folder_path, number_image_files), cv2.IMREAD_UNCHANGED)
125
+ number_image_mask = mask(number_image, number_image)
126
+ number_image_mask_gray_binary = gray_binary(number_image_mask)
127
+
128
+ # トリミング画像 マスク適用 グレースケール+2値化
129
+ region_mask = mask(number_image, region)
130
+ region_mask_gray_binary = gray_binary(region_mask)
131
 
132
  # マッチングを行う
133
+ number_result = cv2.matchTemplate(region_mask_gray_binary, number_image_mask_gray_binary, cv2.TM_CCOEFF_NORMED)
134
 
135
  # 類似度が最大の位置を取得
136
  min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(number_result)
 
151
  array.append([filename_without_extension, item_count])
152
 
153
  # マッチした箇所を四角で囲む
154
+ cv2.rectangle(source_image, top_left, (top_left[0] + width - 1, top_left[1] + height - 1), (0, 255, 0), 2)
155
 
156
  # スタックしないアイテムをスキャン
157
  # フォルダ内の画像ファイル名をすべて取得
 
174
  target_image_top_half = target_image[:half_height, :]
175
 
176
  # 透過を考慮してマスクを作成し適用
177
+ target_image_top_half_rgb = mask(target_image_top_half, target_image_top_half)
178
 
179
  # 最大(unique_item_count)個まで検出
180
  for num in range(unique_item_count):
 
203
  array[index][1] += 1
204
 
205
  # マッチした箇所を四角で囲む
206
+ cv2.rectangle(source_image, top_left, (top_left[0] + width - 1, top_left[1] + height - 1), (0, 255, 0), 2)
207
 
208
  # RGBからBGRに変換
209
  output_image = cv2.cvtColor(source_image, cv2.COLOR_RGB2BGR)