KNN识别:一眼认出你喜欢的明星!

日期:2023-02-20 19:38:04 / 人气:285

全文共7985字,估计学习时长20分钟图源:unsplash你能从下图中认出本人最喜欢的名人吗?当然可以啦。而计算时机如何完成这项义务呢?接上去的几分钟里,让我们来训练一个用来辨认名人人脸的模型!完成这项义务,我们要用到同种算法!图源:Kaggle — Pins Face Recognition该数据集来源于Pinterest,然后经过精心筹划——裁剪和标志。其中,从Adriana Lima到Tom Ellis,包括105位名人及17534张人脸。经过Kaggle暂时令牌导入Kaggle数据集(在Google Colab上):from google.colab import files"""upload your Kaggle temporary token downloaded from yourKaggle account onto your local device"""files.upload() Out:Saving kaggle.json to kaggle.json{kaggle.json: b{"username":"xxx","key":"yyy"}}下载数据集:!mkdir -p ~/.kaggle!cp kaggle.json ~/.kaggle/!chmod 600 ~/.kaggle/kaggle.json!kaggle datasets download -dhereisburak/pins-face-recognitionOut:Downloading pins-face-recognition.zip to /content97% 361M/372M [00:13<00:00, 20.9MB/s]100% 372M/372M [00:14<00:00, 27.8MB/s]反省文件能否下载成功:!lsOut:kaggle.json pins-face-recognition.zip sample_data解压文件:!unzip "pins-face-recognition.zip" -d /tmp反省每个目录下的文件数量:import osprint(len(os.listdir(/tmp)))print(len(os.listdir(/tmp/105_classes_pins_dataset/)))print(len(os.listdir(/tmp/105_classes_pins_dataset/pins_tom ellis/)))print(len(os.listdir(/tmp/105_classes_pins_dataset/pins_margot robbie/)))Out:1105180221随机选取一系列图像并打印它们的外形:import cv2a = 60b = 3241for i in range(8):im =cv2.imread(/tmp/105_classes_pins_dataset/pins_margot robbie/margotrobbie+str(a+i)+_+str(b+i)+.jpg)print(im.shape)Out:(320, 302, 3)(221, 209, 3)(225, 209, 3)(221, 209, 3)(387, 365, 3)(266, 251, 3)(225, 209, 3)(185, 175, 3)这些都是不同维度的外形。尝试在Margot Robbie的目录下绘制一系列图像:from matplotlib import pyplotfrom matplotlib.image import imreadfolder = /tmp/105_classes_pins_dataset/pins_margot robbie/for i in range(8):pyplot.subplot(330 + 1 + i)filename = folder+margotrobbie+str(a+i)+_+str(b+i)+.jpgimage = imread(filename)pyplot.imshow(image)pyplot.show()尝试对2020年一月份的世界首富Elon Musk的目录做异样的打工:from matplotlib import pyplotfrom matplotlib.image import imreadfolder = /tmp/105_classes_pins_dataset/pins_elon musk/a = 191b = 1575for i in range(7):pyplot.subplot(330 + 1 + i)filename = folder+elonmusk+str(a+i)+_+str(b+i)+.jpgimage = imread(filename)pyplot.imshow(image)pyplot.show()看!无价之宝的浅笑!我们需求的一些库:import numpy as npfrom PIL import Imageimport operatorfrom operator import itemgetter尝试重设图像大小——以Talor Swift爲例(更改爲128*128*3):from PIL import Imageimg = Image.open(/tmp/105_classes_pins_dataset/pins_Taylor Swift/TaylorSwift4_4643.jpg)img = img.resize((128,128))imgnp.asarray(img) #display the pixel arrayOut:array([[[187, 191, 202],[187, 191, 202],[187, 191, 202],...,[ 94, 85, 78],[104, 95, 87],[ 98, 89, 80]],[[187, 191, 202],[188, 192, 203],[188, 192, 203],...,[ 82, 72, 66],[ 95, 84, 77],[ 92, 82, 73]],[[187, 191, 202],[187, 191, 202],[187, 191, 202],...,[107, 96, 91],[105, 94, 87],[100, 90, 81]],...,[[244, 143, 171],[243, 143, 171],[242, 145, 172],...,[186, 135, 105],[190, 137, 109],[193, 138, 111]],[[236, 144, 169],[234, 144, 169],[234, 149, 172],...,[187, 147, 117],[191, 148, 121],[184, 140, 114]],[[232, 142, 167],[230, 143, 167],[232, 150, 172],...,[170, 137, 107],[152, 116, 89],[143, 104, 78]]], dtype=uint8)反省重设后图像的宽度和高度:width, height = img.sizeprint(width, height)Out:128 128如今,费事的局部来了:逐一目录停止迭代,然后遍历其中的图像——间接将它们的大小重设爲(128*128),并将每张图像的像素矩阵附加到X上,对应的名人标签附加到y上——跟踪我们的计数:X = []y = []count = 0dir="/tmp/105_classes_pins_dataset/"for i in os.listdir(dir):print(i,":",len(os.listdir(dir+"/"+i)))count+=len(os.listdir(dir+"/"+i))for j inos.listdir(dir+"/"+i):img =Image.open(dir+"/"+i+"/"+j)img = img.resize((128,128))X.append(np.asarray(img))y.append(i)print(count)X = np.asarray(X)y = np.asarray(y)print(X.shape, y.shape)Out:pins_ellen page : 188pins_Avril Lavigne : 162pins_Marie Avgeropoulos : 161pins_Rebecca Ferguson : 178pins_Robert De Niro : 156pins_Emilia Clarke : 210pins_Dwayne Johnson : 141pins_Josh Radnor : 117pins_Ben Affleck : 126pins_Zoe Saldana : 186pins_camila mendes : 162pins_Morgan Freeman : 105pins_Alvaro Morte : 139pins_Pedro Alonso : 125pins_Taylor Swift : 131pins_Natalie Dormer : 198pins_Andy Samberg : 196pins_grant gustin : 183pins_Brie Larson : 169pins_Lindsey Morgan : 169

作者:高德娱乐




现在致电 5243865 OR 查看更多联系方式 →

COPYRIGHT 高德娱乐 版权所有