|
|
@ -76,13 +76,13 @@ ap.add_argument("-n", "--number", type=int, required=False,
|
|
|
|
args = vars(ap.parse_args())
|
|
|
|
args = vars(ap.parse_args())
|
|
|
|
args2 = ap.parse_args()"""
|
|
|
|
args2 = ap.parse_args()"""
|
|
|
|
def detect(calibration_width, img_file, show, quick):
|
|
|
|
def detect(calibration_width, img_file, show, quick):
|
|
|
|
selected = 2
|
|
|
|
list = []
|
|
|
|
#if type(args["number"]) == type(selected):
|
|
|
|
#if type(args["number"]) == type(selected):
|
|
|
|
# selected = args["number"]
|
|
|
|
# selected = args["number"]
|
|
|
|
|
|
|
|
|
|
|
|
# load the image, convert it to grayscale, and blur it slightly
|
|
|
|
# load the image, convert it to grayscale, and blur it slightly
|
|
|
|
image = None
|
|
|
|
image = None
|
|
|
|
print(str(type(img_file)))
|
|
|
|
#print(str(type(img_file)))
|
|
|
|
if str(type(img_file)) == "<class 'numpy.ndarray'>":
|
|
|
|
if str(type(img_file)) == "<class 'numpy.ndarray'>":
|
|
|
|
image = img_file.copy()
|
|
|
|
image = img_file.copy()
|
|
|
|
else:
|
|
|
|
else:
|
|
|
@ -91,7 +91,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
#image = img_file.copy()
|
|
|
|
#image = img_file.copy()
|
|
|
|
#image = cv2.resize(image, (int(image.shape[1]*1), int(image.shape[0]*1)))
|
|
|
|
#image = cv2.resize(image, (int(image.shape[1]*1), int(image.shape[0]*1)))
|
|
|
|
image = cv2.resize(image, (1000, int(
|
|
|
|
image = cv2.resize(image, (1000, int(
|
|
|
|
image.shape[0]/image.shape[1] * 1000)), interpolation=cv2.INTER_NEAREST)
|
|
|
|
image.shape[0]/image.shape[1] * 1000)), interpolation=cv2.INTER_NEAREST)
|
|
|
|
|
|
|
|
|
|
|
|
if show and not quick:
|
|
|
|
if show and not quick:
|
|
|
|
cv2.namedWindow("Item Sorter")
|
|
|
|
cv2.namedWindow("Item Sorter")
|
|
|
@ -232,139 +232,134 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
|
|
|
|
|
|
|
|
if num == num or show:
|
|
|
|
# Item detection
|
|
|
|
area_box = dA * dB
|
|
|
|
area_box = dA * dB
|
|
|
|
(x, y), radius = cv2.minEnclosingCircle(c)
|
|
|
|
(x, y), radius = cv2.minEnclosingCircle(c)
|
|
|
|
area_contour = cv2.contourArea(c)
|
|
|
|
area_contour = cv2.contourArea(c)
|
|
|
|
area_circle = math.pi * pow(radius, 2)
|
|
|
|
area_circle = math.pi * pow(radius, 2)
|
|
|
|
boxiness = area_contour / area_box
|
|
|
|
boxiness = area_contour / area_box
|
|
|
|
circleness = area_contour / area_circle
|
|
|
|
circleness = area_contour / area_circle
|
|
|
|
circular = False
|
|
|
|
circular = False
|
|
|
|
rectangular = False
|
|
|
|
rectangular = False
|
|
|
|
if boxiness > circleness:
|
|
|
|
if boxiness > circleness:
|
|
|
|
rectangular = True
|
|
|
|
rectangular = True
|
|
|
|
#cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
|
|
|
|
#cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
circular = True
|
|
|
|
circular = True
|
|
|
|
cv2.circle(orig, (int(x), int(y)), int(radius), (255, 0, 0), 2)
|
|
|
|
cv2.circle(orig, (int(x), int(y)), int(radius), (255, 0, 0), 2)
|
|
|
|
|
|
|
|
objtype = "Unknown"
|
|
|
|
objtype = "Unknown"
|
|
|
|
itemw = larger(dimA, dimB)
|
|
|
|
itemw = larger(dimA, dimB)
|
|
|
|
itemwr = itemw
|
|
|
|
itemwr = itemw
|
|
|
|
itemwr *= 8
|
|
|
|
itemwr *= 8
|
|
|
|
itemwr = round(itemwr)
|
|
|
|
itemwr = round(itemwr)
|
|
|
|
itemwr /= 8
|
|
|
|
itemwr /= 8
|
|
|
|
itemh = smaller(dimA, dimB)
|
|
|
|
|
|
|
|
itemhr = itemh
|
|
|
|
itemh = smaller(dimA, dimB)
|
|
|
|
itemhr *= 16
|
|
|
|
itemhr = itemh
|
|
|
|
itemhr = round(itemhr)
|
|
|
|
itemhr *= 16
|
|
|
|
itemhr /= 16
|
|
|
|
itemhr = round(itemhr)
|
|
|
|
if circular and itemwr == 0.75:
|
|
|
|
itemhr /= 16
|
|
|
|
objtype = "Penny"
|
|
|
|
if circular and itemwr == 0.75:
|
|
|
|
iteml = 0
|
|
|
|
objtype = "Penny"
|
|
|
|
else:
|
|
|
|
iteml = 0
|
|
|
|
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)
|
|
|
|
|
|
|
|
# print(str(epsilon))
|
|
|
|
|
|
|
|
approx = cv2.approxPolyDP(c, epsilon, True)
|
|
|
|
|
|
|
|
hull = cv2.convexHull(approx, returnPoints=False)
|
|
|
|
|
|
|
|
hull2 = cv2.convexHull(c)
|
|
|
|
|
|
|
|
defects = cv2.convexityDefects(c, hull)
|
|
|
|
|
|
|
|
#print(str(defects.size) + " match")
|
|
|
|
|
|
|
|
cv2.drawContours(orig, (hull2), -1, (0, 0, 255), 3)
|
|
|
|
|
|
|
|
cv2.drawContours(orig, (approx), -1, (255, 0, 0), 3)
|
|
|
|
|
|
|
|
convexness = area_contour / cv2.contourArea(hull2)
|
|
|
|
|
|
|
|
#print(str(convexness) + " % fill")
|
|
|
|
|
|
|
|
# if not cv2.isContourConvex(approx):
|
|
|
|
|
|
|
|
# if cv2.matchShapes(hull, c, 1, 0.0) > 1:
|
|
|
|
|
|
|
|
if defects is not None and defects.size > 5 and (convexness < 0.9 or boxiness < 0.75) and rectangular:
|
|
|
|
|
|
|
|
objtype = "Screw"
|
|
|
|
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
|
|
|
|
#print("Screw Length (RAW): " + str(iteml))
|
|
|
|
|
|
|
|
iteml = sizeVexScrew(radius * 2 / pixelsPerMetric)
|
|
|
|
|
|
|
|
#print("Rounded Length: " + str(iteml))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if circular and near(radius * 2 / pixelsPerMetric, 0.38, 0.03):
|
|
|
|
if itemhr == 0.3125 and rectangular:
|
|
|
|
# Keps nut or spacer
|
|
|
|
objtype = "Standoff"
|
|
|
|
objtype = "Spacer"
|
|
|
|
iteml = sizeStandoff(itemw)
|
|
|
|
mask = np.zeros(gray.shape, np.uint8)
|
|
|
|
if itemhr == 0.1875 and rectangular:
|
|
|
|
cv2.drawContours(mask, [c], 0, 255, -1)
|
|
|
|
objtype = "Axle"
|
|
|
|
#pixelpoints = np.transpose(np.nonzero(mask))
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
hsv = cv2.cvtColor(orig, cv2.COLOR_BGR2HSV)
|
|
|
|
rows, cols = orig.shape[:2]
|
|
|
|
mean_val = cv2.mean(hsv, mask=mask)
|
|
|
|
[vx, vy, xx, yy] = cv2.fitLine(c, cv2.DIST_L2, 0, 0.01, 0.01)
|
|
|
|
#print(str(mean_val[0]))
|
|
|
|
lefty = int((-xx*vy/vx) + yy)
|
|
|
|
if near(mean_val[0], 47, 5) and near(mean_val[1], 70, 5) and near(mean_val[2], 78, 5):
|
|
|
|
righty = int(((cols-xx)*vy/vx)+yy)
|
|
|
|
objtype = "Keps Nut"
|
|
|
|
# cv2.line(orig,(cols-1,righty),(0,lefty),(0,255,0),2)
|
|
|
|
if circular and near(radius / pixelsPerMetric, 0.23, 0.02):
|
|
|
|
slope = (lefty - righty) / (1 - cols)
|
|
|
|
objtype = "Washer"
|
|
|
|
angle = math.atan(slope)
|
|
|
|
epsilon = 3 # 0.02*cv2.arcLength(c,True)
|
|
|
|
xpos = x - math.cos(angle) * radius
|
|
|
|
# print(str(epsilon))
|
|
|
|
ypos = y - math.sin(angle) * radius
|
|
|
|
approx = cv2.approxPolyDP(c, epsilon, True)
|
|
|
|
xpos2 = x + math.cos(angle) * radius
|
|
|
|
hull = cv2.convexHull(approx, returnPoints=False)
|
|
|
|
ypos2 = y + math.sin(angle) * radius
|
|
|
|
hull2 = cv2.convexHull(c)
|
|
|
|
if xpos > xpos2:
|
|
|
|
defects = cv2.convexityDefects(c, hull)
|
|
|
|
swap(xpos, xpos2)
|
|
|
|
#print(str(defects.size) + " match")
|
|
|
|
swap(ypos, ypos2)
|
|
|
|
cv2.drawContours(orig, (hull2), -1, (0, 0, 255), 3)
|
|
|
|
if rectangular:
|
|
|
|
cv2.drawContours(orig, (approx), -1, (255, 0, 0), 3)
|
|
|
|
cv2.line(orig, (int(xpos), int(ypos)),
|
|
|
|
convexness = area_contour / cv2.contourArea(hull2)
|
|
|
|
(int(xpos2), int(ypos2)), (255, 127, 0), 2)
|
|
|
|
#print(str(convexness) + " % fill")
|
|
|
|
# print(str(iteml))
|
|
|
|
# if not cv2.isContourConvex(approx):
|
|
|
|
# draw the object sizes on the image
|
|
|
|
# if cv2.matchShapes(hull, c, 1, 0.0) > 1:
|
|
|
|
# cv2.putText(orig, "{:.5f}in".format(itemhr),
|
|
|
|
if defects is not None and defects.size > 5 and (convexness < 0.9 or boxiness < 0.75) and rectangular:
|
|
|
|
# (int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
objtype = "Screw"
|
|
|
|
# 0.65, (255, 255, 255), 2)
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
if circular:
|
|
|
|
#print("Screw Length (RAW): " + str(iteml))
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
iteml = sizeVexScrew(radius * 2 / pixelsPerMetric)
|
|
|
|
(int(x - 25), int(y + radius + 20)
|
|
|
|
#print("Rounded Length: " + str(iteml))
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
else:
|
|
|
|
0.6, (50, 50, 220), 2)
|
|
|
|
if itemhr == 0.3125 and rectangular:
|
|
|
|
else:
|
|
|
|
objtype = "Standoff"
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
iteml = sizeStandoff(itemw)
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 20)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
if itemhr == 0.1875 and rectangular:
|
|
|
|
0.6, (50, 50, 220), 2)
|
|
|
|
objtype = "Axle"
|
|
|
|
output = ""
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
objname = objtype;
|
|
|
|
|
|
|
|
if objtype == "Unknown":
|
|
|
|
rows, cols = orig.shape[:2]
|
|
|
|
output = "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh)
|
|
|
|
[vx, vy, xx, yy] = cv2.fitLine(c, cv2.DIST_L2, 0, 0.01, 0.01)
|
|
|
|
if objtype == "Screw" or objtype == "Standoff":
|
|
|
|
lefty = int((-xx*vy/vx) + yy)
|
|
|
|
output = str(iteml) + "in"
|
|
|
|
righty = int(((cols-xx)*vy/vx)+yy)
|
|
|
|
objname += str(iteml)
|
|
|
|
# cv2.line(orig,(cols-1,righty),(0,lefty),(0,255,0),2)
|
|
|
|
if objtype == "Axle":
|
|
|
|
slope = (lefty - righty) / (1 - cols)
|
|
|
|
output = "{:.2f}in".format(iteml)
|
|
|
|
angle = math.atan(slope)
|
|
|
|
objname += str(itemwr)
|
|
|
|
xpos = x - math.cos(angle) * radius
|
|
|
|
#print(objname)
|
|
|
|
ypos = y - math.sin(angle) * radius
|
|
|
|
list.append(objname)
|
|
|
|
xpos2 = x + math.cos(angle) * radius
|
|
|
|
if circular:
|
|
|
|
ypos2 = y + math.sin(angle) * radius
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
if xpos > xpos2:
|
|
|
|
(int(x - 25), int(y + radius + 40)
|
|
|
|
swap(xpos, xpos2)
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
swap(ypos, ypos2)
|
|
|
|
0.5, (50, 50, 220), 1)
|
|
|
|
if rectangular:
|
|
|
|
else:
|
|
|
|
cv2.line(orig, (int(xpos), int(ypos)),
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
(int(xpos2), int(ypos2)), (255, 127, 0), 2)
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 40)
|
|
|
|
# print(str(iteml))
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
# draw the object sizes on the image
|
|
|
|
0.5, (50, 50, 220), 1)
|
|
|
|
if show or True:
|
|
|
|
# show the output image
|
|
|
|
# cv2.putText(orig, "{:.5f}in".format(itemhr),
|
|
|
|
if show and not quick:
|
|
|
|
# (int(trbrX + 20), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
cv2.imshow("Item Sorter", orig)
|
|
|
|
# 0.65, (255, 255, 255), 2)
|
|
|
|
#cv2.waitKey(1)
|
|
|
|
if circular:
|
|
|
|
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
|
|
|
|
(int(x - 25), int(y + radius + 20)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
|
|
|
0.6, (50, 50, 220), 2)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 20)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
|
|
|
0.6, (50, 50, 220), 2)
|
|
|
|
|
|
|
|
output = ""
|
|
|
|
|
|
|
|
objname = objtype;
|
|
|
|
|
|
|
|
if objtype == "Unknown":
|
|
|
|
|
|
|
|
output = "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh)
|
|
|
|
|
|
|
|
if objtype == "Screw" or objtype == "Standoff":
|
|
|
|
|
|
|
|
output = str(iteml) + "in"
|
|
|
|
|
|
|
|
objname += str(iteml)
|
|
|
|
|
|
|
|
if objtype == "Axle":
|
|
|
|
|
|
|
|
output = "{:.2f}in".format(iteml)
|
|
|
|
|
|
|
|
objname += str(itemwr)
|
|
|
|
|
|
|
|
print(objname)
|
|
|
|
|
|
|
|
if circular:
|
|
|
|
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
|
|
|
|
(int(x - 25), int(y + radius + 40)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
|
|
|
0.5, (50, 50, 220), 1)
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 40)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
|
|
|
|
0.5, (50, 50, 220), 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# show the output image
|
|
|
|
|
|
|
|
if show:
|
|
|
|
|
|
|
|
cv2.imshow("Item Sorter", orig)
|
|
|
|
|
|
|
|
#cv2.waitKey(1)
|
|
|
|
|
|
|
|
if quick:
|
|
|
|
if quick:
|
|
|
|
return orig
|
|
|
|
return (list, orig)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
cv2.waitKey(0)
|
|
|
|
cv2.waitKey(0)
|
|
|
|