merve HF staff commited on
Commit
0ebcb8d
1 Parent(s): 5b6ac69

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -26,22 +26,22 @@ def infer(img):
26
 
27
  # get point prompt
28
  img_arr = np.array(point_prompt)
29
- nonzero_indices = np.nonzero(img_arr)
30
- if nonzero_indices is None:
31
- gr.Error("Please select a point.")
32
- center_x = int(np.mean(nonzero_indices[1]))
33
- center_y = int(np.mean(nonzero_indices[0]))
34
- input_point = np.array([[center_x, center_y]])
35
-
36
- input_label = np.array([1])
37
- masks, scores, logits = predictor.predict(
 
 
38
  point_coords=input_point,
39
  point_labels=input_label,
40
  )
41
-
42
-
43
- result_label = [(masks[0, :, :], "mask")]
44
- return image, result_label
45
 
46
 
47
  with gr.Blocks() as demo:
@@ -53,8 +53,7 @@ with gr.Blocks() as demo:
53
  im = gr.ImageEditor(
54
  type="pil"
55
  )
56
- submit_btn = gr.Button()
57
  output = gr.AnnotatedImage()
58
- submit_btn.click(infer, inputs=im, outputs=output)
59
 
60
  demo.launch(debug=True)
 
26
 
27
  # get point prompt
28
  img_arr = np.array(point_prompt)
29
+ if not np.any(img_arr):
30
+ gr.Error("Please select a point on top of the image.")
31
+ else:
32
+ nonzero_indices = np.nonzero(img_arr)
33
+ img_arr = np.array(point_prompt)
34
+ nonzero_indices = np.nonzero(img_arr)
35
+ center_x = int(np.mean(nonzero_indices[1]))
36
+ center_y = int(np.mean(nonzero_indices[0]))
37
+ input_point = np.array([[center_x, center_y]])
38
+ input_label = np.array([1])
39
+ masks, scores, logits = predictor.predict(
40
  point_coords=input_point,
41
  point_labels=input_label,
42
  )
43
+ result_label = [(masks[0, :, :], "mask")]
44
+ return image, result_label
 
 
45
 
46
 
47
  with gr.Blocks() as demo:
 
53
  im = gr.ImageEditor(
54
  type="pil"
55
  )
 
56
  output = gr.AnnotatedImage()
57
+ im.change(infer, inputs=im, outputs=output)
58
 
59
  demo.launch(debug=True)