Spaces:
Running
Running
Ubuntu
commited on
Commit
·
c19b663
1
Parent(s):
533425c
add application file
Browse files- .gitignore +5 -0
- app.py +122 -0
- logo.png +0 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.save
|
| 2 |
+
*.pickle
|
| 3 |
+
data
|
| 4 |
+
venv
|
| 5 |
+
input.png
|
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import glob
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
from sklearn import svm
|
| 5 |
+
import zipfile
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from sklearn.decomposition import PCA
|
| 8 |
+
from PIL import Image
|
| 9 |
+
import numpy as np
|
| 10 |
+
from sklearn.preprocessing import StandardScaler
|
| 11 |
+
from sklearn.svm import OneClassSVM
|
| 12 |
+
import numpy as np
|
| 13 |
+
import skimage
|
| 14 |
+
from skimage.feature import hog
|
| 15 |
+
from skimage.color import rgb2gray
|
| 16 |
+
from skimage import io
|
| 17 |
+
from sklearn.decomposition import PCA
|
| 18 |
+
from sklearn.svm import OneClassSVM
|
| 19 |
+
from sklearn.preprocessing import StandardScaler
|
| 20 |
+
import os
|
| 21 |
+
from tqdm import tqdm
|
| 22 |
+
import pickle
|
| 23 |
+
import joblib
|
| 24 |
+
|
| 25 |
+
def extract_hog_features(image_path):
|
| 26 |
+
"""
|
| 27 |
+
画像ファイルからHOG特徴量を抽出します。
|
| 28 |
+
|
| 29 |
+
:param image_path: 画像ファイルのパス
|
| 30 |
+
:return: HOG特徴量のNumPy配列
|
| 31 |
+
"""
|
| 32 |
+
# 画像を読み込む
|
| 33 |
+
img = io.imread(image_path)
|
| 34 |
+
img = img[:,:,:3]
|
| 35 |
+
|
| 36 |
+
# 画像をグレースケールに変換
|
| 37 |
+
gray_img = rgb2gray(img)
|
| 38 |
+
|
| 39 |
+
# HOG特徴量を抽出
|
| 40 |
+
features, _ = hog(gray_img, visualize=True, block_norm='L2-Hys')
|
| 41 |
+
|
| 42 |
+
return features
|
| 43 |
+
|
| 44 |
+
def prepare_features(image_paths):
|
| 45 |
+
"""
|
| 46 |
+
複数の画像からHOG特徴量を抽出し、特徴量の行列を作成します。
|
| 47 |
+
|
| 48 |
+
:param image_paths: 画像ファイルのパスのリスト
|
| 49 |
+
:return: 特徴量のNumPy配列
|
| 50 |
+
"""
|
| 51 |
+
features = []
|
| 52 |
+
for path in tqdm(image_paths):
|
| 53 |
+
features.append(extract_hog_features(path))
|
| 54 |
+
|
| 55 |
+
return np.array(features)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
import streamlit as st
|
| 59 |
+
with st.sidebar:
|
| 60 |
+
st.image("logo.png")
|
| 61 |
+
file_uploaded = st.file_uploader("Upload", type=["zip"])
|
| 62 |
+
if file_uploaded is not None:
|
| 63 |
+
if file_uploaded.type == "application/zip":
|
| 64 |
+
with zipfile.ZipFile(file_uploaded, "r") as z:
|
| 65 |
+
z.extractall("./data/")
|
| 66 |
+
|
| 67 |
+
test_img_path = st.file_uploader("Test image", type=["png","JPG"])
|
| 68 |
+
if test_img_path is not None:
|
| 69 |
+
test_img = Image.open(test_img_path)
|
| 70 |
+
test_img.resize((320,240)).save("input.png")
|
| 71 |
+
|
| 72 |
+
st.write("サイドバーより学習データをZipファイルとしてアップロードしボタンをクリック.")
|
| 73 |
+
if st.button("訓練開始"):
|
| 74 |
+
with st.spinner("1分ほどお待ちください..."):
|
| 75 |
+
image_paths = glob.glob("data/*/*.JPG")
|
| 76 |
+
col1, col2, col3 = st.columns(3) # 2列のコンテナを用意する
|
| 77 |
+
with col1:
|
| 78 |
+
st.image(image_paths[0])
|
| 79 |
+
with col2:
|
| 80 |
+
st.image(image_paths[1])
|
| 81 |
+
with col3:
|
| 82 |
+
st.image(image_paths[2])
|
| 83 |
+
features = prepare_features(image_paths)
|
| 84 |
+
print(features.shape)
|
| 85 |
+
scaler = StandardScaler()
|
| 86 |
+
features_scaled = scaler.fit_transform(features)
|
| 87 |
+
joblib.dump(scaler,"scaler.save")
|
| 88 |
+
print(features_scaled)
|
| 89 |
+
|
| 90 |
+
pca = PCA(n_components=4)
|
| 91 |
+
z_train = pca.fit_transform(features_scaled)
|
| 92 |
+
joblib.dump(pca,"pca.save")
|
| 93 |
+
print(z_train)
|
| 94 |
+
clf = svm.OneClassSVM(nu=0.2, kernel="rbf", gamma=0.001)
|
| 95 |
+
clf.fit(z_train)
|
| 96 |
+
with open('model.pickle', mode='wb') as fp:
|
| 97 |
+
pickle.dump(clf, fp)
|
| 98 |
+
st.info("学習が完了しました。テスト画像を入力してください。")
|
| 99 |
+
|
| 100 |
+
st.write("サイドバーよりテストデータを画像ファイルとしてアップロードしボタンをクリック.")
|
| 101 |
+
if st.button("推論開始"):
|
| 102 |
+
with open('model.pickle', mode='rb') as fp:
|
| 103 |
+
clf = pickle.load(fp)
|
| 104 |
+
|
| 105 |
+
features_test = prepare_features(["input.png"])
|
| 106 |
+
scaler = joblib.load("scaler.save")
|
| 107 |
+
features_scaled_test = scaler.transform(features_test)
|
| 108 |
+
pca = joblib.load("pca.save")
|
| 109 |
+
z_test = pca.transform(features_scaled_test)
|
| 110 |
+
pred = clf.predict(z_test)
|
| 111 |
+
print(pred)
|
| 112 |
+
|
| 113 |
+
st.image(test_img)
|
| 114 |
+
if pred[0] == 1:
|
| 115 |
+
st.info("入力画像は「正常」です。")
|
| 116 |
+
else:
|
| 117 |
+
st.info("入力画像は「異常」である可能性があります。")
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
logo.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
scikit-learn
|
| 3 |
+
scikit-image
|
| 4 |
+
matplotlib
|
| 5 |
+
tqdm
|