RuntimeError: Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images

can some help me to solve “create_from_options
detector = _CppObjectDetector.create_from_options(
RuntimeError: Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images.” for the code "import time
from picamera2 import Picamera2
import cv2
from tflite_support.task import core
from tflite_support.task import processor
from tflite_support.task import vision
import utils

model=‘detect.tflite’
num_threads=4

dispW=1280
dispH=720

picam2=Picamera2()
picam2.preview_configuration.main.size=(dispW,dispH)
picam2.preview_configuration.main.format=‘RGB888’
picam2.preview_configuration.align()
picam2.configure(“preview”)
picam2.start()

webCam=‘/dev/video2’
cam=cv2.VideoCapture(webCam)
cam.set(cv2.CAP_PROP_FRAME_WIDTH, dispW)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, dispH)
cam.set(cv2.CAP_PROP_FPS, 30)

pos=(20,60)
font=cv2.FONT_HERSHEY_SIMPLEX
height=1.5
weight=3
myColor=(255,0,0)

fps=0

base_options=core.BaseOptions(file_name=model,use_coral=False, num_threads=num_threads)
detection_options=processor.DetectionOptions(max_results=3, score_threshold=.3)
options=vision.ObjectDetectorOptions(base_options=base_options,detection_options=detection_options)
detector=vision.ObjectDetector.create_from_options(options)
tStart=time.time()
while True:
#ret, im = cam.read()
im=picam2.capture_array()
im=cv2.flip(im,-1)
imRGB=cv2.cvtColor(im,cv2.COLOR_BGR2RGB)
imTensor=vision.TensorImage.create_from_array(imRGB)
detections=detector.detect(imTensor)
image=utils.visualize(im, detections)
cv2.putText(im,str(int(fps))+’ FPS’,pos,font,height,myColor,weight)
cv2.imshow(‘Camera’,im)
if cv2.waitKey(1)==ord(‘q’):
break
tEnd=time.time()
loopTime=tEnd-tStart
fps= .9fps +.11/loopTime
tStart=time.time()
cv2.destroyAllWindows()
"

1 Like

i have the same problem if you find any solution could you tell me

1 Like

The error message you’re encountering, “RuntimeError: Input tensor has type kTfLiteFloat32: it requires specifying NormalizationOptions metadata to preprocess input images”, suggests that the TensorFlow Lite model you’re using expects the input images to be preprocessed in a specific way before they’re passed to the model for inference. This preprocessing typically involves normalizing the pixel values of the input images.

In the context of TensorFlow Lite models, normalization usually means scaling the pixel values to a range that the model is trained with, often either [0, 1] or [-1, 1]. This is essential for models trained with normalized datasets to perform correctly on new inputs.

To address this issue, you’ll need to modify your code to include normalization options as part of the model’s input preprocessing steps. Unfortunately, based on the code snippet you provided, it seems like the vision.TensorImage.create_from_array method you’re using to convert your image to a tensor doesn’t directly support specifying normalization options.

Here’s a general approach to manually apply normalization to your images before detection:

1.	Determine the correct normalization parameters (mean and std) that your model expects. This information is usually provided in the model’s documentation or the dataset it was trained on.
2.	Normalize the image data by applying these parameters.

Here’s an example snippet that demonstrates how to normalize an image to a [0, 1] range, which is a common requirement:

Assuming imRGB is your input image in RGB format

Normalize pixel values to [0, 1]

imRGB_normalized = imRGB.astype(‘float32’) / 255.0

After normalization, you can convert the normalized image to a tensor

imTensor = vision.TensorImage.create_from_array(imRGB_normalized)

If your model requires normalization to a different range or with specific mean and standard deviation values, you’ll need to adjust the normalization step accordingly. For example, if your model expects the data to be normalized to the [-1, 1] range, you could normalize your data like this:

Normalize pixel values to [-1, 1]

imRGB_normalized = (imRGB.astype(‘float32’) / 127.5) - 1

After normalizing the image data, you can proceed with the detection as you’re already doing in your code. Remember, the key is to match the preprocessing steps (including normalization) with what the model was trained with.