如何上传python中的图片?
How to upload image in python?
我正在开发一个应用程序,如果上传的图像与应用程序文件夹中的图像匹配,returns JSON 数据。选择特定图像后,我单击“上传”图像,它显示“未选择文件”。下面给出了整个代码。我该如何解决这个问题?提前谢谢!
import cv2
import face_recognition
import os
from flask import Flask, jsonify, request, redirect
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
return '''
<!doctype html>
<h1>Upload a picture for image recognition</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
'''
path = "Images"
images = []
myList = os.listdir(path)
#print(myList)
for cl in myList:
curImg = face_recognition.load_image_file(f'{path}/{cl}')
images.append(curImg)
encodeList=[]
for img in images:
encode=face_recognition.face_encodings(img)[0]
encodeList.append(encode)
print(encodeList)
def findEncodings(file_stream):
img = face_recognition.load_image_file(file)
# Get face encodings for any faces in the uploaded image
unknown_face_encodings = face_recognition.face_encodings(img)[0]
is_the_same_person = False
if len(unknown_face_encodings) > 0:
match_results = face_recognition.compare_faces(encodeList, unknown_face_encodings)
if match_results[0]:
is_obama = True
# Return the result as json
result = {
"is_the_same_person": same
}
return jsonify(result)
if __name__ == "__main__":
app.run(debug=True)
findEncodings()
未在任何地方调用。它有任何作用还是应该从代码片段中删除?
我正在开发一个应用程序,如果上传的图像与应用程序文件夹中的图像匹配,returns JSON 数据。选择特定图像后,我单击“上传”图像,它显示“未选择文件”。下面给出了整个代码。我该如何解决这个问题?提前谢谢!
import cv2
import face_recognition
import os
from flask import Flask, jsonify, request, redirect
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def upload_image():
# Check if a valid image file was uploaded
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
return '''
<!doctype html>
<h1>Upload a picture for image recognition</h1>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
'''
path = "Images"
images = []
myList = os.listdir(path)
#print(myList)
for cl in myList:
curImg = face_recognition.load_image_file(f'{path}/{cl}')
images.append(curImg)
encodeList=[]
for img in images:
encode=face_recognition.face_encodings(img)[0]
encodeList.append(encode)
print(encodeList)
def findEncodings(file_stream):
img = face_recognition.load_image_file(file)
# Get face encodings for any faces in the uploaded image
unknown_face_encodings = face_recognition.face_encodings(img)[0]
is_the_same_person = False
if len(unknown_face_encodings) > 0:
match_results = face_recognition.compare_faces(encodeList, unknown_face_encodings)
if match_results[0]:
is_obama = True
# Return the result as json
result = {
"is_the_same_person": same
}
return jsonify(result)
if __name__ == "__main__":
app.run(debug=True)
findEncodings()
未在任何地方调用。它有任何作用还是应该从代码片段中删除?