import matplotlib.pyplot as plt
import matplotlib
import numpy as np
from PIL import Image
from facenet import Facenet
import os
def mypause(interval):
backend = plt.rcParams['backend']
if backend in matplotlib.rcsetup.interactive_bk:
figManager = matplotlib._pylab_helpers.Gcf.get_active()
if figManager is not None:
canvas = figManager.canvas
if canvas.figure.stale:
canvas.draw()
canvas.start_event_loop(interval)
return
if __name__ == "__main__":
model = Facenet()
i=n=m=0
while True:
image_1_list=[]
image_2_list = []
openpath1="D:\\facenet\\facenetpytorch\\main\\img\\hard\\"
openpath2 = "D:\\facenet\\facenetpytorch\\main\\img\\hard2\\"
for image_1 in os.listdir(openpath1):
# print(image_1)
image_1_list.append(image_1)
n=n+1
image_1 = Image.open(openpath1+"\\"+image_1)
for image_2 in os.listdir(openpath2):
image_2_list.append(image_2)
m=m+1
plt.ion()
plt.show()
while(i<n and i<m):
image_1 = Image.open(openpath1 + "\\" + image_1_list[i])
image_2 = Image.open(openpath2+"\\"+image_2_list[i])
i=i+1
plt.ion()
mypause(0.00000001)
plt.subplot(1, 2, 1)
plt.imshow(np.array(image_1))
plt.subplot(1, 2, 2)
plt.imshow(np.array(image_2))
plt.ioff()
注意以下几点:
1.matplotlib.pyplot本身自带的pause函数会调用弹窗,如果使用会出现标题为“Figure 1"、“Figure 2"、“Figure 3"、“Figure 4"......等弹窗循环出现,呈现闪烁状态,解决办法:重新定义mypause函数
2.openpath需要读者根据自己需要遍历图片地址修改
3.调用os.listdir()函数遍历文件夹内图片
4.matplotlib.pyplot默认为阻塞式,即需要手动关闭才继续循环显示;matplotlib.pyplot.ion()为交互式,即不需要手动关闭弹窗即可继续显示;matplotlib.pyplot.ioff()相当于关闭交互式
5.matplotlib.pyplot.subplot()设置子图
6.若想实现视频播放效果,需要先将视频抽帧,保存在文件夹中
呈现效果:弹窗不变,两子图循环遍历,间隔时间短暂,呈现视频效果,可结合moblienet以及facenet模型进行人脸识别,进行人脸距离计算
ps:若读者只想播放视频,不需要对每帧图片进行计算处理,不需要如此操作,只需要调用opencv模块即可