|
|
@ -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,8 +129,10 @@ 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)
|
|
|
@ -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")
|
|
|
@ -200,9 +203,10 @@ for c in cnts:
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
@ -223,7 +227,6 @@ for c in cnts:
|
|
|
|
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)
|
|
|
|
itemwr = itemw
|
|
|
|
itemwr = itemw
|
|
|
@ -283,7 +286,8 @@ 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)),
|
|
|
|
|
|
|
|
(int(xpos2), int(ypos2)), (0, 255, 0), 1)
|
|
|
|
# print(str(iteml))
|
|
|
|
# print(str(iteml))
|
|
|
|
# draw the object sizes on the image
|
|
|
|
# draw the object sizes on the image
|
|
|
|
if args2.show:
|
|
|
|
if args2.show:
|
|
|
@ -291,7 +295,8 @@ for c in cnts:
|
|
|
|
# (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
|
|
|
|