电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Python基于Dlib的人臉識別系統的實現

瀏覽:2日期:2022-08-06 08:52:16

之前已經介紹過人臉識別的基礎概念,以及基于opencv的實現方式,今天,我們使用dlib來提取128維的人臉嵌入,并使用k臨近值方法來實現人臉識別。

人臉識別系統的實現流程與之前是一樣的,只是這里我們借助了dlib和face_recognition這兩個庫來實現。face_recognition是對dlib庫的包裝,使對dlib的使用更方便。所以首先要安裝這2個庫。

pip3 install dlibpip3 install face_recognition

然后,還要安裝imutils庫

pip3 install imutils

我們看一下項目的目錄結構:

.├── dataset│ ├── alan_grant [22 entries exceeds filelimit, not opening dir]│ ├── claire_dearing [53 entries exceeds filelimit, not opening dir]│ ├── ellie_sattler [31 entries exceeds filelimit, not opening dir]│ ├── ian_malcolm [41 entries exceeds filelimit, not opening dir]│ ├── john_hammond [36 entries exceeds filelimit, not opening dir]│ └── owen_grady [35 entries exceeds filelimit, not opening dir]├── examples│ ├── example_01.png│ ├── example_02.png│ └── example_03.png├── output│ ├── lunch_scene_output.avi│ └── webcam_face_recognition_output.avi├── videos│ └── lunch_scene.mp4├── encode_faces.py├── encodings.pickle├── recognize_faces_image.py├── recognize_faces_video_file.py├── recognize_faces_video.py└── search_bing_api.py 10 directories, 12 files

首先,提取128維的人臉嵌入:

命令如下:

python3 encode_faces.py --dataset dataset --encodings encodings.pickle -d hog

記?。喝绻愕碾娔X內存不夠大,請使用hog模型進行人臉檢測,如果內存夠大,可以使用cnn神經網絡進行人臉檢測。

看代碼:

# USAGE# python encode_faces.py --dataset dataset --encodings encodings.pickle # import the necessary packagesfrom imutils import pathsimport face_recognitionimport argparseimport pickleimport cv2import os # construct the argument parser and parse the argumentsap = argparse.ArgumentParser()ap.add_argument('-i', '--dataset', required=True,help='path to input directory of faces + images')ap.add_argument('-e', '--encodings', required=True,help='path to serialized db of facial encodings')ap.add_argument('-d', '--detection-method', type=str, default='hog',help='face detection model to use: either `hog` or `cnn`')args = vars(ap.parse_args()) # grab the paths to the input images in our datasetprint('[INFO] quantifying faces...')imagePaths = list(paths.list_images(args['dataset'])) # initialize the list of known encodings and known namesknownEncodings = []knownNames = [] # loop over the image pathsfor (i, imagePath) in enumerate(imagePaths):# extract the person name from the image pathprint('[INFO] processing image {}/{}'.format(i + 1,len(imagePaths)))name = imagePath.split(os.path.sep)[-2] # load the input image and convert it from RGB (OpenCV ordering)# to dlib ordering (RGB)image = cv2.imread(imagePath)rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # detect the (x, y)-coordinates of the bounding boxes# corresponding to each face in the input imageboxes = face_recognition.face_locations(rgb,model=args['detection_method']) # compute the facial embedding for the faceencodings = face_recognition.face_encodings(rgb, boxes) # loop over the encodingsfor encoding in encodings:# add each encoding + name to our set of known names and# encodingsknownEncodings.append(encoding)knownNames.append(name) # dump the facial encodings + names to diskprint('[INFO] serializing encodings...')data = {'encodings': knownEncodings, 'names': knownNames}f = open(args['encodings'], 'wb')f.write(pickle.dumps(data))f.close()

輸出結果是每張圖片輸出一個人臉的128維的向量和對于的名字,并序列化到硬盤,供后續人臉識別使用。

識別圖像中的人臉:

這里使用KNN方法實現最終的人臉識別,而不是使用SVM進行訓練。

命令如下:

python3 recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png

看代碼:

# USAGE# python recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png # import the necessary packagesimport face_recognitionimport argparseimport pickleimport cv2 # construct the argument parser and parse the argumentsap = argparse.ArgumentParser()ap.add_argument('-e', '--encodings', required=True,help='path to serialized db of facial encodings')ap.add_argument('-i', '--image', required=True,help='path to input image')ap.add_argument('-d', '--detection-method', type=str, default='cnn',help='face detection model to use: either `hog` or `cnn`')args = vars(ap.parse_args()) # load the known faces and embeddingsprint('[INFO] loading encodings...')data = pickle.loads(open(args['encodings'], 'rb').read()) # load the input image and convert it from BGR to RGBimage = cv2.imread(args['image'])rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # detect the (x, y)-coordinates of the bounding boxes corresponding# to each face in the input image, then compute the facial embeddings# for each faceprint('[INFO] recognizing faces...')boxes = face_recognition.face_locations(rgb,model=args['detection_method'])encodings = face_recognition.face_encodings(rgb, boxes) # initialize the list of names for each face detectednames = [] # loop over the facial embeddingsfor encoding in encodings:# attempt to match each face in the input image to our known# encodingsmatches = face_recognition.compare_faces(data['encodings'],encoding)name = 'Unknown' # check to see if we have found a matchif True in matches:# find the indexes of all matched faces then initialize a# dictionary to count the total number of times each face# was matchedmatchedIdxs = [i for (i, b) in enumerate(matches) if b]counts = {} # loop over the matched indexes and maintain a count for# each recognized face facefor i in matchedIdxs:name = data['names'][i]counts[name] = counts.get(name, 0) + 1 # determine the recognized face with the largest number of# votes (note: in the event of an unlikely tie Python will# select first entry in the dictionary)name = max(counts, key=counts.get)# update the list of namesnames.append(name) # loop over the recognized facesfor ((top, right, bottom, left), name) in zip(boxes, names):# draw the predicted face name on the imagecv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)y = top - 15 if top - 15 > 15 else top + 15cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2) # show the output imagecv2.imshow('Image', image)cv2.waitKey(0)

實際效果如下:

Python基于Dlib的人臉識別系統的實現

如果要詳細了解細節,請參考:https://www.pyimagesearch.com/2018/06/18/face-recognition-with-opencv-python-and-deep-learning/

到此這篇關于Python基于Dlib的人臉識別系統的實現的文章就介紹到這了,更多相關Python Dlib人臉識別內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚克力制品定制,上海嘉定有机玻璃加工制作生产厂家—官网 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 过滤器_自清洗过滤器_气体过滤器_苏州华凯过滤技术有限公司 | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 玻璃钢板-玻璃钢防腐瓦-玻璃钢材料-广东壹诺 | 置顶式搅拌器-优莱博化学防爆冰箱-磁驱搅拌器-天津市布鲁克科技有限公司 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | 楼承板-开口楼承板-闭口楼承板-无锡海逵 | 英国雷迪地下管线探测仪-雷迪RD8100管线仪-多功能数字听漏仪-北京迪瑞进创科技有限公司 | 皮带输送机-大倾角皮带输送机-皮带输送机厂家-河南坤威机械 | 蓝莓施肥机,智能施肥机,自动施肥机,水肥一体化项目,水肥一体机厂家,小型施肥机,圣大节水,滴灌施工方案,山东圣大节水科技有限公司官网17864474793 | 纳米二氧化硅,白炭黑,阴离子乳化剂-臻丽拾科技 | 专注氟塑料泵_衬氟泵_磁力泵_卧龙泵阀_化工泵专业品牌 - 梭川泵阀 | 山东商品混凝土搅拌楼-环保型搅拌站-拌合站-分体仓-搅拌机厂家-天宇 | 招商帮-一站式网络营销服务|搜索营销推广|信息流推广|短视视频营销推广|互联网整合营销|网络推广代运营|招商帮企业招商好帮手 | 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | PSI渗透压仪,TPS酸度计,美国CHAI PCR仪,渗透压仪厂家_价格,微生物快速检测仪-华泰和合(北京)商贸有限公司 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 中控室大屏幕-上海亿基自动化控制系统工程有限公司 | 泰兴市热钻机械有限公司-热熔钻孔机-数控热熔钻-热熔钻孔攻牙一体机 | 耐酸泵,耐酸泵厂家-淄博华舜耐腐蚀真空泵 | 兰州UPS电源,兰州山特UPS-兰州万胜商贸 | 浙江上沪阀门有限公司 | 密封无忧网 _ 专业的密封产品行业信息网 | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | ★店家乐|服装销售管理软件|服装店收银系统|内衣店鞋店进销存软件|连锁店管理软件|收银软件手机版|会员管理系统-手机版,云版,App | 专业的新乡振动筛厂家-振动筛品质保障-环保振动筛价格—新乡市德科筛分机械有限公司 | 标策网-专注公司商业知识服务、助力企业发展 | 不锈钢反应釜,不锈钢反应釜厂家-价格-威海鑫泰化工机械有限公司 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 蓝米云-专注于高性价比香港/美国VPS云服务器及海外公益型免费虚拟主机 | 电梯装饰-北京万达中意电梯装饰有限公司 | 不锈钢管件(不锈钢弯头,不锈钢三通,不锈钢大小头),不锈钢法兰「厂家」-浙江志通管阀 | 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 净化工程_无尘车间_无尘车间装修-广州科凌净化工程有限公司 | 超声骨密度仪-骨密度检测仪-经颅多普勒-tcd仪_南京科进实业有限公司 | 磷酸肌酸二钠盐,肌酐磷酰氯-沾化欣瑞康生物科技 | 化妆品加工厂-化妆品加工-化妆品代加工-面膜加工-广东欧泉生化科技有限公司 | 散热器-电子散热器-型材散热器-电源散热片-镇江新区宏图电子散热片厂家 |