Created
March 15, 2019 14:57
-
-
Save linyongping/cb28839f9a47c684856026cb71bfe5f6 to your computer and use it in GitHub Desktop.
python识别
This file contains hidden or 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
| #coding=utf-8 | |
| #视频人脸检测类 - OpenCV版本 | |
| import cv2 | |
| from PIL import Image | |
| import pytesseract | |
| videoPath = 'ipcam/videos/test.mp4' | |
| # 图片识别方法 | |
| def discern(img): | |
| grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| # OpenCV人脸识别分类器 | |
| classifier = cv2.CascadeClassifier( | |
| "C:\Python36\Lib\site-packages\opencv-master\data\haarcascades\haarcascade_frontalface_default.xml" | |
| ) | |
| color = (0, 255, 0) # 定义绘制颜色 | |
| # 调用识别人脸 | |
| faceRects = classifier.detectMultiScale( | |
| grayImg, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32)) | |
| if len(faceRects): # 大于0则检测到人脸 | |
| for faceRect in faceRects: # 单独框出每一张人脸 | |
| x, y, w, h = faceRect | |
| # 框出人脸 | |
| cv2.rectangle(img, (x, y), (x + h, y + w), color, 2) | |
| cv2.imshow("image", img) # 显示图像 | |
| cap = cv2.VideoCapture(videoPath) | |
| while (1): | |
| ret, frame = cap.read() | |
| text = pytesseract.image_to_string(frame, lang='eng') | |
| print(text) | |
| cv2.imshow('frame', frame) | |
| # discern(frame) | |
| if cv2.waitKey(1) & 0xFF == ord('q'): | |
| break | |
| cap.release() | |
| cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment