forked from vipstone/faceai
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
46 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import cv2 | ||
|
||
print(cv2.__version__) | ||
|
||
# 输出:3.4.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() # 释放窗口资源 |