Created
January 4, 2021 18:15
-
-
Save WisnuDS/63eb3f8ec6f1e10f9ee124764701211c to your computer and use it in GitHub Desktop.
This Code for Final Exam in Pattern Recognition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import matplotlib.pyplot as plt | |
import pandas as pd | |
from sklearn.neighbors import KNeighborsClassifier | |
# Segmentation using equalize in colorize feature | |
# Segmentation for green feature | |
green = [] | |
for i in range(1, 41): | |
name = "Hijau " + (("0" + str(i)) if i < 10 else str(i)) | |
img = cv2.imread("PCD cabai jadi/" + name + ".png") | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
gray[:, :, 0] = cv2.equalizeHist(gray[:, :, 0]) | |
image = cv2.cvtColor(gray, cv2.COLOR_HSV2RGB) | |
green.append(gray) | |
# Segmentation for red feature | |
red = [] | |
for i in range(1, 41): | |
name = "Rawit " + (("0" + str(i)) if i < 10 else str(i)) | |
img = cv2.imread("PCD cabai jadi/" + name + ".png") | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
gray[:, :, 0] = cv2.equalizeHist(gray[:, :, 0]) | |
red.append(gray) | |
# Feature Extraction | |
# From the results of segmentation, it can be seen that | |
# there are characteristics where the red chilies after being | |
# segmented will produce a higher green value than green chilies. | |
# Feature Extraction for red | |
loc_red = [] | |
for i in red: | |
total = 0 | |
for j in i: | |
for k in j: | |
if k[1] > 200: | |
total += 1 | |
loc_red.append(total) | |
# Feature Extraction for green | |
loc_green = [] | |
for i in green: | |
total = 0 | |
for j in i: | |
for k in j: | |
if k[1] > 200: | |
total += 1 | |
print("Counting") | |
loc_green.append(total) | |
# Before entering the classification, the dataset is split into training data and test data. | |
dfGreen = pd.DataFrame({ | |
"pixel": loc_green, | |
}) | |
dfGreenLabel = pd.DataFrame({ | |
"color": ["Hijau" for i in range(1, 41)] | |
}) | |
dfRed = pd.DataFrame({ | |
"pixel": loc_red, | |
}) | |
dfRedLabel = pd.DataFrame({ | |
"color": ["Merah" for i in range(1, 41)] | |
}) | |
redTrain = dfRed.iloc[:, :30] | |
greenTrain = dfGreen.iloc[:, :30] | |
labelRedTrain = dfRedLabel.iloc[:, :30] | |
labelGreenTrain = dfGreenLabel.iloc[:, :30] | |
redTest = dfRed.iloc[31:, :] | |
greenTest = dfGreen.iloc[31:, :] | |
labelRedTest = dfRedLabel.iloc[31:, :] | |
labelGreenTest = dfGreenLabel.iloc[31:, :] | |
# Process classification using KNN. I use library KNN in scikit learn | |
# Process Training using data training | |
knn = KNeighborsClassifier(n_neighbors=5) | |
knn.fit(pd.concat([greenTrain, redTrain]), pd.concat([labelGreenTrain, labelRedTrain])) | |
# Process Prediction | |
print("Prediction") | |
print(knn.predict(pd.concat([greenTest, redTest]))) | |
# Process accuracy calculation | |
print("Score") | |
print(knn.score(pd.concat([greenTest, redTest]),pd.concat([labelGreenTest,labelRedTest]))) | |
# Show plot data set from feature extraction | |
plt.plot(range(1, 41), loc_red, 'o', range(1, 41), loc_green, 'o') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment