<reponame>Ascend/samples<gh_stars>10-100
import datetime
import time
import acl
import configparser
import sys
import os

cur_file_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(cur_file_dir + "/../../../../common")

import videocapture as video
from constants import *
from utils import *
from acllite_resource import AclLiteResource
from acllite_image import AclLiteImage
from acllite_logger import log_error, log_info

from vgg_ssd import VggSsd
from preprocess import Preprocess
from postprocess import DetectData, Postprocess

MODEL_PATH = "../model/face_detection.om"
MODEL_WIDTH = 304
MODEL_HEIGHT = 300
FACE_DETEC_CONF="../scripts/face_detection.conf"

def create_threads(detector):
    config = configparser.ConfigParser()
    config.read(FACE_DETEC_CONF)
    video_decoders = []
    for item in config['videostream']:
        preprocesser = Preprocess(config['videostream'][item], 
                                  len(video_decoders),
                                  MODEL_WIDTH, MODEL_HEIGHT)
        video_decoders.append(preprocesser)

    rtsp_num = len(video_decoders)
    if rtsp_num == 0:
        log_error("No video stream name or addr configuration in ",
                  FACE_DETEC_CONF)
        return None, None

    postprocessor = Postprocess(detector)

    display_channel = int(config['display']['channel'])
    if (display_channel is None) or (display_channel >= rtsp_num):
        log_info("No video to display, display configuration: ", 
                 config['display']['channel'])
    else:
        video_decoders[display_channel].set_display(True)
        ret = postprocessor.create_presenter_channel(FACE_DETEC_CONF)
        if ret == False:
            log_error("Create presenter channel failed")
            return None, None
        
    return video_decoders, postprocessor

def main():
    """
    Function description:
        Main function
    """
    acl_resource = AclLiteResource()
    acl_resource.init()   

    detector = VggSsd(acl_resource, MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)

    video_decoders, postprocessor = create_threads(detector)
    if video_decoders is None:
        log_error("Please check the configuration in %s is valid"
                  %(FACE_DETEC_CONF))
        return
    
    while True:
        all_process_fin = True
        for decoder in video_decoders:
            ret, data = decoder.get_data()
            if ret == False:                
                continue
            if data:
                detect_results = detector.execute(data)
                postprocessor.process(data, detect_results)
                
            all_process_fin = False
        if all_process_fin:
            log_info("all video decoder finish")
            break

    postprocessor.exit()

    log_info("sample execute end")  

if __name__ == '__main__':
    main()
