picamera.exc.PiCameraValueError: Incorrect buffer length for resolution 320x240

Viewed 142

I have the code below for detecting cirtain blobs in an image. Before implementing the code with tkinter the camera code worked fine. now that i combined it it puts ou the incorrect buffer error. I tried implementing the rawCapture.truncate(0) function at the end but that resolves in there not being displayed any gui display. Does anyone have any clue why that is?

Below is the code, i am a beginner in python so no doubt it is messy.

import cv2
import numpy as np
from tkinter import*
from PIL import Image, ImageTk
from picamera.array import PiRGBArray
from picamera import PiCamera
import time


# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (320,240)
camera.brightness = 16
camera.framerate = 10
rawCapture = PiRGBArray(camera, size=(320,240))
camera.zoom=(0.495,0.435,0.073,0.073)




win = Tk()
win.geometry("670x600+200+30")
win.resizable(False, False)
w = 320
h = 240
color = "#581845"
frame_1 = Frame(win, width=670, height=700, bg=color).place(x=0, y=0)


var7 = IntVar()
var8 = IntVar()

W = 150


thresh = Scale(frame_1, label="thresh1", from_=0, to=255, orient=HORIZONTAL, variable=var7, activebackground='#339999')
thresh.set(0)
thresh.place(x=500, y=10, width=W)
thresh2 = Scale(frame_1, label="thresh2", from_=255, to=0, orient=HORIZONTAL, variable=var8, activebackground='#339999')
thresh2.set(255)
thresh2.place(x=500, y=80, width=W)


cap = cv2.VideoCapture(0)

label1 = Label(frame_1, width=w, height=h)
label1.place(x=10, y=160)
label2 = Label(frame_1, width=w, height=h)
label2.place(x=350, y=160)
label3 = Label(frame_1, width=w, height=h)
label3.place(x=10, y=370)
label4 = Label(frame_1, width=w, height=h)
label4.place(x=350, y=370)


#def select_img():
  #  _, img = cap.read()


for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=False):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array

    grayFrame = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ret, thresh = cv2.threshold(grayFrame, 50, 255, cv2.THRESH_BINARY)
    
    # Read image
    im = image

# Set up the detector with default parameters.
    im=cv2.bitwise_not(im)

    params = cv2.SimpleBlobDetector_Params()
    params.minThreshold = 40
    params.maxThreshold = 255
    params.filterByArea = True
    params.minArea = 80
    params.filterByCircularity = False
    params.filterByConvexity = False
    params.filterByInertia = False
    
    detector = cv2.SimpleBlobDetector_create(params)


# Detect blobs.
    keypoints = detector.detect(im)
    im=cv2.bitwise_not(im)
# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle corresponds to the size of blob
    im_with_keypoints_or = cv2.drawKeypoints(im, keypoints, np.array([]), (255,255,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    im_with_keypoints_bi = cv2.drawKeypoints(thresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
    im_with_keypoints_gr = cv2.drawKeypoints(grayFrame, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)









    file = open('color.txt', 'w')
    file.write("l_b = " + str(50) + '\n' + "u_b = " + str(50))
    file.close()
    #res = cv2.bitwise_and(im, im, mask=mask)
    #rgb2 = cv2.cvtColor(res, cv2.COLOR_BGR2RGB)

    image = Image.fromarray(im_with_keypoints_or)
    iago = ImageTk.PhotoImage(image)
    label1.configure(image=iago)
    label1.image = iago

    image_2 = Image.fromarray(im_with_keypoints_bi)
    iago_2 = ImageTk.PhotoImage(image_2)
    label2.configure(image=iago_2)
    label2.image = iago_2

    

    image4 = Image.fromarray(im_with_keypoints_gr)
    iago4 = ImageTk.PhotoImage(image4)
    label4.configure(image=iago4)
    label4.image = iago4

    #win.after(10, select_img)
    #rawCapture.truncate(0)
select_img()
win.mainloop()
0 Answers
Related