|
|
|
@ -6,6 +6,7 @@ import argparse
|
|
|
|
|
import imutils
|
|
|
|
|
import cv2
|
|
|
|
|
import math
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
itemw = 0
|
|
|
|
|
itemh = 0
|
|
|
|
@ -44,6 +45,11 @@ def smaller(a, b):
|
|
|
|
|
return a
|
|
|
|
|
else:
|
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
|
def near(a, b, close):
|
|
|
|
|
if abs(a-b) < close:
|
|
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
# construct the argument parse and parse the arguments
|
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
|
ap.add_argument("-i", "--image", required=True,
|
|
|
|
@ -70,16 +76,13 @@ if args2.show:
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
gray = cv2.GaussianBlur(gray, (7, 7), 0)
|
|
|
|
|
if args2.show:
|
|
|
|
|
cv2.imshow("Item Sorter", gray)
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# perform edge detection, then perform a dilation + erosion to
|
|
|
|
|
# close gaps in between object edges
|
|
|
|
|
edged = cv2.Canny(gray, 50, 100)
|
|
|
|
|
edged = cv2.dilate(edged, None, iterations=1)
|
|
|
|
|
edged = cv2.erode(edged, None, iterations=1)
|
|
|
|
|
#edged = cv2.erode(edged, None, iterations=1)
|
|
|
|
|
|
|
|
|
|
if args2.show:
|
|
|
|
|
cv2.imshow("Item Sorter", edged)
|
|
|
|
@ -131,14 +134,20 @@ for c in cnts:
|
|
|
|
|
else:
|
|
|
|
|
circular = True
|
|
|
|
|
cv2.circle(orig,(int(x),int(y)),int(radius),(0,255,0),2)
|
|
|
|
|
|
|
|
|
|
if pixelsPerMetric is None and circular is True:
|
|
|
|
|
mask = np.zeros(gray.shape,np.uint8)
|
|
|
|
|
cv2.drawContours(mask,[c],0,255,-1)
|
|
|
|
|
#pixelpoints = np.transpose(np.nonzero(mask))
|
|
|
|
|
mean_val = cv2.mean(orig,mask = mask)
|
|
|
|
|
#print(str(mean_val))
|
|
|
|
|
if pixelsPerMetric is None and circular is True and near(mean_val[0], 63, 30) is True and near(mean_val[1], 108, 30) is True and near(mean_val[2], 104, 30) is True:
|
|
|
|
|
pixelsPerMetric = smaller(dA, dB) / args["width"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
orig = image.copy()
|
|
|
|
|
# loop over the contours individually
|
|
|
|
|
for c in cnts:
|
|
|
|
|
|
|
|
|
|
num += 1
|
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
|
|
if cv2.contourArea(c) < 100 or pixelsPerMetric is None:
|
|
|
|
@ -233,7 +242,7 @@ for c in cnts:
|
|
|
|
|
#print(str(convexness) + " % fill")
|
|
|
|
|
#if not cv2.isContourConvex(approx):
|
|
|
|
|
#if cv2.matchShapes(hull, c, 1, 0.0) > 1:
|
|
|
|
|
if defects.size > 5 and (convexness < 0.9 or boxiness < 0.75):
|
|
|
|
|
if defects.size > 5 and (convexness < 0.9 and boxiness < 0.7):
|
|
|
|
|
objtype = "Screw"
|
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
|
#print("Screw Length (RAW): " + str(iteml))
|
|
|
|
@ -249,7 +258,7 @@ for c in cnts:
|
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(str(iteml))
|
|
|
|
|
#print(str(iteml))
|
|
|
|
|
# draw the object sizes on the image
|
|
|
|
|
if args2.show:
|
|
|
|
|
#cv2.putText(orig, "{:.5f}in".format(itemhr),
|
|
|
|
@ -258,6 +267,10 @@ for c in cnts:
|
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
|
(int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
|
if objtype == "Unknown":
|
|
|
|
|
cv2.putText(orig, "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh), # print axle length
|
|
|
|
|
(int(trbrX + 20), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
|
if objtype == "Screw":
|
|
|
|
|
cv2.putText(orig, str(iteml) + "in thread", # print screw length
|
|
|
|
|
(int(trbrX + 20), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
@ -272,5 +285,5 @@ for c in cnts:
|
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
|
|
|
|
|
|
# show the output image
|
|
|
|
|
cv2.imshow("Item Sorter", orig)
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
|
cv2.imshow("Item Sorter", orig)
|
|
|
|
|
cv2.waitKey(0)
|