I got a error saying:
AttributeError: 'tuple' object has no attribute 'append' means or fixed?
How do I correct it? move.py
image = cv2.imread('C:/Users/user/Desktop/line.jpg')
image_gray = cv2.imread('C:/Users/user/Desktop/line.jpg', cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (80, 80))
image_gray = cv2.resize(image_gray, (80,80))
blur = cv2.GaussianBlur(image_gray, ksize=(5,5), sigmaX=0)
ret, thresh = cv2.threshold(blur, 127, 255, cv2.THRESH_BINARY)
edged = cv2.Canny(blur, 10, 250)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
closed = cv2.morphologyEx(edged, cv2.MORPH_CLOSE, kernel) #important
# equation of a straight line
x1, y1, x2, y2 = 81, 63, 1035, 679
a = (y1 - y2) / (x1 - x2)
b = y1 - (a * x1)
X = sympy.symbols("X")
func = a*X + b
previous_count = 0
count = 0
# distance between a point and a straight line
def dist(P, A, B):
area = abs((A[0] - P[0]) * (B[1] - P[1]) - (A[1] - P[1]) * (B[0] - P[0]))
AB = ((A[0] - B[0]) ** 2 + (A[1] - B[1]) ** 2) ** 0.5
return (area / AB)
for i in range(closed.shape[0]):
for j in range(closed.shape[1]):
if closed[i][j] == 255:
P = (0, 25)
A0, A1, A2, A3, A4 = (81, 61), (81, 62), (81, 63), (81, 64), (81, 65)
B0, B1, B2, B3, B4 = (1035, 677), (1035, 678), (1035, 679), (1035, 680),
(1035, 681)
# Define (P, A, B) again
P = P.append((closed[i], closed[j]))
A = A0, A1, A2, A3, A4
B = B0, B1, B2, B3, B4
print('-------------------------- closed[i][j] print --------------------------')
print(closed[i][j])
print()
print('---------------------------- i, j print -------------------------------' )
print(i, j)
print()
print('----------------------- dist(P, A[i], B[i] print -----------------------')
for i in range(0, len(A)):
print(dist(P, A[i], B[i]))
if dist(P, A[i], B[i]) < 1:
count += 1
if count > previous_count:
previous_count = count
print("-----------------count print----------------------")
print(count)
print("-----------------previous_count print----------------------")
print(previous_count)
**error**
File "move.py", line 71, in <module>
P = P.append((closed[i], closed[j]))
AttributeError: 'tuple' object has no attribute 'append'
I know that tuples cannot be modified by adding, removing, etc. I made P =[], and I used append. And I changed P = [()] also.
P = [] # P = [()] also I did, but it doesn't work.
P = P. append((closed[i], closed[j]))
But it doesn't work. Like this.
File "move.py", line 84, in <module>
print(dist(P, A[i], B[i]))
File "move.py", line 58, in dist
area = abs((A[0] - P[0]) * (B[1] - P[1]) - (A[1] - P[1]) * (B[0] - P[0]))
TypeError: 'NoneType' object is not subscriptable
Tuple can't allow modify, but I can't think append to tuple. What should I do? I'm really frustrated... Thanks for reading this long post!