Я изучаю opencv и испытываю проблемы при запуске 3 блоков кода. Я пытался это исправить, но не работает вообще. Другие решения заявляют об ошибке в переменной contours, и я изменил ее на исправленную, но все еще не смог запустить этот кусок кода. Кто-нибудь может мне помочь?
import numpy as np
import cv2
image = cv2.imread('images/numbers.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
#cv2.imshow("image", image)
#cv2.imshow("gray", gray)
#cv2.waitKey(0)
# Blur image then find edges using Canny
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
#cv2.imshow("blurred", blurred)
#cv2.waitKey(0)
edged = cv2.Canny(blurred, 30, 150)
#cv2.imshow("edged", edged)
#cv2.waitKey(0)
# Fint Contours
# _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
_, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
#Sort out contours left to right by using their x cordinates
contours = sorted(contours, key = x_cord_contour, reverse = False)
# Create empty array to store entire number
full_number = []
# loop over the contours
for c in contours:
# compute the bounding box for the rectangle
(x, y, w, h) = cv2.boundingRect(c)
#cv2.drawContours(image, contours, -1, (0,255,0), 3)
#cv2.imshow("Contours", image)
if w >= 5 and h >= 25:
roi = blurred[y:y + h, x:x + w]
ret, roi = cv2.threshold(roi, 127, 255,cv2.THRESH_BINARY_INV)
squared = makeSquare(roi)
final = resize_to_pixel(20, squared)
cv2.imshow("final", final)
final_array = final.reshape((1,400))
final_array = final_array.astype(np.float32)
ret, result, neighbours, dist = knn.findNearest(test, k=5)
number = str(int(float(result[0])))
full_number.append(number)
# draw a rectangle around the digit, the show what the
# digit was classified as
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(image, number, (x , y + 155),
cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
print ("The number is: " + ''.join(full_number))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-26-97be138d1b62> in <module>
19 # Fint Contours
20 # _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
---> 21 _, contours,_ = cv2.findContours(edged.copy(), cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
22
23 #Sort out contours left to right by using their x cordinates
ValueError: not enough values to unpack (expected 3, got 2)