【发布时间】:2017-08-10 17:21:36
【问题描述】:
我有两个代码:第一个从实时提要中检测人脸,第二个从网络摄像头捕获图像。
我想将这些代码附加到一个 Python 文件中,以便首先检测到人脸,然后只为人脸捕获图像。
以下是代码:
抓拍人脸:
import cv2
video_capture = cv2.VideoCapture(0)
# Capture frame
ret, frame = video_capture.read()
# Write frame in file
cv2.imwrite('image.jpg', frame)
# When everything is done, release the capture
video_capture.release()
检测人脸:
import cv2
import sys
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=2.0,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
我希望这两个脚本在一个脚本中协同工作。
问候
【问题讨论】:
-
抱歉,Stack Overflow 不是代码编写服务。你的组合代码是什么,你到底遇到了什么问题?
标签: python python-2.7 opencv image-processing