Prajithr04 commited on
Commit
b689602
·
1 Parent(s): 8542af2
Files changed (1) hide show
  1. app.py +5 -12
app.py CHANGED
@@ -6,21 +6,11 @@ import mediapipe as mp
6
  from io import BytesIO
7
  from fastapi import FastAPI, File, UploadFile
8
  from fastapi.responses import Response
9
- from fastapi.middleware.cors import CORSMiddleware # Add CORS support
10
  from PIL import Image
11
 
12
  # Initialize FastAPI app
13
  app = FastAPI()
14
 
15
- # Add CORS middleware
16
- app.add_middleware(
17
- CORSMiddleware,
18
- allow_origins=["*"],
19
- allow_credentials=True,
20
- allow_methods=["*"],
21
- allow_headers=["*"],
22
- )
23
-
24
  # Initialize Mediapipe Pose model
25
  mp_pose = mp.solutions.pose
26
  pose = mp_pose.Pose(
@@ -28,6 +18,7 @@ pose = mp_pose.Pose(
28
  min_detection_confidence=0.5,
29
  min_tracking_confidence=0.5
30
  )
 
31
  # Function to calculate angles between points
32
  def calculate_angle(a, b, c):
33
  ab = (b[0] - a[0], b[1] - a[1])
@@ -102,13 +93,15 @@ def process_frame(image):
102
  # API Route to receive an image and return processed image
103
  @app.post("/upload")
104
  async def upload_image(file: UploadFile = File(...)):
 
105
  contents = await file.read()
106
  image = Image.open(BytesIO(contents))
107
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
108
 
109
- # Process the image in async context
110
  processed_image = process_frame(image)
111
 
112
  # Encode processed image to return
113
  _, buffer = cv2.imencode(".jpg", processed_image)
114
- return Response(content=buffer.tobytes(), media_type="image/jpeg")
 
 
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(
 
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])
 
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
+