Question: This is my code (machine learning - classification): #cell 1 # First we import the necessary libraries import os import scipy import matplotlib import numpy
This is my code (machine learning - classification):
#cell 1
# First we import the necessary libraries
import os
import scipy
import matplotlib
import numpy as np
import random
import scipy.signal
import statistics
import math
import cv2 # you need to install opencv library, coz it will helpin
# getting the peaks in theimage
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
# In[2]:
# Each image has a mid line from where the peaks start.
# we are trying to detect the row number of that line in theimage.
# The way we are detecting it is through the colour pixels in theimage.
# Each blue colour pixel in the image has the RGB values(0,114,189)
# so if a row contains more than 50 pixels consecutively whichcontains this
# colour, we conclude that it is the middle line.
def get_mid_line(image):
count=[0]*len(image)
for y in range(len(image)):
for x in range(len(image[y])):
if(np.array_equal(image[y][x], np.array([0,114,189]))):
count[y] +=1
max=0 #defining max nubmer of points at any 'y' to0
index = 0 #defining intial index to 0
#finding max and saving it's 'y' index
for y in range(len(image)):
if(max
index=y #this tells aboutindex that has highest value
return index #the index with middle line'sy-coordinate
# In[3]:
# now we will get the sum of the peaks in the given image
# To do this we use opencv's cornerHarris function.
# this function gives the corners in an image which in our case ispeaks.
# we get the coordinates of these peaks through the cornerHarrisfunction,
# and we subtract it from the coordinates of mid line in order tofind the
# height of the peak. Then we add all these heights to get the sumof the
# heights which is our feature for classifying the image.
def give_peak_sum(file):
image = cv2.imread(file) # opencv's image readfunction
image_copy = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #converts image from
# BGR color space to RGB color space
image_dims = image.shape
x_dim = image_dims[1]
# converting to gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = np.float32(gray)
# detect corners
dst = cv2.cornerHarris(gray, 2, 3, 0.04)
# dilate corner image to enhance corner points
dst = cv2.dilate(dst, None)
thresh = 0.02*dst.max()
peak_sum = 0
mid_line = get_mid_line(image_copy) # using thepreviously defined function
X = list()
Y = list()
for j in range(0, dst.shape[0]):
for i in range(0, dst.shape[1]):
if (dst[j, i] > threshand i < 0.4*x_dim):
X.append(j)
Y.append(i)
peak_sum +=abs(j-mid_line)
return (Y,peak_sum)
# In[4]:
# Here we are just collecting the data that we have
full_images = []
bottom_images = []
for file inos.listdir("C:/Users/anwer/Desktop/copy/machine/full"): # I don'tknow the path of the data files
# in your system,modify the path accordingly.
full_images.append(file)
for file inos.listdir("C:/Users/anwer/Desktop/copy/machine/bottom"):
bottom_images.append(file)
print (len(full_images), len(bottom_images))
# In[5]:
# Here for each image, we will get the sum of peaks. This is themain
# training step, it might take around 30-45 minutes to rundepending on your
# system capabilities
data_full = []
#data_full3=[]
data_bottom = []
#data_bottom2 = []
for file in full_images:
data_full.append((give_peak_sum("C:/Users/anwer/Desktop/copy/machine/full/"+file),0))
#data_full2.append((give_peak_sum("C:/Users/anwer/Desktop/copy/machine/full/"+file)))
data_full2 = []
for i in range(len(data_full)):
data_full2.append(data_full[i][0])
print(data_full2)
#print(file)
for file in bottom_images:
data_bottom.append((give_peak_sum("C:/Users/anwer/Desktop/copy/machine/bottom/"+file),1))
#data_bottom2.append((give_peak_sum("C:/Users/anwer/Desktop/copy/machine/bottom/"+file)))
data_bottom2 = []
for i in range(len(data_bottom)):
data_bottom2.append(data_bottom[i][0])
print(data_bottom2)
print (len(data_full), len(data_bottom))
# In[6]:
# Here we mix the two lists data_full and data_bottom and shuffleit for
# randomness
data = data_full + data_bottom
random.shuffle(data)
# In[7]:
# here we take features in x variable and labels in y variablebecause
# skikit learn libraries require it differently
x = [[each[0]] for each in data]
y = [[each[1]] for each in data]
print (len(x), len(y))
# In[8]:
# Here we split our dataset of total 228 images into training andtesting
# datasets
x_train, x_test, y_train, y_test = train_test_split(x, y,test_size=0.2, random_state=42)
print (len(x_train), len(x_test))
print (len(y_train), len(y_test))
# In[9]:
# Here we train our data on a Random Forest Classifieralgorithm
from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators=100, max_depth=5,random_state=0)
clf.fit(x_train, y_train)
I am getting this error at the last line:
ValueError: setting an array element with a sequence.
Note:
y-train = [[0], [0], [0], [0], [0], [1], [0], [1], [0],[1], [1], [0], [0], [0], [1], [0], [1], [1], [0], [0], [1], [0],[0], [0], [1], [1], [1], [1], [0], [1], [0], [1], [1], [0], [0],[1], [0], [1], [0], [0], [0], [0], [0], [0], [1], [0],.....etc]
x-train = [[([349, 349, 349, 302, 303, 304, 305, 302, 303,304, 305, 323, 324, 325, 326, 302, 303, 304, 305, 323, 324, 325,326, 302, 303, 304, 305, 323, 324, 325, 326, 327, 303, 304, 305,323, 324, 325, 326, 327, 323, 324, 325, 326, 327, 323, 324, 325,326, 327, 323, 324, 325, 326, 327, 324, 325, 326, 327, 303, 304,305, 306, 303, 304, 305, 306, 303, 304, 305, 306, 303, 304, 305,303, 304, 305, 303, 304, 305, 282, 283, 284, 285, 282, 283, 284,285, 282, 283, 284, 285, 282, 283, 284, 285, 282, 283, 284, 285,282, 283, 284, 285, 282, 283, 284, 285, 282, 283, 284, 285, 262,263, 264, 265, 261, 262, 263, 264, 265, 261, 262, 263, 264, 265,187, 188, 189, 190, 207, 208, 209, 210, 211, 261, 262, 263, 264,265, 187, 188, 189, 190, 207, 208, 209, 210, 211, 261, 262, 263,187, 188, 189, 190, 207, 208, 209, 210, 211, 261, 262, 263, 187,188, 189, 190, 207, 208, 209, 210, 211, 167, 168, 169, 170, 188,189, 190, 207, 208, 209, 210, 211, 235, 236, 237, 238, 146, 147,148, 149, 150, 167, 168, 169, 170, 188, 189, 190, 207, 208, 209,210, 234, 235, 236, 237, 238, 146, 147, 148, 149, 150, 166, 167,168, 169, 170, 188, 189, 190, 208, 209, 210, 234, 235, 236, 237,238, 146, 147, 148, 149, 150, 166, 167, 168, 169, 170, 188, 189,190, 228, 229, 230, 231, 232, 234, 235, 236, 237, 238, 126, 127,128, 129, 146, 147, 148, 149, 150, 166, 167, 168, 169, 170, 228,229, 230, 231, 232, 234, 235, 236, 237, 238, 126, 127, 128, 129,146, 147, 148, 149, 150, 166, 167, 168, 169, 228, 229, 230, 231,232, 126, 127, 128, 129, 147, 148, 149, 167, 168, 169, 228, 229,230, 231, 232, 126, 127, 128, 129, 147, 148, 149, 228, 229, 230,231, 119, 120, 121, 119, 120, 121, 134, 135, 136, 153, 154, 155,156, 160, 161, 162, 163, 119, 120, 121, 134, 135, 136, 140, 141,142, 143, 153, 154, 155, 156, 157, 159, 160, 161, 162, 163, 119,120, 121, 134, 135, 136, 139, 140, 141, 142, 143, 153, 154, 155,156, 157, 159, 160, 161, 162, 163, 173, 174, 175, 176, 177, 134,135, 136, 139, 140, 141, 142, 143, 153, 154, 155, 156, 157, 159,160, 161, 162, 163, 173, 174, 175, 176, 177, 180, 181, 182, 139,140, 141, 142, 143, 153, 154, 155, 156, 160, 161, 162, 163, 173,174, 175, 176, 177, 180, 181, 182, 243, 244, 245, 140, 141, 142,143, 173, 174, 175, 176, 177, 180, 181, 182, 243, 244, 245, 180,181, 182, 200, 201, 202, 203, 243, 244, 245, 200, 201, 202, 203,204, 248, 249, 250, 251, 200, 201, 202, 203, 204, 221, 222, 223,247, 248, 249, 250, 251, 200, 201, 202, 203, 204, 221, 222, 223,247, 248, 249, 250, 251, 200, 201, 202, 203, 204, 221, 222, 223,247, 248, 249, 250, 251, 200, 201, 202, 203, 204, 221, 222, 223,248, 249, 250, 251, 200, 201, 202, 203, 204, 221, 222, 223, 224,248, 249, 250, 251, 221, 222, 223, 224, 221, 222, 223, 224, 221,222, 223, 224, 270, 271, 272, 270, 271, 272, 270, 271, 272, 269,270, 271, 272, 268, 269, 270, 271, 272, 268, 269, 270, 271, 272,268, 269, 270, 271, 272, 268, 269, 270, 271, 272, 295, 296, 297,295, 296, 297, 295, 296, 297, 295, 296, 297, 295, 296, 297, 295,296, 297, 295, 296, 297, 298, 295, 296, 297, 298, 295, 296, 297,298, 295, 296, 297, 298, 299, 295, 296, 297, 298, 299, 295, 296,297, 298, 299, 295, 296, 297, 298, 299, 316, 317, 318, 316, 317,318, 316, 317, 318, 316, 317, 318, 319, 316, 317, 318, 319, 337,338, 339, 316, 317, 318, 319, 337, 338, 339, 337, 338, 339, 316,317, 318, 336, 337, 338, 339, 316, 317, 318, 336, 337, 338, 339,316, 317, 318, 336, 337, 338, 339, 316, 317, 318, 316, 317, 318,316, 317, 318, 319, 316, 317, 318, 319, 316, 317, 318, 319, 316,317, 318, 319, 337, 338, 339, 337, 338, 339, 337, 338, 339, 337,338, 339, 336, 337, 338, 339, 336, 337, 338, 339, 336, 337, 338,339, 336, 337, 338, 339], 20474)], [(..rtc]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
