0

I'm trying to do an object detection script and save the results into a csv with tensorflow and opencv.

camera = PiCamera()
    camera.resolution = (IM_WIDTH,IM_HEIGHT)
    camera.framerate = 10
    rawCapture = PiRGBArray(camera, size=(IM_WIDTH,IM_HEIGHT))
    rawCapture.truncate(0)

    for frame1 in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):

        t1 = cv2.getTickCount()

        frame = np.copy(frame1.array)
        frame.setflags(write=1)
        frame_expanded = np.expand_dims(frame, axis=0)

        (boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: frame_expanded})

        i=0
        j=0

        vis_util.visualize_boxes_and_labels_on_image_array(
            frame,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.40)

        # cv2.putText(frame,"FPS: {0:.2f}".format(frame_rate_calc),(30,50),font,1,(255,255,0),2,cv2.LINE_AA)

        #cv2.imshow('Object detector', frame)

        for element in boxes:
            current_time = int(round(time.time() * 1000))
            for score in scores[i]:
                if score > 0.6:
                    if classes[i][j] in (1,2,3,4,6,7,8):
                        if 'old_class' not in locals() or 'old_element' not in locals():
                            save_picture_and_csv(frame,classes[i][j],node_id,score)

                        elif classes[i][j] != old_class:
                            save_picture_and_csv(frame,classes[i][j],node_id,score)

                        elif not check_coords(element[i], old_element):
                            save_picture_and_csv(frame,classes[i][j],node_id,score)

                        old_class = classes[i][j]
                        old_element = element[i]        
                j=j+1
            i = i+1
        t2 = cv2.getTickCount()
        time1 = (t2-t1)/freq
        frame_rate_calc = 1/time1

        if cv2.waitKey(1) == ord('q'):
            break


    camera.close()

And the error output:

Traceback (most recent call last):
  File "ironeye.py", line 178, in <module>
    for frame1 in camera.capture_continuous(rawCapture, format="bgr",use_video_port=True):
  File "/usr/lib/python2.7/dist-packages/picamera/camera.py", line 1710, in capture_continuous
    encoder.close()
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 431, in close
    self.stop()
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 419, in stop
    self._close_output()
  File "/usr/lib/python2.7/dist-packages/picamera/encoders.py", line 349, in _close_output
    mo.close_stream(output, opened)
  File "/usr/lib/python2.7/dist-packages/picamera/mmalobj.py", line 371, in close_stream
    stream.flush()
  File "/usr/lib/python2.7/dist-packages/picamera/array.py", line 238, 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 127, in bytes_to_rgb
    'Incorrect buffer length for resolution %dx%d' % (width, height))
picamera.exc.PiCameraValueError: Incorrect buffer length for resolution 640x480

This is really weird cause was working fine in Raspbian Stretch and I flashed a fresh new SD card with Raspbian Buster and stopped working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.