haya-alwarthan commited on
Commit
4ee0032
·
1 Parent(s): eac0473

Add instance segm

Browse files
Files changed (1) hide show
  1. app.py +28 -21
app.py CHANGED
@@ -47,25 +47,20 @@ def convert_to_pairs(three):
47
 
48
 
49
  #function to output binary mask img
50
- def binary_mask_img(prediction,rec_img):
51
  w= rec_img.shape[0]
52
  h=rec_img.shape[1]
53
- # colors={"LOWER_BONE":"#fabee6","UPPER_BONE":"#96e7e6","MIDDLE_BONE":"#fffa5b"}
54
- colors={"LOWER_BONE":"w","UPPER_BONE":"w","MIDDLE_BONE":"w"}
55
  figure, axes = plt.subplots(figsize =(h/100.0,w/100.0))
56
- img = np.zeros((w, h), dtype = np.float64)
57
  for prediction in prediction["predictions"]:
58
  points = [[p["x"], p["y"]] for p in prediction["points"]]
59
  polygon = patches.Polygon(
60
- points, linewidth=2, edgecolor=colors[prediction["class"]],facecolor= to_rgba(colors[prediction["class"]],1),zorder=2
61
  )
62
  axes.add_patch(polygon)
63
- ax=plt.gca()
64
- ax.invert_yaxis()
65
- plt.style.use("dark_background")
66
- plt.axis("off") # turns off axes
67
- plt.axis("tight") # gets rid of white border
68
- plt.axis("image")
69
  plt.tight_layout()
70
  canvas = plt.get_current_fig_manager().canvas
71
  canvas.draw()
@@ -77,6 +72,16 @@ def binary_mask_img(prediction,rec_img):
77
  gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
78
  return gray_image
79
 
 
 
 
 
 
 
 
 
 
 
80
  def extend_line(p1, p2, distance=10000):
81
  diff = np.arctan2(p1[1] - p2[1], p1[0] - p2[0])
82
  p3_x = int(p1[0] + distance*np.cos(diff))
@@ -84,20 +89,21 @@ def extend_line(p1, p2, distance=10000):
84
  p4_x = int(p1[0] - distance*np.cos(diff))
85
  p4_y = int(p1[1] - distance*np.sin(diff))
86
  return ((p3_x, p3_y), (p4_x, p4_y))
87
- def visualize(image,pred):
88
 
 
 
89
  pred=[int(i) for i in pred]
90
  (p1_left,p2_left)=extend_line((pred[6],pred[7]),(pred[4],pred[5]))
91
  (p1_h,p2_h)=extend_line((pred[4],pred[5]),(pred[2],pred[3]))
92
  (p1_right,p2_right)=extend_line((pred[2],pred[3]),(pred[0],pred[1]))
93
- image= cv2.line(image, p1_left, (pred[4],pred[5]),(152, 216, 170),2)
94
- image= cv2.line(image, p1_h, p2_h,(152, 216, 170),2)
95
- image= cv2.line(image, (pred[2],pred[3]), p2_right,(152, 216, 170),2)
96
 
97
  for i in range(0,7,2):
98
-
99
- image = cv2.circle(image, (round(pred[i]),round(pred[i+1])), int ((image.shape[0]+image.shape[1])/150), (255, 150, 128), -1)
100
- return image
101
 
102
 
103
 
@@ -142,14 +148,15 @@ kp_predictor = DefaultPredictor(kp_cfg)
142
 
143
  def predict_fn(img_path):
144
  #Read and tranform input image
145
- p=model.predict(img_path).json()
146
  og_img=cv2.imread(img_path)
147
- img=binary_mask_img(p,og_img)
148
  outputs = kp_predictor(img) # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
149
  print("outputs==".format(outputs["instances"].to("cpu")))
150
  p=np.asarray(outputs["instances"].to("cpu").pred_keypoints, dtype='float32')[0].reshape(-1)
151
  pairss=convert_to_pairs(p)
152
- output=visualize(img,pairss)
 
153
  return output
154
 
155
 
 
47
 
48
 
49
  #function to output binary mask img
50
+ def segm_imf(prediction,rec_img):
51
  w= rec_img.shape[0]
52
  h=rec_img.shape[1]
53
+ colors={"LOWER_BONE":"#fabee6","UPPER_BONE":"#96e7e6","MIDDLE_BONE":"#fffa5b"}
54
+ # colors={"LOWER_BONE":(253,226,243),"UPPER_BONE":(173,228,219),"MIDDLE_BONE": (253,247,195)}
55
  figure, axes = plt.subplots(figsize =(h/100.0,w/100.0))
 
56
  for prediction in prediction["predictions"]:
57
  points = [[p["x"], p["y"]] for p in prediction["points"]]
58
  polygon = patches.Polygon(
59
+ points, linewidth=2, edgecolor=colors[prediction["class"]],facecolor= to_rgba(colors[prediction["class"]],0.4)
60
  )
61
  axes.add_patch(polygon)
62
+ plt.imshow(rec_img)
63
+ plt.axis("off")
 
 
 
 
64
  plt.tight_layout()
65
  canvas = plt.get_current_fig_manager().canvas
66
  canvas.draw()
 
72
  gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
73
  return gray_image
74
 
75
+ def binary_cv2(rec_img,prediction):
76
+ w= rec_img.shape[0]
77
+ h=rec_img.shape[1]
78
+ mask = np.zeros((w, h), dtype = np.float64)
79
+ for prediction in prediction["predictions"]:
80
+ points = [[p["x"], p["y"]] for p in prediction["points"]]
81
+ cv2.fillPoly(mask, np.array([points]).astype(np.int32), color=(255, 0, 0))
82
+ masked_gray = cv2.merge((mask,mask,mask))
83
+ return masked_gray
84
+
85
  def extend_line(p1, p2, distance=10000):
86
  diff = np.arctan2(p1[1] - p2[1], p1[0] - p2[0])
87
  p3_x = int(p1[0] + distance*np.cos(diff))
 
89
  p4_x = int(p1[0] - distance*np.cos(diff))
90
  p4_y = int(p1[1] - distance*np.sin(diff))
91
  return ((p3_x, p3_y), (p4_x, p4_y))
 
92
 
93
+ def visualize(image,pred):
94
+ op_img=image.copy()
95
  pred=[int(i) for i in pred]
96
  (p1_left,p2_left)=extend_line((pred[6],pred[7]),(pred[4],pred[5]))
97
  (p1_h,p2_h)=extend_line((pred[4],pred[5]),(pred[2],pred[3]))
98
  (p1_right,p2_right)=extend_line((pred[2],pred[3]),(pred[0],pred[1]))
99
+ op_img= cv2.line(op_img, p1_left, (pred[4],pred[5]),(152, 216, 170),1)
100
+ op_img= cv2.line(op_img, p1_h, p2_h,(152, 216, 170),1)
101
+ op_img= cv2.line(op_img, (pred[2],pred[3]), p2_right,(152, 216, 170),1)
102
 
103
  for i in range(0,7,2):
104
+ op_img = cv2.circle(op_img, (round(pred[i]),round(pred[i+1])), int ((image.shape[0]+image.shape[1])/150), (255, 150, 128), -1)
105
+ return op_img
106
+
107
 
108
 
109
 
 
148
 
149
  def predict_fn(img_path):
150
  #Read and tranform input image
151
+ preds=model.predict(img_path).json()
152
  og_img=cv2.imread(img_path)
153
+ img=binary_cv2(og_img,preds)
154
  outputs = kp_predictor(img) # format is documented at https://detectron2.readthedocs.io/tutorials/models.html#model-output-format
155
  print("outputs==".format(outputs["instances"].to("cpu")))
156
  p=np.asarray(outputs["instances"].to("cpu").pred_keypoints, dtype='float32')[0].reshape(-1)
157
  pairss=convert_to_pairs(p)
158
+ segm=segm_imf(preds,og_img)
159
+ output=visualize(segm,pairss)
160
  return output
161
 
162