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.
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 ()).
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 ()
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"))
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
And there is nothing
- python - i want to make a return value that returns multiple data in aws lambda function
- ios - swift return value cannot be returned as expected
- python - i want to display the return value of cv2calchist of opencv with hvcurve of holoview
- java - argument and return value errors
- c - when using return as an return value
- php - the return value becomes 0 with last_insert_id ()
- python - the return value of tfgradienttape () is none
- evaluation of the return value of ruby&&||
- xml - i want to return the value in the debug area to the code in swift5's border display using googlemap
- Solving the problem of return value of shell script
- i want the return value of the function to be an arbitrary name (python)
- Shell script return value and its implementation
- java - reference type in the return value of the method
- xcode - i want to return the value obtained by findobjectsinbackground
- python - i would like to know why the capacity changes when i load an image and just save it
- opencv - i want to resize an image to a specified size
- avoid double for loops with opencv in python
- python - display image with cvimshow
- i want to binarize by color in python
- opencv - i want to check the original data set
- python - install opencv
- opencv - about the processing result when the size is set to 0 in the bilateral filter cv2bilateralfilter
- opencv - yolov2 can't compile with error in python 383
- python - i want to detect the position of a traffic light and display a movie with opencv