File size: 1,254 Bytes
3afff35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from sys import argv
from time import sleep

import cv2 as cv

from app.camera import Camera
from app.server import HTTPDaemon
from app.translator import Translator


def camera_loop(camera: Camera, translator: Translator):

    retry_count = 0

    while not camera.is_capturing():
        if retry_count > 3:
            raise Exception("No camera frames found.")

        retry_count += 1
        sleep(1)

    while True:
        cv.imshow('Input', camera.buffer[-1])
        translator.video_to_asl(camera.buffer)

        if cv.waitKey(1) == 27:
            break

    cv.destroyAllWindows()


def main(translator: Translator):

    with Camera(0, 64) as camera:
        camera_loop(camera, translator)


def init_server():

    host = 'localhost'
    port = parse_args()
    translator = Translator(confidence=0.7)

    with HTTPDaemon(host, port, translator):
        try:
            main(translator)
            
        except KeyboardInterrupt:
            print("\nManual exit detected.")

        finally:
            print("Exiting..")


def parse_args() -> int:

    try:
        return 5000 if len(argv) < 2 else int(argv[1])

    except ValueError:
        print("\nPort must be an integer.\ne.g. python server.py 5000\n")
        raise