-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (93 loc) · 3.44 KB
/
main.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# API + CLI imports
import logging
import typer
from typing_extensions import Annotated
from pathlib import Path
# Internal imports
import debug
import src.api.main as api
import src.model.migrate as db
import face_recognition
from src.extractor.embedding import FeatureExtractor, FaceExtractionStatus
from src.contracts.oracle import ContestContract
from src.config.env import EnvConfig
logging.basicConfig(level=logging.INFO)
app = typer.Typer(help="Face recognition API for the Black Pepper Team")
# --- Debugging commands ---
@app.command()
def debug_embeddings(
img_1_path: Annotated[Path, typer.Option(help='Path to image 1')] = 'test/anton_1.jpg',
img_2_path: Annotated[Path, typer.Option(help='Path to image 2')] = 'test/ivan_1.jpg',
) -> None:
debug.debug_embeddings(img_1_path, img_2_path)
@app.command()
def debug_discrete_embeddings(
img_1_path: Annotated[Path, typer.Option(help='Path to image 1')] = 'test/ivan_1.jpg',
img_2_path: Annotated[Path, typer.Option(help='Path to image 2')] = 'test/ivan_2.jpg',
) -> None:
debug.debug_discrete_embeddings(img_1_path, img_2_path)
@app.command()
def debug_encode(img_path: Annotated[Path, typer.Argument(help='Path to image')]) -> None:
debug.debug_encode(img_path=img_path)
@app.command()
def debug_show_npy_img(img_path: Annotated[Path, typer.Argument(help='Path to image')]) -> None:
debug.debug_show_npy_img(img_path=img_path)
@app.command()
def debug_embeddings(
img_1_path: Annotated[Path, typer.Option(help='Path to image 1')] = 'test/anton_1.jpg',
img_2_path: Annotated[Path, typer.Option(help='Path to image 2')] = 'test/ivan_1.jpg',
) -> None:
debug.debug_embeddings(img_1_path, img_2_path)
@app.command()
def create_contest(
img_path: Annotated[Path, typer.Option(help='Path to image')] = 'test/ivan_1.jpg',
duration: Annotated[int, typer.Option(help='Duration of contest')] = 60,
) -> None:
img = face_recognition.load_image_file(img_path)
extractor = FeatureExtractor(img)
embedding, status = extractor.extract_discrete_features()
match status:
case FaceExtractionStatus.NO_FACE_FOUND:
print("No face in image")
exit(1)
case FaceExtractionStatus.TOO_MANY_PEOPLE:
print("too many people on image")
exit(2)
cfg = EnvConfig()
contest = ContestContract(cfg.eth_provider, cfg.contest_address, cfg.private_key)
txid = contest.createcontest(embedding, duration)
print(f"Create contest {txid=}")
@app.command()
def finalize_contest() -> None:
cfg = EnvConfig()
contest = ContestContract(cfg.eth_provider, cfg.contest_address, cfg.private_key)
contest_id = contest.latest_contest_id()
txid = contest.finalizecontest(contest_id)
print(txid.hex())
# --- Actually useful commands ---
@app.command()
def run_api() -> None:
"""Runs the service API"""
api.run_api()
@app.command()
def migrate_up() -> None:
"""
Migrates the database up
"""
try:
db.migrate_up()
print('Database migrated up')
except Exception as e:
logging.error(f'Error while migrating up: {e}')
@app.command()
def migrate_down() -> None:
"""
Migrates the database down
"""
try:
db.migrate_down()
print('Database migrated down')
except Exception as e:
logging.error(f'Error while migrating down: {e}')
if __name__ == '__main__':
app()