Spaces:
Sleeping
Sleeping
svm
Browse files
app.py
CHANGED
|
@@ -44,9 +44,11 @@ dt = package['model']
|
|
| 44 |
|
| 45 |
with open("custom_logreg_hog.pkl", "rb") as f:
|
| 46 |
logreg_trained_model = pickle.load(f)
|
|
|
|
| 47 |
|
| 48 |
with open("svm2.pkl", "rb") as f:
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
with open("svmScaler.pkl", "rb") as f:
|
| 52 |
svm_scaler = pickle.load(f)
|
|
@@ -70,7 +72,7 @@ def preprocess_image(image, model="GNB"):
|
|
| 70 |
feature_vector=True
|
| 71 |
)
|
| 72 |
|
| 73 |
-
if model == "DT" or model == "SVM":
|
| 74 |
return image_hog
|
| 75 |
|
| 76 |
image_hog = image_hog.reshape(1, -1)
|
|
@@ -97,33 +99,13 @@ def classify_image(image):
|
|
| 97 |
|
| 98 |
|
| 99 |
def classify_with_logreg(image):
|
| 100 |
-
if
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
if np.mean(image) > 127:
|
| 107 |
-
image = cv2.bitwise_not(image)
|
| 108 |
-
|
| 109 |
-
image = image / 255.0
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
features = hog(
|
| 113 |
-
image,
|
| 114 |
-
orientations=9,
|
| 115 |
-
pixels_per_cell=(4, 4),
|
| 116 |
-
cells_per_block=(2, 2),
|
| 117 |
-
block_norm='L2-Hys',
|
| 118 |
-
feature_vector=True
|
| 119 |
-
)
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
processed_image = features.reshape(1, -1)
|
| 123 |
-
|
| 124 |
-
prediction = logreg_trained_model.predict(processed_image)
|
| 125 |
|
| 126 |
-
return "
|
| 127 |
|
| 128 |
def classify_with_DT(image):
|
| 129 |
if image is None:
|
|
@@ -131,30 +113,16 @@ def classify_with_DT(image):
|
|
| 131 |
processedImage = preprocess_image(image , "DT")
|
| 132 |
processedImage = processedImage.reshape(1, -1)
|
| 133 |
result = dt.predict(processedImage)
|
| 134 |
-
return "
|
| 135 |
|
| 136 |
|
| 137 |
def classify_with_SVM(image):
|
| 138 |
-
# if len(image.shape) == 3:
|
| 139 |
-
# image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 140 |
-
|
| 141 |
-
# image = cv2.resize(image, (28, 28))
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
# if np.mean(image) > 127:
|
| 145 |
-
# image = cv2.bitwise_not(image)
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
# image = image.reshape(1, -1)
|
| 149 |
-
# scaled_img = svm_scaler.transform(image)
|
| 150 |
-
|
| 151 |
-
|
| 152 |
if image is None:
|
| 153 |
return "Please upload an image."
|
| 154 |
processedImage = preprocess_image(image , "SVM")
|
| 155 |
processedImage = processedImage.reshape(1, -1)
|
| 156 |
result = svm_model.predict(processedImage)
|
| 157 |
-
return "
|
| 158 |
|
| 159 |
|
| 160 |
|
|
|
|
| 44 |
|
| 45 |
with open("custom_logreg_hog.pkl", "rb") as f:
|
| 46 |
logreg_trained_model = pickle.load(f)
|
| 47 |
+
print(type(logreg_trained_model))
|
| 48 |
|
| 49 |
with open("svm2.pkl", "rb") as f:
|
| 50 |
+
svm_package = pickle.load(f)
|
| 51 |
+
svm_model = svm_package['model']
|
| 52 |
|
| 53 |
with open("svmScaler.pkl", "rb") as f:
|
| 54 |
svm_scaler = pickle.load(f)
|
|
|
|
| 72 |
feature_vector=True
|
| 73 |
)
|
| 74 |
|
| 75 |
+
if model == "DT" or model == "LogReg" or model == "SVM":
|
| 76 |
return image_hog
|
| 77 |
|
| 78 |
image_hog = image_hog.reshape(1, -1)
|
|
|
|
| 99 |
|
| 100 |
|
| 101 |
def classify_with_logreg(image):
|
| 102 |
+
if image is None:
|
| 103 |
+
return "Please upload an image."
|
| 104 |
+
processedImage = preprocess_image(image , "LogReg")
|
| 105 |
+
processedImage = processedImage.reshape(1, -1)
|
| 106 |
+
prediction = logreg_trained_model.predict(processedImage)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
return "LogReg : " + "6" if prediction[0] == 1 else "Not 6"
|
| 109 |
|
| 110 |
def classify_with_DT(image):
|
| 111 |
if image is None:
|
|
|
|
| 113 |
processedImage = preprocess_image(image , "DT")
|
| 114 |
processedImage = processedImage.reshape(1, -1)
|
| 115 |
result = dt.predict(processedImage)
|
| 116 |
+
return "Decision Tree : " + "6" if result[0] == 1 else "Not 6"
|
| 117 |
|
| 118 |
|
| 119 |
def classify_with_SVM(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
if image is None:
|
| 121 |
return "Please upload an image."
|
| 122 |
processedImage = preprocess_image(image , "SVM")
|
| 123 |
processedImage = processedImage.reshape(1, -1)
|
| 124 |
result = svm_model.predict(processedImage)
|
| 125 |
+
return "SVM : " + "6" if result[0] == 1 else "Not 6"
|
| 126 |
|
| 127 |
|
| 128 |
|