TypeError: 'NoneType' object is not iterable, borderdetection OpenCV

906 Views Asked by At

I know there are many questions here about this type error: TypeError: 'NoneType' object is not iterable, but I could not find an answer that helped me. I'm making Tetris board detection, following the text an video from this site: https://pysource.com/2019/12/07/detect-tetris-board-and-tetrominoes-python-plays-tetris-p-3/. But when I try to run my code I get the error and I have no idea how to fix it/what even is wrong. This is my code:

import numpy as np
import argparse
import cv2
import sys



image = cv2.imread("tetris.png")

boundaries = (164, 120, 104)
boundaries1 = (166, 122, 106)

for (boundaries) in boundaries:
    lower = np.array(boundaries, dtype = "uint8")
    upper = np.array(boundaries1, dtype = "uint8")

    board_mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask = board_mask)

    _, contours = cv2.findContours(board_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
        
    cnt = contours[0]
    (board_x, board_y, board_w, board_h) = cv2.boundingRect(cnt)
    cv2.drawContours(img, [cnt], -1, (0, 255, 0), 3)
    cv2.drawContours(virtual_board, [cnt], -1, (0, 255, 0), 3)

and this is the error:

Traceback (most recent call last):
  File "C:\Users\-my.user-\Desktop\detection.py", line 21, in <module>
    contours = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=True)
TypeError: 'NoneType' object is not iterable

please help me, what is wrong and how do I fix it?

0

There are 0 best solutions below