顔認証をして検出した顔の中心に別の画像の貼り付けをしたい。
画像の貼り付けをするのは、顔を予め登録されている人物の顔と照合した時に一致した1人のみ。
顔の検出、画像の貼り付け、画像貼り付けをしたい顔のtop, right, bottom, leftそれぞれの座標の獲得はできている。
発生している問題:画像の貼り付けの位置が指定できない。ちょうど顔の中心に貼り付けをしたいのに、画像全体の左上にしか貼り付けがされない。
エラーコード:なし
importnumpyasnpimportface_recognitionimportmatplotlib.patchesasmpatchesimportmatplotlib.pyplotaspltfromPILimportImage,ImageDraw,ImageFilterim1=Image.open('sannin.jpg')im2=Image.open('output_test.jpg')defdraw_face_locations(img,locations):fig,ax=plt.subplots(figsize=(9,9))ax.imshow(img)ax.set_axis_off()fori,(top,right,bottom,left)inenumerate(locations):w,h=right-left,bottom-topax.add_patch(mpatches.Rectangle(xy=(left,top),width=w,height=h,ec='orange',lw=3,fill=None))ax.text(left,top-30,'No {}'.format(i),color='k',backgroundcolor='w')print('face (Top: {}, Left: {}, Bottom: {}, Right: {})'.format(top,left,bottom,right))back_im=im1.copy()back_im.paste(im2,(left+w/2inlocations[0],bottom+h/2inlocations[0]))back_im.save('sannin_paste.jpg',quality=95)plt.show()img1=face_recognition.load_image_file('sannin.jpg')# 検出対象の画像
img2=face_recognition.load_image_file('kao.jpg')# 登録されている人物の画像
# 検出対象の画像から顔検出する。
locations1=face_recognition.face_locations(img1,model='cnn')draw_face_locations(img1,locations1)# 登録されている人物の画像から顔検出する。
locations2=face_recognition.face_locations(img2,model='cnn')draw_face_locations(img2,locations2)# 検出対象の顔から特徴量を抽出する
encodings=face_recognition.face_encodings(img1,locations1)# 登録されている人物の顔から特徴量を抽出する。
encoding=face_recognition.face_encodings(img2,locations2)# 登録されている人物の顔の特徴量 encoding[0] と検出対象の各顔の特徴量 encodingsとの距離を計算する。
dist=face_recognition.face_distance(encodings,encoding[0])print(dist)# 登録されている人物の顔の特徴量 encoding[0] と最も距離が近い特徴量を持つ顔
print("最も距離が近いNo",np.argmin(dist))# 最も距離が近いNo 0
[試したこと]
back_im.paste(im2,(left+w/2inlocations[0],bottom+h/2inlocations[0]))
この部分を色々試している
どなたかお力を貸してください。