I'm trying to get integrate tensorflow and picamera2 to do some basic object detection.

The logic works out fine (I think), but I'm struggling to draw the boxes/labels deduced on top of the image.

camera = Picamera2()
while True:
      frame = camera.capture_array()
      #Changing the dimensions of the frame to feed to model
      frame1 = np.copy(frame)
      frame1.setflags(write =1)
      frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
      frame_expanded = np.expand_dims(frame_rgb, axis = 0)
      #feeding frame into model "sess"
      (boxes, scores, classes, num) = sess.run(
             [detection_boxes, detection_scores, detection_classes, num_detections], feed_dict = {image_tensor:frame_expanded})

      #Here's the issues: 
            frame, np.squeeze(boxes), np.squeeze(classes).astype(np.int32), np.squeeze(scores), category_index, use_normalized_coordinates = True, line_thickness = 8)

      #Other stuff

This is a truncated version of my code.

Running this we run into errors at vizualize_boxes_and_labels_on_image_array: Traceback (most recent call last):

File "/home/[user]/tensorflow1/models/research/object_detection/Object_detection_picamera.py", line 176, in


File "/home/[user]/tensorflow1/models/research/object_detection/utils/visualization_utils.py", line 1251, in

visualize_boxes_and_labels_on_image_array draw_bounding_box_on_image_array( File "/home/[user]/tensorflow1/models/research/object_detection/utils/visualization_utils.py", line 163, in draw_bounding_box_on_image_array np.copyto(image, np.array(image_pil))

ValueError: could not broadcast input array from shape (768,1024,3) into shape (768,1024,4)

I actually can't find a solid example of picamera2 + opencv + tensorflow (everything I can see is picamera which seems to use different methods).

1 Answer 1


One possible solution might be to use frame[:,:,:3] for the visualize_boxes_and_labels_on_image_array function in your example code, which worked for me.


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.