EdjeElectronics / TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi

A tutorial showing how to train, convert, and run TensorFlow Lite object detection models on Android devices, the Raspberry Pi, and more!
Apache License 2.0
1.5k stars 683 forks source link

FPS drop while using tts to speak out the objects. #84

Open akashshingha850 opened 3 years ago

akashshingha850 commented 3 years ago

Hello. I tried to add some lines in the script to use tts (epeak) to call out the object names. but when an object is detected, it slows down drastically ( it is very obvious because it needs to finish the process of tts before continuing the loop)

So I tried to write the detected object names in a separate file(txt) and use another script to extract the text and apply tts on that.. But this process is not that efficient.

Do you have idea or suggestion to resolve this issue?

tCode5 commented 3 months ago

You could consider using threading or asyncio to create a seperate Thread whenever an object is on screen. Something like:

import espeak
from threading import Thread # The script already imports Threads, for demonstration

es = espeak.ESpeak()

def speak_text(text):
    es.speak(text)

# The initial script...

# Line 199 onwards:
for i in range(len(scores)):
    if ((scores[i] > min_conf_threshold) and (scores[i] <= 1.0)):
         ymin = int(max(1,(boxes[i][0] * imH)))
         xmin = int(max(1,(boxes[i][1] * imW)))
         ymax = int(min(imH,(boxes[i][2] * imH)))
         xmax = int(min(imW,(boxes[i][3] * imW)))

         cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)

         object_name = labels[int(classes[i])]
         # Start a new thread and pass the object's name as an argument
         Thread(target=speak_text, args=(object_name,), daemon=True).start()

But the overall performance always depends on the hardware this is running on.