mirror of
https://github.com/hacksider/Deep-Live-Cam
synced 2026-06-04 21:04:20 +08:00
Introduces pyproject.toml + .github/workflows/ruff.yml that gate E701, E711, E712, F401, F541 on every PR and push to main. Fixes the existing findings for those rules: - Remove unused imports (sklearn.silhouette_score, numpy in several files, typing.Optional, get_one_face, gpu_cvt_color, sys, insightface.face_align) - Annotate the intentional tkinter_fix side-effect import with `# noqa: F401` - Split multi-statement `if x: y` one-liners onto separate lines - Replace `state == True` / `state == False` with truthiness checks - Drop `f` prefix from f-strings with no placeholders F841 (unused-variable), E402 (module-level-import-not-at-top), and F821 (undefined-name) are left out of the gate for now — they surface real findings (including a latent NameError in face_swapper.py) that require human review to fix safely. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
31 lines
1.0 KiB
Python
31 lines
1.0 KiB
Python
import numpy as np
|
|
from sklearn.cluster import KMeans
|
|
from typing import Any
|
|
|
|
|
|
def find_cluster_centroids(embeddings, max_k=10) -> Any:
|
|
inertia = []
|
|
cluster_centroids = []
|
|
K = range(1, max_k+1)
|
|
|
|
for k in K:
|
|
kmeans = KMeans(n_clusters=k, random_state=0)
|
|
kmeans.fit(embeddings)
|
|
inertia.append(kmeans.inertia_)
|
|
cluster_centroids.append({"k": k, "centroids": kmeans.cluster_centers_})
|
|
|
|
diffs = [inertia[i] - inertia[i+1] for i in range(len(inertia)-1)]
|
|
optimal_centroids = cluster_centroids[diffs.index(max(diffs)) + 1]['centroids']
|
|
|
|
return optimal_centroids
|
|
|
|
def find_closest_centroid(centroids: list, normed_face_embedding) -> list:
|
|
try:
|
|
centroids = np.array(centroids)
|
|
normed_face_embedding = np.array(normed_face_embedding)
|
|
similarities = np.dot(centroids, normed_face_embedding)
|
|
closest_centroid_index = np.argmax(similarities)
|
|
|
|
return closest_centroid_index, centroids[closest_centroid_index]
|
|
except ValueError:
|
|
return None |