前端也可以这样零基础入门Pinecone五

假如你和我一样在准备24年的春招,在前端全栈外,再准备一些AI的内容是非常有必要的。24年是AI红利年,AIGC+各种岗位大厂机会会多些,同意的请点赞。也欢迎朋友们加我微信shunwuyu, 一起交流。

前言

上篇文章我们使用混合向量开发了时尚品的文搜图功能,本文我们来看Pinecone在面部识别领域的使用。

安装依赖

yaml 复制代码
# DeepFace 是一个基于 Python 的开源人脸识别库,它封装了深度学习模型来实现人脸识别、人脸验证、人脸检测和人脸属性分析等功能
!pip install deepface

引入依赖

python 复制代码
from deepface import DeepFace
# 向量数据库
from pinecone import Pinecone, ServerlessSpec
# Scikit-learn是基于Python的开源机器学习库
# PCA 成份分析
from sklearn.decomposition import PCA
# 
from sklearn.manifold import TSNE
# 命令行工具
from tqdm import tqdm
# 上下文管理器
import contextlib
# 匹配格式
import glob
# 可视化库
import matplotlib.pyplot as plt
# python中的科学计算库
import numpy as np
import os
# 数据分析
import pandas as pd
# 时间标准库
import time

准备数据集

我们这里的数据集主要是图片集,使用wget把它下载下来,主要是面部图片。

下载并解压数据

python 复制代码
# wget 下载命令
# -q 安静模式
# --show-progress 显示下载进度条
# -o 重命名为family_photos.zip  
# 资源来自dropbox
!wget -q --show-progress -O family_photos.zip https://www.dropbox.com/scl/fi/yg0f2ynbzzd2q4nsweti5/family_photos.zip?rlkey=00oeuiii3jgapz2b1bfj0vzys&dl=0%22
# 解压缩
!unzip -q family_photos.zip

解压后图片文件夹是family,里面包含child,dad,woman三种图片

封装显示图片函数

python 复制代码
# matplotlib 是可视化库  取名为plt
import matplotlib.pyplot as plt
# 定义一个显示图片的函数
def show_img(f):
  # 使用imread方法将文件读入内存
  img = plt.imread(f)
  # 新建一个图表 大小为宽4inch  高3inch
  plt.figure(figsize=(4,3))
  将图片显示出来
  plt.imshow(img)

show_img('family/dad/P06260_face5.jpg')

这不是美国那位吉吉国王吗?就是他和建国同志在任的时候,我国发展非常迅猛... 期待明年建国同志胜出。

再看一张

scss 复制代码
show_img('family/child/P04414_face1.jpg')

child? 这85年的吧....

实例化Pinecone

ini 复制代码
INDEX_NAME = 'dl-ai'
pinecone = Pinecone(api_key='你自己的key')

使用DeepFace生成嵌入

python 复制代码
# 但若使用 Facenet 模型进行特征提取,则意味着在调用此函数时,它会根据 Facenet 的架构和训练参数来处理输入图像,并返回相应的人脸特征向量
MODEL="Facenet"
def generate_vectors():
  # 后缀为vec的文件
  VECTOR_FILE = "./vectors.vec"
  # 当出现文件不存在错误时,不中止程序运行,移除文件
  # contextlib 表示python运行上下文
  with contextlib.suppress(FileNotFoundError):
    os.remove(VECTOR_FILE)
  # 以写的方式打开文件 with 会在操作完后关闭文件
  with open(VECTOR_FILE, "w") as f:
    # 遍历每个子文件夹
    for person in ["mom", "dad", "child"]:
      # 使用glob匹配文件夹下的所有文件
      files = glob.glob(f'family/{person}/*')
      # 遍历这些文件
      for file in tqdm(files):
        # try except 
        try:
          # 提取人脸特征
          # img_path 图片  model_name指定模型  
          # enforce_detection在提取特征前进行人脸检测
          embedding = DeepFace.represent(img_path=file, model_name=MODEL, enforce_detection=False)[0]['embedding']
          # 向文件写入embedding 某个类型:文件名:embedding
          f.write(f'{person}:{os.path.basename(file)}:{embedding}\n')
        except (ValueError, UnboundLocalError, AttributeError) as e:
          print(e)

generate_vectors()

这样我们就完成了头像图片的向量化, 这次是保存在了本地文件中。使用!head -10 vectors.vec命令查看内容:

ini 复制代码
mom:P11968_face0.jpg:[-0.968216061592102, 0.445657879114151, -0.3362976908683777, -1.7201595306396484, -2.2699625492095947, 0.09954167902469635, 1.3653056621551514, 1.2067477703094482, 0.717734694480896, 0.15687556564807892, 1.3817498683929443, -1.1955968141555786, -0.09449852257966995, -1.1754977703094482, 0.580062210559845, 0.6901472806930542, 0.24539628624916077, -0.3208266496658325, -0.12135984003543854, -2.2810440063476562, -0.7281702756881714, -1.360960602760315, 0.04452701658010483, 1.062891960144043, 0.752934992313385, -0.06543054431676865, 1.2959623336791992, 0.0018401220440864563, -0.9969148635864258, 0.9450318217277527, -0.5798383951187134, -0.8587795495986938, 0.8826146721839905, 0.7304753065109253, 1.8957456350326538, -0.7780979871749878, -0.8198385834693909, 2.914519786834717, 0.6683512330055237, 0.7456151247024536, -1.2877490520477295, -1.9466755390167236, -1.9843796491622925, -0.4559321403503418, 0.7317990660667419, -0.7632724046707153, 0.7336459755897522, 0.33599770069122314, 1.6119154691696167, -2.933316946029663, -0.9780626893043518, 0.6531170606613159, -0.7011042237281799, 0.7603591084480286, -1.05730402469635, 1.9409853219985962, 0.7820557355880737, 1.0014525651931763, -1.3079169988632202, -0.5261071920394897, -0.5466775894165039, 2.2767152786254883, -1.9704135656356812, -0.07503129541873932, 2.7618868350982666, 0.16430547833442688, -0.7275070548057556, 0.7848039865493774, -0.22396039962768555, 1.4048340320587158, 1.9987218379974365, -0.07592078298330307, 0.2544087767601013, -0.8740766048431396, -0.04062746837735176, -1.2916429042816162, -0.6586376428604126, -0.0978061854839325, -1.0827528238296509, 0.2725602984428406, 0.9461637735366821, -1.8358964920043945, 0.9150750041007996, -0.9315770864486694, 1.0054022073745728, 1.333577275276184, 0.6469479203224182, -0.7008591294288635, -2.018449068069458, -0.3792751133441925, 1.1759753227233887, -1.714511752128601, 0.4413492977619171, -1.2517683506011963, -0.46000176668167114, 2.300297737121582, 0.06113551929593086, 1.3942131996154785, 0.2523031532764435, 1.0765972137451172, -1.5842845439910889, 1.158530592918396, 1.401320457458496, 1.6537895202636719, -0.4092678427696228, 2.0831165313720703, -0.5654659867286682, -0.0054601505398750305, 0.7226055860519409, 0.41471683979034424, -1.4260722398757935, -1.465698003768921, -1.611175298690796, -1.009490728378296, -1.5150566101074219, 0.3955858051776886, 0.2544900178909302, 0.19256356358528137, 0.5462937355041504, -0.9514122605323792, 1.7075181007385254, 1.5241360664367676, -0.47267794609069824, -0.13427861034870148, -0.24972860515117645, 0.3868950307369232, -0.9492344260215759, -0.12300881743431091] mom:P12008_face0.jpg:[-0.5887061357498169, 0.5304117202758789, 0.15951555967330933, -0.7273534536361694, 0.5044214725494385, 0.3508467674255371, 0.8140199780464172, 2.5588343143463135, -0.02461611106991768, -0.016419153660535812, 1.0168629884719849, -2.4186062812805176, -2.042652130126953, 1.2099231481552124, -0.4493248760700226, -0.7965881824493408, 1.569126844406128, -0.7159217000007629, -0.20405295491218567, -4.574435710906982, -2.083658456802368, -0.8110235333442688, -0.7481233477592468, 1.7991057634353638, 0.8001275062561035, 2.14353609085083, 0.9354774951934814, -0.9551846981048584, -0.31353819370269775, 0.06915420293807983, 1.0977894067764282, -0.3768317401409149, 0.4468839764595032, 0.7259497046470642, 1.2255383729934692, 1.0808725357055664, 1.6841777563095093, 2.0497231483459473, -0.5104880332946777, 1.8673440217971802, 0.1619165539741516, -0.17470593750476837, 1.3562804460525513, 0.6717728972434998, -0.39138591289520264, 0.7143511176109314, 1.373578667640686, -0.6004765033721924, 1.558753252029419, -2.419576644897461, -0.03739243000745773, -0.5415610074996948, 0.16832733154296875, 0.1574731171131134, -0.8309834599494934, 1.972556710243225, 0.7992818355560303, -0.33195385336875916, 0.2991611659526825, -0.6857103705406189, -2.122177839279175, 0.9075217247009277, -0.5933864116668701, 1.5317074060440063, 0.8415638208389282, 0.06221981346607208, -2.8011629581451416, 2.293999195098877, 1.0794209241867065, 1.1287868022918701, 1.7543919086456299, 1.2709848880767822, -0.24446424841880798, -0.6684283018112183, -0.4790151119232178, 0.6425163149833679, 0.23230403661727905, 1.738755464553833, -1.2423267364501953, 0.8756443858146667, -0.903393566608429, -0.8291692733764648, -0.26432299613952637, -2.237520933151245, -0.1407259702682495, 1.10054349899292, 0.7019841074943542, 0.35065382719039917, -1.0594089031219482, 1.3586798906326294, 0.7695993781089783, -0.2256750464439392, 0.04144620522856712, -1.5318025350570679, 0.08052477240562439, 1.3280327320098877, 0.20645885169506073, 0.8057777881622314, 1.460421085357666, 1.7605066299438477, -1.6875425577163696, 0.6496641039848328, 1.1807749271392822, -0.09192568063735962, -0.16714735329151154, 0.6099921464920044, -1.2543073892593384, -0.3728870749473572, -0.5456810593605042, -0.8408977389335632, -0.9488580822944641, -1.3055018186569214, -0.6944471597671509, 0.33265402913093567, -0.802715539932251, 0.4412311017513275, 0.9702012538909912, -0.3406965136528015, 2.3435862064361572, -0.3081841766834259, -0.04755358397960663, -0.6835049390792847, -0.42298680543899536, 0.31368690729141235, -1.1777665615081787, -0.08537508547306061, 0.6878381371498108, 0.45548215508461] mom:P11998_face0.jpg:[-0.412310391664505, -0.49975332617759705, 0.5541101694107056, -2.0296390056610107, -1.29654860496521, -0.925977885723114, -0.6339708566665649, 0.3960404098033905, -0.5907497406005859, -0.6090978384017944, 0.19209453463554382, 0.42242810130119324, 0.5719138383865356, 0.14031006395816803, 1.2219016551971436, 0.5340520739555359, 3.018969774246216, -1.5609216690063477, -0.3702901005744934, -1.0981788635253906, 0.15212541818618774, -1.68440580368042, -0.8833028078079224, -0.08193070441484451, 0.5595159530639648, 0.08149875700473785, 0.6767213344573975, 0.6471714973449707, -0.9967234134674072, -0.18320824205875397, -0.18644705414772034, 0.2762395143508911, 0.6485350131988525, 1.2515606880187988, 1.2737799882888794, -0.008919823914766312, 0.8799481987953186, 1.361369013786316, 0.2291206419467926, 0.9038780927658081, -1.4925864934921265, 0.4586428999900818, -0.9554730653762817, -0.265735924243927, -0.6024324893951416, -0.9552448987960815, 1.0663208961486816, 0.9577836990356445, 0.46832823753356934, -1.6386908292770386, 0.06304614245891571, -0.8836005926132202, -1.859339714050293, -0.13134419918060303, 1.2443015575408936, -0.028765583410859108, 1.8374849557876587, 1.70358145236969, -1.2315516471862793, 0.1273612380027771, -0.6808502674102783, 0.15935121476650238, -1.0713361501693726, 1.2929383516311646, 1.1189982891082764, -0.6142616868019104, -1.4537580013275146, 0.7996835708618164, 0.555777907371521, 0.3726046681404114, 1.1624181270599365, 0.11145506799221039, -1.7699902057647705, 0.13315172493457794, 0.8052123188972473, 0.0030783191323280334, -0.17470338940620422, -0.9704499244689941, -0.0985419899225235, -1.0522327423095703, -0.6576271057128906, -0.8856227397918701, 0.6831147074699402, -0.4371185898780823, -0.049655117094516754, 1.1984407901763916, 2.0618908405303955, -1.138979196548462, -3.046938419342041, -0.43273794651031494, -0.4909491240978241, -1.4836468696594238, 0.8971360325813293, -2.575829267501831, 1.2005133628845215, 1.4617562294006348, 0.5873136520385742, 1.5286787748336792, 0.49795103073120117, 1.2523051500320435, -0.5701546669006348, 0.9773967862129211, -1.0553195476531982, 0.770100474357605, -0.3590904176235199, 0.0060963742434978485, -1.103289008140564, -1.6778644323349, 0.37991225719451904, -0.8330119848251343, -1.9392001628875732, -1.8394346237182617, 0.12664157152175903, -1.4610909223556519, -1.259731650352478, 0.03365954011678696, 0.23877811431884766, 1.2200695276260376, 1.2244476079940796, -0.3733665943145752, 0.7175990343093872, 0.5796172618865967, 0.14974528551101685, -0.9250121712684631, 0.26799213886260986, 0.6178540587425232, -1.2215116024017334, 0.8878392577171326] mom:P06319_face0.jpg:[-0.6978793144226074, 0.2951180040836334, -0.07888594269752502, -1.6831132173538208, 0.6121690273284912, 0.26322001218795776, -0.23936022818088531, 2.282067060470581, 0.649990439414978, -1.0031386613845825, 1.2148176431655884, 0.18154875934123993, -0.23976898193359375, -0.15431073307991028, -0.06951509416103363, 0.5285994410514832, 0.10464830696582794, -0.8303992748260498, -0.3329645097255707, -1.2330975532531738, -0.6142452955245972, -0.906767725944519, -0.36039087176322937, 0.3742542266845703, -0.3867936432361603, -0.5678381323814392, 0.5982503890991211, -0.32306286692619324, -0.7716593742370605, 0.24828223884105682, -0.16026702523231506, -1.6291357278823853, 0.30355319380760193, 0.6977019309997559, -0.25949403643608093, 0.2595826983451843, 1.387563705444336, 0.44351378083229065, -0.6232916116714478, -0.20366737246513367, 0.24274009466171265, 0.2544693648815155, 0.11326797306537628, 0.4013108015060425, -0.6009215116500854, -1.05990469455719, 0.2042100578546524, -0.9813692569732666, 1.1998162269592285, -0.820530891418457, -1.112257957458496, -0.4910621643066406, 0.2810116410255432, -0.429147332906723, -0.41194629669189453, 0.18993724882602692, 0.8246186971664429, 0.16777841746807098, 0.2653616666793823, -0.6815406084060669, -1.1632399559020996, 1.9196869134902954, 0.5922282338142395, 1.6646971702575684, -0.3209769129753113, 0.8828633427619934, -1.9000046253204346, 0.45068180561065674, 0.18111780285835266, 0.4551704525947571, 1.1250200271606445, 0.12527617812156677, -1.3230386972427368, -0.48410606384277344, 0.5171006917953491, 0.3290998339653015, -1.1819047927856445, 1.441285252571106, 0.23847579956054688, -0.11736217886209488, -0.8324164748191833, -0.6398590803146362, -0.15160998702049255, -1.4209833145141602, 1.0133196115493774, 0.9304929971694946, 0.20119813084602356, 1.4900023937225342, -1.5750808715820312, 0.5845708847045898, 0.6659305691719055, -1.0399303436279297, 0.09400197863578796, -0.5270532369613647, -0.0012016966938972473, 0.8077495694160461, -0.4670514464378357, -0.9216868281364441, 0.8245352506637573, 1.5923129320144653, -1.079023003578186, 0.6358773112297058, 1.061441421508789, 0.5862792730331421, 0.2772138714790344, 0.6339959502220154, -0.5398303866386414, -0.29504504799842834, -1.6228588819503784, 0.7800408005714417, 0.7189667820930481, -1.2810548543930054, -0.11344262957572937, 0.29423555731773376, -1.2561767101287842, 0.5515936613082886, 0.24917122721672058, -0.06340353190898895, 0.6128406524658203, -0.8296294212341309, 0.3648151159286499, 0.03687950223684311, -0.05337192863225937, -0.7167938351631165, -0.8252075910568237, -1.1338683366775513, -0.152583047747612, -0.27112746238708496] mom:P12002_face1.jpg:[-0.885124683380127, 0.6306835412979126, -0.3750532269477844, -1.2202718257904053, -0.7217987775802612, -1.4949822425842285, -0.28204166889190674, 2.3062474727630615, 0.5463194847106934, 0.5607392191886902, 1.0689350366592407, -1.4936589002609253, -1.4912099838256836, 0.010227873921394348, -0.4643124043941498, -0.29835355281829834, 0.2863622307777405, 0.5378085374832153, -0.4816110134124756, -3.132833480834961, -2.2471492290496826, -1.3842750787734985, -0.17272451519966125, 1.9392586946487427, 0.21197031438350677, 1.249950647354126, 0.37684130668640137, -0.07924258708953857, -1.2172281742095947, 0.682195246219635, -0.40554389357566833, -0.47258949279785156, 0.10679369419813156, 0.9779988527297974, 0.6140255331993103, 0.9869796633720398, -0.2814521789550781, 2.6250641345977783, -0.268978089094162, 0.6022970676422119, 0.06238745152950287, -1.73556649684906, 0.19239382445812225, 1.6656718254089355, 0.6455463171005249, -0.09237413853406906, 0.9862394332885742, 0.14129523932933807, 1.4076915979385376, -2.186302661895752, 0.3377764821052551, -0.21479687094688416, -0.16554853320121765, 0.12405816465616226, -1.5206180810928345, 1.430692195892334, -0.7146801352500916, -0.3788992166519165, -0.5793479681015015, 0.16952133178710938, -1.2386231422424316, 1.9026070833206177, -3.2165801525115967, 0.8605889081954956, 2.708988904953003, 0.3363560438156128, -2.8755550384521484, 0.5664072036743164, 1.0124539136886597, 1.9866660833358765, 1.9415844678878784, 0.19327810406684875, 0.7214629054069519, -0.7500799894332886, 0.48836538195610046, -1.0336616039276123, 0.37422609329223633, 0.8824674487113953, -0.8497307300567627, 0.25139880180358887, 0.24295157194137573, -1.9339185953140259, 0.869602382183075, -1.123957872390747, 0.6174283027648926, 1.0181294679641724, 0.17748810350894928, -0.4278793931007385, -1.2413644790649414, 0.2623375654220581, 1.2436797618865967, -0.7355471253395081, -0.7516276836395264, -1.5256097316741943, 0.2690696120262146, 1.0358872413635254, 0.7260578274726868, 1.0721415281295776, -0.45442289113998413, 0.6256517171859741, -1.5307313203811646, 1.2722358703613281, 1.103416085243225, 0.3125779330730438, -1.0041112899780273, 1.5153605937957764, 0.8859564065933228, -0.614554762840271, -0.6509791612625122, 0.2631025016307831, -0.7489981651306152, -0.31177353858947754, -2.1315369606018066, 0.1815538853406906, -0.7350023984909058, 0.675926685333252, 0.9616477489471436, -0.6617802381515503, 0.6276552677154541, -0.8181003928184509, 1.2272578477859497, -0.22821007668972015, -0.49723827838897705, 0.26307743787765503, -1.4480643272399902, -1.7838152647018433, 0.26933956146240234, 0.9980124235153198] mom:P12001_face0.jpg:[0.4693429470062256, 0.9918367862701416, 0.02007773518562317, -1.5618599653244019, -0.22373534739017487, 0.5171708464622498, 0.48277372121810913, 0.17384673655033112, 0.16891202330589294, -0.8163312673568726, 0.8353631496429443, 0.09166836738586426, 0.16889390349388123, 0.9555816650390625, 1.5206122398376465, 0.4048248529434204, 2.2600769996643066, -0.6840249300003052, -1.062713384628296, 0.245119109749794, 0.06255093216896057, -0.39653879404067993, -1.2627493143081665, -0.7242907285690308, 1.7307084798812866, 0.6602165699005127, -0.4494488835334778, -0.6941928267478943, -0.47229325771331787, 1.2308567762374878, 0.5949972867965698, -0.4066430926322937, -1.021105408668518, 2.113630533218384, 1.4105480909347534, 0.7747635245323181, 1.6734883785247803, 0.6265256404876709, -1.1951305866241455, 0.3117653429508209, 0.08784806728363037, 0.7807608842849731, -0.986487627029419, 1.0981240272521973, -1.0295556783676147, -1.286541223526001, 0.4907180964946747, 0.30867406725883484, 1.1489590406417847, -0.44140714406967163, -0.040123939514160156, -2.019763708114624, -0.5195972919464111, -0.1560002565383911, 0.3730444610118866, -1.89228355884552, 1.5825437307357788, 0.7163602709770203, -0.08207932114601135, -0.9935861825942993, -1.5291852951049805, 1.722339391708374, -0.40479984879493713, 1.3052579164505005, 0.8719670176506042, -0.5667884945869446, -2.253321647644043, -0.49765902757644653, 1.2847648859024048, 0.4328918755054474, 2.4247219562530518, -0.20932427048683167, -1.2683731317520142, -0.18767088651657104, -0.38693079352378845, -0.3175557553768158, 0.4823887050151825, -0.6262771487236023, 0.894889235496521, 0.4935898780822754, -0.6679566502571106, -1.253635287284851, 0.29130470752716064, -0.7233673334121704, 0.9059659242630005, 1.6626814603805542, 1.2112971544265747, 0.05038514733314514, -1.0216580629348755, 0.36348071694374084, 0.2580470144748688, -0.33946436643600464, -0.5675704479217529, -1.844472050666809, 0.3761652112007141, 2.317988634109497, 1.838986873626709, -0.07075905054807663, 0.6494247913360596, 1.2986479997634888, -0.40989822149276733, 1.5628677606582642, -1.646567463874817, 0.07968255132436752, -0.937900185585022, 1.0311492681503296, -0.13589276373386383, -1.887349247932434, 0.811596155166626, -0.473477840423584, -1.9599701166152954, -1.351845622062683, 0.8204154968261719, -1.4972585439682007, 0.006597137078642845, -0.5490708947181702, 0.0948934555053711, 0.13342607021331787, 2.2057838439941406, -1.0433807373046875, 1.3479440212249756, 0.39364683628082275, 0.5054987668991089, -2.2562460899353027, -0.7263520956039429, 0.375112920999527, -0.06425844877958298, 0.041148748248815536] mom:P11974_face14.jpg:[-0.7379695177078247, -0.4042418897151947, -0.7573766112327576, -1.1562144756317139, 0.045250434428453445, 1.702317714691162, -0.32682347297668457, 1.844452142715454, 0.20980212092399597, -1.1324323415756226, 1.5319277048110962, -0.9454723000526428, 1.020400881767273, 0.28789404034614563, 0.020332135260105133, 0.09135210514068604, 0.5389947891235352, -0.7356133460998535, -0.10856220126152039, 0.0923469290137291, -0.45839622616767883, -0.5532999634742737, -0.7391647100448608, -0.10520298033952713, 0.054744623601436615, -0.5845881104469299, 0.0314536914229393, 0.010552473366260529, -0.10917693376541138, 0.0877712070941925, -0.4552958309650421, 0.29505330324172974, 0.5146048069000244, -0.11087215691804886, 0.444480836391449, 1.3351536989212036, 0.5968567728996277, 1.8010985851287842, -0.043206870555877686, 0.02584788389503956, 1.444579839706421, 1.2336641550064087, -0.6704355478286743, -0.5405761003494263, -1.7722827196121216, -1.4664045572280884, 1.0087275505065918, -0.48075583577156067, 1.9538272619247437, -0.489391952753067, -0.6167401671409607, 0.929837703704834, 0.6613889932632446, 0.18012741208076477, -0.2694687843322754, 0.729958713054657, -2.0286569595336914, -0.6718850135803223, 0.40930038690567017, 0.7863317728042603, -0.41988345980644226, 1.228569507598877, 0.32327497005462646, 1.2496274709701538, 1.2459369897842407, 0.4137904644012451, -0.7690682411193848, 2.3409030437469482, 2.7970902919769287, 0.9571412801742554, -0.21243643760681152, 1.1087311506271362, -1.7283456325531006, -0.4932098984718323, -0.6435301303863525, 0.718601405620575, -1.107370376586914, -0.8024804592132568, -0.36060190200805664, 0.31663691997528076, 0.15504121780395508, -1.047003149986267, 0.4551919996738434, 1.2000256776809692, 1.6232168674468994, 0.5910592079162598, 1.5044678449630737, 0.08462825417518616, -2.2243151664733887, 0.4480581283569336, 1.1809412240982056, -0.2041378617286682, -0.2699142098426819, -0.2707596719264984, -1.630637526512146, 2.556103229522705, -0.07803045213222504, -0.9746042490005493, 0.8865504860877991, 1.8931903839111328, -1.5178061723709106, 1.5465965270996094, -0.0706569254398346, 0.3818765878677368, -0.866223156452179, 0.8535383939743042, -0.6714646816253662, -0.7217880487442017, -0.62645024061203, -0.3445039987564087, 0.6652464866638184, -1.217846155166626, -0.73545241355896, -0.24863317608833313, -0.4368624687194824, 0.29625076055526733, -0.44234707951545715, 0.20916563272476196, 0.677785336971283, -0.4471585750579834, 0.8431254029273987, 0.5042789578437805, 0.4140622019767761, -0.2667626142501831, -1.0652058124542236, -0.6822239756584167, 0.048341624438762665, 0.27830231189727783] mom:P11993_face3.jpg:[-1.123497486114502, 0.3329506516456604, -0.39872509241104126, -1.1060476303100586, -0.0035009789280593395, 1.2555912733078003, 0.9569195508956909, 0.866183340549469, 0.01923675835132599, 0.16477108001708984, 0.26663339138031006, -0.09605305641889572, -0.38517439365386963, 0.19702312350273132, 0.508479118347168, 0.6809326410293579, 0.9020063877105713, -0.7882815599441528, -0.9461567401885986, -2.6976218223571777, -0.5036293864250183, -1.0711071491241455, -1.7211778163909912, -0.7269402742385864, 0.85201096534729, -1.0554232597351074, 0.21610823273658752, 0.16784486174583435, -1.3572380542755127, 0.7617456316947937, 0.20106439292430878, -0.40298575162887573, -0.39954450726509094, 0.23617640137672424, 0.7527325749397278, -0.6248590350151062, 1.2226283550262451, 0.45911693572998047, -1.0374853610992432, 0.03357537463307381, 0.4536304175853729, 0.02197526954114437, -0.311689555644989, 1.4029793739318848, -0.8910120725631714, -0.6181951761245728, 1.2046805620193481, -0.6118389368057251, 1.6380995512008667, -0.5860062837600708, -2.162771463394165, -0.6221408843994141, 0.3121047019958496, 0.4055946469306946, 0.25862228870391846, -0.648304283618927, -0.14123442769050598, 0.11790795624256134, -0.04261499643325806, -0.7070632576942444, -1.8121979236602783, 1.3741048574447632, 0.05148409679532051, 1.3364571332931519, 1.3082977533340454, 0.732647716999054, -2.139827251434326, 1.1205025911331177, 1.5330952405929565, -0.23695912957191467, 1.1592611074447632, 0.8417230844497681, -0.8436424732208252, -0.8675229549407959, -0.5544761419296265, 0.3982168138027191, -0.16909852623939514, 0.72883141040802, 1.2420923709869385, 1.4833438396453857, -0.5588183403015137, -1.1721683740615845, -0.1667061597108841, -1.1322131156921387, 0.5617260932922363, 0.7630230784416199, -0.0687982365489006, -0.005390331149101257, -1.5427924394607544, 1.9241856336593628, 1.667749047279358, -1.1904804706573486, -0.5765088200569153, -1.066537857055664, -2.8185546398162842e-05, 2.7865827083587646, 0.09398944675922394, 0.7560099959373474, 0.8308619260787964, 1.8771098852157593, -0.8682879209518433, 1.2611900568008423, -0.08927035331726074, 0.4481830298900604, -0.8522242307662964, 0.5717689394950867, -1.3774298429489136, -0.8273438215255737, -0.6800111532211304, -0.2260066270828247, -0.9232903122901917, -0.8459019660949707, 0.23453620076179504, -0.3910328447818756, -1.2191736698150635, -0.2571910321712494, 0.35482436418533325, 0.27631038427352905, 0.9732153415679932, -0.31667599081993103, 1.0457793474197388, -0.3087473213672638, 0.3912655711174011, -0.22965779900550842, -1.3781200647354126, -0.001312844455242157, 0.6271800398826599, -0.08744017779827118] mom:P04408_face1.jpg:[-1.840051293373108, 0.22440817952156067, 0.4242424964904785, -0.8953762054443359, -0.6533581018447876, -0.7603031396865845, 1.1534945964813232, 2.6610302925109863, 0.5274031162261963, 0.004054466262459755, 0.5439660549163818, -2.027517795562744, -2.16428804397583, 0.2713313102722168, 0.36724478006362915, 0.9925891757011414, 0.5424047708511353, 0.017632849514484406, -0.9081400632858276, -3.2198615074157715, -1.2091883420944214, -1.076210856437683, -1.2595034837722778, 0.7938194870948792, -0.43745842576026917, 2.220089912414551, 1.105002999305725, -0.561704158782959, -0.7561405897140503, 0.9024154543876648, 0.7943824529647827, -0.5986282229423523, 1.2396701574325562, -0.5071181654930115, 1.27314031124115, 0.5751702785491943, -0.2373080998659134, 1.3109606504440308, -0.37839990854263306, 1.8219406604766846, -0.9801859259605408, -1.0643404722213745, 0.3074835240840912, 0.1774088740348816, 1.1048239469528198, 1.1685893535614014, 1.2174092531204224, -0.06923776865005493, 0.34243690967559814, -1.7516289949417114, -0.4651883840560913, 0.1487419456243515, -0.25553449988365173, 0.46395954489707947, -0.5510984659194946, 1.057896375656128, 0.8919547200202942, 1.1942631006240845, 0.3808321952819824, -0.15798264741897583, -0.7534332275390625, 1.335855484008789, -1.4320331811904907, 1.5475040674209595, 1.919617772102356, -0.29917460680007935, -1.7121920585632324, 0.47750893235206604, 0.7679278254508972, 0.8516662120819092, 1.728156566619873, -0.4331072270870209, 1.266777753829956, -0.8815213441848755, 0.2180769294500351, -0.1921214908361435, -0.13689762353897095, 1.1614031791687012, -0.4271100163459778, 0.8891932964324951, -0.6034945249557495, -1.4771969318389893, 0.3081287145614624, -2.3695380687713623, -0.38527488708496094, 0.4135417640209198, -0.09685802459716797, 0.13916443288326263, -0.7718480825424194, 1.1201646327972412, 0.27840277552604675, -0.14088581502437592, -0.2119666337966919, -1.3032640218734741, -0.8874457478523254, 1.2188917398452759, -0.5170309543609619, 1.3099908828735352, 0.22862428426742554, 1.550218939781189, -1.3275991678237915, 0.6737821102142334, 0.900898277759552, 1.1720936298370361, 0.7808798551559448, -0.22235876321792603, -0.8069254755973816, 1.0128695964813232, 0.08904552459716797, 0.966199517250061, -0.49528634548187256, -1.67842698097229, -0.5314193964004517, 0.9697139263153076, -1.4126033782958984, 0.053105391561985016, 1.539473533630371, -0.17980462312698364, 1.1963423490524292, -1.3669707775115967, 1.301892876625061, -0.13904909789562225, -1.995430827140808, 0.09063214063644409, -1.4832825660705566, -0.3776320219039917, -0.28616762161254883, -0.3036036491394043] mom:P12014_face0.jpg:[-0.45776596665382385, -0.1608966439962387, -0.4403732120990753, -0.9605675935745239, -0.060026124119758606, 0.20870406925678253, -0.7747851014137268, 1.3989213705062866, 0.8480031490325928, -1.2829774618148804, 0.9499905705451965, -0.286859393119812, -0.5458780527114868, 0.22331076860427856, -0.19667401909828186, -0.1265624463558197, 2.292637825012207, -0.49668198823928833, -0.30734387040138245, -0.22986377775669098, 0.22077691555023193, -0.3161260485649109, -0.9985576272010803, -0.04209045320749283, 0.0705764889717102, 1.0892188549041748, -0.03980613499879837, -1.0140001773834229, -0.5935903787612915, -0.8512312769889832, 0.806475043296814, 0.06018230319023132, -0.0829833447933197, 1.6641591787338257, 0.6843864321708679, 0.41341274976730347, 1.0313209295272827, 1.7354482412338257, -0.3849307894706726, 0.05874037742614746, -0.3718493580818176, 0.454889714717865, 0.6878658533096313, -0.17548754811286926, -1.6029307842254639, -0.15262645483016968, 1.2134536504745483, -0.3561040461063385, -1.016880989074707, -0.923904538154602, 0.39905065298080444, -0.672386109828949, -1.0823421478271484, 0.178731769323349, 0.8551586270332336, 0.275662899017334, 1.7003824710845947, 0.23287203907966614, -1.1980153322219849, -1.1331011056900024, -0.32555830478668213, 0.8534132242202759, -0.20691707730293274, 1.8863321542739868, 0.6990032196044922, -0.4517958164215088, -1.4100182056427002, 0.08767521381378174, -0.12874531745910645, 0.2875566780567169, 2.021920919418335, -0.458909809589386, -2.2322306632995605, 0.24853722751140594, 0.6579110622406006, 0.6900768280029297, -0.3293004631996155, -0.4528201222419739, -0.5904261469841003, -0.6629689931869507, -0.023045483976602554, -0.6328351497650146, 0.7827434539794922, -0.10722246021032333, 0.04432966187596321, 1.1510006189346313, 1.8603888750076294, 0.4526074528694153, -1.9472222328186035, -0.6422443389892578, -1.3227499723434448, -2.5703225135803223, 0.5626535415649414, -1.3982484340667725, 1.2651842832565308, 0.056130703538656235, 1.1428440809249878, 0.9297282695770264, 1.4345769882202148, 0.9096906185150146, -0.8568684458732605, 0.03077957034111023, -0.8177101016044617, 1.8104140758514404, 0.34706950187683105, 0.34468621015548706, -0.3368653953075409, -0.4985691010951996, 0.05004199594259262, -0.7683719396591187, -0.12789031863212585, -1.8638317584991455, 0.664151668548584, 0.05632844567298889, -1.8920429944992065, -0.7893905639648438, -0.622077226638794, 0.5538994073867798, 1.162582516670227, -1.4523274898529053, 0.15236160159111023, 1.0238903760910034, -0.5238248109817505, -0.40052345395088196, -0.4414052367210388, 0.4711957275867462, -1.4518650770187378, 0.721942663192749]

绘制图像数据

python 复制代码
# 因为DeepFace的向量是高维的,所以要做一些转化工作 
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
def gen_tsne_df(person, perplexity):
    # 准备向量数组
    vectors =[]
    # 以读的方式打开vectors文件
    with open('./vectors.vec', 'r') as f:
      # 逐行读取 并匹配进度
      for line in tqdm(f):
        # 将本行切割成 类别,文件名,向量
        p, orig_img, v = line.split(':')
        # 如果类型一致 比如都是child
        if person == p:
            # 添加向量
            vectors.append(eval(v))
    # 实例化 降维 只保留8个部份
    pca = PCA(n_components=8)
    # 非线性降维 第一个参数2 二维空间 
    # perplexity 困惑度 
    # n_iter 找到降维,最多迭代次数
    # metric='euclidean' 欧几里得计算
    # 更新低维空间坐标的速度
    tsne = TSNE(2, perplexity=perplexity, random_state = 0, n_iter=1000,
        verbose=0, metric='euclidean', learning_rate=75)
    print(f'transform {len(vectors)} vectors')
    
    pca_transform = pca.fit_transform(vectors)
    embeddings2d = tsne.fit_transform(pca_transform)
    # 返回pd 数据结构 
    return pd.DataFrame({'x':embeddings2d[:,0], 'y':embeddings2d[:,1]})

封装tsne绘制函数

ini 复制代码
def plot_tsne(perplexity, model):
    #新建一个包含多个子区域的图, 8,5 是尺寸
    # ax 代表坐标轴对象
    (_, ax) = plt.subplots(figsize=(8,5))
    #plt.style.use('seaborn-whitegrid')
    # 画grid
    plt.grid(color='#EAEAEB', linewidth=0.5)
    ax.spines['top'].set_color(None)
    ax.spines['right'].set_color(None)
    ax.spines['left'].set_color('#2B2F30')
    ax.spines['bottom'].set_color('#2B2F30')
    #不同的类型用不同的颜色 
    colormap = {'dad':'#ee8933', 'child':'#4fad5b', 'mom':'#4c93db'}

    for person in colormap:
        embeddingsdf = gen_tsne_df(person, perplexity)
        # 绘制散点图
        ax.scatter(embeddingsdf.x, embeddingsdf.y, alpha=.5, 
                   label=person, color=colormap[person])
    plt.title(f'Scatter plot of faces using {model}', fontsize=16, fontweight='bold', pad=20)
    plt.suptitle(f't-SNE [perplexity={perplexity}]', y=0.92, fontsize=13)
    plt.legend(loc='best', frameon=True)
    plt.show()
bash 复制代码
# 44的熵
plot_tsne(44, 'facenet')
  • 创建index
ini 复制代码
from pinecone import Pinecone, ServerlessSpec

INDEX_NAME='dl-ai'
pinecone = Pinecone(api_key='515c9a29-ebf3-4b0b-ab55-e67e50cf31cc')
if INDEX_NAME in [index.name for index in pinecone.list_indexes()]:
  pinecone.delete_index(INDEX_NAME)
pinecone.create_index(name=INDEX_NAME, dimension=128, metric='cosine',
  spec=ServerlessSpec(cloud='aws', region='us-west-2'))

index = pinecone.Index(INDEX_NAME)
  • 将向量存入数据库
python 复制代码
def store_vectors():
  # 以读的方式打开文件
  with open("vectors.vec", "r") as f:
    # 遍历每一行
    for line in tqdm(f):
        # 以:切割开 类型 文件名  向量
        person, file, vec = line.split(':')
        # upsert 存入 
        index.upsert([(f'{person}-{file}', eval(vec), {"person":person, "file":file})])
store_vectors()
index.describe_index_stats()
  • 执行查询
ini 复制代码
child_base = 'family/child/P06310_face1.jpg'
show_img(child_base)

这哥们发量有点感人啊。

输入也是一个头像,生成嵌入。

scss 复制代码
# represent 方法负责生成嵌入
embedding = DeepFace.represent(img_path=child_base, model_name=MODEL)[0]['embedding']
print(embedding)
ini 复制代码
# 这里的query方法,多了一个filter查询 
query_response = index.query(
      top_k=3,
      vector = embedding,
      # 分类 等于  dad
      filter={
        "person": {"$eq": "dad"}
      },
      include_metadata=True
)

photo = query_response['matches'][0]['metadata']['file']
show_img(f'family/dad/{photo}')

至此,我们就完成了相似脸型的查询

总结

  • 面部数据集family_photos
  • 面部识别模型 DeepFace
  • 降维函数gen_tsne_df
  • query方法的filter 参数

参考资料

相关推荐
baiduopenmap14 分钟前
百度世界2024精选公开课:基于地图智能体的导航出行AI应用创新实践
前端·人工智能·百度地图
小任同学Alex17 分钟前
浦语提示词工程实践(LangGPT版,服务器上部署internlm2-chat-1_8b,踩坑很多才完成的详细教程,)
人工智能·自然语言处理·大模型
新加坡内哥谈技术23 分钟前
微软 Ignite 2024 大会
人工智能
江瀚视野1 小时前
Q3净利增长超预期,文心大模型调用量大增,百度未来如何分析?
人工智能
陪学1 小时前
百度遭初创企业指控抄袭,维权还是碰瓷?
人工智能·百度·面试·职场和发展·产品运营
QCN_1 小时前
湘潭大学人工智能考试复习1(软件工程)
人工智能
Landy_Jay1 小时前
深度学习:GPT-1的MindSpore实践
人工智能·gpt·深度学习
白光白光1 小时前
量子神经网络
人工智能·深度学习·神经网络
全域观察1 小时前
如何复制只读模式下的腾讯文档
人工智能·新媒体运营·媒体·内容运营·程序员创富
panpantt3211 小时前
【参会邀请】第二届大数据与数据挖掘国际会议(BDDM 2024)邀您相聚江城!
大数据·人工智能·数据挖掘