详情请进入 湖南阳光电子学校 已关注:人 咨询电话:13807313137 微信号:yp941688, yp94168
我们将通过连接附录A中定义的标志点来定义口罩的形状。例如,为了形成宽覆盖和中覆盖口罩,我们将用29点的标志点坐标连接(绘制)下颚线[0,16]的标志点。可以使用OpenCV中椭圆和其他三种规则形状函数绘制口罩轮廓。然后我们可以使用cv2.fillpoly函数将绘制的口罩填充颜色。
points=[]foriinrange(1,16):point=[landmarks.part(i).x,landmarks.part(i).y]points.append(point)#print(points)#宽,高覆盖口罩mask_a=[((landmarks.part(42).x),(landmarks.part(15).y)),((landmarks.part(27).x),(landmarks.part(27).y)),((landmarks.part(39).x),(landmarks.part(1).y))]#宽,中覆盖口罩mask_c=[((landmarks.part(29).x),(landmarks.part(29).y))]#宽、低覆盖口罩mask_e=[((landmarks.part(35).x),(landmarks.part(35).y)),((landmarks.part(34).x),(landmarks.part(34).y)),((landmarks.part(33).x),(landmarks.part(33).y)),((landmarks.part(32).x),(landmarks.part(32).y)),((landmarks.part(31).x),(landmarks.part(31).y))]fmask_a=points+mask_afmask_c=points+mask_cfmask_e=points+mask_e#mask_type={1:fmask_a,2:fmask_c,3:fmask_e}#mask_type[choice2]#使用PythonOpenCV-cv2.polylines()方法为[mask_type]绘制口罩轮廓:#fmask_a=wide,highcoveragemask,#fmask_c=wide,mediumcoveragemask,#fmask_e=wide,lowcoveragemaskfmask_a=np.array(fmask_a,dtype=np.int32)fmask_c=np.array(fmask_c,dtype=np.int32)fmask_e=np.array(fmask_e,dtype=np.int32)mask_type={1:fmask_a,2:fmask_c,3:fmask_e}mask_type[choice2]#更改参数[mask_type]和color_type用于各种组合img2=cv2.polylines(img,[mask_type[choice2]],True,choice1,thickness=2,lineType=cv2.LINE_8)#使用PythonOpenCV-cv2.fillPoly()
方法填充口罩#更改参数[mask_type]和color_type用于各种组合img3=cv2.fillPoly(img2,[mask_type[choice2]],choice1,lineType=cv2.LINE_AA)#cv2.imshow(“imagewithmaskoutline”,img2)cv2.imshow(“imagewithmask”,img3)#为测试保存输出文件outputNameofImage=“output/imagetest.jpg”print(“Savingoutputimageto”,outputNameofImage)cv2.imwrite(outputNameofImage,img3)points=[]foriinrange(1,16):point=[landmarks.part(i).x,landmarks.part(i).y]points.append(point)#print(points)#椭圆参数为高,圆形是覆盖口罩top_ellipse=landmarks.part(27).y+(landmarks.part(28).y-landmarks.part(27).y)/2centre_x=landmarks.part(28).xcentre_y=landmarks.part(8).y-((landmarks.part(8).y-(top_ellipse))/2)#椭圆高度axis_major=(landmarks.part(8).y-top_ellipse)/2#椭圆宽度axis_minor=((landmarks.part(13).x-landmarks.part(3).x)*0.8)/2centre_x=int(round(centre_x))centre_y=int(round(centre_y))axis_major=int(round(axis_major))axis_minor=int(round(axis_minor))centre=(centre_x,centre_y)axes=(axis_major,axis_minor)#使用PythonOpenCV-cv2.ellipse()
方法绘制口罩轮廓#更改 后一个参数-linethickness和color_type为各种组合img_2=cv2.ellipse(img,centre,axes,0,0,360,color_type,thickness=2)#使用PythonOpenCV-cv2.ellipse()方法绘制口罩轮廓#更改 后一个参数-linethickness为负数用于填充,color_type用于各种组合img_3=cv2.ellipse(img,centre,axes,0,0,360,color_type,thickness=-1)#cv2.imshow(“imagewithmaskoutline”,img_2)cv2.imshow(“imagewithmask”,img_3)在图像检测开始之前,用户可以选择预先确定口罩的颜色和类型。我们预先选择了两种颜色的口罩-蓝色和黑色#使用input()函数根据用户需求获取口罩类型和口罩颜色choice1=input(“PleaseselectthechoiceofmaskcolorEnter1forblueEnter2forblack:”)choice1=int(choice1)ifchoice1==1:choice1=color_blueprint(‘Youselectedmaskcolor=blue’)elifchoice1==2:choice1=color_blackprint(‘Youselectedmaskcolor=black’)else:print(“invalidselection,pleaseselectagain.”)input(“PleaseselectthechoiceofmaskcolorEnter1forblueEnter2forblack:”)choice2=input(“PleaseenterchoiceofmasktypecoverageEnter1forhighEnter2formediumEnter3forlow:”)choice2=int(choice2)ifchoice2==1:#choice2=fmask_aprint(f‘Youchosenwide,highcoveragemask’)elifchoice2==2:#choice2=fmask_cprint(f‘Youchosenwide,mediumcoveragemask’)elifchoice2==3:#choice2=fmask_eprint(f‘Youchosenwide,lowcoveragemask’)else:print(“invalidselection,pleaseselectagain.”)input(“PleaseenterchoiceofmasktypecoverageEnter1forhighEnter2formediumEnter3forlow:”)#print(choice2)
结果
图5显示了原始输入图像(BarackObama的图像)与使用脚本生成了口罩的输出图像之间的比较。我们也可以在人群镜头使用这个脚本。如图6所示,在著名的Ellen‘swefie拍摄中,在检测到的人脸上叠加口罩的结果。我们能够成功地复制生成5种不同类型的口罩的过程(详见附录A),这些口罩可以使用dlib和OpenCV叠加在未带口罩的人脸的图像上。图7到图9显示了在不直接看相机的脸上的更多示例。
结论
该脚本能够在检测到的人脸上生成合成口罩脸,输出图像可用于测试或验证其他面向应用的ML网络,如室内考勤系统的人脸识别、口罩检测等。
原文标题:使用Python+OpenCV+dlib为人脸生成口罩
文章出处:【微信公众号:新机器视觉】欢迎添加关注!文章转载请注明出处。
责任编辑:haq