簡體   English   中英

如何在 python 中上傳圖像?

[英]How to upload image in python?

我正在開發一個應用程序,如果上傳的圖像與應用程序文件夾中的圖像匹配,則返回 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()沒有在任何地方被調用。 它是否有任何用途,還是應該從代碼片段中刪除?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM