tensorfactory / PiFaceCam

Facial Recognition for Raspberry Pi
1 stars 1 forks source link

PiFaceCam Process Tracking or Error Handling #3

Closed james-e-morris closed 3 years ago

james-e-morris commented 3 years ago

PiFaceCam launches many parallel processes and sometimes these processes end, but the main PiFaceCam process is still active. My application has no way of knowing if PiFaceCam is actually still functional if the run( ) process is still active.

Below is a simple example of this. If I run PiFaceCam too soon after ending it while streaming to the browser, sometimes the videa stream thread crashes right away. When this happens, the run( ) call should error out, so I should at least be able to check the status of the full PiFaceCam call stack.

test code:

from pifacecam import pifacecam
from time import sleep

def example_callback_function(data_dict):
    returned_faceID_list = data_dict["face_id_list"]
    returned_face_is_dimensional_check_passed_list = data_dict["face_is_dimensional_check_passed_list"]

    no_of_faces_passed_dimensional_check = 0
    for this_face_is_dimensional_check_passed in returned_face_is_dimensional_check_passed_list:
        if this_face_is_dimensional_check_passed:
            no_of_faces_passed_dimensional_check += 1

    if no_of_faces_passed_dimensional_check:
        for face_idx in range(len(returned_faceID_list)):
            print("Face:", returned_faceID_list[face_idx])

        # Perform post recognition task here....
        sleep(5)

pifacecam.run(
    callback_function=example_callback_function,
    faceids_folder_pathname='assets/prod/faces/images',
    device_id='CAM',
    cam_setup='MONO_USB',
    detect_conf_percent=95.0,
    full_face_only=False,
    high_precision_mode=False,
    max_num_for_hp_mode=1,
    max_num_for_std_mode=1,
    show_bbox=True,
    show_faceid=True,
    show_camid=True,
    show_fps=True,
    show_positioning_guides=True,
    show_conf_percentage=True,
    show_precision_mode=True,
    status_pin_num=7, # 19 default
    shutdown_pin_num=8 # 26 default
    )

console output indicating the process failed, but the run( ) code is still operating:

1) 07/06/2021 10:21:22:  Checking for attached Picamera.
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
2) 07/06/2021 10:21:22:  No Picamera found.
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
3) 07/06/2021 10:21:22:  Error at PI Cam: Camera is not enabled. Try running 'sudo raspi-config' and ensure that the camera has been enabled.
4) 07/06/2021 10:21:23:  Checking for attached USB camera.
[ WARN:0] global /home/pi/opencv/modules/videoio/src/cap_gstreamer.cpp (597) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
5) 07/06/2021 10:21:26:  USB camera found at index[0].
Load saved face ids from file.
Process Process-8:
Traceback (most recent call last):
  File "/usr/lib/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "pifacecam.py", line 9165, in pifacecam.p_http_video_streaming_for_general
OSError: [Errno 98] Address already in use
tensorfactory commented 3 years ago

Hi Morrious, Thank you for your feedback. We are reviewing the feasibility of your request to have "run( ) process exit" when any of the sub-processes has crashed. Let us come back to you in one to two days.

Thanks KC Yee

james-e-morris commented 3 years ago

Thank you, KC. This issue is more important than the GPIO one I would say. I am currently having a problem where the PiFaceCam functionality stops responding after a few days, so I would like to be able to reinitialize when something has crashed. I plan to set it up as a constantly running service, but as it works right now that service would just freeze without knowing it has partially stopped.

tensorfactory commented 3 years ago

Hi Morrious, From the example in your first message, we believe the error occurred because the port for video streaming was locked when the earlier streaming was interrupted and not immediately available when you restart PiFaceCam. We also understand that this is not the only problem you have encountered, and more importantly, the run() process does not return when these occur. We planned to make the following improvements to address them. 1) We will add a function to detect exceptions in each process and log them. 2) Trigger all running sub-processes to properly close and run() to return when an exception is detected. We have been running some tests and these seem to be doable. We should have a running code in a week time. However, we will not able to upload it to pypi.org immediately as to do this, we will have to run many more tests and this will take weeks. If you prefer, we can upload the code to github for you to use/test first, once it is available (estimated by early next week).

james-e-morris commented 3 years ago

@tensorfactory thank you! Yes please, I would gladly test with the code via GitHub in the meantime.

tensorfactory commented 3 years ago

Hi Morrious, We have uploaded pifacecam-1.1.1a1 to the pre-releases folder. It has the following upgrades.

1) Add an option to disable status/shutdown gpios by setting the value to -1. 2) When an exception is encountered, the run() method will return an integer indicating the reason of return when exception encountered. More information on exceptions can be obtained in the log file. (Note: From exception trigger to run() return will take around 5 to 10seconds as it has to wait for all running processes to properly close.)

SHUTDOWN_PIN_TRIGGERED = 1 ERROR_ENCOUNTERED_WHILE_READING_USB_CAMERA = 2 USB_CAMERA_IS_REQUIRED_BUT_NOT_AVAILABLE = 3 ERROR_ENCOUNTERED_WHILE_READING_PICAMERA = 4 PICAMERA_IS_REQUIRED_BUT_NOT_AVAILABLE = 5 ERROR_ENCOUNTERED_WHILE_PROCESSING_EMBEDDING = 6 ERROR_ENCOUNTERED_IN_CALLBACK_FUNCTION = 7 ERROR_ENCOUNTERED_WHILE_SETTING_UP_VIDEO_STREAMING_SERVER = 8 ERROR_ENCOUNTERED_WHILE_SETTING_UP_STATUS_SHUTDOWN_GPIOS = 9 ERROR_ENCOUNTERED_WHILE_SETTING_UP_VERIFICATION_SERVER = 10

Hope this will resolve those issues you encountered. Kindly help to test and feedback.

Rgds KC Yee

james-e-morris commented 3 years ago

Great, thank you so much! I will integrate the pre-release into my application and let you know if I have any issues.

james-e-morris commented 3 years ago

I'm getting an error when I try to install this pre-release version. I've tried installing using the --upgrade tag as well as a full uninstall and reinstall. image

tensorfactory commented 3 years ago

Hi Morrious, Can you try "wget https://github.com/tensorfactory/PiFaceCam/raw/master/pre-releases/pifacecam-1.1.1a1-cp37-abi3-linux_armv7l.whl" ? Once downloaded the whl file size should be around 67.3 MB. wget pifacecam pre-releases

Rgds KC Yee

james-e-morris commented 3 years ago

That did the trick, thank you. I guess the Copy Permalink button isn't the correct wget link.

I have been testing all afternoon with the prerelease and it looks to be working well. The return codes are working and I have the pins set to -1 without any errors. Thanks for the updates!

tensorfactory commented 3 years ago

Thank you Morrious for your valuable feedbacks.