Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,17 +78,22 @@ def classifyCar(im):
|
|
| 78 |
|
| 79 |
car_class_true = outputs["instances"].pred_classes == 2
|
| 80 |
boxes = list(outputs["instances"].pred_boxes[car_class_true])
|
| 81 |
-
max_idx = torch.tensor([(x[2] - x[0])*(x[3] - x[1]) for x in boxes]).argmax().item()
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
except:
|
| 93 |
label = "fail2"
|
| 94 |
#im2 = carTransforms(im).unsqueeze(0) # transform and add batch dimension
|
|
@@ -96,7 +101,7 @@ def classifyCar(im):
|
|
| 96 |
# scores = torch.nn.functional.softmax(DesignModernityModel(im2)[0])
|
| 97 |
#{LABELS[i]: float(scores[i]) for i in range(n_labels)}
|
| 98 |
#Image.fromarray(np.uint8(out.get_image())).convert('RGB')
|
| 99 |
-
return im2,
|
| 100 |
|
| 101 |
#examples = [[example_img.jpg], [example_img2.jpg]] # must be uploaded in repo
|
| 102 |
|
|
|
|
| 78 |
|
| 79 |
car_class_true = outputs["instances"].pred_classes == 2
|
| 80 |
boxes = list(outputs["instances"].pred_boxes[car_class_true])
|
|
|
|
| 81 |
|
| 82 |
+
if len(boxes) != 0:
|
| 83 |
+
max_idx = torch.tensor([(x[2] - x[0])*(x[3] - x[1]) for x in boxes]).argmax().item()
|
| 84 |
+
|
| 85 |
+
im2 = Image.fromarray(np.uint8(im)).convert('RGB').crop(boxes[max_idx].to(torch.int64).numpy())
|
| 86 |
+
|
| 87 |
+
carResize = transforms.Compose([transforms.Resize((224, 224))])
|
| 88 |
+
im2 = carResize(im2)
|
| 89 |
+
|
| 90 |
+
with torch.no_grad():
|
| 91 |
+
scores = torch.nn.functional.softmax(DesignModernityModel(carTransforms(im2).unsqueeze(0))[0])
|
| 92 |
+
label = {LABELS[i]: float(scores[i]) for i in range(n_labels)}
|
| 93 |
+
|
| 94 |
+
else:
|
| 95 |
+
im2 = Image.fromarray(np.uint8(im)).convert('RGB')
|
| 96 |
+
label = "No car detected"
|
| 97 |
except:
|
| 98 |
label = "fail2"
|
| 99 |
#im2 = carTransforms(im).unsqueeze(0) # transform and add batch dimension
|
|
|
|
| 101 |
# scores = torch.nn.functional.softmax(DesignModernityModel(im2)[0])
|
| 102 |
#{LABELS[i]: float(scores[i]) for i in range(n_labels)}
|
| 103 |
#Image.fromarray(np.uint8(out.get_image())).convert('RGB')
|
| 104 |
+
return im2, label
|
| 105 |
|
| 106 |
#examples = [[example_img.jpg], [example_img2.jpg]] # must be uploaded in repo
|
| 107 |
|