Prajithr04 commited on
Commit
c846193
·
1 Parent(s): af92acb

first commit

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +107 -0
  3. requirements.txt +0 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM python:3.12.3
5
+
6
+ RUN useradd -m -u 1000 user
7
+ USER user
8
+ ENV PATH="/home/user/.local/bin:$PATH"
9
+
10
+ WORKDIR /app
11
+
12
+ COPY --chown=user ./requirements.txt requirements.txt
13
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
14
+
15
+ COPY --chown=user . /app
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import math
3
+ import base64
4
+ import numpy as np
5
+ import mediapipe as mp
6
+ from io import BytesIO
7
+ from fastapi import FastAPI, File, UploadFile
8
+ from fastapi.responses import Response
9
+ from PIL import Image
10
+
11
+ # Initialize FastAPI app
12
+ app = FastAPI()
13
+
14
+ # Initialize Mediapipe Pose model
15
+ mp_pose = mp.solutions.pose
16
+ pose = mp_pose.Pose(
17
+ static_image_mode=False,
18
+ min_detection_confidence=0.5,
19
+ min_tracking_confidence=0.5
20
+ )
21
+
22
+ # Function to calculate angles between points
23
+ def calculate_angle(a, b, c):
24
+ ab = (b[0] - a[0], b[1] - a[1])
25
+ bc = (c[0] - b[0], c[1] - b[1])
26
+
27
+ dot_product = ab[0] * bc[0] + ab[1] * bc[1]
28
+ magnitude_ab = math.sqrt(ab[0]**2 + ab[1]**2)
29
+ magnitude_bc = math.sqrt(bc[0]**2 + bc[1]**2)
30
+
31
+ angle_radians = math.acos(dot_product / (magnitude_ab * magnitude_bc))
32
+ angle_degrees = math.degrees(angle_radians)
33
+
34
+ return angle_degrees
35
+
36
+ # Process image with Mediapipe Pose Estimation
37
+ def process_frame(image):
38
+ h, w, _ = image.shape
39
+
40
+ # Convert to RGB
41
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
42
+ image_rgb.flags.writeable = False
43
+ results = pose.process(image_rgb)
44
+ image_rgb.flags.writeable = True
45
+
46
+ # Convert back to BGR for display
47
+ image = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
48
+
49
+ if results.pose_landmarks:
50
+ # Get landmarks
51
+ right_shoulder = results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_SHOULDER]
52
+ right_hip = results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_HIP]
53
+ right_ear = results.pose_landmarks.landmark[mp_pose.PoseLandmark.RIGHT_EAR]
54
+
55
+ # Convert to pixel coordinates
56
+ cx_rs, cy_rs = int(right_shoulder.x * w), int(right_shoulder.y * h)
57
+ cx_rh, cy_rh = int(right_hip.x * w), int(right_hip.y * h)
58
+ cx_re, cy_re = int(right_ear.x * w), int(right_ear.y * h)
59
+
60
+ # Create upper reference points
61
+ offset = 60
62
+ upper_shoulder = (cx_rs, max(0, cy_rs - offset))
63
+ upper_hip = (cx_rh, max(0, cy_rh - offset))
64
+
65
+ # Draw landmarks
66
+ cv2.circle(image, upper_shoulder, 5, (0, 255, 0), -1)
67
+ cv2.circle(image, upper_hip, 5, (0, 255, 0), -1)
68
+
69
+ # Draw lines
70
+ cv2.line(image, (cx_rh, cy_rh), (cx_rs, cy_rs), (255, 0, 255), 2) # Hip to shoulder
71
+ cv2.line(image, (cx_rs, cy_rs), (cx_re, cy_re), (255, 255, 0), 2) # Shoulder to ear
72
+ cv2.line(image, (cx_rh, cy_rh), upper_hip, (0, 165, 255), 2) # Hip to upper hip
73
+ cv2.line(image, (cx_rs, cy_rs), upper_shoulder, (0, 255, 255), 2) # Shoulder to upper shoulder
74
+
75
+ # Calculate angles
76
+ angle_hip = calculate_angle(upper_hip, (cx_rh, cy_rh), (cx_rs, cy_rs))
77
+ angle_neck = calculate_angle((cx_rs, cy_rs), (cx_re, cy_re), upper_shoulder)
78
+
79
+ # Determine posture status
80
+ hip_posture = "Good" if 160 <= angle_hip <= 180 else "Poor"
81
+ neck_posture = "Good" if 150 <= angle_neck <= 180 else "Poor"
82
+ hip_color = (0, 255, 0) if hip_posture == "Good" else (0, 0, 255)
83
+ neck_color = (0, 255, 0) if neck_posture == "Good" else (0, 0, 255)
84
+
85
+ # Display angles
86
+ cv2.putText(image, f"Hip Angle: {angle_hip:.1f} ({hip_posture})", (10, 60),
87
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, hip_color, 2)
88
+ cv2.putText(image, f"Neck Angle: {angle_neck:.1f} ({neck_posture})", (10, 90),
89
+ cv2.FONT_HERSHEY_SIMPLEX, 0.6, neck_color, 2)
90
+
91
+ return image
92
+
93
+ # API Route to receive an image and return processed image
94
+ @app.post("/upload")
95
+ async def upload_image(file: UploadFile = File(...)):
96
+ # Read the image
97
+ contents = await file.read()
98
+ image = Image.open(BytesIO(contents))
99
+ image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
100
+
101
+ # Process the image
102
+ processed_image = process_frame(image)
103
+
104
+ # Encode processed image to return
105
+ _, buffer = cv2.imencode(".jpg", processed_image)
106
+ return Response(content=buffer.tobytes(), media_type="image/jpeg")
107
+
requirements.txt ADDED
Binary file (3.92 kB). View file