Spaces:
Sleeping
Sleeping
unfinity
commited on
Commit
·
fadf95f
1
Parent(s):
ff2b03c
switch to streamlit
Browse files
Dockerfile
CHANGED
@@ -6,11 +6,14 @@ 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 |
RUN apt update && apt install fonts-dejavu -y
|
10 |
RUN wget https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8l-pose.pt
|
11 |
|
12 |
COPY . .
|
13 |
|
14 |
-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
|
15 |
|
16 |
# curl -X POST "https://unfinity-yolov8-pose.hf.space/predict-image" -F "[email protected]"
|
|
|
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 pip install streamlit
|
10 |
RUN apt update && apt install fonts-dejavu -y
|
11 |
RUN wget https://github.com/ultralytics/assets/releases/download/v8.2.0/yolov8l-pose.pt
|
12 |
|
13 |
COPY . .
|
14 |
|
15 |
+
# CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
16 |
+
|
17 |
+
CMD ["streamlit", "run", "app.py"]
|
18 |
|
19 |
# curl -X POST "https://unfinity-yolov8-pose.hf.space/predict-image" -F "[email protected]"
|
app.py
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
import streamlit as st
|
2 |
-
|
3 |
-
from PIL import Image, ImageDraw, ImageFont
|
4 |
from ultralytics import YOLO
|
5 |
import torch
|
6 |
import utils
|
7 |
|
|
|
|
|
|
|
8 |
|
9 |
@st.cache_resource()
|
10 |
def load_model():
|
@@ -16,49 +18,8 @@ def load_model():
|
|
16 |
|
17 |
|
18 |
def draw_output(image_pil: Image.Image, keypoints: dict):
|
19 |
-
|
20 |
-
|
21 |
-
font = ImageFont.truetype("DejaVuSerif-Bold.ttf", 70)
|
22 |
-
|
23 |
-
ear, eye = None, None
|
24 |
-
if keypoints["left_ear"] and keypoints["left_eye"]:
|
25 |
-
ear = keypoints["left_ear"]
|
26 |
-
eye = keypoints["left_eye"]
|
27 |
-
elif keypoints["right_ear"] and keypoints["right_eye"]:
|
28 |
-
ear = keypoints["right_ear"]
|
29 |
-
eye = keypoints["right_eye"]
|
30 |
-
|
31 |
-
# draw extended left and right eye lines
|
32 |
-
if ear and eye:
|
33 |
-
left_new_point = utils.extend_line(ear, eye, 3)
|
34 |
-
l1 = [ear, left_new_point]
|
35 |
-
draw.line(l1, fill='red', width=line_width)
|
36 |
-
# draw a horizontal line from ear forwards
|
37 |
-
ear = np.array(ear)
|
38 |
-
l1 = np.array(l1)
|
39 |
-
l1_vector = l1[1] - l1[0]
|
40 |
-
x_s = np.sign(l1_vector)[0]
|
41 |
-
length_l1 = np.linalg.norm(l1_vector)
|
42 |
-
p2 = ear + np.array([length_l1*x_s, 0])
|
43 |
-
ear = tuple(ear.tolist())
|
44 |
-
l = [ear, tuple(p2.tolist())]
|
45 |
-
draw.line(l, fill='gray', width=line_width//2)
|
46 |
-
# draw angle
|
47 |
-
angle = utils.calculate_angle_to_horizontal(l1_vector)
|
48 |
-
draw.text(ear, f'{angle:.2f}', fill='red', font=font)
|
49 |
-
|
50 |
-
# draw elbow angles
|
51 |
-
left_elbow_angle, right_elbow_angle = utils.get_elbow_angles(keypoints)
|
52 |
-
if left_elbow_angle:
|
53 |
-
draw.text(keypoints['left_elbow'], f'{left_elbow_angle:.2f}', fill='red', font=font)
|
54 |
-
# draw polyline for left arm
|
55 |
-
draw.line([keypoints['left_shoulder'], keypoints['left_elbow'], keypoints['left_wrist']], fill='blue', width=line_width)
|
56 |
-
if right_elbow_angle:
|
57 |
-
draw.text(keypoints['right_elbow'], f'{right_elbow_angle:.2f}', fill='red', font=font)
|
58 |
-
# draw polyline for right arm
|
59 |
-
draw.line([keypoints['right_shoulder'], keypoints['right_elbow'], keypoints['right_wrist']], fill='blue', width=line_width)
|
60 |
-
|
61 |
-
return image_pil
|
62 |
|
63 |
|
64 |
st.title('Pose Estimation App')
|
@@ -82,16 +43,18 @@ if img is not None:
|
|
82 |
# predict
|
83 |
with st.spinner('Predicting...'):
|
84 |
model = load_model()
|
85 |
-
|
86 |
st.markdown('**Results:**')
|
87 |
-
keypoints = utils.get_keypoints(
|
88 |
if keypoints is not None:
|
89 |
img = draw_output(img, keypoints)
|
90 |
st.image(img, caption='Predicted image', use_column_width=True)
|
|
|
91 |
lea, rea = utils.get_eye_angles(keypoints)
|
92 |
lba, rba = utils.get_elbow_angles(keypoints)
|
|
|
93 |
st.write('Angles:')
|
94 |
-
st.json(
|
95 |
st.write('Raw keypoints:')
|
96 |
st.json(keypoints)
|
97 |
else:
|
|
|
1 |
import streamlit as st
|
2 |
+
from PIL import Image
|
|
|
3 |
from ultralytics import YOLO
|
4 |
import torch
|
5 |
import utils
|
6 |
|
7 |
+
import utils
|
8 |
+
from drawing import draw_keypoints
|
9 |
+
|
10 |
|
11 |
@st.cache_resource()
|
12 |
def load_model():
|
|
|
18 |
|
19 |
|
20 |
def draw_output(image_pil: Image.Image, keypoints: dict):
|
21 |
+
output_image = draw_keypoints(image_pil, keypoints).convert("RGB")
|
22 |
+
return output_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
|
25 |
st.title('Pose Estimation App')
|
|
|
43 |
# predict
|
44 |
with st.spinner('Predicting...'):
|
45 |
model = load_model()
|
46 |
+
result = model(img)[0]
|
47 |
st.markdown('**Results:**')
|
48 |
+
keypoints = utils.get_keypoints(result)
|
49 |
if keypoints is not None:
|
50 |
img = draw_output(img, keypoints)
|
51 |
st.image(img, caption='Predicted image', use_column_width=True)
|
52 |
+
# calculate angles
|
53 |
lea, rea = utils.get_eye_angles(keypoints)
|
54 |
lba, rba = utils.get_elbow_angles(keypoints)
|
55 |
+
angles = {'left_eye_angle': lea, 'right_eye_angle': rea, 'left_elbow_angle': lba, 'right_elbow_angle': rba}
|
56 |
st.write('Angles:')
|
57 |
+
st.json(angles)
|
58 |
st.write('Raw keypoints:')
|
59 |
st.json(keypoints)
|
60 |
else:
|
start.py
DELETED
@@ -1,3 +0,0 @@
|
|
1 |
-
import subprocess
|
2 |
-
|
3 |
-
subprocess.run("uvicorn api:app --host 0.0.0.0 --port 7860", shell=True)
|
|
|
|
|
|
|
|
test.py
DELETED
@@ -1,7 +0,0 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
|
3 |
-
app = FastAPI()
|
4 |
-
|
5 |
-
@app.get("/")
|
6 |
-
def read_root():
|
7 |
-
return {"Hello": "World!"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|