|
|
@ -15,6 +15,7 @@ itemh = 0
|
|
|
|
def midpoint(ptA, ptB):
|
|
|
|
def midpoint(ptA, ptB):
|
|
|
|
return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
|
|
|
|
return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sizeVexScrew(iteml):
|
|
|
|
def sizeVexScrew(iteml):
|
|
|
|
# Screw Sizing code
|
|
|
|
# Screw Sizing code
|
|
|
|
# subtract screw head size to find thread length
|
|
|
|
# subtract screw head size to find thread length
|
|
|
@ -26,6 +27,7 @@ def sizeVexScrew(iteml):
|
|
|
|
iteml /= 8
|
|
|
|
iteml /= 8
|
|
|
|
return iteml
|
|
|
|
return iteml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def sizeStandoff(iteml):
|
|
|
|
def sizeStandoff(iteml):
|
|
|
|
# Standoff Sizing code
|
|
|
|
# Standoff Sizing code
|
|
|
|
iteml *= 2
|
|
|
|
iteml *= 2
|
|
|
@ -40,17 +42,20 @@ def larger(a, b):
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def smaller(a, b):
|
|
|
|
def smaller(a, b):
|
|
|
|
if a < b:
|
|
|
|
if a < b:
|
|
|
|
return a
|
|
|
|
return a
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return b
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def near(a, b, close):
|
|
|
|
def near(a, b, close):
|
|
|
|
if abs(a-b) < close:
|
|
|
|
if abs(a-b) < close:
|
|
|
|
return True
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def swap(a, b):
|
|
|
|
def swap(a, b):
|
|
|
|
tmp = a
|
|
|
|
tmp = a
|
|
|
|
a = b
|
|
|
|
a = b
|
|
|
@ -76,7 +81,8 @@ if type(args["number"]) == type(selected):
|
|
|
|
# load the image, convert it to grayscale, and blur it slightly
|
|
|
|
# load the image, convert it to grayscale, and blur it slightly
|
|
|
|
image = cv2.imread(args["image"])
|
|
|
|
image = cv2.imread(args["image"])
|
|
|
|
#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.shape[0]/image.shape[1] * 1000)), interpolation = cv2.INTER_NEAREST)
|
|
|
|
image = cv2.resize(image, (1000, int(
|
|
|
|
|
|
|
|
image.shape[0]/image.shape[1] * 1000)), interpolation=cv2.INTER_NEAREST)
|
|
|
|
|
|
|
|
|
|
|
|
if args2.show:
|
|
|
|
if args2.show:
|
|
|
|
cv2.namedWindow("Item Sorter")
|
|
|
|
cv2.namedWindow("Item Sorter")
|
|
|
@ -107,9 +113,6 @@ pixelsPerMetric = None
|
|
|
|
num = 0
|
|
|
|
num = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Calibration loop
|
|
|
|
# Calibration loop
|
|
|
|
for c in cnts:
|
|
|
|
for c in cnts:
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
@ -126,10 +129,12 @@ for c in cnts:
|
|
|
|
(blbrX, blbrY) = midpoint(bl, br)
|
|
|
|
(blbrX, blbrY) = midpoint(bl, br)
|
|
|
|
(tlblX, tlblY) = midpoint(tl, bl)
|
|
|
|
(tlblX, tlblY) = midpoint(tl, bl)
|
|
|
|
(trbrX, trbrY) = midpoint(tr, br)
|
|
|
|
(trbrX, trbrY) = midpoint(tr, br)
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) - np.array((blbrX, blbrY, 0)))
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) -
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) - np.array((trbrX, trbrY, 0)))
|
|
|
|
np.array((blbrX, blbrY, 0)))
|
|
|
|
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) -
|
|
|
|
|
|
|
|
np.array((trbrX, trbrY, 0)))
|
|
|
|
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
|
|
|
@ -141,12 +146,12 @@ for c in cnts:
|
|
|
|
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),(0,255,0),2)
|
|
|
|
cv2.circle(orig, (int(x), int(y)), int(radius), (0, 255, 0), 2)
|
|
|
|
mask = np.zeros(gray.shape,np.uint8)
|
|
|
|
mask = np.zeros(gray.shape, np.uint8)
|
|
|
|
cv2.drawContours(mask,[c],0,255,-1)
|
|
|
|
cv2.drawContours(mask, [c], 0, 255, -1)
|
|
|
|
#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]))
|
|
|
@ -155,7 +160,6 @@ for c in cnts:
|
|
|
|
pixelsPerMetric = smaller(dA, dB) / args["width"]
|
|
|
|
pixelsPerMetric = smaller(dA, dB) / args["width"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
orig = image.copy()
|
|
|
|
orig = image.copy()
|
|
|
|
# loop over the contours individually
|
|
|
|
# loop over the contours individually
|
|
|
|
for c in cnts:
|
|
|
|
for c in cnts:
|
|
|
@ -166,7 +170,6 @@ for c in cnts:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
# compute the rotated bounding box of the contour
|
|
|
|
# compute the rotated bounding box of the contour
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
box = cv2.minAreaRect(c)
|
|
|
|
box = cv2.minAreaRect(c)
|
|
|
|
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
|
|
|
|
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
|
|
|
|
box = np.array(box, dtype="int")
|
|
|
|
box = np.array(box, dtype="int")
|
|
|
@ -178,7 +181,7 @@ for c in cnts:
|
|
|
|
#box = perspective.order_points(box)
|
|
|
|
#box = perspective.order_points(box)
|
|
|
|
|
|
|
|
|
|
|
|
# loop over the original points and draw them
|
|
|
|
# loop over the original points and draw them
|
|
|
|
#for (x, y) in box:
|
|
|
|
# for (x, y) in box:
|
|
|
|
#cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
|
|
|
|
#cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
|
|
|
|
|
|
|
|
|
|
|
|
# unpack the ordered bounding box, then compute the midpoint
|
|
|
|
# unpack the ordered bounding box, then compute the midpoint
|
|
|
@ -199,17 +202,18 @@ for c in cnts:
|
|
|
|
#cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
|
|
|
|
#cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
|
|
|
|
|
|
|
|
|
|
|
|
# draw lines between the midpoints
|
|
|
|
# draw lines between the midpoints
|
|
|
|
# compute the Euclidean distance between the midpoints
|
|
|
|
# compute the Euclidean distance between the midpoints
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) - np.array((blbrX, blbrY, 0)))
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) -
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) - np.array((trbrX, trbrY, 0)))
|
|
|
|
np.array((blbrX, blbrY, 0)))
|
|
|
|
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) -
|
|
|
|
|
|
|
|
np.array((trbrX, trbrY, 0)))
|
|
|
|
|
|
|
|
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
|
|
|
|
|
|
|
|
if num == selected or args2.show:
|
|
|
|
if num == selected or args2.show:
|
|
|
|
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
|
|
|
@ -221,8 +225,7 @@ for c in cnts:
|
|
|
|
#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),(0,255,0),1)
|
|
|
|
cv2.circle(orig, (int(x), int(y)), int(radius), (0, 255, 0), 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
objtype = "Unknown"
|
|
|
|
objtype = "Unknown"
|
|
|
|
itemw = larger(dimA, dimB)
|
|
|
|
itemw = larger(dimA, dimB)
|
|
|
@ -240,19 +243,19 @@ for c in cnts:
|
|
|
|
objtype = "Penny"
|
|
|
|
objtype = "Penny"
|
|
|
|
iteml = 0
|
|
|
|
iteml = 0
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
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)
|
|
|
|
hull = cv2.convexHull(approx, returnPoints=False)
|
|
|
|
hull = cv2.convexHull(approx, returnPoints=False)
|
|
|
|
hull2 = cv2.convexHull(c)
|
|
|
|
hull2 = cv2.convexHull(c)
|
|
|
|
defects = cv2.convexityDefects(c,hull)
|
|
|
|
defects = cv2.convexityDefects(c, hull)
|
|
|
|
#print(str(defects.size) + " match")
|
|
|
|
#print(str(defects.size) + " match")
|
|
|
|
cv2.drawContours(orig, (hull2), -1, (0, 0, 255), 3)
|
|
|
|
cv2.drawContours(orig, (hull2), -1, (0, 0, 255), 3)
|
|
|
|
cv2.drawContours(orig, (approx), -1, (255, 0, 0), 3)
|
|
|
|
cv2.drawContours(orig, (approx), -1, (255, 0, 0), 3)
|
|
|
|
convexness = area_contour / cv2.contourArea(hull2)
|
|
|
|
convexness = area_contour / cv2.contourArea(hull2)
|
|
|
|
#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.size > 5 and (convexness < 0.9 or boxiness < 0.75):
|
|
|
|
objtype = "Screw"
|
|
|
|
objtype = "Screw"
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
@ -268,11 +271,11 @@ for c in cnts:
|
|
|
|
objtype = "Axle"
|
|
|
|
objtype = "Axle"
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
|
|
|
|
|
|
|
|
rows,cols = orig.shape[:2]
|
|
|
|
rows, cols = orig.shape[:2]
|
|
|
|
[vx,vy,xx,yy] = cv2.fitLine(c, cv2.DIST_L2,0,0.01,0.01)
|
|
|
|
[vx, vy, xx, yy] = cv2.fitLine(c, cv2.DIST_L2, 0, 0.01, 0.01)
|
|
|
|
lefty = int((-xx*vy/vx) + yy)
|
|
|
|
lefty = int((-xx*vy/vx) + yy)
|
|
|
|
righty = int(((cols-xx)*vy/vx)+yy)
|
|
|
|
righty = int(((cols-xx)*vy/vx)+yy)
|
|
|
|
#cv2.line(orig,(cols-1,righty),(0,lefty),(0,255,0),2)
|
|
|
|
# cv2.line(orig,(cols-1,righty),(0,lefty),(0,255,0),2)
|
|
|
|
slope = (lefty - righty) / (1 - cols)
|
|
|
|
slope = (lefty - righty) / (1 - cols)
|
|
|
|
angle = math.atan(slope)
|
|
|
|
angle = math.atan(slope)
|
|
|
|
xpos = x - math.cos(angle) * radius
|
|
|
|
xpos = x - math.cos(angle) * radius
|
|
|
@ -283,15 +286,17 @@ for c in cnts:
|
|
|
|
swap(xpos, xpos2)
|
|
|
|
swap(xpos, xpos2)
|
|
|
|
swap(ypos, ypos2)
|
|
|
|
swap(ypos, ypos2)
|
|
|
|
if rectangular:
|
|
|
|
if rectangular:
|
|
|
|
cv2.line(orig,(int(xpos),int(ypos)),(int(xpos2), int(ypos2)),(0,255,0),1)
|
|
|
|
cv2.line(orig, (int(xpos), int(ypos)),
|
|
|
|
#print(str(iteml))
|
|
|
|
(int(xpos2), int(ypos2)), (0, 255, 0), 1)
|
|
|
|
|
|
|
|
# print(str(iteml))
|
|
|
|
# draw the object sizes on the image
|
|
|
|
# draw the object sizes on the image
|
|
|
|
if args2.show:
|
|
|
|
if args2.show:
|
|
|
|
#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)
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
cv2.putText(orig, str(objtype),
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 20)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 20)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
output = ""
|
|
|
|
output = ""
|
|
|
|
if objtype == "Unknown":
|
|
|
|
if objtype == "Unknown":
|
|
|
@ -301,7 +306,8 @@ for c in cnts:
|
|
|
|
if objtype == "Axle":
|
|
|
|
if objtype == "Axle":
|
|
|
|
output = "{:.2f}in".format(iteml)
|
|
|
|
output = "{:.2f}in".format(iteml)
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 40)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
(int(xpos2 + 10), int(ypos2 + 40)
|
|
|
|
|
|
|
|
), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
|
|
|
|
|
|
|
|
# show the output image
|
|
|
|
# show the output image
|
|
|
|