-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
34 lines (30 loc) · 1.38 KB
/
utils.py
File metadata and controls
34 lines (30 loc) · 1.38 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
# Links directly in scanner.py
from color_detector import predict_color
# Return list of LAB values for 9 dots
def sample_face_colors(cv2, frame, grid_centers):
face_colors = []
for (x, y) in grid_centers:
# Getting avg LAB for ROI around dots
roi = frame[y-10:y+10, x-10:x+10] # A 20x20 region around individual dot
roi_lab = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)
lab_avg = cv2.mean(roi_lab)[:3] # Only L, A, B
lab_avg_rounded = tuple(map(int, lab_avg))
face_colors.append(lab_avg_rounded)
return face_colors
# Function to check for similar face scanned by the center piece of that face
# Uses Euclidean Distance
def is_similar_color(np, c1, c2, threshold=12):
diff = np.linalg.norm(np.array(c1) - np.array(c2))
return diff < threshold
# Classify face colors
def classify_face_colors(rf_model, scanned_faces):
return ''.join(''.join(predict_color(rf_model, lab) for lab in face) for face in scanned_faces)
# Save the data collected to dataset.csv one-by-one
def save_to_csv(os, csv, LAB_values, label, dataset_path="dataset.csv"):
file_exists = os.path.isfile(dataset_path)
with open(dataset_path, mode="a", newline="") as file:
writer = csv.writer(file)
if not file_exists:
writer.writerow(["L", "A", "B", "label"]) # header
for LAB in LAB_values:
writer.writerow([*LAB, label])