'IndexError: invalid index to scalar variable while testing
This is the code used in testing. I have been getting the error shown bellow. Can someone please tell me how to solve it? This is a custom trained model. I have done it for images, but while trying for Videos I am getting this error. There are 177 images in my dataset.
The error message points to the following line:
confidence_threshold = output_data_2[0][i]
import numpy as np
import tensorflow as tf
import cv2
import time
print(tf.__version__)
Model_Path = "/content/android.tflite"
Video_path = "/content/gdrive/MyDrive/CAPSTONE/VID_20220426_164917.mp4"
interpreter = tf.lite.Interpreter(model_path=Model_Path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
class_names = ['Car', 'ambulance', 'firebrigade']
cap = cv2.VideoCapture(Video_path)
ok, frame_image = cap.read()
original_image_height, original_image_width, _ = frame_image.shape
thickness = original_image_height // 500
fontsize = original_image_height / 1500
print(thickness)
print(fontsize)
while True:
ok, frame_image = cap.read()
if not ok:
break
model_interpreter_start_time = time.time()
resize_img = cv2.resize(frame_image, (320, 320), interpolation=cv2.INTER_CUBIC)
reshape_image = resize_img.reshape(320, 320, 3)
image_np_expanded = np.expand_dims(reshape_image, axis=0)
image_np_expanded = image_np_expanded.astype('uint8') # float32
interpreter.set_tensor(input_details[0]['index'], image_np_expanded)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
output_data_1 = interpreter.get_tensor(output_details[1]['index'])
output_data_2 = interpreter.get_tensor(output_details[2]['index'])
output_data_3 = interpreter.get_tensor(output_details[3]['index'])
each_interpreter_time = time.time() - model_interpreter_start_time
for i in range(len(output_data_1[0])):
confidence_threshold = output_data_2[0][i] //Error in this line
if confidence_threshold > 0.3:
label = "{}: {:.2f}% ".format(class_names[int(output_data_1[0][i])], output_data_2[0][i] * 100)
label2 = "inference time : {:.3f}s" .format(each_interpreter_time)
left_up_corner = (int(output_data[0][i][1]*original_image_width), int(output_data[0][i][0]*original_image_height))
left_up_corner_higher = (int(output_data[0][i][1]*original_image_width), int(output_data[0][i][0]*original_image_height)-20)
right_down_corner = (int(output_data[0][i][3]*original_image_width), int(output_data[0][i][2]*original_image_height))
cv2.rectangle(frame_image, left_up_corner_higher, right_down_corner, (0, 255, 0), thickness)
cv2.putText(frame_image, label, left_up_corner_higher, cv2.FONT_HERSHEY_DUPLEX, fontsize, (255, 255, 255), thickness=thickness)
cv2.putText(frame_image, label2, (30, 30), cv2.FONT_HERSHEY_DUPLEX, fontsize, (255, 255, 255), thickness=thickness)
cv2.namedWindow('detect_result', cv2.WINDOW_NORMAL)
# cv2.resizeWindow('detect_result', 800, 600)
cv2.imshow("detect_result", frame_image)
key = cv2.waitKey(10) & 0xFF
if key == ord("q"):
break
elif key == 32:
cv2.waitKey(0)
continue
cap.release()
cv2.destroyAllWindows()
2.8.0
2
0.72
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-25-15b378ffd621> in <module>()
45
46 for i in range(len(output_data_1[0])):
---> 47 confidence_threshold = output_data_2[0][i]
48 if confidence_threshold > 0.3:
49 label = "{}: {:.2f}% ".format(class_names[int(output_data_1[0][i])], output_data_2[0][i] * 100)
IndexError: invalid index to scalar variable.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|