-
Notifications
You must be signed in to change notification settings - Fork 0
Putting it all together
We've been busy working separately on each element of the project, it's now time to add them all together !
We've been throwing all of our code into a master script that was starting to look a bit messy. We took the time out to clean it up and piece everything together, but first, how are we going to do this ?
def threshold(image):
edge_image = cv2.Canny(image,100,200)
blur = cv2.blur(edge_image, (3, 3)) # blur the image
ret, thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY)
return threshdef find_length_and_height(thresh,image):
# Finding contours for the thresholded image
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
c = max(contours, key=cv2.contourArea)
# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
# Find the length and height
length = extRight[0] - extLeft[0]
height = extBot[1] - extTop[1]
float_length = float(extRight[0] - extLeft[0])
float_height = float(extBot[1] - extTop[1])
sizings = [length,height, round(float_length/float_height,2),extLeft,(extLeft[0] + length, extLeft[1]),extBot,(extBot[0], extBot[1] - height)]
ratio = float_length/float_height
# Draw lines on the orginal image showing the height and length of the vehicle
cv2.line(img = image, pt1 = extLeft, pt2 =(extLeft[0] + length, extLeft[1] ), color = (0,0,255), thickness = 9)
cv2.line(img = image, pt1 = extBot, pt2 =(extBot[0], extBot[1] - height), color = (0,0,255), thickness = 9)
sizeData = pd.DataFrame([sizings], columns=['Vehicle Length','Vehicle Height','Ratio','Hpoint1','Hpoint2','Vpoint1','Vpoint2'])
return sizeDatadef getWheelDistanceRatio(wheelDetails,vehicleDetails):
try:
return (abs( vehicleDetails.iloc[0][0] /(wheelDetails["X Coords"][0] - wheelDetails["X Coords"][1])))
except:
return 0 def getWheelCount(wheelDetails):
return(wheelDetails.shape[0])def getWheelDiamaterVehicleRatio(wheel_details,vehicle_details):
return ([round((vehicle_details['Vehicle Length'][0]/wheel_details['Wheel Diam'][0]),1),round((vehicle_details['Vehicle Height'][0]/wheel_details['Wheel Diam'][0]),1)])We see here a sample of our functions used to classify a vehicle built through functional programming. This allowed us to quickly and easily test out various different ratios from the vehicle, and try to resolve some sort of correlation between these values. In order to analyse these values, we needed to interate through each test vehicle image:
for i in range(1,8):
fileName = "Vehicles" + str(i)
image = cv2.imread("Project Images/"+ fileName + ".jpg")
imageHeight, imageWidth, channels = image.shape
orignal_image = image.copy()
extracted = background_extract(image,i)
thresholded = threshold(extracted)
#plt.imsave("threshed.jpg",extracted)
#plt.imshow(thresholded)
wheel_details = getAllActualWheels(cv2.cvtColor(orignal_image,cv2.COLOR_BGR2GRAY),i)
vehicle_details = find_length_and_height(thresholded, image)
wheel_count = getWheelCount(wheel_details)
vehicle_sizes = find_length_and_height(thresholded, image)
vehicle_length,vehicle_height,vehicle_ratio,hPoint1,hPoint2,vPoint1,vPoint2 = find_length_and_height(thresholded, image).iloc[0]
vehicle_wheel_distance = getWheelDistanceRatio(wheel_details,vehicle_details)
vehicle_wheel_distsance_ratio = round(getWheelDistanceRatio(vehicle_ratio,vehicle_details),2)
WheelDiamRatio = getWheelDiamaterVehicleRatio(wheel_details,vehicle_details)Within Jupyter Notebook Environment, we can quickly and easily view all of the calculated values within a dataframe:
FullReport.append([fileName,
imageWidth,
vehicle_sizes.iloc[0][0],
imageHeight,
vehicle_sizes.iloc[0][1],
vehicle_sizes.iloc[0][2],
vehicle_wheel_distance,
wheel_count,
WheelDiamRatio[0],
WheelDiamRatio[1],
wheel_details['Wheel Diam'][0],
vehicle_wheel_distsance_ratio,
None])
Report = pd.DataFrame(FullReport,columns=cols)
ReportFrom the results above, we started to develop a set of rules where we can differentiate one set of vehicle's ratios from the others. We had already calculated the length and width of the vehicles, so we knew we only had 2 levels of granularity with them. Now that we were able to calculate the number of wheels, along with the distance between the two of them, relative to the length/width of the vehicle. We hope to use these values down the road to help classify vehicles with a higher conference
