如何使用Opencv Python将多个面孔保存为单独的图像

时间:2018-10-17 14:18:49

标签: python opencv

我想从实时屏幕中检测面部,并在按下特定键时将每个检测到的面部保存为单独的图像。 问题是当我按键保存的图像时,但每个保存的图像都相同。

如何保存屏幕上没有重复图像的所有面孔?

import numpy as np
from PIL import ImageGrab 
import cv2
import imutils



face_cascade = cv2.CascadeClassifier('D:/Programs/openCV/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')


count = 0
while True:

img = ImageGrab.grab()
img_np = np.array(img)
frame = cv2.cvtColor(img_np,cv2.COLOR_BGR2RGB)

faces = face_cascade.detectMultiScale(frame,scaleFactor=1.2,minNeighbors=5,minSize=(40,40))
for (i,(x,y,w,h)) in enumerate (faces):
    cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
    cv2.putText(frame, "member #{}".format(i + 1), (x, y - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)
    roi_gray = frame[y:y+h, x:x+w]
    roi_color = frame[y:y+h, x:x+w]

cv2.imshow("screen", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):

    for (i,(x,y,w,h)) in enumerate (faces):
        cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)



        cv2.imwrite('E:\Work Folder\ML\Export\opencv'+str(count)+'.jpg',roi_color)  
        count = count +1

0 个答案:

没有答案