From be6da7f99c54ede63006a5045339aa9d8a40557a Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 10:45:41 +0900 Subject: [PATCH 1/7] Update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b479755..de03f38 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -"これ見えてない?" .idea +.env From ad293ff91cd16386d972b76b3fb6e33aaa914ea6 Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 10:51:58 +0900 Subject: [PATCH 2/7] =?UTF-8?q?=E5=B0=86=E6=A3=8B=E7=9B=A4=E3=81=AE?= =?UTF-8?q?=E6=9E=A0=E8=AA=8D=E8=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mepi_test.java | 5 ----- mepi_test.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) delete mode 100644 mepi_test.java create mode 100644 mepi_test.py diff --git a/mepi_test.java b/mepi_test.java deleted file mode 100644 index bb70802..0000000 --- a/mepi_test.java +++ /dev/null @@ -1,5 +0,0 @@ -class Hello { - public static void main(String[] args) { - System.out.println("Hello, World!"); - } -} \ No newline at end of file diff --git a/mepi_test.py b/mepi_test.py new file mode 100644 index 0000000..cd8c781 --- /dev/null +++ b/mepi_test.py @@ -0,0 +1,49 @@ +import cv2 +import numpy as np + +# 画像読み込み +image = cv2.imread("test2.png") +image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) + +# ノイズ軽減 +image_blur = cv2.GaussianBlur(image_gray, (5,5), 0) + +# Cannyエッジ +edges = cv2.Canny(image_blur, 30, 100) + +# 線を太らせる +kernel = np.ones((3,3), np.uint8) +edges = cv2.dilate(edges, kernel, iterations=2) + +# 輪郭検出 +contours, _ = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) + +image_result = image.copy() + +# 四角形数をカウント +square_count = 0 + +# 白背景画像(描画用) +image_blank = np.ones_like(image) * 255 + +for cnt in contours: + area = cv2.contourArea(cnt) + if area < 5000 or area>20000: # 小さい輪郭を除外 + cv2.drawContours(image_result, [cnt], -1, (255,0,0), 2), + continue + + peri = cv2.arcLength(cnt, True) + approx = cv2.approxPolyDP(cnt, 0.04 * peri, True) + + if len(approx) == 4: + square_count += 1 + cv2.drawContours(image_result, [approx], -1, (0,255,0), 2) + else: + cv2.drawContours(image_result, [approx], -1, (0,0,255), 2) + + +# 結果保存 +cv2.imwrite('detected_squares.png', image_result) + +# 結果出力 +print(f"検出された四角形の数: {square_count}") \ No newline at end of file From 29001cbd0b2187b3dda3f60fb84577dbaba004be Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 10:53:31 +0900 Subject: [PATCH 3/7] Create mepi_test_1.py --- mepi_test_1.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 mepi_test_1.py diff --git a/mepi_test_1.py b/mepi_test_1.py new file mode 100644 index 0000000..6d5f160 --- /dev/null +++ b/mepi_test_1.py @@ -0,0 +1,47 @@ +from sklearn import svm +import cv2 +import numpy as np +import glob +from joblib import dump + +# HOG +hog = cv2.HOGDescriptor( + _winSize=(64,64), + _blockSize=(16,16), + _blockStride=(8,8), + _cellSize=(8,8), + _nbins=9 +) + +X = [] +y = [] + +# データセット読み込み +for label in ['fu_up','fu_down', + 'tokinn_up','tokinn_down', + 'ou_up','ou_down', + 'gyoku_up','gyoku_down', + 'keima_up','keima_down', + 'None']: + for file in glob.glob(f'data/{label}/*.png'): + img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) + img = cv2.resize(img, (64,64)) + descriptor = hog.compute(img) + X.append(descriptor.flatten()) + y.append(label) + +X = np.array(X) +y = np.array(y) + +# SVM学習 +clf = svm.SVC(kernel='linear') +clf.fit(X, y) +dump(clf, 'shogi_model.joblib') + + +img = cv2.imread('test_keima.png', cv2.IMREAD_GRAYSCALE) +img = cv2.resize(img, (64,64)) +descriptor = hog.compute(img) +predicted = clf.predict([descriptor.flatten()]) + +print(f"推論結果: {predicted[0]}") \ No newline at end of file From 64f0e0749a7dae240849f717fab570e3b63eda25 Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 10:54:26 +0900 Subject: [PATCH 4/7] Update mepi_test_1.py --- mepi_test_1.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/mepi_test_1.py b/mepi_test_1.py index 6d5f160..0b0d35d 100644 --- a/mepi_test_1.py +++ b/mepi_test_1.py @@ -39,9 +39,10 @@ dump(clf, 'shogi_model.joblib') -img = cv2.imread('test_keima.png', cv2.IMREAD_GRAYSCALE) -img = cv2.resize(img, (64,64)) -descriptor = hog.compute(img) -predicted = clf.predict([descriptor.flatten()]) +img = cv2.imread('test_keima.png', cv2.IMREAD_GRAYSCALE); +img = cv2.resize(img, (64,64)); +descriptor = hog.compute(img); +predicted = clf.predict([descriptor.flatten()]); + +print(f"推論結果: {predicted[0]}"); -print(f"推論結果: {predicted[0]}") \ No newline at end of file From a11ee216b0f3a319178bec1536d00bc190927038 Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 10:55:03 +0900 Subject: [PATCH 5/7] Update mepi_test.py --- mepi_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mepi_test.py b/mepi_test.py index cd8c781..29d2c3d 100644 --- a/mepi_test.py +++ b/mepi_test.py @@ -37,13 +37,13 @@ if len(approx) == 4: square_count += 1 - cv2.drawContours(image_result, [approx], -1, (0,255,0), 2) + cv2.drawContours(image_result, [approx], -1, (0,255,0), 2); else: - cv2.drawContours(image_result, [approx], -1, (0,0,255), 2) + cv2.drawContours(image_result, [approx], -1, (0,0,255), 2); # 結果保存 -cv2.imwrite('detected_squares.png', image_result) +cv2.imwrite('detected_squares.png', image_result); # 結果出力 -print(f"検出された四角形の数: {square_count}") \ No newline at end of file +print(f"検出された四角形の数: {square_count}"); \ No newline at end of file From cd0f4cee4e8d0401cf8d8d94f46e0f894701793b Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 11:04:21 +0900 Subject: [PATCH 6/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mepi_test.py | 8 ++++---- mepi_test_1.py | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/mepi_test.py b/mepi_test.py index 29d2c3d..cd8c781 100644 --- a/mepi_test.py +++ b/mepi_test.py @@ -37,13 +37,13 @@ if len(approx) == 4: square_count += 1 - cv2.drawContours(image_result, [approx], -1, (0,255,0), 2); + cv2.drawContours(image_result, [approx], -1, (0,255,0), 2) else: - cv2.drawContours(image_result, [approx], -1, (0,0,255), 2); + cv2.drawContours(image_result, [approx], -1, (0,0,255), 2) # 結果保存 -cv2.imwrite('detected_squares.png', image_result); +cv2.imwrite('detected_squares.png', image_result) # 結果出力 -print(f"検出された四角形の数: {square_count}"); \ No newline at end of file +print(f"検出された四角形の数: {square_count}") \ No newline at end of file diff --git a/mepi_test_1.py b/mepi_test_1.py index 0b0d35d..1887ebc 100644 --- a/mepi_test_1.py +++ b/mepi_test_1.py @@ -39,10 +39,10 @@ dump(clf, 'shogi_model.joblib') -img = cv2.imread('test_keima.png', cv2.IMREAD_GRAYSCALE); -img = cv2.resize(img, (64,64)); -descriptor = hog.compute(img); -predicted = clf.predict([descriptor.flatten()]); +img = cv2.imread('test_keima.png', cv2.IMREAD_GRAYSCALE) +img = cv2.resize(img, (64,64)) +descriptor = hog.compute(img) +predicted = clf.predict([descriptor.flatten()]) -print(f"推論結果: {predicted[0]}"); +print(f"推論結果: {predicted[0]}") From 71d3f3e3736bf7dd8ff1347b42239d5ffbc5ada4 Mon Sep 17 00:00:00 2001 From: mepi02 Date: Tue, 19 Aug 2025 11:05:29 +0900 Subject: [PATCH 7/7] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=89=88=EF=BC=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mepi_test_1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mepi_test_1.py b/mepi_test_1.py index 1887ebc..5d164cf 100644 --- a/mepi_test_1.py +++ b/mepi_test_1.py @@ -13,7 +13,7 @@ _nbins=9 ) -X = [] +x = [] y = [] # データセット読み込み @@ -27,10 +27,10 @@ img = cv2.imread(file, cv2.IMREAD_GRAYSCALE) img = cv2.resize(img, (64,64)) descriptor = hog.compute(img) - X.append(descriptor.flatten()) + x.append(descriptor.flatten()) y.append(label) -X = np.array(X) +x = np.array(x) y = np.array(y) # SVM学習