fix bug with circle vs rectangle; basic detection for spacers, washers, and keps nuts
This commit is contained in:
parent
b852d57345
commit
58b90af9c3
53
main.py
53
main.py
@ -66,6 +66,8 @@ def swap(a, b):
|
|||||||
ap = argparse.ArgumentParser()
|
ap = argparse.ArgumentParser()
|
||||||
ap.add_argument("-i", "--image", required=True,
|
ap.add_argument("-i", "--image", required=True,
|
||||||
help="path to the input image")
|
help="path to the input image")
|
||||||
|
#ap.add_argument("-c", "--cascade", required=True,
|
||||||
|
# help="path to the cascade")
|
||||||
ap.add_argument("-w", "--width", type=float, required=True,
|
ap.add_argument("-w", "--width", type=float, required=True,
|
||||||
help="width of the left-most object in the image (in inches)")
|
help="width of the left-most object in the image (in inches)")
|
||||||
ap.add_argument("-n", "--number", type=int, required=False,
|
ap.add_argument("-n", "--number", type=int, required=False,
|
||||||
@ -89,14 +91,20 @@ if args2.show:
|
|||||||
cv2.imshow("Item Sorter", image)
|
cv2.imshow("Item Sorter", image)
|
||||||
cv2.waitKey(0)
|
cv2.waitKey(0)
|
||||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
gray = cv2.GaussianBlur(gray, (7, 7), 0)
|
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
||||||
|
if args2.show:
|
||||||
|
cv2.imshow("Item Sorter", gray)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
|
||||||
# perform edge detection, then perform a dilation + erosion to
|
# perform edge detection, then perform a dilation + erosion to
|
||||||
# close gaps in between object edges
|
# close gaps in between object edges
|
||||||
edged = cv2.Canny(gray, 50, 100)
|
edged = cv2.Canny(gray, 50, 100)
|
||||||
|
if args2.show:
|
||||||
|
cv2.imshow("Item Sorter", edged)
|
||||||
|
cv2.waitKey(0)
|
||||||
|
|
||||||
edged = cv2.dilate(edged, None, iterations=1)
|
edged = cv2.dilate(edged, None, iterations=1)
|
||||||
#edged = cv2.erode(edged, None, iterations=1)
|
edged = cv2.erode(edged, None, iterations=1)
|
||||||
|
|
||||||
if args2.show:
|
if args2.show:
|
||||||
cv2.imshow("Item Sorter", edged)
|
cv2.imshow("Item Sorter", edged)
|
||||||
@ -155,7 +163,7 @@ for c in cnts:
|
|||||||
#pixelpoints = np.transpose(np.nonzero(mask))
|
#pixelpoints = np.transpose(np.nonzero(mask))
|
||||||
hsv = cv2.cvtColor(orig, cv2.COLOR_BGR2HSV)
|
hsv = cv2.cvtColor(orig, cv2.COLOR_BGR2HSV)
|
||||||
mean_val = cv2.mean(hsv, mask=mask)
|
mean_val = cv2.mean(hsv, mask=mask)
|
||||||
print(str(mean_val[0]))
|
#print(str(mean_val[0]))
|
||||||
#print(", " + str(mean_val[0]/mean_val[2]))
|
#print(", " + str(mean_val[0]/mean_val[2]))
|
||||||
#print(", " + str(mean_val[2]/mean_val[1]))
|
#print(", " + str(mean_val[2]/mean_val[1]))
|
||||||
if pixelsPerMetric is None and circular is True and near(mean_val[0], 16, 4.5):
|
if pixelsPerMetric is None and circular is True and near(mean_val[0], 16, 4.5):
|
||||||
@ -246,6 +254,19 @@ for c in cnts:
|
|||||||
objtype = "Penny"
|
objtype = "Penny"
|
||||||
iteml = 0
|
iteml = 0
|
||||||
else:
|
else:
|
||||||
|
if circular and near(radius * 2 / pixelsPerMetric, 0.38, 0.03):
|
||||||
|
# Keps nut or spacer
|
||||||
|
objtype = "Spacer"
|
||||||
|
mask = np.zeros(gray.shape, np.uint8)
|
||||||
|
cv2.drawContours(mask, [c], 0, 255, -1)
|
||||||
|
#pixelpoints = np.transpose(np.nonzero(mask))
|
||||||
|
hsv = cv2.cvtColor(orig, cv2.COLOR_BGR2HSV)
|
||||||
|
mean_val = cv2.mean(hsv, mask=mask)
|
||||||
|
#print(str(mean_val[0]))
|
||||||
|
if near(mean_val[0], 47, 5) and near(mean_val[1], 70, 5) and near(mean_val[2], 78, 5):
|
||||||
|
objtype = "Keps Nut"
|
||||||
|
if circular and near(radius / pixelsPerMetric, 0.23, 0.02):
|
||||||
|
objtype = "Washer"
|
||||||
epsilon = 3 # 0.02*cv2.arcLength(c,True)
|
epsilon = 3 # 0.02*cv2.arcLength(c,True)
|
||||||
# print(str(epsilon))
|
# print(str(epsilon))
|
||||||
approx = cv2.approxPolyDP(c, epsilon, True)
|
approx = cv2.approxPolyDP(c, epsilon, True)
|
||||||
@ -259,18 +280,18 @@ for c in cnts:
|
|||||||
#print(str(convexness) + " % fill")
|
#print(str(convexness) + " % fill")
|
||||||
# if not cv2.isContourConvex(approx):
|
# if not cv2.isContourConvex(approx):
|
||||||
# if cv2.matchShapes(hull, c, 1, 0.0) > 1:
|
# if cv2.matchShapes(hull, c, 1, 0.0) > 1:
|
||||||
if defects.size > 5 and (convexness < 0.9 or boxiness < 0.75):
|
if defects is not None and defects.size > 5 and (convexness < 0.9 or boxiness < 0.75) and rectangular:
|
||||||
objtype = "Screw"
|
objtype = "Screw"
|
||||||
iteml = larger(dimA, dimB)
|
iteml = larger(dimA, dimB)
|
||||||
#print("Screw Length (RAW): " + str(iteml))
|
#print("Screw Length (RAW): " + str(iteml))
|
||||||
iteml = sizeVexScrew(radius * 2 / pixelsPerMetric)
|
iteml = sizeVexScrew(radius * 2 / pixelsPerMetric)
|
||||||
#print("Rounded Length: " + str(iteml))
|
#print("Rounded Length: " + str(iteml))
|
||||||
else:
|
else:
|
||||||
if itemhr == 0.3125:
|
if itemhr == 0.3125 and rectangular:
|
||||||
objtype = "Standoff"
|
objtype = "Standoff"
|
||||||
iteml = sizeStandoff(itemw)
|
iteml = sizeStandoff(itemw)
|
||||||
|
|
||||||
if itemhr == 0.1875:
|
if itemhr == 0.1875 and rectangular:
|
||||||
objtype = "Axle"
|
objtype = "Axle"
|
||||||
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
||||||
|
|
||||||
@ -297,10 +318,16 @@ for c in cnts:
|
|||||||
# cv2.putText(orig, "{:.5f}in".format(itemhr),
|
# cv2.putText(orig, "{:.5f}in".format(itemhr),
|
||||||
# (int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
# (int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
||||||
# 0.65, (255, 255, 255), 2)
|
# 0.65, (255, 255, 255), 2)
|
||||||
|
if circular:
|
||||||
|
cv2.putText(orig, str(objtype),
|
||||||
|
(int(x - 25), int(y + radius + 20)
|
||||||
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
||||||
|
0.55, (255, 255, 255), 2)
|
||||||
|
else:
|
||||||
cv2.putText(orig, str(objtype),
|
cv2.putText(orig, str(objtype),
|
||||||
(int(xpos2 + 10), int(ypos2 + 20)
|
(int(xpos2 + 10), int(ypos2 + 20)
|
||||||
), cv2.FONT_HERSHEY_SIMPLEX,
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
||||||
0.65, (255, 255, 255), 2)
|
0.55, (255, 255, 255), 2)
|
||||||
output = ""
|
output = ""
|
||||||
if objtype == "Unknown":
|
if objtype == "Unknown":
|
||||||
output = "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh)
|
output = "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh)
|
||||||
@ -308,10 +335,16 @@ for c in cnts:
|
|||||||
output = str(iteml) + "in"
|
output = str(iteml) + "in"
|
||||||
if objtype == "Axle":
|
if objtype == "Axle":
|
||||||
output = "{:.2f}in".format(iteml)
|
output = "{:.2f}in".format(iteml)
|
||||||
|
if circular:
|
||||||
cv2.putText(orig, output, # print data
|
cv2.putText(orig, output, # print data
|
||||||
(int(xpos2 + 10), int(ypos2 + 40)
|
(int(x - 25), int(y + radius + 35)
|
||||||
), cv2.FONT_HERSHEY_SIMPLEX,
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
||||||
0.65, (255, 255, 255), 2)
|
0.5, (255, 255, 255), 1)
|
||||||
|
else:
|
||||||
|
cv2.putText(orig, output, # print data
|
||||||
|
(int(xpos2 + 10), int(ypos2 + 35)
|
||||||
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
||||||
|
0.5, (255, 255, 255), 1)
|
||||||
|
|
||||||
# show the output image
|
# show the output image
|
||||||
cv2.imshow("Item Sorter", orig)
|
cv2.imshow("Item Sorter", orig)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user