unfinity commited on
Commit
6fda8a4
1 Parent(s): 4ca2511
Files changed (2) hide show
  1. Dockerfile +2 -0
  2. main.py +48 -2
Dockerfile CHANGED
@@ -6,6 +6,8 @@ RUN pip install ultralytics
6
  RUN pip install opencv-python==4.6.0.66
7
  RUN pip install Pillow==10.3.0
8
  RUN pip install uvicorn fastapi
 
 
9
 
10
  COPY . .
11
 
 
6
  RUN pip install opencv-python==4.6.0.66
7
  RUN pip install Pillow==10.3.0
8
  RUN pip install uvicorn fastapi
9
+ RUN apt update && apt install fonts-dejavu -y
10
+ RUN wget https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8l-pose.pt -O /app/yolov8l-pose.pt
11
 
12
  COPY . .
13
 
main.py CHANGED
@@ -10,8 +10,54 @@ import utils
10
  from drawing import draw_keypoints
11
 
12
 
 
 
 
 
 
 
13
  app = FastAPI()
14
 
 
15
  @app.get("/")
16
- def read_root():
17
- return {"Hello": "World!"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from drawing import draw_keypoints
11
 
12
 
13
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
14
+ print('Using device:', device)
15
+
16
+ model_pose = YOLO('yolov8l-pose.pt')
17
+ model_pose.to(device)
18
+
19
  app = FastAPI()
20
 
21
+
22
  @app.get("/")
23
+ async def health():
24
+ return JSONResponse(content={"status": "ok"})
25
+
26
+
27
+ @app.post("/predict-image")
28
+ async def predict_image(file: UploadFile = File(...)):
29
+ contents = await file.read()
30
+ input_image = Image.open(io.BytesIO(contents)).convert("RGB")
31
+ input_image = ImageOps.exif_transpose(input_image)
32
+
33
+ # predict
34
+ result = model_pose(input_image)[0]
35
+ keypoints = utils.get_keypoints(result)
36
+
37
+ # draw keypoints
38
+ output_image = draw_keypoints(input_image, keypoints).convert("RGB")
39
+
40
+ # calculate angles
41
+ lea, rea = utils.get_eye_angles(keypoints)
42
+ lba, rba = utils.get_elbow_angles(keypoints)
43
+ angles = {'left_eye_angle': lea, 'right_eye_angle': rea, 'left_elbow_angle': lba, 'right_elbow_angle': rba}
44
+
45
+ # encode to base64
46
+ img_buffer = io.BytesIO()
47
+ output_image.save(img_buffer, format="JPEG")
48
+ img_buffer.seek(0)
49
+ img_base64 = base64.b64encode(img_buffer.getvalue()).decode("utf-8")
50
+
51
+ # prepare json response
52
+ json_data = {
53
+ "keypoints": keypoints,
54
+ "angles": angles,
55
+ "output_image": img_base64
56
+ }
57
+
58
+ return JSONResponse(content=json_data)
59
+
60
+
61
+ # if __name__ == "__main__":
62
+ # import uvicorn
63
+ # uvicorn.run(app, host="0.0.0.0", port=7860)