Spaces:
Sleeping
Sleeping
Miguel Cid Flor commited on
Commit ·
65f6a85
1
Parent(s): 92ca8c0
receiving an image and predicting it
Browse files- Models.py +291 -0
- PreProcessor.py +260 -0
- app.py +41 -5
- posev0.01126.pth +3 -0
- yolov8n.pt +3 -0
Models.py
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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ms as transforms
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# First Model
|
| 12 |
+
|
| 13 |
+
# In[ ]:
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
class PoseNetV1(nn.Module):
|
| 17 |
+
def __init__(self):
|
| 18 |
+
super(PoseNetV1, self).__init__()
|
| 19 |
+
self.conv = nn.Sequential(
|
| 20 |
+
|
| 21 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
|
| 22 |
+
nn.ReLU(),
|
| 23 |
+
nn.MaxPool2d(2, 2), # 112x112
|
| 24 |
+
|
| 25 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 26 |
+
nn.ReLU(),
|
| 27 |
+
nn.MaxPool2d(2, 2), # 56x56
|
| 28 |
+
|
| 29 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 30 |
+
nn.ReLU(),
|
| 31 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 32 |
+
|
| 33 |
+
)
|
| 34 |
+
self.fc = nn.Sequential(
|
| 35 |
+
nn.Flatten(),
|
| 36 |
+
nn.Linear(512 * 14 * 14, 512),
|
| 37 |
+
nn.ReLU(),
|
| 38 |
+
nn.Dropout(0.3),
|
| 39 |
+
nn.Linear(512, 32)
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
def forward(self, x):
|
| 43 |
+
x = self.conv(x)
|
| 44 |
+
x = self.fc(x)
|
| 45 |
+
return x
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Here, we added one more layer and we added Dropout to the fully connected layer. We also added a Flatten layer to flatten the output of the convolutional layers before passing it to the fully connected layers.
|
| 49 |
+
|
| 50 |
+
# In[ ]:
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class PoseNetV2(nn.Module):
|
| 54 |
+
def __init__(self):
|
| 55 |
+
super(PoseNetV2, self).__init__()
|
| 56 |
+
self.conv = nn.Sequential(
|
| 57 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
|
| 58 |
+
nn.ReLU(),
|
| 59 |
+
nn.MaxPool2d(2, 2), # 112x112
|
| 60 |
+
|
| 61 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 62 |
+
nn.ReLU(),
|
| 63 |
+
nn.MaxPool2d(2, 2), # 56x56
|
| 64 |
+
|
| 65 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 66 |
+
nn.ReLU(),
|
| 67 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 68 |
+
|
| 69 |
+
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
|
| 70 |
+
nn.ReLU(),
|
| 71 |
+
nn.MaxPool2d(2, 2), # 14x14
|
| 72 |
+
|
| 73 |
+
)
|
| 74 |
+
self.fc = nn.Sequential(
|
| 75 |
+
nn.Flatten(),
|
| 76 |
+
nn.Linear(256 * 14 * 14, 512),
|
| 77 |
+
nn.ReLU(),
|
| 78 |
+
nn.Dropout(0.3),
|
| 79 |
+
nn.Linear(512, 32)
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
def forward(self, x):
|
| 83 |
+
x = self.conv(x)
|
| 84 |
+
x = self.fc(x)
|
| 85 |
+
return x
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# In[ ]:
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
class PoseNetV3(nn.Module):
|
| 92 |
+
def __init__(self):
|
| 93 |
+
super(PoseNetV3, self).__init__()
|
| 94 |
+
self.conv = nn.Sequential(
|
| 95 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
|
| 96 |
+
nn.ReLU(),
|
| 97 |
+
nn.MaxPool2d(2, 2), # 112x112
|
| 98 |
+
|
| 99 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 100 |
+
nn.ReLU(),
|
| 101 |
+
nn.MaxPool2d(2, 2), # 56x56
|
| 102 |
+
|
| 103 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 104 |
+
nn.ReLU(),
|
| 105 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 106 |
+
|
| 107 |
+
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
|
| 108 |
+
nn.ReLU(),
|
| 109 |
+
nn.MaxPool2d(2, 2), # 14x14
|
| 110 |
+
)
|
| 111 |
+
self.fc = nn.Sequential(
|
| 112 |
+
nn.Flatten(),
|
| 113 |
+
nn.Linear(256 * 14 * 14, 512),
|
| 114 |
+
nn.ReLU(),
|
| 115 |
+
nn.Dropout(0.3),
|
| 116 |
+
nn.Linear(512, 32)
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
def forward(self, x):
|
| 120 |
+
x = self.conv(x)
|
| 121 |
+
x = self.fc(x)
|
| 122 |
+
return x
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
# We added batch normalization in each layer, Adaptive Pooling and a Tahn function at the end of the fully conected layers
|
| 126 |
+
|
| 127 |
+
# In[ ]:
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
class PoseNetV4(nn.Module):
|
| 131 |
+
def __init__(self):
|
| 132 |
+
super(PoseNetV4, self).__init__()
|
| 133 |
+
self.conv = nn.Sequential(
|
| 134 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
|
| 135 |
+
nn.BatchNorm2d(32),
|
| 136 |
+
nn.ReLU(),
|
| 137 |
+
nn.MaxPool2d(2, 2), # 112x112
|
| 138 |
+
|
| 139 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 140 |
+
nn.BatchNorm2d(64),
|
| 141 |
+
nn.ReLU(),
|
| 142 |
+
nn.MaxPool2d(2, 2), # 56x56
|
| 143 |
+
|
| 144 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 145 |
+
nn.BatchNorm2d(128),
|
| 146 |
+
nn.ReLU(),
|
| 147 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 148 |
+
|
| 149 |
+
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
|
| 150 |
+
nn.BatchNorm2d(256),
|
| 151 |
+
nn.ReLU(),
|
| 152 |
+
nn.AdaptiveAvgPool2d((7, 7)) # Adaptive pooling to make output size consistent
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
self.fc = nn.Sequential(
|
| 156 |
+
nn.Flatten(),
|
| 157 |
+
nn.Linear(256 * 7 * 7, 512),
|
| 158 |
+
nn.ReLU(),
|
| 159 |
+
nn.Dropout(0.4), # Increased dropout to prevent overfitting
|
| 160 |
+
nn.Linear(512, 32),
|
| 161 |
+
nn.Tanh() # Normalizing keypoint predictions
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
def forward(self, x):
|
| 165 |
+
x = self.conv(x)
|
| 166 |
+
x = self.fc(x)
|
| 167 |
+
return x
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
# 4 Layers -> 5 Layers
|
| 171 |
+
#
|
| 172 |
+
# Tahn() -> Sigmoid()
|
| 173 |
+
|
| 174 |
+
# In[ ]:
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
class PoseNetV5(nn.Module):
|
| 178 |
+
def __init__(self):
|
| 179 |
+
super(PoseNetV5, self).__init__()
|
| 180 |
+
self.conv = nn.Sequential(
|
| 181 |
+
nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1),
|
| 182 |
+
nn.BatchNorm2d(32),
|
| 183 |
+
nn.ReLU(),
|
| 184 |
+
nn.MaxPool2d(2, 2), # 112x112
|
| 185 |
+
|
| 186 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 187 |
+
nn.BatchNorm2d(64),
|
| 188 |
+
nn.ReLU(),
|
| 189 |
+
nn.MaxPool2d(2, 2), # 56x56
|
| 190 |
+
|
| 191 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 192 |
+
nn.BatchNorm2d(128),
|
| 193 |
+
nn.ReLU(),
|
| 194 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 195 |
+
|
| 196 |
+
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
|
| 197 |
+
nn.BatchNorm2d(256),
|
| 198 |
+
nn.ReLU(),
|
| 199 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 200 |
+
|
| 201 |
+
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
|
| 202 |
+
nn.BatchNorm2d(512),
|
| 203 |
+
nn.ReLU(),
|
| 204 |
+
nn.AdaptiveAvgPool2d((7, 7)) # Adaptive pooling to make output size consistent
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
self.fc = nn.Sequential(
|
| 208 |
+
nn.Flatten(),
|
| 209 |
+
nn.Linear(512 * 7 * 7, 512),
|
| 210 |
+
nn.ReLU(),
|
| 211 |
+
nn.Dropout(0.50), # Increased dropout to prevent overfitting
|
| 212 |
+
nn.Linear(512, 32),
|
| 213 |
+
nn.Sigmoid() # Normalizing keypoint predictions
|
| 214 |
+
)
|
| 215 |
+
|
| 216 |
+
def forward(self, x):
|
| 217 |
+
x = self.conv(x)
|
| 218 |
+
x = self.fc(x)
|
| 219 |
+
return x
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# In[ ]:
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
class ResidualBlock(nn.Module):
|
| 226 |
+
def __init__(self, in_channels, out_channels):
|
| 227 |
+
super(ResidualBlock, self).__init__()
|
| 228 |
+
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
|
| 229 |
+
self.bn1 = nn.BatchNorm2d(out_channels)
|
| 230 |
+
self.relu = nn.ReLU()
|
| 231 |
+
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
|
| 232 |
+
self.bn2 = nn.BatchNorm2d(out_channels)
|
| 233 |
+
|
| 234 |
+
# Skip connection (identity mapping)
|
| 235 |
+
self.shortcut = nn.Sequential()
|
| 236 |
+
if in_channels != out_channels:
|
| 237 |
+
self.shortcut = nn.Sequential(
|
| 238 |
+
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0),
|
| 239 |
+
nn.BatchNorm2d(out_channels)
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
def forward(self, x):
|
| 243 |
+
out = self.relu(self.bn1(self.conv1(x)))
|
| 244 |
+
out = self.bn2(self.conv2(out))
|
| 245 |
+
out += self.shortcut(x) # Adding the residual connection
|
| 246 |
+
out = self.relu(out)
|
| 247 |
+
return out
|
| 248 |
+
|
| 249 |
+
class ResPoseNet(nn.Module):
|
| 250 |
+
def __init__(self):
|
| 251 |
+
super(ResPoseNet, self).__init__()
|
| 252 |
+
# Using residual blocks for feature extraction
|
| 253 |
+
self.conv = nn.Sequential(
|
| 254 |
+
ResidualBlock(3, 32), # Initial Conv + Residual Block
|
| 255 |
+
nn.MaxPool2d(2, 2), # 112x112
|
| 256 |
+
|
| 257 |
+
ResidualBlock(32, 64), # Residual Block
|
| 258 |
+
nn.MaxPool2d(2, 2), # 56x56
|
| 259 |
+
|
| 260 |
+
ResidualBlock(64, 128), # Residual Block
|
| 261 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 262 |
+
|
| 263 |
+
ResidualBlock(128, 256), # Residual Block
|
| 264 |
+
nn.MaxPool2d(2, 2), # 28x28
|
| 265 |
+
|
| 266 |
+
ResidualBlock(256, 512), # Residual Block
|
| 267 |
+
nn.AdaptiveAvgPool2d((7, 7)) # 14x14 output
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
self.fc = nn.Sequential(
|
| 271 |
+
nn.Flatten(),
|
| 272 |
+
|
| 273 |
+
nn.Linear(512 * 7 * 7, 1024),
|
| 274 |
+
nn.ReLU(),
|
| 275 |
+
nn.Dropout(0.40),
|
| 276 |
+
|
| 277 |
+
nn.Linear(1024, 32), # Assuming 16 keypoints, each with x, y = 32 values
|
| 278 |
+
nn.Sigmoid() # Output keypoint coordinates between [0,1]
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
def forward(self, x):
|
| 282 |
+
x = self.conv(x)
|
| 283 |
+
x = self.fc(x)
|
| 284 |
+
return x
|
| 285 |
+
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
transform = transforms.Compose([
|
| 289 |
+
transforms.ToTensor(), # Convert to tensor (3, 224, 224)
|
| 290 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) # Normalize RGB
|
| 291 |
+
])
|
PreProcessor.py
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
| 13 |
+
yolo = YOLO("yolov8n.pt")
|
| 14 |
+
|
| 15 |
+
# In[59]:
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
#plt.imshow(cv2.imread("Datasets/images/000003072.jpg"))
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# In[60]:
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def resize_with_padding(points,image, target_size=(224, 224), padding_color=(0, 0, 0)):
|
| 25 |
+
h, w = image.shape[:2]
|
| 26 |
+
target_w, target_h = target_size
|
| 27 |
+
|
| 28 |
+
# Compute the scaling factor
|
| 29 |
+
scale = min(target_w / w, target_h / h)
|
| 30 |
+
new_w, new_h = int(w * scale), int(h * scale)
|
| 31 |
+
|
| 32 |
+
# Resize while maintaining aspect ratio
|
| 33 |
+
resized = cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA)
|
| 34 |
+
|
| 35 |
+
# Create a new blank image (padded) with the target size
|
| 36 |
+
padded_image = np.full((target_h, target_w, 3), padding_color, dtype=np.uint8)
|
| 37 |
+
#ajust points
|
| 38 |
+
points = [(int(x * scale + (target_w - new_w) // 2), int(y * scale + (target_h - new_h) // 2)) for x, y in points]
|
| 39 |
+
|
| 40 |
+
# Compute padding (center the image)
|
| 41 |
+
x_offset = (target_w - new_w) // 2
|
| 42 |
+
y_offset = (target_h - new_h) // 2
|
| 43 |
+
|
| 44 |
+
# Place the resized image onto the padded canvas
|
| 45 |
+
padded_image[y_offset:y_offset + new_h, x_offset:x_offset + new_w] = resized
|
| 46 |
+
|
| 47 |
+
#lambdas to reverse x and y
|
| 48 |
+
reverse = lambda lm,bm,x, y: (int((x - (target_w - new_w) // 2) / scale)+lm, int((y - (target_h - new_h) // 2) / scale)+bm)
|
| 49 |
+
|
| 50 |
+
return padded_image,points,reverse
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# In[61]:
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def get_persons(image,points):
|
| 57 |
+
|
| 58 |
+
results = yolo(image)
|
| 59 |
+
max = 0
|
| 60 |
+
crop = 0,0,0,0
|
| 61 |
+
# Get detected objects
|
| 62 |
+
i = 0
|
| 63 |
+
for result in results:
|
| 64 |
+
for box in result.boxes:
|
| 65 |
+
cls = int(box.cls[0].item()) # Get class ID
|
| 66 |
+
if cls == 0: # Class '0' is "person" in COCO dataset
|
| 67 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) #
|
| 68 |
+
if i == 0:
|
| 69 |
+
crop = x1,y1,x2,y2
|
| 70 |
+
i = 1
|
| 71 |
+
#if this area contains all the points of the person
|
| 72 |
+
sumed = sum([x1 <= x <= x2 and y1 <= y <= y2 for x, y in points])
|
| 73 |
+
if sumed > max:
|
| 74 |
+
#plt.imshow(cropped_image)
|
| 75 |
+
max = sumed
|
| 76 |
+
crop = x1,y1,x2,y2
|
| 77 |
+
return crop
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# In[62]:
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def transform_data(name,points):
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
if isinstance(name, str):
|
| 87 |
+
image = cv2.imread(path)
|
| 88 |
+
path = "Datasets/images/"+name
|
| 89 |
+
if len(points) == 0:
|
| 90 |
+
path = name
|
| 91 |
+
else:
|
| 92 |
+
image = name
|
| 93 |
+
leftmost,bottommost,rightmost,topmost = get_persons(image,points)
|
| 94 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 95 |
+
# Ensure the coordinates are within the image bounds
|
| 96 |
+
leftmost = max(leftmost, 0)
|
| 97 |
+
bottommost = max(bottommost, 0)
|
| 98 |
+
rightmost = min(rightmost, image.shape[1])
|
| 99 |
+
topmost = min(topmost, image.shape[0])
|
| 100 |
+
|
| 101 |
+
# Cut image from the points
|
| 102 |
+
image = image[bottommost:topmost, leftmost:rightmost]
|
| 103 |
+
|
| 104 |
+
# Adjust points coordinates
|
| 105 |
+
points = [(x - leftmost, y - bottommost) for x, y in points]
|
| 106 |
+
padded_image,new_points,reverse = resize_with_padding(points,image)
|
| 107 |
+
reverse_complete = lambda x, y: reverse(leftmost, bottommost,x, y )
|
| 108 |
+
return padded_image,new_points,reverse_complete
|
| 109 |
+
# Plot image
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# In[13]:
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# In[63]:
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
#df = pd.read_csv('./Datasets/mpii_human_pose.csv')
|
| 124 |
+
#df = df[df["NAME"]=="000003072.jpg"]
|
| 125 |
+
# Load image using OpenCV (convert BGR to RGB for Matplotlib)
|
| 126 |
+
|
| 127 |
+
keypoints = [
|
| 128 |
+
("r ankle_X", "r ankle_Y"),
|
| 129 |
+
("r knee_X", "r knee_Y"),
|
| 130 |
+
("r hip_X", "r hip_Y"),
|
| 131 |
+
("l hip_X", "l hip_Y"),
|
| 132 |
+
("l knee_X", "l knee_Y"),
|
| 133 |
+
("l ankle_X", "l ankle_Y"),
|
| 134 |
+
("pelvis_X", "pelvis_Y"),
|
| 135 |
+
("thorax_X", "thorax_Y"),
|
| 136 |
+
("upper neck_X", "upper neck_Y"),
|
| 137 |
+
("head top_X", "head top_Y"),
|
| 138 |
+
("r wrist_X", "r wrist_Y"),
|
| 139 |
+
("r elbow_X", "r elbow_Y"),
|
| 140 |
+
("r shoulder_X", "r shoulder_Y"),
|
| 141 |
+
("l shoulder_X", "l shoulder_Y"),
|
| 142 |
+
("l elbow_X", "l elbow_Y"),
|
| 143 |
+
("l wrist_X", "l wrist_Y")
|
| 144 |
+
]
|
| 145 |
+
|
| 146 |
+
## Select the first row (example: first image)
|
| 147 |
+
#row = df.iloc[0] # Change index for other images
|
| 148 |
+
#
|
| 149 |
+
## Convert keypoints into a list of (x, y) tuples
|
| 150 |
+
#points = [(int(row[x]), int(row[y])) for x, y in keypoints]
|
| 151 |
+
#image,points,reverse = transform_data("000003072.jpg",points)
|
| 152 |
+
## Plot image
|
| 153 |
+
#plt.imshow(image)
|
| 154 |
+
#for (x, y) in points:
|
| 155 |
+
# plt.scatter(x, y, color="red", s=30) # Red points
|
| 156 |
+
#
|
| 157 |
+
#plt.show()
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
# In[64]:
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
#original_points = [reverse(x,y) for x, y in points]
|
| 164 |
+
#plt.imshow(cv2.imread("Datasets/images/000003072.jpg"))
|
| 165 |
+
#for (x, y) in original_points:
|
| 166 |
+
# plt.scatter(x, y, color="red", s=30) # Red points
|
| 167 |
+
#
|
| 168 |
+
#plt.show()
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# In[65]:
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
# change the datasenumpy.core._exceptions._UFuncNoLoopError: ufunc 'add' did not contain a loop with signature matching types (dtype('<U16'), dtype('uint8')) -> None
|
| 175 |
+
#t using a function that will return the image with the alterations and the new points
|
| 176 |
+
def process_row(row):
|
| 177 |
+
points = [(int(row[x]), int(row[y])) for x, y in keypoints]
|
| 178 |
+
try:
|
| 179 |
+
image, points,_ = transform_data(row["NAME"], points)
|
| 180 |
+
except Exception as e:
|
| 181 |
+
print(f"Error processing row {row['ID']}: {e}")
|
| 182 |
+
row["image"] = None
|
| 183 |
+
return row
|
| 184 |
+
row["r ankle_X"], row["r ankle_Y"] = points[0]
|
| 185 |
+
row["r knee_X"], row["r knee_Y"] = points[1]
|
| 186 |
+
row["r hip_X"], row["r hip_Y"] = points[2]
|
| 187 |
+
row["l hip_X"], row["l hip_Y"] = points[3]
|
| 188 |
+
row["l knee_X"], row["l knee_Y"] = points[4]
|
| 189 |
+
row["l ankle_X"], row["l ankle_Y"] = points[5]
|
| 190 |
+
row["pelvis_X"], row["pelvis_Y"] = points[6]
|
| 191 |
+
row["thorax_X"], row["thorax_Y"] = points[7]
|
| 192 |
+
row["upper neck_X"], row["upper neck_Y"] = points[8]
|
| 193 |
+
row["head top_X"], row["head top_Y"] = points[9]
|
| 194 |
+
row["r wrist_X"], row["r wrist_Y"] = points[10]
|
| 195 |
+
row["r elbow_X"], row["r elbow_Y"] = points[11]
|
| 196 |
+
row["r shoulder_X"], row["r shoulder_Y"] = points[12]
|
| 197 |
+
row["l shoulder_X"], row["l shoulder_Y"] = points[13]
|
| 198 |
+
row["l elbow_X"], row["l elbow_Y"] = points[14]
|
| 199 |
+
row["l wrist_X"], row["l wrist_Y"] = points[15]
|
| 200 |
+
row["image"] = image
|
| 201 |
+
|
| 202 |
+
return row
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
# In[66]:
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def process_dataset(name,df,numberRows):
|
| 209 |
+
df = pd.read_csv(name)
|
| 210 |
+
df= df[(df != -1).all(axis=1)]
|
| 211 |
+
df = df[:numberRows].apply(process_row, axis=1)
|
| 212 |
+
#takes a long TIME !! for me 1h 30 min
|
| 213 |
+
|
| 214 |
+
df.to_pickle('dataset'+str(df.shape[0])+'.pkl')
|
| 215 |
+
|
| 216 |
+
return df
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
|
| 221 |
+
# In[ ]:
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
#newDF = process_dataset("./Datasets/mpii_human_pose.csv")
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
# In[ ]:
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
#row = newDF.iloc[5] # Change index for other images
|
| 231 |
+
## Convert keypoints into a list of (x, y) tuples
|
| 232 |
+
#points = [(int(row[x]), int(row[y])) for x, y in keypoints]
|
| 233 |
+
#plt.imshow(row["image"])
|
| 234 |
+
#for (x, y) in points:
|
| 235 |
+
# plt.scatter(x, y, color="red", s=30) # Red points
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
# In[ ]:
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
#get all rows that have image null
|
| 242 |
+
#df_nulls = newDF[newDF["image"].isnull()]
|
| 243 |
+
## count how mutch image nulls it has
|
| 244 |
+
#print(df_nulls.shape)
|
| 245 |
+
#row = df_nulls.iloc[0] # Change index for other images
|
| 246 |
+
#points = [(int(row[x]), int(row[y])) for x, y in keypoints]
|
| 247 |
+
#print(get_persons(cv2.imread("./Datasets/images/"+row["NAME"]),points))
|
| 248 |
+
#plt.imshow(cv2.imread("./Datasets/images/"+row["NAME"]))
|
| 249 |
+
#for (x, y) in points:
|
| 250 |
+
# plt.scatter(x, y, color="red", s=30)
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
# In[47]:
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
#df5000 = pd.read_pickle('dataset5000.pkl')
|
| 257 |
+
#df6231 = pd.read_pickle('dataset6231.pkl')
|
| 258 |
+
#df = pd.concat([df5000, df6231], ignore_index=True)
|
| 259 |
+
#df.to_pickle('dataset11231.pkl')
|
| 260 |
+
|
app.py
CHANGED
|
@@ -1,7 +1,43 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import torch # or tensorflow
|
| 8 |
+
from Models import ResPoseNet,transform
|
| 9 |
+
from PreProcessor import transform_data
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# Load your model
|
| 13 |
+
model = ResPoseNet()
|
| 14 |
+
model.load_state_dict(torch.load('posev0.01126.pth', map_location=torch.device('cpu')))
|
| 15 |
+
|
| 16 |
+
def predict_keypoints(image,model):
|
| 17 |
+
|
| 18 |
+
model.eval()
|
| 19 |
+
with torch.no_grad():
|
| 20 |
+
img_tensor = transform(image).unsqueeze(0)
|
| 21 |
+
output =model(img_tensor)*224
|
| 22 |
+
|
| 23 |
+
keypoints = output.squeeze() # Remove extra dimension if necessary
|
| 24 |
+
points = [(keypoints[i].item(), keypoints[i+1].item()) for i in range(0, len(keypoints), 2)]
|
| 25 |
+
return points
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def decode_base64_image(data):
|
| 29 |
+
header, encoded = data.split(",", 1)
|
| 30 |
+
img = Image.open(BytesIO(base64.b64decode(encoded)))
|
| 31 |
+
return np.array(img)
|
| 32 |
+
|
| 33 |
+
@app.post("/predict")
|
| 34 |
+
async def predict(request: Request):
|
| 35 |
+
data = await request.json()
|
| 36 |
+
img = decode_base64_image(data["image"])
|
| 37 |
+
processed, _ , reverse = transform_data(img,[])
|
| 38 |
+
|
| 39 |
+
results = predict(processed,model)
|
| 40 |
+
keypoints = [reverse(x,y) for x, y in results]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
return {"keypoints": keypoints.tolist()}
|
posev0.01126.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6d634be5179db3e6d93307c7bcf50d22d343db094c9aa94df34b6eecee4741db
|
| 3 |
+
size 122529293
|
yolov8n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
| 3 |
+
size 6549796
|