format code

cascade
Cole Deck 5 years ago
parent 57abd697ba
commit 0e00ba7fca

@ -15,6 +15,7 @@ itemh = 0
def midpoint(ptA, ptB):
return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
def sizeVexScrew(iteml):
# Screw Sizing code
# subtract screw head size to find thread length
@ -26,6 +27,7 @@ def sizeVexScrew(iteml):
iteml /= 8
return iteml
def sizeStandoff(iteml):
# Standoff Sizing code
iteml *= 2
@ -40,17 +42,20 @@ def larger(a, b):
else:
return b
def smaller(a, b):
if a < b:
return a
else:
return b
def near(a, b, close):
if abs(a-b) < close:
return True
return False
def swap(a, b):
tmp = a
a = b
@ -76,7 +81,8 @@ if type(args["number"]) == type(selected):
# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread(args["image"])
#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:
cv2.namedWindow("Item Sorter")
@ -107,9 +113,6 @@ pixelsPerMetric = None
num = 0
# Calibration loop
for c in cnts:
# if the contour is not sufficiently large, ignore it
@ -126,10 +129,12 @@ for c in cnts:
(blbrX, blbrY) = midpoint(bl, br)
(tlblX, tlblY) = midpoint(tl, bl)
(trbrX, trbrY) = midpoint(tr, br)
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)))
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)))
area_box = dA * dB
(x,y),radius = cv2.minEnclosingCircle(c)
(x, y), radius = cv2.minEnclosingCircle(c)
area_contour = cv2.contourArea(c)
area_circle = math.pi * pow(radius, 2)
boxiness = area_contour / area_box
@ -141,12 +146,12 @@ for c in cnts:
cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
else:
circular = True
cv2.circle(orig,(int(x),int(y)),int(radius),(0,255,0),2)
mask = np.zeros(gray.shape,np.uint8)
cv2.drawContours(mask,[c],0,255,-1)
cv2.circle(orig, (int(x), int(y)), int(radius), (0, 255, 0), 2)
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)
mean_val = cv2.mean(hsv, mask=mask)
print(str(mean_val[0]))
#print(", " + str(mean_val[0]/mean_val[2]))
#print(", " + str(mean_val[2]/mean_val[1]))
@ -155,7 +160,6 @@ for c in cnts:
pixelsPerMetric = smaller(dA, dB) / args["width"]
orig = image.copy()
# loop over the contours individually
for c in cnts:
@ -166,7 +170,6 @@ for c in cnts:
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")
@ -178,7 +181,7 @@ for c in cnts:
#box = perspective.order_points(box)
# 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)
# 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)
# 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)))
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) - np.array((trbrX, trbrY, 0)))
# compute the Euclidean distance between the midpoints
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)))
dimA = dA / pixelsPerMetric
dimB = dB / pixelsPerMetric
if num == selected or args2.show:
area_box = dA * dB
(x,y),radius = cv2.minEnclosingCircle(c)
(x, y), radius = cv2.minEnclosingCircle(c)
area_contour = cv2.contourArea(c)
area_circle = math.pi * pow(radius, 2)
boxiness = area_contour / area_box
@ -221,8 +225,7 @@ for c in cnts:
#cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 2)
else:
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"
itemw = larger(dimA, dimB)
@ -240,19 +243,19 @@ for c in cnts:
objtype = "Penny"
iteml = 0
else:
epsilon = 3#0.02*cv2.arcLength(c,True)
#print(str(epsilon))
approx = cv2.approxPolyDP(c,epsilon,True)
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)
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 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"
iteml = larger(dimA, dimB)
@ -268,11 +271,11 @@ for c in cnts:
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)
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)
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)
angle = math.atan(slope)
xpos = x - math.cos(angle) * radius
@ -283,15 +286,17 @@ for c in cnts:
swap(xpos, xpos2)
swap(ypos, ypos2)
if rectangular:
cv2.line(orig,(int(xpos),int(ypos)),(int(xpos2), int(ypos2)),(0,255,0),1)
#print(str(iteml))
cv2.line(orig, (int(xpos), int(ypos)),
(int(xpos2), int(ypos2)), (0, 255, 0), 1)
# print(str(iteml))
# draw the object sizes on the image
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,
# 0.65, (255, 255, 255), 2)
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)
output = ""
if objtype == "Unknown":
@ -301,7 +306,8 @@ for c in cnts:
if objtype == "Axle":
output = "{:.2f}in".format(iteml)
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)
# show the output image

Loading…
Cancel
Save