Python:编写一个函数来捕获OpenCV框架并返回结果

时间:2016-03-07 18:12:50

标签: python opencv

嗨,有人可以帮我写一个openCV函数并返回面矩形的坐标。

我当前的代码看起来像这样,但程序挂起。

def WatchMe():
    while(video.isOpened()):
        ret, frame = video.read()
        if frame is not None:
            gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            faces = face_cascade.detectMultiScale(gray, 1.3, 5)
            for (x,y,w,h) in faces:
                cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
                t =(x+w)/2
                v =(y+h)/2
                #cv2.circle(frame, (t,v), 1, (0,0,255), thickness=1, lineType=8, shift=0)
                #watchme(t,v)
                roi_gray = gray[y:y+h, x:x+w]
                roi_color = frame[y:y+h, x:x+w]
                eyes = eye_cascade.detectMultiScale(roi_gray)
                for (ex,ey,ew,eh) in eyes:
                    cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
            cv2.imshow('Video', frame)
            time.sleep(3) # delays for 5 seconds
        #return t,v

     # part of original face code      
        #if cv2.waitKey(1) & 0xFF == ord('q'):
            #break

1 个答案:

答案 0 :(得分:0)

功能定义 -

def WatchMe2(frame):
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                faces = face_cascade.detectMultiScale(gray, 1.3, 5)
                for (x,y,w,h) in faces:
                    cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)
                    t =(x+w)/2
                    v =(y+h)/2                                      
                    roi_gray = gray[y:y+h, x:x+w]
                    roi_color = frame[y:y+h, x:x+w]
                    eyes = eye_cascade.detectMultiScale(roi_gray)
                    for (ex,ey,ew,eh) in eyes:
                        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

函数调用 -

while(video.isOpened()):
        ret, frame = video.read()
        if frame is not None:
           WatchMe2(frame)
           cv2.imshow('Video', frame)