Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Motion tracker using Raspberry pi 3, OpenCV and Python

error tells me "too many values to unpack".

This code is written for OpenCV 2.0, however I am using OpenCV3.1.

Am I encountering a reverse compatibility issue here or is it something more trivial?

like image 647
XOR Avatar asked Jul 01 '26 12:07

XOR


2 Answers

The line contours, hierarchy = cv2.findContours(thresholdimage,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) is giving the error as cv2.findContours returns 3 values but you are assigning them to two variables. So, correct code is -,contours, hierarchy = cv2.findContours(thresholdimage,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE) assuming you don't want the first returned value.

cv2.findContours doc: http://docs.opencv.org/3.1.0/d4/d73/tutorial_py_contours_begin.html

like image 147
tanmoy Avatar answered Jul 03 '26 02:07

tanmoy


I have updated this code to run under opencv2 and 3 per code snippet below. Rather than test for opencv3, I just do a try except and use alternate syntax if there is an error. This works OK but may slow code a little due to continually having to perform check. This is the price of adapting to code differences. I also thought of putting in a boolean variable to indicate if code is running under opencv2 or 3 but the code would still need to check the boolean for the proper syntax. The code below adapts without user intervention.

    differenceimage = cv2.absdiff(grayimage1, grayimage2)
    differenceimage = cv2.blur(differenceimage,(BLUR_SIZE,BLUR_SIZE))
    # Get threshold of difference image based on THRESHOLD_SENSITIVITY variable
    retval, thresholdimage = cv2.threshold( differenceimage, THRESHOLD_SENSITIVITY, 255, cv2.THRESH_BINARY )         
    try:
        thresholdimage, contours, hierarchy = cv2.findContours( thresholdimage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )        
    except:       
        contours, hierarchy = cv2.findContours( thresholdimage, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE )         
like image 36
Claude Avatar answered Jul 03 '26 00:07

Claude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!