Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
def greet(name):
|
| 5 |
return "Hello " + name + "!!"
|
|
@@ -15,27 +87,6 @@ examples = [ # need to manually delete cache everytime new examples are added
|
|
| 15 |
["clip2_-1450-_jpg.jpg"],
|
| 16 |
["clip2_-539-_jpg.jpg"]]
|
| 17 |
|
| 18 |
-
def speclab(img):
|
| 19 |
-
|
| 20 |
-
# initialize the model
|
| 21 |
-
model = torch.hub.load('SamDaaLamb/ValorantTracker', 'srdetect', force_reload=True) # for some reasons loads the model in src rather than demo
|
| 22 |
-
model.eval()
|
| 23 |
-
|
| 24 |
-
# preprocess image to be used as input
|
| 25 |
-
transforms = A.Compose([
|
| 26 |
-
A.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
|
| 27 |
-
ToTensorV2()
|
| 28 |
-
])
|
| 29 |
-
input = transforms(image=img)['image']
|
| 30 |
-
input = input.unsqueeze(0)
|
| 31 |
-
|
| 32 |
-
# model prediction
|
| 33 |
-
output = model(input)
|
| 34 |
-
|
| 35 |
-
# overlay output onto original image
|
| 36 |
-
img[output==255] = [0, 255, 0]
|
| 37 |
-
|
| 38 |
-
return img
|
| 39 |
|
| 40 |
# define app features and run
|
| 41 |
title = "SpecLab Demo"
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
|
| 5 |
+
import cv2
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import numpy as np
|
| 8 |
+
import requests
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
from super_gradients.common.object_names import Models
|
| 13 |
+
from super_gradients.training import models
|
| 14 |
+
from super_gradients.training.utils.visualization.detection import draw_bbox
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# Initialize your pose estimation model
|
| 18 |
+
yolo_nas_pose = models.get("best.pt",
|
| 19 |
+
num_classes=1,
|
| 20 |
+
checkpoint_path="./best.pt")
|
| 21 |
+
|
| 22 |
+
def process_and_predict(url=None,
|
| 23 |
+
image=None,
|
| 24 |
+
confidence=0.5,
|
| 25 |
+
iou=0.5):
|
| 26 |
+
# If a URL is provided, use it directly for prediction
|
| 27 |
+
if url is not None and url.strip() != "":
|
| 28 |
+
response = requests.get(url)
|
| 29 |
+
image = Image.open(BytesIO(response.content))
|
| 30 |
+
image = np.array(image)
|
| 31 |
+
result = yolo_nas_pose.predict(image, conf=confidence,iou=iou)
|
| 32 |
+
# If a file is uploaded, read it, convert it to a numpy array and use it for prediction
|
| 33 |
+
elif image is not None:
|
| 34 |
+
result = yolo_nas_pose.predict(image, conf=confidence,iou=iou)
|
| 35 |
+
else:
|
| 36 |
+
return None # If no input is provided, return None
|
| 37 |
+
|
| 38 |
+
# Extract prediction data
|
| 39 |
+
image_prediction = result._images_prediction_lst[0]
|
| 40 |
+
|
| 41 |
+
pose_data = image_prediction.prediction
|
| 42 |
+
|
| 43 |
+
# Visualize the prediction
|
| 44 |
+
output_image = PoseVisualization.draw_poses(
|
| 45 |
+
image=image_prediction.image,
|
| 46 |
+
poses=pose_data.poses,
|
| 47 |
+
boxes=pose_data.bboxes_xyxy,
|
| 48 |
+
scores=pose_data.scores,
|
| 49 |
+
is_crowd=None,
|
| 50 |
+
edge_links=pose_data.edge_links,
|
| 51 |
+
edge_colors=pose_data.edge_colors,
|
| 52 |
+
keypoint_colors=pose_data.keypoint_colors,
|
| 53 |
+
joint_thickness=2,
|
| 54 |
+
box_thickness=2,
|
| 55 |
+
keypoint_radius=5
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
blank_image = np.zeros_like(image_prediction.image)
|
| 59 |
+
|
| 60 |
+
skeleton_image = PoseVisualization.draw_poses(
|
| 61 |
+
image=blank_image,
|
| 62 |
+
poses=pose_data.poses,
|
| 63 |
+
boxes=pose_data.bboxes_xyxy,
|
| 64 |
+
scores=pose_data.scores,
|
| 65 |
+
is_crowd=None,
|
| 66 |
+
edge_links=pose_data.edge_links,
|
| 67 |
+
edge_colors=pose_data.edge_colors,
|
| 68 |
+
keypoint_colors=pose_data.keypoint_colors,
|
| 69 |
+
joint_thickness=2,
|
| 70 |
+
box_thickness=2,
|
| 71 |
+
keypoint_radius=5
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
return output_image, skeleton_image
|
| 75 |
|
| 76 |
def greet(name):
|
| 77 |
return "Hello " + name + "!!"
|
|
|
|
| 87 |
["clip2_-1450-_jpg.jpg"],
|
| 88 |
["clip2_-539-_jpg.jpg"]]
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
# define app features and run
|
| 92 |
title = "SpecLab Demo"
|