1

I am trying to detect the largest red contour my picamera captures. The following is the code

from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2 
import numpy as np

camera = PiCamera()
camera.resoultion =(640,480)
camera.framerate = 30
rawCapture = PiRGBArray(camera)


for frame in camera.capture_continuous(rawCapture, format='bgr',use_video_port=True):
image = frame.array
hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)

lower = np.array ([0,100,100])
upper = np.array ([18,255,255])

mask = cv2.inRange(hsv,lower,upper)
areaArray = []
count = 1

_, contours, ret = cv2.findContours(mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i, c in enumerate(contours):
    area = cv2.contourArea(c)
    areaArray.append(area)

    sorteddata = sorted(zip(areaArray, contours), key=lambda x: x[0], reverse=True)
    largest = sorteddata[0][1]
    image = cv2.drawContours (image, largest, -1, (0,0,0), 3)
    cv2.imshow('Image',image)
    rawCapture.truncate(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break
cv2.destroyAllWindows()

When I execute this, I get the following error message

Traceback (most recent call last):
  File "/home/pi/Downloads/detect_color.py", line 12, in <module>
    for frame in camera.capture_continuous(rawCapture, format='bgr', use_video_port=True):
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1851, in capture_continuous
    if not encoder.wait(self.CAPTURE_TIMEOUT):
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 850, in wait
    self.stop()
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 881, in stop
    self._close_output()
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 795, in _close_output
    output.flush()
  File "/usr/lib/python2.7/dist-packages/picamera/array.py", line 285, in flush
    self.array = bytes_to_rgb(self.getvalue(), self.size or self.camera.resolution)
  File "/usr/lib/python2.7/dist-packages/picamera/array.py", line 174, in bytes_to_rgb
    'Incorrect buffer length for resolution %dx%d' % (width, height))
PiCameraValueError: Incorrect buffer length for resolution 1680x1050

can anyone highlight what I could be doing wrong? Thanks :)

1 Answer 1

1

You need to "clear" the rawCapture stream each time you load the frame. (if that makes sense. Not exactly sure how to say it :))

from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2 
import numpy as np

camera = PiCamera()
camera.resoultion =(640,480)
camera.framerate = 30
rawCapture = PiRGBArray(camera)


for frame in camera.capture_continuous(rawCapture, format='bgr',use_video_port=True):
image = frame.array

rawCapture.seek(0) #Added these line
rawCapture.truncate() #This too

hsv = cv2.cvtColor(image,cv2.COLOR_BGR2HSV)

lower = np.array ([0,100,100])
upper = np.array ([18,255,255])

mask = cv2.inRange(hsv,lower,upper)
areaArray = []
count = 1

_, contours, ret = cv2.findContours(mask, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i, c in enumerate(contours):
    area = cv2.contourArea(c)
    areaArray.append(area)

    sorteddata = sorted(zip(areaArray, contours), key=lambda x: x[0], reverse=True)
    largest = sorteddata[0][1]
    image = cv2.drawContours (image, largest, -1, (0,0,0), 3)
    cv2.imshow('Image',image)
    rawCapture.truncate(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
    break
cv2.destroyAllWindows()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.