iT邦幫忙

1

OPENCV人臉辨識 1

  • 分享至 

  • xImage

import cv2
ESC = 27

畫面數量計數

n = 1

存檔檔名用

index = 0

人臉取樣總數

total = 100

def saveImage(face_image, index):
filename = 'images/h0/{:03d}.pgm'.format(index)
cv2.imwrite(filename, face_image)
print(filename)

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cv2.namedWindow('video', cv2.WINDOW_NORMAL)

while n > 0:
ret, frame = cap.read()
# frame = cv2.resize(frame, (600, 336))
frame = cv2.flip(frame, 1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

在while內

faces = face_cascade.detectMultiScale(gray, 1.1, 3)
for (x, y, w, h) in faces:
    frame = cv2.rectangle(
        frame, 
        (x, y), (x + w, y + h), 
        (0, 255, 0), 3
    )
    if n % 5 == 0:
        face_img = gray[y: y + h, x: x + w]
        face_img = cv2.resize(face_img, (400, 400))
        saveImage(face_img, index)
        index += 1
        if index >= total:
            print('get training data done')
            n = -1
            break
    n += 1

在while內

cv2.imshow('video', frame)
if cv2.waitKey(1) == 27:
    cv2.destroyAllWindows()
    break
    
    
    
    
    在做此專案時
    如跑出
    [ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback

Traceback (most recent call last):
File "C:/Users/ADMIN/AppData/Local/Temp/Rar$DIa6752.48196/1capture.py", line 26, in
faces = face_cascade.detectMultiScale(gray, 1.1, 3)
cv2.error: OpenCV(4.1.2) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'
該如何解決

https://ithelp.ithome.com.tw/upload/images/20220713/201486936Rg2q9UanD.png

hokou iT邦好手 1 級 ‧ 2022-07-13 16:13:11 檢舉
你的這幾個問題都是出在檔案路徑讀取,這要依你的檔案去設定
u09127104 iT邦新手 5 級 ‧ 2022-07-13 16:24:48 檢舉
可以加你LINE方便詢問你問題嗎 謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答