Spaces:
No application file
No application file
Upload Notes_260603_125402.txt
Browse files- Notes_260603_125402.txt +106 -0
Notes_260603_125402.txt
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import cv2
|
| 4 |
+
import mediapipe as mp
|
| 5 |
+
import numpy as np
|
| 6 |
+
from sklearn.cluster import KMeans
|
| 7 |
+
import webcolors
|
| 8 |
+
|
| 9 |
+
app = FastAPI()
|
| 10 |
+
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"],
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
mp_pose = mp.solutions.pose
|
| 20 |
+
mp_face_mesh = mp.solutions.face_mesh
|
| 21 |
+
pose = mp_pose.Pose(static_image_mode=True, min_detection_confidence=0.5)
|
| 22 |
+
face_mesh = mp_face_mesh.FaceMesh(static_image_mode=True, max_num_faces=1)
|
| 23 |
+
|
| 24 |
+
def get_closest_color_name(rgb):
|
| 25 |
+
min_colors = {}
|
| 26 |
+
for key, name in webcolors.CSS3_HEX_TO_NAMES.items():
|
| 27 |
+
r_c, g_c, b_c = webcolors.hex_to_rgb(key)
|
| 28 |
+
rd = (r_c - rgb[0]) ** 2
|
| 29 |
+
gd = (g_c - rgb[1]) ** 2
|
| 30 |
+
bd = (b_c - rgb[2]) ** 2
|
| 31 |
+
min_colors[(rd + gd + bd)] = name
|
| 32 |
+
return min_colors[min(min_colors.keys())]
|
| 33 |
+
|
| 34 |
+
@app.post("/analyze-character")
|
| 35 |
+
async def analyze_character(file: UploadFile = File(...)):
|
| 36 |
+
contents = await file.read()
|
| 37 |
+
nparr = np.frombuffer(contents, np.uint8)
|
| 38 |
+
image = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
| 39 |
+
h, w, _ = image.shape
|
| 40 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 41 |
+
|
| 42 |
+
pitch, yaw, roll = 0, 0, 0
|
| 43 |
+
cx, cy, radius = 0.5, 0.25, 0.12
|
| 44 |
+
|
| 45 |
+
face_results = face_mesh.process(image_rgb)
|
| 46 |
+
if face_results.multi_face_landmarks:
|
| 47 |
+
face = face_results.multi_face_landmarks[0].landmark
|
| 48 |
+
face_2d = np.array([[int(face[i].x * w), int(face[i].y * h)] for i in [1, 33, 263, 61, 291, 199]], dtype=np.float64)
|
| 49 |
+
face_3d = np.array([[face[i].x, face[i].y, face[i].z] for i in [1, 33, 263, 61, 291, 199]], dtype=np.float64)
|
| 50 |
+
focal_length = 1 * w
|
| 51 |
+
cam_matrix = np.array([[focal_length, 0, w / 2], [0, focal_length, h / 2], [0, 0, 1]])
|
| 52 |
+
dist_matrix = np.zeros((4, 1), dtype=np.float64)
|
| 53 |
+
|
| 54 |
+
success, rot_vec, trans_vec = cv2.solvePnP(face_3d, face_2d, cam_matrix, dist_matrix)
|
| 55 |
+
rmat, _ = cv2.Rodrigues(rot_vec)
|
| 56 |
+
angles, _, _, _, _, _ = cv2.RQDecomp3x3(rmat)
|
| 57 |
+
|
| 58 |
+
pitch, yaw, roll = angles[0] * 360, angles[1] * 360, angles[2] * 360
|
| 59 |
+
cx, cy = face[1].x, face[1].y
|
| 60 |
+
|
| 61 |
+
body_joints = {}
|
| 62 |
+
pose_results = pose.process(image_rgb)
|
| 63 |
+
if pose_results.pose_landmarks:
|
| 64 |
+
lm = pose_results.pose_landmarks.landmark
|
| 65 |
+
body_joints = {
|
| 66 |
+
"tl": {"x": lm[11].x, "y": lm[11].y}, "tr": {"x": lm[12].x, "y": lm[12].y},
|
| 67 |
+
"bl": {"x": lm[23].x, "y": lm[23].y}, "br": {"x": lm[24].x, "y": lm[24].y},
|
| 68 |
+
"limbs": [
|
| 69 |
+
{"start": {"x": lm[11].x, "y": lm[11].y}, "end": {"x": lm[13].x, "y": lm[13].y}},
|
| 70 |
+
{"start": {"x": lm[12].x, "y": lm[12].y}, "end": {"x": lm[14].x, "y": lm[14].y}},
|
| 71 |
+
{"start": {"x": lm[23].x, "y": lm[23].y}, "end": {"x": lm[25].x, "y": lm[25].y}},
|
| 72 |
+
{"start": {"x": lm[24].x, "y": lm[24].y}, "end": {"x": lm[26].x, "y": lm[26].y}}
|
| 73 |
+
]
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
resized_img = cv2.resize(image_rgb, (50, 50)).reshape(-1, 3)
|
| 77 |
+
clt = KMeans(n_clusters=4, n_init=10).fit(resized_img)
|
| 78 |
+
palette = []
|
| 79 |
+
for rgb in clt.cluster_centers_:
|
| 80 |
+
rgb_ints = [int(x) for x in rgb]
|
| 81 |
+
hex_code = f"#{rgb_ints[0]:02x}{rgb_ints[1]:02x}{rgb_ints[2]:02x}"
|
| 82 |
+
palette.append({"name": get_closest_color_name(rgb_ints).title(), "hex": hex_code})
|
| 83 |
+
|
| 84 |
+
return {
|
| 85 |
+
"palette": palette,
|
| 86 |
+
"steps": [
|
| 87 |
+
{
|
| 88 |
+
"step_number": 1,
|
| 89 |
+
"instruction": f"Draw the Loomis Head Structure. Angles - Pitch: {round(pitch)}°, Yaw: {round(yaw)}°, Roll: {round(roll)}°.",
|
| 90 |
+
"action": "drawLoomisHead",
|
| 91 |
+
"data": {"cx": cx, "cy": cy, "radius": radius, "pitch": pitch, "yaw": yaw, "roll": roll}
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"step_number": 2,
|
| 95 |
+
"instruction": "Block out the 3D core torso boundary.",
|
| 96 |
+
"action": "drawTorsoBox",
|
| 97 |
+
"data": body_joints
|
| 98 |
+
},
|
| 99 |
+
{
|
| 100 |
+
"step_number": 3,
|
| 101 |
+
"instruction": "Map structural gesture bones.",
|
| 102 |
+
"action": "drawLimbs",
|
| 103 |
+
"data": body_joints
|
| 104 |
+
}
|
| 105 |
+
]
|
| 106 |
+
}
|