|
|
|
@ -98,7 +98,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
gray = cv2.GaussianBlur(gray, (7, 7), 0)
|
|
|
|
|
|
|
|
|
|
gray = cv2.GaussianBlur(gray, (7, 7), 0)
|
|
|
|
|
# perform edge detection, then perform a dilation + erosion to
|
|
|
|
|
# close gaps in between object edges
|
|
|
|
|
edged = cv2.Canny(gray, 50, 100)
|
|
|
|
@ -107,6 +107,8 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
edged = cv2.dilate(edged, None, iterations=1)
|
|
|
|
|
edged = cv2.erode(edged, None, iterations=1)
|
|
|
|
|
edged = cv2.dilate(edged, None, iterations=1)
|
|
|
|
|
#edged = cv2.erode(edged, None, iterations=1)
|
|
|
|
|
#edged = cv2.dilate(edged, None, iterations=1)
|
|
|
|
|
if show and not quick:
|
|
|
|
|
cv2.imshow("Item Sorter", edged)
|
|
|
|
|
cv2.waitKey(0)
|
|
|
|
@ -122,6 +124,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
num = 0
|
|
|
|
|
|
|
|
|
|
# Calibration loop
|
|
|
|
|
"""
|
|
|
|
|
for c in cnts:
|
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
|
|
if cv2.contourArea(c) < 100:
|
|
|
|
@ -170,8 +173,8 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
# and near(mean_val[0], 63, 40) is True and near(mean_val[1], 108, 40) is True and near(mean_val[2], 104, 40) is True:
|
|
|
|
|
pixelsPerMetric = smaller(dA, dB) / calibration_width
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
#pixelsPerMetric = 25
|
|
|
|
|
"""
|
|
|
|
|
pixelsPerMetric = 25
|
|
|
|
|
orig = image.copy()
|
|
|
|
|
objtype = "Unknown"
|
|
|
|
|
objname = ""
|
|
|
|
@ -181,42 +184,22 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
num += 1
|
|
|
|
|
# if the contour is not sufficiently large, ignore it
|
|
|
|
|
#pixelsPerMetric = 75
|
|
|
|
|
if cv2.contourArea(c) < 100 or pixelsPerMetric is None:
|
|
|
|
|
if cv2.contourArea(c) < 300 or pixelsPerMetric is None:
|
|
|
|
|
continue
|
|
|
|
|
# compute the rotated bounding box of the contour
|
|
|
|
|
|
|
|
|
|
box = cv2.minAreaRect(c)
|
|
|
|
|
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
|
|
|
|
|
box = np.array(box, dtype="int")
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
#box = perspective.order_points(box)
|
|
|
|
|
|
|
|
|
|
# loop over the original points and draw them
|
|
|
|
|
# for (x, y) in box:
|
|
|
|
|
#cv2.circle(orig, (int(x), int(y)), 5, (0, 0, 255), -1)
|
|
|
|
|
|
|
|
|
|
# unpack the ordered bounding box, then compute the midpoint
|
|
|
|
|
# between the top-left and top-right coordinates, followed by
|
|
|
|
|
# the midpoint between bottom-left and bottom-right coordinates
|
|
|
|
|
(tl, tr, br, bl) = box
|
|
|
|
|
(tltrX, tltrY) = midpoint(tl, tr)
|
|
|
|
|
(blbrX, blbrY) = midpoint(bl, br)
|
|
|
|
|
|
|
|
|
|
# compute the midpoint between the top-left and top-right points,
|
|
|
|
|
# followed by the midpoint between the top-right and bottom-right
|
|
|
|
|
(tlblX, tlblY) = midpoint(tl, bl)
|
|
|
|
|
(trbrX, trbrY) = midpoint(tr, br)
|
|
|
|
|
# draw the midpoints on the image
|
|
|
|
|
#cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
|
|
|
|
|
#cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
|
|
|
|
|
#cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
|
|
|
|
|
#cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
|
|
|
|
|
|
|
|
|
|
# draw lines between the midpoints
|
|
|
|
|
# compute the Euclidean distance between the midpoints
|
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) -
|
|
|
|
|
np.array((blbrX, blbrY, 0)))
|
|
|
|
@ -255,7 +238,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
if circular and itemwr == 0.75:
|
|
|
|
|
objtype = "Penny"
|
|
|
|
|
iteml = 0
|
|
|
|
|
else:
|
|
|
|
|
"""else:
|
|
|
|
|
|
|
|
|
|
if circular and near(radius * 2 / pixelsPerMetric, 0.4, 0.03):
|
|
|
|
|
# Keps nut or spacer
|
|
|
|
@ -298,6 +281,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
if itemhr == 0.1875 and rectangular:
|
|
|
|
|
objtype = "Axle"
|
|
|
|
|
iteml = (radius * 2 / pixelsPerMetric + itemw) / 2
|
|
|
|
|
"""
|
|
|
|
|
rows, cols = orig.shape[:2]
|
|
|
|
|
[vx, vy, xx, yy] = cv2.fitLine(c, cv2.DIST_L2, 0, 0.01, 0.01)
|
|
|
|
|
lefty = int((-xx*vy/vx) + yy)
|
|
|
|
@ -337,7 +321,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
0.6, (50, 50, 220), 2)
|
|
|
|
|
output = ""
|
|
|
|
|
objname = objtype;
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if objtype == "Screw" or objtype == "Standoff":
|
|
|
|
|
output = str(iteml) + "in"
|
|
|
|
|
objname += str(iteml)
|
|
|
|
@ -345,6 +329,7 @@ def detect(calibration_width, img_file, show, quick):
|
|
|
|
|
output = "{:.2f}in".format(iteml)
|
|
|
|
|
objname += str(itemwr)
|
|
|
|
|
#print(objname)
|
|
|
|
|
"""
|
|
|
|
|
list.append(objname)
|
|
|
|
|
if circular:
|
|
|
|
|
cv2.putText(orig, output, # print data
|
|
|
|
@ -385,8 +370,8 @@ def magicSort(contour):
|
|
|
|
|
name += ", " + str(abs(int(humoments[i][0])))
|
|
|
|
|
#magicNumber2 += abs(humoments[i][0])
|
|
|
|
|
#magicNumber += humoments[i][0]
|
|
|
|
|
print(str(humoments))
|
|
|
|
|
#print(str(humoments))
|
|
|
|
|
#print(magicNumber)
|
|
|
|
|
#name = "Unknown: " + str(int(magicNumber1)) + ", " + str(int(magicNumber2))
|
|
|
|
|
print(name)
|
|
|
|
|
#print(name)
|
|
|
|
|
return name
|