-1

I am in the process of making a Real-Time feedback program using Python and OpenCV: a webcam will be observing a process and generating feedbacks based on what is happening.

Here is my code:

points = get_points_xml()
rect = cv2.boundingRect(np.array(points))
x, y, w, h = rect

cap = cv2.VideoCapture(0)

while (True):
    ret, frame = cap.read()

    cropped = frame[y: y + h, x: x + w]

    ycb = cv2.cvtColor(cropped.copy(), cv2.COLOR_BGR2YCrCb)
    y, cr, br = cv2.split(ycb)

    blur = cv2.blur(y, (5, 5))

    size = blur.size

    ret, thresh = cv2.threshold(blur, 60, 255, cv2.THRESH_BINARY)
    saturated, area_sat = get_saturated(thresh)
    print(saturated, "pixels", area_sat, "cm2")

    ret, thresh = cv2.threshold(blur, 80, 140, cv2.THRESH_BINARY_INV)
    empty, area_empty = get_saturated(thresh)
    print(empty, "pixels", area_empty, "cm2")

    unsaturated = size - saturated - empty
    area_unsat = unsaturated/cm2
    print(unsaturated, "pixels", area_unsat, "cm2")

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

cap.release()
cv2.destroyAllWindows()

Executing the line

cropped = frame[y: y + h, x: x + w]

results in the following error:

TypeError: only integer scalar arrays can be converted to a scalar index

This is weird, because in the 'first round' of the while loop my program is working fine, then in the 'second round' of the while loop it prompts me the above mentioned error.

What could be causing this?

Employee
  • 3,109
  • 5
  • 31
  • 50
Athylus
  • 193
  • 1
  • 1
  • 10
  • What's different between the first and the second iteration: `frame`? No. `h`? No. `x`? No. `w`? No. `y`? Yes, it gets reassigned. In the first iteration it's presumably the vertical position of a rectangle, from the second iteration on it's the Y component of a color. The two things should be named differently. – mkrieger1 Jul 31 '18 at 09:05

1 Answers1

0

You should provide any relevant information when you ask a question.

A possible answer to your question could be as follows :

Change this line in your code:

cropped = frame[y: y + h, x: x + w]

To

cropped = frame[int(round(y)):int(round(y + h)), int(round(x)):int(round(x + w))]   

In this way, you can make sure the values you used to crop the frame are all integers.

Howard GENG
  • 1,075
  • 7
  • 16