Skip to content

Commit

Permalink
添加视频实现代码
Browse files Browse the repository at this point in the history
  • Loading branch information
vipstone committed Apr 20, 2018
1 parent 4ec3486 commit ce0e94d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
18 changes: 15 additions & 3 deletions doc/jiance.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 人脸检测
# 图片人脸检测

人脸检测使用到的技术是OpenCV,上一节已经介绍了OpenCV的环境安装,[点击查看](https://github.com/vipstone/faceai/blob/master/doc/huanjingdajian.md).

Expand All @@ -11,7 +11,7 @@

## 技术实现思路 ##

图片转换成灰色(去除色彩干扰,让图片识别更准确
图片转换成灰色(降低为一维的灰度,减低计算强度

图片上画矩形

Expand Down Expand Up @@ -96,4 +96,16 @@ c = cv2.waitKey(10)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
```
分类器classifier.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))**参数说明**

gray:转换的灰图

scaleFactor:图像缩放比例,可理解为相机的X倍镜

minNeighbors:对特征检测点周边多少有效点同时检测,这样可避免因选取的特征检测点太小而导致遗漏

minSize:特征检测点的最小尺寸



1 change: 1 addition & 0 deletions faceai/jiance.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
# OpenCV版本的图片检测
import cv2

filepath = "img/xingye-1.jpg"
Expand Down
1 change: 0 additions & 1 deletion faceai/versionDemo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import cv2

print(cv2.__version__)

# 输出:3.4.1
30 changes: 30 additions & 0 deletions faceai/videojiance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding:utf-8 -*-
# OpenCV版本的视频检测
import cv2


# 图片识别方法封装
def discern(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cap = cv2.CascadeClassifier(
"C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml"
)
faceRects = cap.detectMultiScale(
gray, scaleFactor=1.2, minNeighbors=3, minSize=(50, 50))
if len(faceRects):
for faceRect in faceRects:
x, y, w, h = faceRect
cv2.rectangle(img, (x, y), (x + h, y + w), (0, 255, 0), 2) # 框出人脸
cv2.imshow("Image", img)


# 获取摄像头0表示第一个摄像头
cap = cv2.VideoCapture(0)
while (1):
ret, img = cap.read()
# cv2.imshow("Image", img)
discern(img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 释放窗口资源

0 comments on commit ce0e94d

Please sign in to comment.