当前位置:网站首页>Opencv for face recognition
Opencv for face recognition
2022-06-11 07:46:00 【Keep_ Trying_ Go】
Corresponding libraries need to be downloaded :
pip install cmake
pip install dlib
import os
import cv2
import dlib
import numpy as np
from keras.models import load_model
detector=dlib.get_frontal_face_detector()
# The second parameter represents the resolution of the captured image
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
margin=0.2
# Set the font displayed
font=cv2.FONT_HERSHEY_SIMPLEX
model=load_model('./models/recognition.h5')
labels={
0:'cabbage',1:'car',2:'dog',3:'mobilePhone',4:'person'}
while True:
OK,frame=cap.read()
if OK==False:
print(' Please face the camera !')
break
img_h,img_w,_=np.shape(frame)
detected=detector(frame)
faces=[]
preprocess_images=[]
if len(detected)>0:
for i,locate in enumerate(detected):
x1,y1,x2,y2,w,h=locate.left(),locate.top(),locate.right()+1,locate.bottom()+1,locate.width(),locate.height()
xw1=max(int(x1-margin*w),0)
yw1=max(int(y1-margin*h),0)
xw2=min(int(x2+margin*w),img_w-1)
yw2=min(int(y2+margin*h),img_h-1)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
face=frame[yw1:yw2+1,xw1:xw2+1,:]
face=cv2.resize(face,(128,128))
face=face.astype('float')/255.0
face=np.expand_dims(face,axis=0)
preprocess_images.append(face)
for i,d in enumerate(detected):
preds=model.predict(preprocess_images[i])[0]
face_labels=labels[preds.argmax()]
cv2.putText(frame,face_labels,(d.left(),d.top()-10),font,1.2,(255,0,0),3)
cv2.imshow('face',frame)
if cv2.waitKey(1)&0xFF==27:
break
cap.release()
cv2.destroyAllWindows()
边栏推荐
- Tidb Cloud est en ligne sur le marché Google Cloud pour permettre aux développeurs du monde entier d'utiliser une nouvelle pile de bases de données htap en temps réel
- QT table display data
- [atcoder1984] wide swap
- 运筹学导论
- 【AtCoder2387】+/- Rectangle
- Configuration software -- control import
- 批量拼接字符串
- 【AtCoder2306】Rearranging(拓扑)
- mpi
- String Simulation Implementation
猜你喜欢

QT picture adaptive display control

Zero foundation self-study SQL course | union joint query

C# 微信上传Form-data

Tidb Cloud est en ligne sur le marché Google Cloud pour permettre aux développeurs du monde entier d'utiliser une nouvelle pile de bases de données htap en temps réel

Import on CSDN MD file

2022.6.7 特长生模拟

C wechat upload form data

How to prepare for the new PMP syllabus exam?

Storage of floating point in memory

C language - Growth Diary -02- function
随机推荐
Implementation of stack (C language)
【集群】haproxy负载均衡
【AtCoder2000】Leftmost Ball (DP+组合数)
Remote office experience sharing | community essay solicitation
零基础自学SQL课程 | UNION 联合查询
Implementation of queue (C language)
【IoT】智能硬件:如何获取硬件产品的wifi信号强度
Qunhui ds918 creates m.2 SSD read / write cache
You got 8K in the 3-year function test, but you were actually pretending to work hard
C language function stack frame
How to output the percent sign "%" in printf function in C language
Arduino_ STM development record
[atcoder1981] short diameter (graph theory thinking)
Simple configuration of vscade
欧拉定理及扩展(附证明)
放大镜子效果图
Pat class A by category
Sort - merge sort
零基础自学SQL课程 | OUTER JOIN外连接
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]