format code
This commit is contained in:
parent
57abd697ba
commit
0e00ba7fca
36
main.py
36
main.py
@ -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,8 +129,10 @@ 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)
|
||||
area_contour = cv2.contourArea(c)
|
||||
@ -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")
|
||||
@ -200,9 +203,10 @@ for c in cnts:
|
||||
|
||||
# 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)))
|
||||
|
||||
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
|
||||
@ -223,7 +227,6 @@ for c in cnts:
|
||||
circular = True
|
||||
cv2.circle(orig, (int(x), int(y)), int(radius), (0, 255, 0), 1)
|
||||
|
||||
|
||||
objtype = "Unknown"
|
||||
itemw = larger(dimA, dimB)
|
||||
itemwr = itemw
|
||||
@ -283,7 +286,8 @@ 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)
|
||||
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:
|
||||
@ -291,7 +295,8 @@ for c in cnts:
|
||||
# (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…
x
Reference in New Issue
Block a user