|
|
@ -1,5 +1,5 @@
|
|
|
|
# import the necessary packages
|
|
|
|
# import the necessary packages
|
|
|
|
from imutils import perspective
|
|
|
|
#from imutils import perspective
|
|
|
|
from imutils import contours
|
|
|
|
from imutils import contours
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
import argparse
|
|
|
|
import argparse
|
|
|
@ -17,7 +17,7 @@ def midpoint(ptA, ptB):
|
|
|
|
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
|
|
|
|
shead = 0.09
|
|
|
|
shead = 0.1
|
|
|
|
iteml -= shead
|
|
|
|
iteml -= shead
|
|
|
|
#print("Thread Length: " + str(iteml))
|
|
|
|
#print("Thread Length: " + str(iteml))
|
|
|
|
iteml *= 8
|
|
|
|
iteml *= 8
|
|
|
@ -27,11 +27,9 @@ def sizeVexScrew(iteml):
|
|
|
|
|
|
|
|
|
|
|
|
def sizeStandoff(iteml):
|
|
|
|
def sizeStandoff(iteml):
|
|
|
|
# Standoff Sizing code
|
|
|
|
# Standoff Sizing code
|
|
|
|
|
|
|
|
iteml *= 2
|
|
|
|
#print("Thread Length: " + str(iteml))
|
|
|
|
|
|
|
|
iteml *= 4
|
|
|
|
|
|
|
|
iteml = round(iteml)
|
|
|
|
iteml = round(iteml)
|
|
|
|
iteml /= 4
|
|
|
|
iteml /= 2
|
|
|
|
return iteml
|
|
|
|
return iteml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -64,7 +62,9 @@ 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]*0.2), int(image.shape[0]*0.2)), interpolation = cv2.INTER_NEAREST)
|
|
|
|
#image = cv2.resize(image, (int(image.shape[1]*0.2), int(image.shape[0]*0.2)), 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.imshow("Item Sorter", image)
|
|
|
|
cv2.imshow("Item Sorter", image)
|
|
|
|
cv2.waitKey(0)
|
|
|
|
cv2.waitKey(0)
|
|
|
@ -109,7 +109,7 @@ for c in cnts:
|
|
|
|
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")
|
|
|
|
box = perspective.order_points(box)
|
|
|
|
#box = perspective.order_points(box)
|
|
|
|
(tl, tr, br, bl) = box
|
|
|
|
(tl, tr, br, bl) = box
|
|
|
|
(tltrX, tltrY) = midpoint(tl, tr)
|
|
|
|
(tltrX, tltrY) = midpoint(tl, tr)
|
|
|
|
(blbrX, blbrY) = midpoint(bl, br)
|
|
|
|
(blbrX, blbrY) = midpoint(bl, br)
|
|
|
@ -134,27 +134,14 @@ for c in cnts:
|
|
|
|
|
|
|
|
|
|
|
|
if pixelsPerMetric is None and circular is True:
|
|
|
|
if pixelsPerMetric is None and circular is True:
|
|
|
|
pixelsPerMetric = smaller(dA, dB) / args["width"]
|
|
|
|
pixelsPerMetric = smaller(dA, dB) / args["width"]
|
|
|
|
#cv2.imshow("Screw Length Detection", orig)
|
|
|
|
|
|
|
|
#cv2.waitKey(0)
|
|
|
|
|
|
|
|
# order the points in the contour such that they appear
|
|
|
|
|
|
|
|
# in top-left, top-right, bottom-right, and bottom-left
|
|
|
|
|
|
|
|
# order, then draw the outline of the rotated bounding
|
|
|
|
|
|
|
|
# box
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if the pixels per metric has not been initialized, then
|
|
|
|
|
|
|
|
# compute it as the ratio of pixels to supplied metric
|
|
|
|
|
|
|
|
# (in this case, inches)
|
|
|
|
|
|
|
|
# compute the size of the object
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
orig = image.copy()
|
|
|
|
orig = image.copy()
|
|
|
|
# loop over the contours individually
|
|
|
|
# loop over the contours individually
|
|
|
|
for c in cnts:
|
|
|
|
for c in cnts:
|
|
|
|
num += 1
|
|
|
|
num += 1
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
|
if cv2.contourArea(c) < 100:
|
|
|
|
if cv2.contourArea(c) < 100 or pixelsPerMetric is None:
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
# compute the rotated bounding box of the contour
|
|
|
|
# compute the rotated bounding box of the contour
|
|
|
|
|
|
|
|
|
|
|
@ -167,7 +154,7 @@ for c in cnts:
|
|
|
|
# in top-left, top-right, bottom-right, and bottom-left
|
|
|
|
# in top-left, top-right, bottom-right, and bottom-left
|
|
|
|
# order, then draw the outline of the rotated bounding
|
|
|
|
# order, then draw the outline of the rotated bounding
|
|
|
|
# box
|
|
|
|
# box
|
|
|
|
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:
|
|
|
@ -196,12 +183,7 @@ for c in cnts:
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) - np.array((blbrX, blbrY, 0)))
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) - np.array((blbrX, blbrY, 0)))
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) - np.array((trbrX, trbrY, 0)))
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) - np.array((trbrX, trbrY, 0)))
|
|
|
|
|
|
|
|
|
|
|
|
# if the pixels per metric has not been initialized, then
|
|
|
|
|
|
|
|
# compute it as the ratio of pixels to supplied metric
|
|
|
|
|
|
|
|
# (in this case, inches)
|
|
|
|
|
|
|
|
#if pixelsPerMetric is None:
|
|
|
|
|
|
|
|
# pixelsPerMetric = dB / args["width"]
|
|
|
|
|
|
|
|
# compute the size of the object
|
|
|
|
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
|
|
|
|
|
|
|
@ -238,11 +220,19 @@ for c in cnts:
|
|
|
|
objtype = "Penny"
|
|
|
|
objtype = "Penny"
|
|
|
|
iteml = 0
|
|
|
|
iteml = 0
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
epsilon = 4#0.05*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)
|
|
|
|
|
|
|
|
hull2 = cv2.convexHull(c)
|
|
|
|
|
|
|
|
defects = cv2.convexityDefects(c,hull)
|
|
|
|
|
|
|
|
#print(str(defects.size) + " match")
|
|
|
|
cv2.drawContours(orig, (approx.astype("int")), -1, (255, 0, 0), 8)
|
|
|
|
cv2.drawContours(orig, (approx.astype("int")), -1, (255, 0, 0), 8)
|
|
|
|
if not cv2.isContourConvex(approx):
|
|
|
|
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.size > 5 and (convexness < 0.9 or boxiness < 0.75):
|
|
|
|
objtype = "Screw"
|
|
|
|
objtype = "Screw"
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
#print("Screw Length (RAW): " + str(iteml))
|
|
|
|
#print("Screw Length (RAW): " + str(iteml))
|
|
|
@ -251,10 +241,12 @@ for c in cnts:
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
if itemhr == 0.3125:
|
|
|
|
if itemhr == 0.3125:
|
|
|
|
objtype = "Standoff"
|
|
|
|
objtype = "Standoff"
|
|
|
|
iteml = sizeStandoff(radius * 2 / pixelsPerMetric)
|
|
|
|
iteml = sizeStandoff(itemw)
|
|
|
|
|
|
|
|
|
|
|
|
if itemhr == 0.1875:
|
|
|
|
if itemhr == 0.1875:
|
|
|
|
objtype = "Axle"
|
|
|
|
objtype = "Axle"
|
|
|
|
iteml = radius * 2 / pixelsPerMetric
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(str(iteml))
|
|
|
|
print(str(iteml))
|
|
|
|
# draw the object sizes on the image
|
|
|
|
# draw the object sizes on the image
|
|
|
|