PDG commited on
Commit
17cf5c8
·
1 Parent(s): 42d70a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
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
- im2 = Image.fromarray(np.uint8(im)).convert('RGB').crop(boxes[max_idx].to(torch.int64).numpy())
84
-
85
- carResize = transforms.Compose([transforms.Resize((224, 224))])
86
- im2 = carResize(im2)
87
-
88
- with torch.no_grad():
89
- scores = torch.nn.functional.softmax(DesignModernityModel(carTransforms(im2).unsqueeze(0))[0])
90
-
91
- label = "success"
 
 
 
 
 
 
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, {LABELS[i]: float(scores[i]) for i in range(n_labels)}
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