人臉識別,使用 deepface + api + flask, 改寫 + 調試

1. 起因, 目的, 感受:

  • github deepface 這個項目寫的很好, 繼續研究
  • 使用這個項目,改寫 api。
  • 增加一個前端 flask app

2. 先看效果

請添加圖片描述
請添加圖片描述
請添加圖片描述

3. 過程:

大力改寫原始項目中 api 這部分的代碼,
原始項目的文件結構太繁雜了:

在這里插入圖片描述
我把這部分的內容,合為一個文件,即 api.py, 能刪盡刪。

代碼 1, api
from flask import Flask
from flask_cors import CORS
import argparse
from typing import Union
from flask import Blueprint, request
import numpy as np
import os
import tempfile
import logging
from deepface import DeepFace
from deepface.api.src.modules.core import service
from deepface.commons import image_utils
from deepface.commons.logger import Logger# 配置日志
logging.basicConfig(level=logging.INFO)
logger = Logger()
blueprint = Blueprint("routes", __name__)# 輔助函數:將 NumPy 類型轉換為 JSON 可序列化格式
def convert_numpy(obj):if isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.ndarray):return obj.tolist()elif isinstance(obj, dict):return {k: convert_numpy(v) for k, v in obj.items()}elif isinstance(obj, list):return [convert_numpy(i) for i in obj]return objdef extract_image_from_request(img_key: str) -> Union[str, np.ndarray]:"""Extracts an image from the request either from json or a multipart/form-data file.Args:img_key (str): The key used to retrieve the image datafrom the request (e.g., 'img').Returns:img (str or np.ndarray): Given image detail (base64 encoded string, image path or url)or the decoded image as a numpy array."""if request.files:logging.info(f"request: {request}")logging.info(f"request.files: {request.files}")file = request.files.get(img_key)logging.info(f"img_key: {img_key}")logging.info(f"file: {file}")if file is None:raise ValueError(f"Request form data doesn't have {img_key}")if file.filename == "":raise ValueError(f"No file uploaded for '{img_key}'")# 獲取文件擴展名_, ext = os.path.splitext(file.filename)if not ext:ext = '.jpg'# 保存到臨時文件with tempfile.NamedTemporaryFile(delete=False, suffix=ext) as temp_file:file.save(temp_file.name)temp_file_path = temp_file.namelogging.info(f"Saved temp file: {temp_file_path}, size: {os.path.getsize(temp_file_path)} bytes")try:if not os.path.exists(temp_file_path):raise ValueError(f"Temporary file not found: {temp_file_path}")img, _ = image_utils.load_image(temp_file_path)if img is None:raise ValueError(f"Failed to load image from {temp_file_path}")logging.info(f"Loaded image shape: {img.shape if isinstance(img, np.ndarray) else 'not a numpy array'}")return imgfinally:if os.path.exists(temp_file_path):os.unlink(temp_file_path)elif request.is_json or request.form:logging.info(f"request.json: {request.json}")logging.info(f"request.form: {request.form}")input_args = request.get_json() or request.form.to_dict()if input_args is None:raise ValueError("empty input set passed")img = input_args.get(img_key)if not img:raise ValueError(f"'{img_key}' not found in either json or form data request")return imgraise ValueError(f"'{img_key}' not found in request in either json or form data")@blueprint.route("/")
def home():return f"<h1>Welcome to DeepFace API v{DeepFace.__version__}!</h1>"@blueprint.route("/represent", methods=["POST"])
def represent():input_args = (request.is_json and request.get_json()) or (request.form and request.form.to_dict())try:img = extract_image_from_request("img")except Exception as err:return {"exception": str(err)}, 400obj = service.represent(img_path=img,model_name=input_args.get("model_name", "VGG-Face"),detector_backend=input_args.get("detector_backend", "opencv"),enforce_detection=input_args.get("enforce_detection", True),align=input_args.get("align", True),anti_spoofing=input_args.get("anti_spoofing", False),max_faces=input_args.get("max_faces"),)logger.debug(obj)return convert_numpy(obj)  # 轉換 NumPy 類型@blueprint.route("/verify", methods=["POST"])
def verify():input_args = (request.is_json and request.get_json()) or (request.form and request.form.to_dict())try:img1 = extract_image_from_request("img1")except Exception as err:return {"exception": str(err)}, 400try:img2 = extract_image_from_request("img2")except Exception as err:return {"exception": str(err)}, 400verification = service.verify(img1_path=img1,img2_path=img2,model_name=input_args.get("model_name", "VGG-Face"),detector_backend=input_args.get("detector_backend", "opencv"),distance_metric=input_args.get("distance_metric", "cosine"),align=input_args.get("align", True),enforce_detection=input_args.get("enforce_detection", True),anti_spoofing=input_args.get("anti_spoofing", False),)logger.debug(verification)return convert_numpy(verification)  # 轉換 NumPy 類型@blueprint.route("/analyze", methods=["POST"])
def analyze():input_args = (request.is_json and request.get_json()) or (request.form and request.form.to_dict())try:img = extract_image_from_request("img")logging.info(f"api 里面收到的 img 是: {type(img)}")except Exception as err:return {"exception": str(err)}, 400actions = input_args.get("actions", ["age", "gender", "emotion", "race"])if isinstance(actions, str):actions = (actions.replace("[", "").replace("]", "").replace("(", "").replace(")", "").replace('"', "").replace("'", "").replace(" ", "").split(","))try:demographies = service.analyze(img_path=img,actions=actions,detector_backend=input_args.get("detector_backend", "opencv"),enforce_detection=input_args.get("enforce_detection", True),align=input_args.get("align", True),anti_spoofing=input_args.get("anti_spoofing", False),)except Exception as e:return {"error": f"Exception while analyzing: {str(e)}"}, 400logger.debug(demographies)return convert_numpy(demographies)  # 轉換 NumPy 類型def create_app():app = Flask(__name__)CORS(app)app.register_blueprint(blueprint)logger.info(f"Welcome to DeepFace API v{DeepFace.__version__}!")return appif __name__ == "__main__":deepface_app = create_app()parser = argparse.ArgumentParser()parser.add_argument("-p", "--port", type=int, default=5005, help="Port of serving api")args = parser.parse_args()deepface_app.run(host="0.0.0.0", port=args.port, debug=True)
代碼 2, flask app.py
  • 此項目,后端 api 是用 flask 寫的, 前端我也用 flask 來寫。
from flask import Flask, render_template, request, redirect, url_for, flash
from werkzeug.utils import secure_filename
import os
import uuid
import requests
import json
import numpy as npapp = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'static/uploads'
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 限制上傳文件大小為16MB
app.secret_key = 'your_secret_key'  # 用于 flash 消息# DeepFace API 的地址
DEEPFACE_API_URL = 'http://127.0.0.1:5005/analyze'# 允許的圖片擴展名
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}# 檢查文件擴展名是否允許
def allowed_file(filename):return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS# 確保上傳文件夾存在
if not os.path.exists(app.config['UPLOAD_FOLDER']):os.makedirs(app.config['UPLOAD_FOLDER'])# 輔助函數:將 NumPy 數據轉換為 JSON 可序列化格式
def convert_numpy(obj):if isinstance(obj, np.floating):return float(obj)elif isinstance(obj, np.integer):return int(obj)elif isinstance(obj, np.ndarray):return obj.tolist()elif isinstance(obj, dict):return {k: convert_numpy(v) for k, v in obj.items()}elif isinstance(obj, list):return [convert_numpy(i) for i in obj]return obj@app.route('/')
def index():# return render_template('index.html')return render_template('home.html')@app.route('/analyze', methods=['POST'])
def analyze():# 處理文件上傳if 'file' in request.files and request.files['file'].filename:file = request.files['file']if not allowed_file(file.filename):flash('不支持的文件類型,僅支持 PNG、JPG、JPEG')return redirect(url_for('index'))# 保存文件(用于前端顯示)filename = str(uuid.uuid4()) + '.' + file.filename.rsplit('.', 1)[1].lower()file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)file.save(file_path)# 重置文件流指針file.stream.seek(0)# 發送到 DeepFace APIfiles = {'img': (filename, file.stream, file.content_type)}data = {'actions': json.dumps(['age', 'gender', 'emotion', 'race']),'detector_backend': 'opencv','enforce_detection': 'true','align': 'true','anti_spoofing': 'false'}response = requests.post(DEEPFACE_API_URL, files=files, data=data)# 處理 Base64 輸入(保留以兼容現有前端)elif request.form.get('base64'):base64_string = request.form['base64']if 'base64,' in base64_string:base64_string = base64_string.split('base64,')[1]payload = {'img': f'data:image/jpeg;base64,{base64_string}','actions': ['age', 'gender', 'emotion', 'race'],'detector_backend': 'opencv','enforce_detection': True,'align': True,'anti_spoofing': False}headers = {'Content-Type': 'application/json'}response = requests.post(DEEPFACE_API_URL, json=payload, headers=headers)else:flash('請上傳圖片文件或提供 Base64 字符串')return render_template('home.html')# 檢查響應if response.status_code == 200:results = response.json()results = convert_numpy(results)flash('分析成功!')print(f"results: {results}")return render_template('home.html',   results=results, image_url=file_path if 'file' in request.files else None)else:print("API 響應:", response.text)error_msg = response.json()flash(f'API 調用失敗:{error_msg}')return  render_template('home.html')if __name__ == '__main__':app.run(debug=True, host='0.0.0.0', port=8989)

4. 結論 ,todo, 感受

  • 有些地方我覺得能自己寫,但是卻不行。 步子太大了。 即便是有AI, 很多地方我還是不理解。
  • 這個項目只能說是,不盡完善。 所以我做起來,麻煩重重。
  • 一個球投不進,也不能全怪我,有可能是隊友球傳的不好,傳的太偏了,太低了。

希望對大家有幫助。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/81889.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/81889.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/81889.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

三維表面輪廓儀的維護保養是確保其長期穩定運行的關鍵

三維表面輪廓儀是一種高精度測量設備&#xff0c;用于非接觸式或接觸式測量物體表面的三維形貌、粗糙度、臺階高度、紋理特征等參數。其主要基于光學原理進行測量。它利用激光或其他光源投射到被測物體表面&#xff0c;通過接收反射光或散射光&#xff0c;結合計算機圖像處理技…

Lambda表達式的高級用法

今天來分享下Java的Lambda表達式&#xff0c;以及它的高級用法。 使用它可以提高代碼的簡潔度&#xff0c;使代碼更優雅。 一、什么是lambda表達式 Lambda 表達式是 Java 8 引入的特性&#xff0c;用于簡化匿名內部類的語法&#xff0c;使代碼更簡潔&#xff0c;尤其在處理函…

31-35【動手學深度學習】深度學習硬件

1. CPU和GPU 1.1 CPU CPU每秒鐘計算的浮點運算數為0.15&#xff0c;GPU為12。GPU的顯存很低&#xff0c;16GB&#xff08;可能32G封頂&#xff09;&#xff0c;CPU可以一直插內存。 左邊是GPU&#xff08;只能做些很簡單的游戲&#xff0c;視頻處理&#xff09;&#xff0c;中…

【MySQL成神之路】MySQL常見命令匯總

目錄 MySQL常用命令總結 1. 數據庫操作 2. 表操作 3. 數據操作&#xff08;DML&#xff09; 4. 索引與優化 5. 用戶與權限管理 6. 備份與恢復 7. 事務控制 8. 常用函數 9. 系統狀態與日志 總結 MySQL常用命令總結 MySQL作為最流行的關系型數據庫之一&#xff0c;提供…

Dify的大語言模型(LLM) AI 應用開發平臺-本地部署

前言 今天閑著&#xff0c;搗鼓一下 Dify 這個開源平臺&#xff0c;在 mac 系統上&#xff0c;本地部署并運行 Dify 平臺&#xff0c;下面記錄個人在本地部署Dify 的過程。 Dify是什么&#xff1f; Dify是一個開源的大語言模型&#xff08;LLM&#xff09;應用開發平臺&#…

【論文閱讀】針對BEV感知的攻擊

Understanding the Robustness of 3D Object Detection with Bird’s-Eye-View Representations in Autonomous Driving 這篇文章是發表在CVPR上的一篇文章&#xff0c;針對基于BEV的目標檢測算法進行了兩類可靠性分析&#xff0c;即惡劣自然條件以及敵對攻擊。同時也提出了一…

SonarQube的核心作用與用途

SonarQube作為一個開源的代碼質量管理平臺&#xff0c;致力于持續分析代碼的健康狀態&#xff0c;幫助開發團隊提升代碼質量。以下是其核心作用與用途的詳細說明&#xff1a; 1、靜態代碼分析 SonarQube通過靜態代碼分析技術&#xff0c;自動識別代碼中的潛在問題。它能夠檢測…

AI工程師系列——面向copilot編程

前言 ? 筆者已經使用copilot協助開發有一段時間了,但一直沒有總結一個協助代碼開發的案例,特別是怎么問copilot,按照什么順序問,哪些方面可以高效的生成需要的代碼,這一次,筆者以IP解析需求為例,沉淀一個實踐案例,供大家參考 當然,其實也不局限于copilot本身,類似…

【軟件設計師】知識點簡單整理

文章目錄 數據結構與算法排序算法圖關鍵路徑 軟件工程決策表耦合類型 編程思想設計模式 計算機網絡域名請求過程 數據結構與算法 排序算法 哪些排序算法是穩定的算法?哪些不是穩定的算法,請舉出例子。 穩定排序算法&#xff1a;冒泡排序、插入排序、歸并排序、基數排序、計數…

FastAPI 支持文件下載和上傳

文章目錄 1. 文件下載處理1.1. 服務端處理1.1.1. 下載小文件1.1.2. 下載大文件&#xff08;yield 支持預覽的&#xff09;1.1.3. 下載大文件&#xff08;bytes&#xff09;1.1.4. 提供靜態文件服務1.1.5. 中文文件名錯誤 1.2. 客戶端處理1.2.1. 普通下載1.2.2. 分塊下載1.2.3. …

naive-ui切換主題

1、在App.vue文件中使用 <script setup lang"ts"> import Dashboard from ./views/dashboard/index.vue import { NConfigProvider, NGlobalStyle, darkTheme } from naive-ui import { useThemeStore } from "./store/theme"; // 獲取存儲的主題類…

Kotlin 協程 (三)

協程通信是協程之間進行數據交換和同步的關鍵機制。Kotlin 協程提供了多種通信方式&#xff0c;使得協程能夠高效、安全地進行交互。以下是對協程通信的詳細講解&#xff0c;包括常見的通信原語、使用場景和示例代碼。 1.1 Channel 定義&#xff1a;Channel 是一個消息隊列&a…

使用SQLite Studio導出/導入SQL修復損壞的數據庫

使用SQLite Studio導出/導入SQL修復損壞的數據庫 使用Zotero時遇到了數據庫損壞&#xff0c;在軟件中寸步難行&#xff0c;遂嘗試修復數據庫。 一、SQLite Studio簡介 SQLite Studio是一款專為SQLite數據庫設計的免費開源工具&#xff0c;支持Windows/macOS/Linux。相較于其…

【git config --global alias | Git分支操作效率提升實踐指南】

git config --global alias | Git分支操作效率提升實踐指南 背景與痛點分析 在現代軟件開發團隊中&#xff0c;Git分支管理是日常工作的重要組成部分。特別是在規范的開發流程中&#xff0c;我們經常會遇到類似 feature/user-management、bugfix/login-issue 或 per/cny/dev …

(八)深度學習---計算機視覺基礎

分類問題回歸問題聚類問題各種復雜問題決策樹√線性回歸√K-means√神經網絡√邏輯回歸√嶺回歸密度聚類深度學習√集成學習√Lasso回歸譜聚類條件隨機場貝葉斯層次聚類隱馬爾可夫模型支持向量機高斯混合聚類LDA主題模型 一.圖像數字化表示及建模基礎 二.卷積神經網絡CNN基本原…

在tensorflow源碼環境里,編譯出獨立的jni.so,避免依賴libtensorflowlite.so,從而實現apk體積最小化

需要在APP里使用tensorflow lite來運行PC端訓練的model.tlite&#xff0c;又想apk的體積最小&#xff0c;嘗試了如下方法&#xff1a; 1. 在gradle里配置 implementation("org.tensorflow:tensorflow-lite:2.16.1") 這樣會引入tensorflow.jar&#xff0c;最終apk的…

neo4j框架:java安裝教程

安裝使用neo4j需要事先安裝好java&#xff0c;java版本的選擇是一個犯難的問題。本文總結了在安裝java和使用Java過程中遇到的問題以及相應的解決方法。 Java的安裝包可以在java官方網站Java Downloads | Oracle 中國進行下載 以java 8為例&#xff0c;選擇最后一行的x64 compr…

[服務器備份教程] Rclone實戰:自動備份數據到阿里云OSS/騰訊云COS等對象存儲

更多服務器知識&#xff0c;盡在hostol.com 各位服務器的守護者們&#xff0c;咱們都知道&#xff0c;數據是數字時代的“黃金”&#xff0c;而服務器上的數據更是我們業務的命脈。可天有不測風云&#xff0c;硬盤可能會突然“壽終正寢”&#xff0c;手滑執行了“毀滅性”命令…

Nextjs App Router 開發指南

Next.js是一個用于構建全棧web應用的React框架。App Router 是 nextjs 的基于文件系統的路由器&#xff0c;它使用了React的最新特性&#xff0c;比如 Server Components, Suspense, 和 Server Functions。 術語 樹(Tree): 一種用于可視化的層次結構。例如&#xff0c;包含父…

山東大學計算機圖形學期末復習15——CG15

CG15 OpenGL緩沖區、讀寫操作以及混合&#xff08;Blending&#xff09; 一、OpenGL緩沖區概述 OpenGL中的緩沖區是用于存儲像素數據的內存區域&#xff0c;主要包括以下類型&#xff1a; 顏色緩沖區&#xff08;Color Buffer&#xff09;&#xff1a;存儲每個像素的顏色值…