【问题标题】:How to solve this error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'如何解决此错误: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'cv::contourArea'
【发布时间】:2021-04-05 12:04:41
【问题描述】:
  • 我正在尝试构建一个大学项目手语翻译器,检测手势的代码如下。
  • 此代码用于为手势创建数据集,每当我运行此代码时都会收到此错误:

这是我第一次问问题,所以我知道问问题的正确方法。

这是我得到的错误:

    Traceback (most recent call last):
      File "create_gesture_data.py", line 74, in <module>
        hand = segment_hand(gray_frame)
      File "create_gesture_data.py", line 40, in segment_hand
        hand_segment_max_cont = max(contours, key=cv2.contourArea(cv2.UMat()))
    cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-zsozjuva\openc
    \modules\imgproc\src\shapedescr.cpp:315: error: (-215:Assertion failed) npoints >= 0 &&
    (depth==CV_32F||depth == CV_32S) in function 'cv::contourArea'
    
    [ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-zsozjuva\opencv\module\video
    \src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async
    callback

    import cv2
    import numpy as np
    
    background = None
    accumulated_weight = 0.5
    
    ROI_top = 100
    ROI_bottom = 300
    ROI_right = 150
    ROI_left = 350
    
    
    def cal_accum_avg(frame, accumulated_weight):
    
        global background
        
        if background is None:
            background = frame.copy().astype("float")
            return None
    
        cv2.accumulateWeighted(frame, background, accumulated_weight)
    
    
    def segment_hand(frame, threshold=25):
        global background
        
        diff = cv2.absdiff(background.astype("uint8"), frame)
    
        _ , thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)
    
        # Grab the external contours for the image
        image = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        contours = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        hierarchy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
        if len(contours) == 0:
            return None
        else:
            
            hand_segment_max_cont = max(contours, key=cv2.contourArea(cv2.UMat()))
            
            return (thresholded, hand_segment_max_cont)
    
    
    cam = cv2.VideoCapture(0)
    
    num_frames = 0
    element = 10
    num_imgs_taken = 0
    
    while True:
        ret, frame = cam.read()
    
        # filpping the frame to prevent inverted image of captured frame...
        frame = cv2.flip(frame, 1)
    
        frame_copy = frame.copy()
    
        roi = frame[ROI_top:ROI_bottom, ROI_right:ROI_left]
    
        gray_frame = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
        gray_frame = cv2.GaussianBlur(gray_frame, (9, 9), 0)
    
        if num_frames < 60:
            cal_accum_avg(gray_frame, accumulated_weight)
            if num_frames <= 59:
                
                cv2.putText(frame_copy, "FETCHING BACKGROUND...PLEASE WAIT", (80, 400),
    cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,0,255), 2)
                #cv2.imshow("Sign Detection",frame_copy)
             
        #Time to configure the hand specifically into the ROI...
        elif num_frames <= 300: 
    
            hand = segment_hand(gray_frame)
            
            cv2.putText(frame_copy, "Adjust hand...Gesture for" + str(element), (200, 400),
    cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
            
            # Checking if hand is actually detected by counting number of contours detected...
            if hand is not None:
                
                thresholded, hand_segment = hand
    
                # Draw contours around hand segment
                cv2.drawContours(frame_copy, [hand_segment + (ROI_right, ROI_top)], -1, (255, 0, 0),1)
                
                cv2.putText(frame_copy, str(num_frames)+"For" + str(element), (70, 45),
    cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
    
                # Also display the thresholded image
                cv2.imshow("Thresholded Hand Image", thresholded)
        
        else: 
            
            # Segmenting the hand region...
            hand = segment_hand(gray_frame)
            
            # Checking if we are able to detect the hand...
            if hand is not None:
                
                # unpack the thresholded img and the max_contour...
                thresholded, hand_segment = hand
    
                # Drawing contours around hand segment
                cv2.drawContours(frame_copy, [hand_segment + (ROI_right, ROI_top)], -1, (255, 0, 0),1)
                
                cv2.putText(frame_copy, str(num_frames), (70, 45), cv2.FONT_HERSHEY_SIMPLEX, 1,
    (0,0,255), 2)
                #cv2.putText(frame_copy, str(num_frames)+"For" + str(element), (70, 45),
    cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
                cv2.putText(frame_copy, str(num_imgs_taken) + 'images' +"For" + str(element), (200,400),
    cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2)
                
                # Displaying the thresholded image
                cv2.imshow("Thresholded Hand Image", thresholded)
                if num_imgs_taken <= 300:
                    #cv2.imwrite(r"D:\\gesture\\train\\"+str(element)+"\\" + str(num_imgs_taken+300)
    +'.jpg', thresholded)
                    cv2.imwrite(r"D:\\gesture\\x"+"\\" + str(num_imgs_taken) + '.jpg', thresholded)
                else:
                    break
                num_imgs_taken +=1
            else:
                cv2.putText(frame_copy, 'No hand detected...', (200, 400), cv2.FONT_HERSHEY_SIMPLEX, 1,
    (0,0,255), 2)
    
        # Drawing ROI on frame copy
        cv2.rectangle(frame_copy, (ROI_left, ROI_top), (ROI_right, ROI_bottom), (255,128,0), 3)
        
        cv2.putText(frame_copy, "DataFlair hand sign recognition_ _ _", (10, 20), cv2.FONT_ITALIC, 0.5,
    (51,255,51), 1)
        
        # increment the number of frames for tracking
        num_frames += 1
    
        # Display the frame with segmented hand
        cv2.imshow("Sign Detection", frame_copy)
    
        # Closing windows with Esc key...(any other key with ord can be used too.)
        k = cv2.waitKey(30) & 0xFF
    
        if k == 27:
            break
    
    # Releasing camera & destroying all the windows...
    
    cv2.destroyAllWindows()
    cam.release()

【问题讨论】:

  • 感谢 bilal 编辑我的问题,非常感谢。

标签: python opencv


【解决方案1】:

由于python max doc,它应该看起来像:

hand_segment_max_cont = max(contours, key=cv2.contourArea)

cv2.findContours 返回轮廓列表和层次结构的元组,但不返回轮廓列表:

contours,hierachy = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

【讨论】:

  • 感谢您回答我的问题,我尝试使用您提出的编辑,但随后出现此错误:文件“create_gesture_data.py”,第 74 行,在 hand = segment_hand(gray_frame) 文件中“ create_gesture_data.py",第 40 行,在 segment_hand hand_segment_max_cont = max(contours, key=cv2.contourArea) TypeError: Expected Ptr<:umat> for argument 'contour'
  • 感谢您帮助我,真正帮助我解决了我的问题。
猜你喜欢
  • 2022-10-17
  • 2019-11-11
  • 1970-01-01
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 2019-06-26
相关资源
最近更新 更多