Home>

Call and execute function smile_recognition
Save smile_img.jpg to the specified path and
Since we want to pass the size of the image (smile_height, smile_width) as a return value,
Please give me advice on how to deliver.
I don't think it's possible to simply hand over some sort of behavior within the function.

Error message

In the main program that calls the function smile_recognition
For example, the values ​​of smile_height and smile_width are not output as print (smile_recognition ()).

Applicable source code
Python3, OpencCV3
#-*-coding: utf-8-*-
import cv2
def smile_recognition ():
    # Constant definition
    ESC_KEY = 27 # Esc key
    INTERVAL = 33 # Wait time
    FRAME_RATE = 30 # fps
    smile_flag = 0 #Smile detection
    smile_img_path = "../ images /"
    #ORG_WINDOW_NAME = "org"
    GAUSSIAN_WINDOW_NAME = "gaussian"
    DEVICE_ID = 0
    # Specify classifier
    cascade_face_file = "../etc/haarcascade_frontalface_alt2.xml"
    cascade_smile_file = "../etc/haarcascade_smile.xml"
    cascade_face = cv2.CascadeClassifier (cascade_face_file)
    cascade_smile = cv2.CascadeClassifier (cascade_smile_file)
    # Get camera video
    cap = cv2.VideoCapture (DEVICE_ID)
    # Read initial frame
    end_flag, c_frame = cap.read ()
    height, width, channels = c_frame.shape
    # Prepare window
    # cv2.namedWindow (ORG_WINDOW_NAME)
    cv2.namedWindow (GAUSSIAN_WINDOW_NAME)
    # Conversion processing loop
    while end_flag == True:
        # Image acquisition and face detection
        img = c_frame
        img_gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
        face_list = cascade_face.detectMultiScale (img_gray, minSize = (100, 100))
        # Mark the detected face
        for (x, y, w, h) in face_list:
            color = (0, 0, 225)
            pen_w = 3
            cv2.rectangle (img_gray, (x, y), (x + w, y + h), color, thickness = pen_w)
            #Mark smiles in the face
            roi_img_gray = img_gray [y: y + h, x: x + w]
            smile_list = cascade_smile .detectMultiScale (roi_img_gray, scaleFactor = 1.1, minNeighbors = 7, minSize = (25, 25))
            if len (smile_list)>0:
                print ("smile detected!")
                smile_flag = 1
                smile_height, smile_width = roi_img_gray.shape [: 2]
                cv2.waitKey (0)
                break
            #for (sx, sy, sw, sh) in smile_list:
                #color = (0, 0, 225)
                #pen_w = 3
                # cv2.rectangle (roi_img_gray, (sx, sy), (sx + sw, sy + sh), color, thickness = pen_w)
        # Frame display
        # cv2.imshow (ORG_WINDOW_NAME, c_frame)
        cv2.imshow (GAUSSIAN_WINDOW_NAME, img_gray)
        # End with Esc key
        key = cv2.waitKey (INTERVAL)
        if key == ESC_KEY:
            break
        if smile_flag == 1:
            cv2.imwrite (smile_img_path + 'smile_img.jpg', roi_img_gray)
            break
        # Load next frame
        end_flag, c_frame = cap.read ()
    # End processing
    cv2.destroyAllWindows ()
    cap.release ()
    return smile_height, smile_width
if __name__ == '__main__':
    smile_recognition ()
Tried

Please describe what you tried for the problem here.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

  • Answer # 1

    This code is running on Raspberry Pi 3B.
    When the problem occurred, everything was running in Thonny.

    I tried running on Terminal,
    The return value was passed without any problem (when print (smile_recognition ()), the values ​​of smile_height and smile_width are output).

    It seems that print is not working only when running this code in Thonny.
    (In the function smile_recognition (), adding a separate print statement does not work)
    Certainly it worked in another code.

    The cause is unknown.
    As for the point to be worried about, let's make this code "Debug current script" with Thonny,
    The following error message appears.
    This error only occurs during debugging.
    It may be related to the occurrence of this problem.

    Traceback (most recent call last):
    File"/usr/lib/python3/dist-packages/thonny/shared/thonny/ast_utils.py" ;, line 120, in get_last_child
    and hasattr (node.slice,"step"))
    AssertionError

  • Answer # 2

    I understand that I want to pass it as a return value, but
    Where is the return value delivered?

    Your code will include
    smile_recognition ()
    And there is nothing