Ejemplo n.º 1
0
def get_face(frame):
    # print frame
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

    
    if len(faces) == 0:
        if C.Debug:
            # print frame
            cv2.imshow('face', frame)            
        return None
    else:        
        # Draw a rectangle around the faces
        mx = my = mh = mw = 0
        mh = 1
        mw = 256
        for (x, y, w, h) in faces:            
            if w*h > mh*mw:
                if C.Debug:                
                    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
                mx = x
                my = y
                mh = h
                mw = w
                
        if C.Debug:
            # print frame
            cv2.imshow('face', frame)            
        if mx == my and mx == 0:
            return None
        
        msg = CVMessage()
        shape = frame.shape
        msg.x,msg.y,msg.width,msg.height = util.scaled_rect_coords(mx,my,mw,mh,shape[0],shape[1])
        if C.Debug:            
            # print "face height: ",msg.height
            pass
            
        return msg
Ejemplo n.º 2
0
def get_cone(cv_image):    
    msg = CVMessage()
    msg.x = 0
    msg.y = 0
    msg.height = 0
    
    hsv = cv2.cvtColor(cv_image, cv2.COLOR_BGR2HSV)
    lower_green = np.array([60, 50, 0], dtype=np.uint8)
    upper_green = np.array([100, 200, 200], dtype=np.uint8)
    mask = cv2.inRange(hsv, lower_green, upper_green).astype(np.uint8)
    kernel = np.ones((11,11), np.uint8)
    denoised = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    denoised = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    

    allContours, hierarchy = cv2.findContours(denoised, cv2.cv.CV_RETR_EXTERNAL, cv2.cv.CV_CHAIN_APPROX_SIMPLE)

    if len(allContours) == 0:
        if C.Debug:            
            cv2.imshow('cone',cv_image)
        return None
    else:        
        largestContour = allContours[0]
        largestArea = cv2.contourArea(largestContour)

        for i in range (1, len(allContours)):
            area = cv2.contourArea(allContours[i])
            if area > largestArea:
                largestContour = allContours[i]
                largestArea = area

        x, y, w, h = cv2.boundingRect(largestContour)
        
        if C.Debug:            
            cv2.rectangle(cv_image, (x, y), (x+w, y+h), (0, 255, 0), 4)
            cv2.imshow('cone',cv_image)
        if (w*h > 512):
            nRows_nCols = cv_image.shape
            msg.x, msg.y,msg.width, msg.height = util.scaled_rect_coords(x, y, w, h, nRows_nCols[0], nRows_nCols[1])        
        else:
            return None
    
        return msg;
Ejemplo n.º 3
0
global cone,face,speech
cone = face = speech = None

bridge = CvBridge()


def sendCone(m):
    global cone
    
    cone.publish(m)
def sendFace(m):
    global face
    face.publish(m)


not_found_msg = CVMessage()
not_found_msg.height = C.ITEM_NOT_FOUND



# import cv2
# cv2.startWindowThread()
# cv2.namedWindow("preview")
def camera_callback(data):
    
    cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
    # cv2.imshow('preview',cv_image)
    
    cone_msg = cv_cone.get_cone(cv_image.copy())
    face_msg = cv_face.get_face(cv_image.copy())
    if cone_msg: