"""
Hand and face detection inference on webcam feed
"""
import cv2

from modules.inference_utils import init_detector, Timer, preprocess, timed_predict, postprocess, draw_boxes
from modules.config import CFG


def main(model_cfg):
    """
    Args:
        model_cfg (str): model configuration, see model implementation for implemented configurations
    """
    # load model
    model = init_detector(model_cfg)
    # create video capture object from available webcam
    cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    # make timer object
    timer = Timer()
    while cam.isOpened():
        # read frame
        ret, img = cam.read()
        if not ret:
            continue
        # make prediction
        preprocessed = preprocess(img, CFG.COMBINED_PARAMS.IMG_SHAPE)
        out = timed_predict(model, preprocessed, timer)
        boxes, scores, classes = postprocess(out, img.shape[1::-1])
        # visualize results
        draw_boxes(img, (boxes, scores, classes))
        # show results
        cv2.imshow('img', img)
        if cv2.waitKey(1) == 27:
            break
    # release resources
    cv2.destroyAllWindows()
    cam.release()


if __name__ == '__main__':
    main('yolov5s')
