-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeypoints_forwardpass.py
More file actions
70 lines (56 loc) · 2.12 KB
/
keypoints_forwardpass.py
File metadata and controls
70 lines (56 loc) · 2.12 KB
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
import os
import sys
import dotenv
dotenv.load_dotenv()
# check that PYTORCH_CUDA_ALLOC_CONF is set in the environment
if "PYTORCH_CUDA_ALLOC_CONF" not in os.environ:
raise ValueError("PYTORCH_CUDA_ALLOC_CONF environment variable is not set")
sys.path.append(os.getcwd())
from src.helpers.load_waybetter_db import ( # noqa: E402
load_waybetter_db,
get_pictures,
)
from src.models.keypoint_detection import KeyPointDetectionModule # noqa: E402
import pandas as pd # noqa: E402
import sqlite3 # noqa: E402
import logging # noqa: E402
from pathlib import Path # noqa: E402
WOKRING_DIR = Path("~/DigitalScale")
DATA_DIR = Path(os.getenv("DATA_DIR"))
if DATA_DIR is None:
raise ValueError("DATA_DIR environment variable is not set")
DB_PATH = str(DATA_DIR) + "/subset.sqlite3"
PHOTOS_DIR = DATA_DIR / "images"
logger = logging.getLogger("custom")
logging.basicConfig(filename="bounding_box_forward_pass.log", level=logging.CRITICAL)
dataset = load_waybetter_db(DB_PATH)
dataset = dataset[dataset["image_present"] == 1]
sample_pictures = get_pictures(dataset, PHOTOS_DIR)
try:
keypoint_detection_module = KeyPointDetectionModule(batch_size=16)
keypoint_detection_module.run(sample_pictures)
except Exception as e:
logger.critical(f"Error while running bounding box forward pass: {e}")
raise
finally:
print("Saving bounding boxes to database")
all_keypoints = []
for picture in sample_pictures:
if picture.keypoints is None:
continue
for keypoint in picture.keypoints:
all_keypoints.append(
{
"image_id": picture.original_path.name, # type: ignore
"x": keypoint.x,
"y": keypoint.y,
"confidence": keypoint.confidence,
"label": keypoint.label,
}
)
keypoints_df = pd.DataFrame(all_keypoints)
# Dump the DataFrame to an SQLite database
conn = sqlite3.connect("keypoints.db")
keypoints_df.to_sql("keypoints", conn, if_exists="replace", index=False)
conn.close()
print(f"Total keypoints: {len(keypoints_df)}")