|
|
@ -50,6 +50,13 @@ 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):
|
|
|
|
|
|
|
|
tmp = a
|
|
|
|
|
|
|
|
a = b
|
|
|
|
|
|
|
|
b = tmp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# construct the argument parse and parse the arguments
|
|
|
|
# construct the argument parse and parse the arguments
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
ap.add_argument("-i", "--image", required=True,
|
|
|
|
ap.add_argument("-i", "--image", required=True,
|
|
|
@ -210,7 +217,7 @@ for c in cnts:
|
|
|
|
rectangular = False
|
|
|
|
rectangular = False
|
|
|
|
if boxiness > circleness:
|
|
|
|
if boxiness > circleness:
|
|
|
|
rectangular = True
|
|
|
|
rectangular = True
|
|
|
|
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)
|
|
|
@ -260,7 +267,22 @@ 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]
|
|
|
|
|
|
|
|
[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)
|
|
|
|
|
|
|
|
slope = (lefty - righty) / (1 - cols)
|
|
|
|
|
|
|
|
angle = math.atan(slope)
|
|
|
|
|
|
|
|
xpos = x - math.cos(angle) * radius
|
|
|
|
|
|
|
|
ypos = y - math.sin(angle) * radius
|
|
|
|
|
|
|
|
xpos2 = x + math.cos(angle) * radius
|
|
|
|
|
|
|
|
ypos2 = y + math.sin(angle) * radius
|
|
|
|
|
|
|
|
if xpos > xpos2:
|
|
|
|
|
|
|
|
swap(xpos, xpos2)
|
|
|
|
|
|
|
|
swap(ypos, ypos2)
|
|
|
|
|
|
|
|
if rectangular:
|
|
|
|
|
|
|
|
cv2.line(orig,(int(xpos),int(ypos)),(int(xpos2), int(ypos2)),(0,255,0),2)
|
|
|
|
#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:
|
|
|
@ -268,23 +290,23 @@ 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(trbrX + 20), int(trbrY)), 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)
|
|
|
|
if objtype == "Unknown":
|
|
|
|
if objtype == "Unknown":
|
|
|
|
cv2.putText(orig, "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh), # print axle length
|
|
|
|
cv2.putText(orig, "{:.2f}in".format(itemw) + " x {:.2f}in".format(itemh), # print axle length
|
|
|
|
(int(trbrX + 20), int(trbrY + 20)), 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)
|
|
|
|
if objtype == "Screw":
|
|
|
|
if objtype == "Screw":
|
|
|
|
cv2.putText(orig, str(iteml) + "in thread", # print screw length
|
|
|
|
cv2.putText(orig, str(iteml) + "in", # print screw length
|
|
|
|
(int(trbrX + 20), int(trbrY + 20)), 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)
|
|
|
|
if objtype == "Standoff":
|
|
|
|
if objtype == "Standoff":
|
|
|
|
cv2.putText(orig, str(iteml) + "in", # print standoff length
|
|
|
|
cv2.putText(orig, str(iteml) + "in", # print standoff length
|
|
|
|
(int(trbrX + 20), int(trbrY + 20)), 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)
|
|
|
|
if objtype == "Axle":
|
|
|
|
if objtype == "Axle":
|
|
|
|
cv2.putText(orig, "{:.2f}in".format(iteml), # print axle length
|
|
|
|
cv2.putText(orig, "{:.2f}in".format(iteml), # print axle length
|
|
|
|
(int(trbrX + 20), int(trbrY + 20)), 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
|
|
|
|