TypeError when using cv2.rectangle in Python OpenCV

77 views Asked by At

this is my code

colors = [(245,117,16), (117,245,16), (16,117,245)]
def prob_viz(res, actions, input_frame, colors):
    output_frame = input_frame.copy()
    for num, prob in enumerate(res):
        cv2.rectangle(output_frame, (0,60+num*40), (int(prob*100), 90+num*40), colors[num], -1)
        cv2.putText(output_frame, actions[num], (0, 85+num*40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)
        
    return output_frame
plt.figure(figsize=(18,18))
plt.imshow(prob_viz(res, actions, image, colors)) 

and this is the error I am getting

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[76], line 10
      8     return output_frame
      9 plt.figure(figsize=(18,18))
---> 10 plt.imshow(prob_viz(res, actions, image, colors))

Cell In[76], line 5, in prob_viz(res, actions, input_frame, colors)
      3 output_frame = input_frame.copy()
      4 for num, prob in enumerate(res):
----> 5     cv2.rectangle(output_frame, (0,60+num*40), (int(prob*100), 90+num*40), colors[num], -1)
      6     cv2.putText(output_frame, actions[num], (0, 85+num*40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2, cv2.LINE_AA)
      8 return output_frame

TypeError: only size-1 arrays can be converted to Python scalars
<Figure size 1800x1800 with 0 Axes>

I was expecting a display of output by using imshow. I have tried converting it into int and list(map(int) but nothing was working and I was not getting the output. What is wrong with my code ? please help me figure this out.

res =model.predict(x_test)  
#res is the prediction results of the x_test data using the model i had trained and

actions = np.array(['hello', 'yes', 'no'])
#actions refers to a NumPy array that contains a list of strings for which I have collected training and testing data

image,results = mediapipe_detection(frame,holistic)
#image is a variable used to store the captured frame for training

input_frame is the image being provided to the function 
1

There are 1 answers

0
Nikhil On

I think, the problem is you are inputting an array instead of a single value. If that is the case, you need to change prob to a single value. The index will depend on how you have used it, but I am using zero for this example:

 def prob_viz(res, actions, input_frame, colors):
    output_frame = input_frame.copy()
    for num, prob_array in enumerate(res):
      prob = prob_array[0]  # This index depends on your model output structure
      prob_length = int(prob * 100)
      cv2.rectangle(output_frame, (0, 60 + num * 40), (prob_length, 90 + num * 40), 
      colors[num], -1)
      cv2.putText(output_frame, str(actions[num]), (0, 85 + num * 40), 
      cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
    return output_frame.