|
|
@ -1,5 +1,5 @@
|
|
|
|
# import the necessary packages
|
|
|
|
# import the necessary packages
|
|
|
|
from scipy.spatial import distance as dist
|
|
|
|
#from scipy.spatial import distance as dist
|
|
|
|
from imutils import perspective
|
|
|
|
from imutils import perspective
|
|
|
|
from imutils import contours
|
|
|
|
from imutils import contours
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
@ -26,6 +26,11 @@ def sizeVexScrew(iteml):
|
|
|
|
iteml /= 8
|
|
|
|
iteml /= 8
|
|
|
|
return iteml
|
|
|
|
return iteml
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def larger(a, b):
|
|
|
|
|
|
|
|
if a >= b:
|
|
|
|
|
|
|
|
return a
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
return b
|
|
|
|
|
|
|
|
|
|
|
|
# construct the argument parse and parse the arguments
|
|
|
|
# construct the argument parse and parse the arguments
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
@ -45,6 +50,7 @@ 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, (image.shape[1]*2, image.shape[0]*2), interpolation = cv2.INTER_AREA)
|
|
|
|
if args2.show:
|
|
|
|
if args2.show:
|
|
|
|
cv2.imshow("Image", image)
|
|
|
|
cv2.imshow("Image", image)
|
|
|
|
cv2.waitKey(0)
|
|
|
|
cv2.waitKey(0)
|
|
|
@ -142,8 +148,8 @@ for c in cnts:
|
|
|
|
cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
|
|
|
|
cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
|
|
|
|
(255, 0, 255), 2)
|
|
|
|
(255, 0, 255), 2)
|
|
|
|
# compute the Euclidean distance between the midpoints
|
|
|
|
# compute the Euclidean distance between the midpoints
|
|
|
|
dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
|
|
|
|
dA = np.linalg.norm(np.array((tltrX, tltrY, 0)) - np.array((blbrX, blbrY, 0)))
|
|
|
|
dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
|
|
|
|
dB = np.linalg.norm(np.array((tlblX, tlblY, 0)) - np.array((trbrX, trbrY, 0)))
|
|
|
|
|
|
|
|
|
|
|
|
# if the pixels per metric has not been initialized, then
|
|
|
|
# if the pixels per metric has not been initialized, then
|
|
|
|
# compute it as the ratio of pixels to supplied metric
|
|
|
|
# compute it as the ratio of pixels to supplied metric
|
|
|
@ -154,23 +160,18 @@ for c in cnts:
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimA = dA / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
dimB = dB / pixelsPerMetric
|
|
|
|
|
|
|
|
|
|
|
|
if num == selected:
|
|
|
|
if num == num:
|
|
|
|
itemw = dimA
|
|
|
|
iteml = larger(dimA, dimB)
|
|
|
|
itemh = dimB
|
|
|
|
|
|
|
|
if itemw >= itemh:
|
|
|
|
|
|
|
|
iteml = itemw
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
iteml = itemh
|
|
|
|
|
|
|
|
print("Screw Length (RAW): " + str(iteml))
|
|
|
|
print("Screw Length (RAW): " + str(iteml))
|
|
|
|
iteml = sizeVexScrew(iteml)
|
|
|
|
iteml = sizeVexScrew(iteml)
|
|
|
|
print("Rounded Length: " + str(iteml))
|
|
|
|
print("Rounded Length: " + str(iteml))
|
|
|
|
# draw the object sizes on the image
|
|
|
|
# draw the object sizes on the image
|
|
|
|
if args2.show:
|
|
|
|
if args2.show:
|
|
|
|
cv2.putText(orig, "{:.5f}in".format(dimA),
|
|
|
|
cv2.putText(orig, "{:.5f}in".format(larger(dimA, dimB)),
|
|
|
|
(int(tltrX - 15), int(tltrY - 10)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
(int(trbrX), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
cv2.putText(orig, "{:.5f}in".format(dimB),
|
|
|
|
cv2.putText(orig, "{:.3f}in".format(iteml), # print screw length
|
|
|
|
(int(trbrX + 10), int(trbrY)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
(int(trbrX), int(trbrY + 20)), cv2.FONT_HERSHEY_SIMPLEX,
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
0.65, (255, 255, 255), 2)
|
|
|
|
|
|
|
|
|
|
|
|
# show the output image
|
|
|
|
# show the output image
|
|
|
|