人脸描点检测
This commit is contained in:
parent
c2b93af1c0
commit
a5981b19e7
11 changed files with 1644 additions and 0 deletions
|
@ -12,6 +12,7 @@ encoding//QGraphicsView\u7EC3\u4E60/\u6DFB\u52A0QWidget.py=utf-8
|
|||
encoding//tmp/\u622A\u56FE\u753B\u77E9\u5F62/DrawRectangle.py=utf-8
|
||||
encoding//tmp/\u7B80\u5355\u63D2\u4EF6/Widget.py=utf-8
|
||||
encoding//\u4E0B\u62C9\u9009\u62E9\u8054\u52A8/ComboBox.py=utf-8
|
||||
encoding//\u4EBA\u8138\u63CF\u70B9\u68C0\u6D4B/OpencvWidget.py=utf-8
|
||||
encoding//\u5168\u5C40\u70ED\u952E/HotKey.py=utf-8
|
||||
encoding//\u56FE\u7247\u52A0\u8F7D/LoadImage.py=utf-8
|
||||
encoding//\u56FE\u7247\u52A0\u8F7D/res_rc.py=utf-8
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
- [1.15 气泡提示](气泡提示/)
|
||||
- [1.16 自定义import](自定义import/)
|
||||
- [1.17 下拉选择联动](下拉选择联动/)
|
||||
- [1.18 人脸描点检测](人脸描点检测/)
|
||||
|
||||
### [2.QGraphicsView练习](QGraphicsView练习/)
|
||||
- [2.1 世界地图](QGraphicsView练习/世界地图)
|
||||
|
|
113
人脸描点检测/OpencvWidget.py
Normal file
113
人脸描点检测/OpencvWidget.py
Normal file
|
@ -0,0 +1,113 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Created on 2018年1月29日
|
||||
@author: Irony."[讽刺]
|
||||
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
|
||||
@email: 892768447@qq.com
|
||||
@file: OpencvWidget
|
||||
@description:
|
||||
'''
|
||||
import sys
|
||||
|
||||
from PyQt5.QtCore import QTimer
|
||||
from PyQt5.QtGui import QImage, QPixmap
|
||||
from PyQt5.QtWidgets import QLabel, QMessageBox, QApplication
|
||||
import cv2 # @UnresolvedImport
|
||||
import dlib
|
||||
import numpy
|
||||
|
||||
|
||||
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
|
||||
__Copyright__ = "Copyright (c) 2018 Irony.\"[讽刺]"
|
||||
__Version__ = "Version 1.0"
|
||||
|
||||
DOWNSCALE = 4
|
||||
|
||||
|
||||
class OpencvWidget(QLabel):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(OpencvWidget, self).__init__(*args, **kwargs)
|
||||
self.fps = 24
|
||||
self.resize(800, 600)
|
||||
self.setText("请稍候,正在初始化数据和摄像头。。。")
|
||||
|
||||
def start(self):
|
||||
try:
|
||||
# 检测相关
|
||||
self.detector = dlib.get_frontal_face_detector()
|
||||
self.predictor = dlib.shape_predictor(
|
||||
"data/shape_predictor_68_face_landmarks.dat")
|
||||
cascade_fn = "data/lbpcascades/lbpcascade_frontalface.xml"
|
||||
self.cascade = cv2.CascadeClassifier(cascade_fn)
|
||||
if not self.cascade:
|
||||
return QMessageBox.critical(self, "错误", cascade_fn + " 无法找到")
|
||||
self.cap = cv2.VideoCapture(0)
|
||||
if not self.cap or not self.cap.isOpened():
|
||||
return QMessageBox.critical(self, "错误", "打开摄像头失败")
|
||||
# 开启定时器定时捕获
|
||||
self.timer = QTimer(self, timeout=self.onCapture)
|
||||
self.timer.start(1000 / self.fps)
|
||||
except Exception as e:
|
||||
QMessageBox.critical(self, "错误", str(e))
|
||||
|
||||
def closeEvent(self, event):
|
||||
if hasattr(self, "timer"):
|
||||
self.timer.stop()
|
||||
self.timer.deleteLater()
|
||||
self.cap.release()
|
||||
del self.predictor, self.detector, self.cascade, self.cap
|
||||
super(OpencvWidget, self).closeEvent(event)
|
||||
self.deleteLater()
|
||||
|
||||
def onCapture(self):
|
||||
_, frame = self.cap.read()
|
||||
|
||||
minisize = (
|
||||
int(frame.shape[1] / DOWNSCALE), int(frame.shape[0] / DOWNSCALE))
|
||||
tmpframe = cv2.resize(frame, minisize)
|
||||
tmpframe = cv2.cvtColor(tmpframe, cv2.COLOR_BGR2GRAY) # 做灰度处理
|
||||
tmpframe = cv2.equalizeHist(tmpframe)
|
||||
|
||||
# minNeighbors表示每一个目标至少要被检测到5次
|
||||
faces = self.cascade.detectMultiScale(tmpframe, minNeighbors=5)
|
||||
del tmpframe
|
||||
if len(faces) < 1: # 没有检测到脸
|
||||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
img = QImage(
|
||||
frame.data, frame.shape[1], frame.shape[0], frame.shape[1] * 3, QImage.Format_RGB888)
|
||||
del frame
|
||||
return self.setPixmap(QPixmap.fromImage(img))
|
||||
# 特征点检测描绘
|
||||
for x, y, w, h in faces:
|
||||
x, y, w, h = x * DOWNSCALE, y * DOWNSCALE, w * DOWNSCALE, h * DOWNSCALE
|
||||
# 画脸矩形
|
||||
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0))
|
||||
# 截取的人脸部分
|
||||
tmpframe = frame[y:y + h, x:x + w]
|
||||
# 进行特征点描绘
|
||||
rects = self.detector(tmpframe, 1)
|
||||
if len(rects) > 0:
|
||||
landmarks = numpy.matrix(
|
||||
[[p.x, p.y] for p in self.predictor(tmpframe, rects[0]).parts()])
|
||||
for _, point in enumerate(landmarks):
|
||||
pos = (point[0, 0] + x, point[0, 1] + y)
|
||||
# 在原来画面上画点
|
||||
cv2.circle(frame, pos, 3, color=(0, 255, 0))
|
||||
# 转成Qt能显示的
|
||||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
img = QImage(
|
||||
frame.data, frame.shape[1], frame.shape[0], frame.shape[1] * 3, QImage.Format_RGB888)
|
||||
del frame
|
||||
self.setPixmap(QPixmap.fromImage(img))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
w = OpencvWidget()
|
||||
w.show()
|
||||
# 5秒后启动
|
||||
QTimer.singleShot(5000, w.start)
|
||||
sys.exit(app.exec_())
|
16
人脸描点检测/README.md
Normal file
16
人脸描点检测/README.md
Normal file
|
@ -0,0 +1,16 @@
|
|||
# Opencv 人脸描点检测
|
||||
|
||||
简单联系PyQt 结合 Opencv 进行人脸检测
|
||||
|
||||
### 依赖文件
|
||||
- [opencv](https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv)
|
||||
- [numpy](https://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy)
|
||||
- [dlib](http://dlib.net/)
|
||||
- [dlib-19.4.0.win32-py2.7.exe](dist/dlib-19.4.0.win32-py2.7.exe)
|
||||
- [dlib-19.4.0.win32-py3.4.exe](dist/dlib-19.4.0.win32-py3.4.exe)
|
||||
- [dlib-19.4.0.win32-py3.5.exe](dist/dlib-19.4.0.win32-py3.5.exe)
|
||||
- [shape-predictor-68-face-landmarks.dat.bz2](http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2)
|
||||
|
||||
截图
|
||||
|
||||
![1](ScreenShot/1.png)
|
BIN
人脸描点检测/ScreenShot/1.png
Normal file
BIN
人脸描点检测/ScreenShot/1.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 306 KiB |
1
人脸描点检测/data/.gitignore
vendored
Normal file
1
人脸描点检测/data/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/shape_predictor_68_face_landmarks.dat
|
1505
人脸描点检测/data/lbpcascades/lbpcascade_frontalface.xml
Normal file
1505
人脸描点检测/data/lbpcascades/lbpcascade_frontalface.xml
Normal file
File diff suppressed because it is too large
Load diff
7
人脸描点检测/data/readme.txt
Normal file
7
人脸描点检测/data/readme.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
This folder contains various data that is used by cv libraries and/or demo applications.
|
||||
----------------------------------------------------------------------------------------
|
||||
|
||||
haarcascades - the folder contains trained classifiers for detecting objects
|
||||
of a particular type, e.g. faces (frontal, profile), pedestrians etc.
|
||||
Some of the classifiers have a special license - please,
|
||||
look into the files for details.
|
BIN
人脸描点检测/dist/dlib-19.4.0.win32-py2.7.exe
vendored
Normal file
BIN
人脸描点检测/dist/dlib-19.4.0.win32-py2.7.exe
vendored
Normal file
Binary file not shown.
BIN
人脸描点检测/dist/dlib-19.4.0.win32-py3.4.exe
vendored
Normal file
BIN
人脸描点检测/dist/dlib-19.4.0.win32-py3.4.exe
vendored
Normal file
Binary file not shown.
BIN
人脸描点检测/dist/dlib-19.4.0.win32-py3.5.exe
vendored
Normal file
BIN
人脸描点检测/dist/dlib-19.4.0.win32-py3.5.exe
vendored
Normal file
Binary file not shown.
Loading…
Reference in a new issue