I use gesture recognition mediapipe and wanted to mix with `listen_in_background` function ceased to function after code modifications. The cursor movement has become slow and laggy, with pauses occurring when Speech Recognition is activated.
def callback(recognizer, audio):
try:
print(recognizer.recognize_google(audio))
text = recognizer.recognize_google(audio, language = "id")
if "Tuliskan" in text.lower():
speak("Silahkan Bicara!")
teks = recognizer.recognize_google(audio, language = "id")
pyautogui.write(teks)
except sr.UnknownValueError:
pass
def main():
recognizer = sr.Recognizer()
mic = sr.Microphone(0)
with mic as source:
recognizer.adjust_for_ambient_noise(source)
stop_listening = recognizer.listen_in_background(mic, callback)
stop_listening(wait_for_stop=True)
while True:
pTime = 0 # Digunakan untuk menghitung frame rate
width = 320 # Lebar Kamera
height = 240 # Tinggi Kamera
frameR = 60 # Frame Rate
smoothening = 8 # Faktor Pelancaran
prev_x, prev_y = 0, 0 # Koordinat Sebelumnya
curr_x, curr_y = 0, 0 # Koordinat Saat Ini
cap = cv2.VideoCapture(0) # Mendapatkan feed video dari webcam
cap.set(3, width) # Menyesuaikan ukuran
cap.set(4, height)
detector = ht.handDetector(maxHands=1) # Mendeteksi satu tangan maksimal
screen_width, screen_height = pyautogui.size() # Mendapatkan ukuran layar
while True:
success, img = cap.read()
img = detector.findHands(img) # Mencari tangan
lmlist, bbox = detector.findPosition(img) # Mendapatkan posisi tangan
if len(lmlist)!=0:
x1, y1 = lmlist[8][1:]
x2, y2 = lmlist[12][1:]
fingers = detector.fingersUp()
cv2.rectangle(img, (frameR, frameR), (width - frameR, height - frameR), (255, 0, 255), 2)
if fingers[1] == 1 and fingers[2] == 0:
x3 = np.interp(x1, (frameR,width-frameR), (0,screen_width))
y3 = np.interp(y1, (frameR, height-frameR), (0, screen_height))
curr_x = prev_x + (x3 - prev_x) / smoothening
curr_y = prev_y + (y3 - prev_y) / smoothening
pyautogui.moveTo(screen_width - curr_x, curr_y)
cv2.circle(img, (x1, y1), 7, (255, 0, 255), cv2.FILLED)
prev_x, prev_y = curr_x, curr_y
if fingers[0] == 1 and fingers[1] == 1:
length, img, lineInfo = detector.findDistance(4, 6, img)
print(length)
if length < 30:
cv2.circle(img, (lineInfo[4], lineInfo[5]), 15, (0, 255, 0), cv2.FILLED)
pyautogui.click() # Melakukan Klik
if fingers[1] == 1 and (fingers[0] == 0 and fingers[4] == 1):
pyautogui.scroll(100)
if fingers[1] == 1 and (fingers[0] == 1 and fingers[4] == 1):
pyautogui.scroll(-100)
cTime = time.time()
fps = 1/(cTime-pTime)
pTime = cTime
cv2.putText(img, str(int(fps)), (20, 50), cv2.FONT_HERSHEY_PLAIN, 3, (255, 0, 0), 3)
cv2.imshow("Image", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
I’ve encountered performance issues after integrating gesture recognition with the listen_in_background
function for speech recognition. As a result, cursor movement has become sluggish and pauses occur during speech recognition activation. Help me to fix this code! Thanks.
Ryas Rafi Karim is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.