Ask Your Question
1

Crop ROI from Face-Eye detection

asked 2016-10-23 03:45:51 -0600

Minu gravatar image

updated 2016-10-23 03:57:16 -0600

berak gravatar image

Hey there guys!

I'm a newbie in python thing. I'm trying to crop the ROI from video stream. But i got no result at the moment. The code i'm using is:

import numpy as np
import cv2


face_cascade = cv2.CascadeClassifier('C:\opencv\sources\data\haarcascades\haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('C:\opencv\sources\data\haarcascades\haarcascade_eye.xml')

cap = cv2.VideoCapture(0)

while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[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('img',img)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()

crop_img = roi_color[?]
cv2.imwrite("path", roi_color)

Any advice is welcome! Have a nice day

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-10-23 04:33:37 -0600

berak gravatar image

if you look at the code, they're already cropping a roi for the faces:

 roi_color = img[y:y+h, x:x+w]

now you'll do the same, just for the eyes:

# global var:
eye_counter = 0

        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            # here's your eye-roi, see, it's the very same pattern
            roi_color_eye = roi_color[ey:ey+eh, ex:ex+ew]
            # write image *before* drawing stuff on it
            cv2.imwrite("eye_%d.png" % eye_counter, roi_color_eye)
            eye_counter += 1
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
edit flag offensive delete link more

Comments

1

I really appreciate your help and answers! I'm a newbie in programming at all, not only python. I have to do some work for my degree with it :). Before your answer i was using: For face crop:

cropped = roi_color[y:y+h, x:x+w]
cv2.imshow ("cropped", cropped)
cv2.waitKey(0)

Idem for eyes, but i was able to get just one of them. Your idea of code definetly fits better since i have to take every frame and apply several imaging processes to identify the iris and its center.

Thanks again for your time!

Minu gravatar imageMinu ( 2016-10-23 13:20:10 -0600 )edit

How Can we crop same as Human Body by using the same code but i use haarcascade_fullbody.xml instead of eyes??? Is it same procedure of crop??

salmaUCP gravatar imagesalmaUCP ( 2020-03-25 09:23:08 -0600 )edit

@salmaUCP -- same procedure trying to do what ?

berak gravatar imageberak ( 2020-03-25 09:45:33 -0600 )edit

I just want to crop human detected frame from live video I have already done human detection but don't know how to crop detected frames of video and save it in video format...

salmaUCP gravatar imagesalmaUCP ( 2020-03-25 13:49:58 -0600 )edit

can you please suggest me something???

salmaUCP gravatar imagesalmaUCP ( 2020-03-25 13:51:09 -0600 )edit

please ask a new, seperate question

berak gravatar imageberak ( 2020-03-25 13:52:52 -0600 )edit

okay thanks!

salmaUCP gravatar imagesalmaUCP ( 2020-03-25 16:02:50 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-23 03:45:51 -0600

Seen: 3,295 times

Last updated: Oct 23 '16