I made a very simple arabic speech recognition program and attempted to make an exe of it using Pyinstaller. The exe file is succcesfully generated, but when I run it It does not recognize the speech, while, when I run the program from the IDE (Pycharm) It works fin an recognize the speech as expected. Here is my code:
import speech_recognition
import pyttsx3
import pyperclip
import pyautogui
from PIL import ImageTk
import PIL.Image
import os
from tkinter import *
import tkinter as tk
import tkinter.font as font
def my_function():
print("Start:")
recognizer = speech_recognition.Recognizer()
text =""
if len(text)==0:
try:
with speech_recognition.Microphone() as mic:
recognizer.adjust_for_ambient_noise(mic, duration=0.2)
audio = recognizer.listen(mic)
text = recognizer.recognize_google(audio,language="ar-AR")
print("Recognized {}".format(text))
pyperclip.copy(text)
# Hotkey the paste command
pyautogui.hotkey("ctrl", "v")
text =""
except speech_recognition.UnknownValueError():
recognizer = speech_recognition.Recognizer()
text = ""
# create a tkinter window
root = Tk()
root.title('محوّل الصوت إلى كتابة')
root.iconbitmap('icon.ico')
root.geometry('300x400')
myFont = font.Font(size=30)
title_font = font.Font(size=20, weight="bold")
text = Label( text="محوّل الصوت إلى كتابة" )
text.place(relx=0.5, rely=0.1, anchor=CENTER)
text['font'] = title_font
btn_text = tk.StringVar()
btn = Button(root, text='بداية التسجيل' , bd='5',bg='green',activebackground='red',fg='white',command=my_function)
btn['font'] = myFont
btn.place(relx=0.5, rely=0.8, anchor=CENTER)
image = PIL.Image.open('background.jpg')
zoom = 0.35
pixels_x, pixels_y = tuple([int(zoom * x) for x in image.size])
img = ImageTk.PhotoImage(image.resize((pixels_x, pixels_y)))
label = Label(root, image=img)
label.image = img
label.place(relx=0.5, rely=0.4, anchor=CENTER)
root.mainloop()
I just figured out that the problem occurred when I convert using -w
flag. Hence, to solve that I converted my .py
script to .exe
with the console (which means with out -w
flag)
so the command was : pyinstaller -F main.py
And in order to hide the console I added the following code to my main.py
:
import ctypes
ctypes.windll.user32.ShowWindow( ctypes.windll.kernel32.GetConsoleWindow(), 0 )