I'm creating an app that takes the text from an image and translates to a language that the user chooses from a dropdown menu. This dropdown menu is based off a dictionary which has all google translate supported languages and their respective codes. However, i'm having trouble getting the input from this dropdown menu and placing it back in python code for translation. the code to translate does work as i can put a fixed language in and it will translate to that. But how do I make the target language based on the users input. I've tried putting the language dropdown in the same form where you choose the image, giving it name "lang_target", using request.method = "POST"
, and then using target = request.args['lang_target']
. but this return the error: werkzeug.exceptions.HTTPException.wrap.<locals>.newcls: 400 Bad Request: KeyError: 'lang_target'
Python:
def Lang_target():
language_targ={
'af': 'Afrikaans', 'sq':'Albanian', 'ar': 'Arabic', 'az': 'Azerbaijani',
'be': 'Belarusian', 'bn': 'Bengali', 'ca': 'Catalan', 'zh-CN': 'Chinese Simplified',
'zh-TW': 'Chinese Traditional', 'hr': 'Croatian', 'cs': 'Czech', 'da': 'Danish',
'nl': 'Dutch', 'en': 'English', 'eo': 'Esperanto', 'et': 'Estonian',
'tl': 'Filipino', 'fi': 'Finnish', 'fr': 'French', 'gl': 'Galician',
'ka': 'Georgian', 'de': 'German', 'el': 'Greek', 'gu': 'Gujarati',
'ht': 'Haitian Creole', 'iw': 'Hebrew', 'hi': 'Hindi', 'hu': 'Hungarian',
'is': 'Icelandic', 'ga': 'Irish', 'it': 'Italian', 'id': 'Indonesian',
'ja': 'Japanese', 'kn': 'Kannada', 'ko': 'Korean', 'la': 'Latin',
'lv': 'Latvian', 'lt': 'Lithuanian', 'mk': 'Macedonian', 'ms': 'Malay',
'mt': 'Maltese', 'no': 'Norwegian', 'fa': 'Persian', 'pl': 'Polish',
'pt': 'Portuguese', 'ro': 'Romanian', 'ru': 'Russian', 'sr': 'Serbian',
'sk': 'Slovak', 'es': 'Spanish', 'sl': 'Slovenian', 'sw': 'Swahili',
'sv': 'Swedish', 'ta': 'Tamil', 'te': 'Telugu', 'th': 'Thai',
'tr': 'Turkish', 'uk': 'Ukrainian', 'ur': 'Urdu', 'vi': 'Vietnamese',
'cy': 'Welsh', 'yi': 'Yiddish',
}
return language_targ
@app.route('/selectImage')
def selectImage():
fn = image_name()
language_target = Lang_target()
return render_template("selectImage.html", image_name=image_name, fn=fn, language_target=language_target)
@app.route('/getfileHelper', methods=['GET','POST'])
def getfileHelper():
if request.method == 'POST':
#get the file name
#hfile upload: ttps://www.tutorialspoint.com/flask/flask_file_uploading.htm
file = request.files['imgfile']
filename = secure_filename(file.filename) #from werkzeug import secure_filename
#TEMP PRINT STATEMENT
#print("filename : ",filename)
#if the user did not select a file return to the selectImage.html page
if file.filename == '':
flash("No file selected. Please select an image file")
return render_template('selectImage.html')
#call the funcion whihc does the AI
texts = detect_text('static/images/'+filename)
text_translations = [] #emty list for dictionary of original text and translation
#loop through list of texts, call the transpate AI function,
# add origi nal text and tranlation to list of dictionaries
for text in texts:
#print('\n"{}"'.format(text.description))
translate_client = translate.Client() # Instantiates a client
translate_text = text.description # The text to translate
source = 'en' # The source language
target = request.args['lang_target'] # The target language
# Translate text into targget language
translation = translate_client.translate(translate_text, source_language=source, target_language=target)
# add original text and translation as a dictionary to a list
text_translations.append({'text':translate_text, 'translation':translation['translatedText']})
db_append(filename, translate_text, translation['translatedText'])
#print('TRANSLATIONS')
#print(text_translations)
return render_template('home.html', filename=filename, text_translations=text_translations)
HTML
<div class="container" style="padding-bottom: 100%; background-color: #f4976c; color: #66fcf1; ">
<h1>Select image...</h1>
<div style="height:100px;"></div>
<form action="getfileHelper" method="POST" enctype="multipart/form-data">
Project file path: <input type="file" name="imgfile">
<br>
<select name="lang_target">
{% for x in language_target%}
<option value='1'> {{ language_target[x] }}</option>
{% endfor %}
</select>
<br>
<input type="submit" value="Submit">
</form>
<br>
{% if filename %}
<img src="{{ url_for('static', filename='images/'+filename) }} " style="max-width:300px">
{% endif %}