PolarisFTL commited on
Commit
4fa071a
·
verified ·
1 Parent(s): bb3ea01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -38
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import torch
2
  from PIL import Image
3
  import matplotlib.pyplot as plt
4
- import matplotlib.patches as patches
5
  import io
6
  from random import choice
7
  import gradio as gr
@@ -9,27 +8,22 @@ from yolo import YOLO
9
 
10
  yolo = YOLO()
11
 
12
- def predict_single_image(image, crop=False, count=True):
13
- try:
14
- r_image = yolo.detect_image(image, crop=crop, count=count)
15
- return r_image
16
- except Exception as e:
17
- return str(e)
18
-
19
  COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
20
- "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
21
- "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
22
 
 
23
  fdic = {
24
- "family" : "DejaVu Serif",
25
- "style" : "normal",
26
- "size" : 18,
27
- "color" : "yellow",
28
- "weight" : "bold"
29
  }
30
 
31
-
32
  def get_figure(in_pil_img, in_results):
 
33
  plt.figure(figsize=(16, 10))
34
  plt.imshow(in_pil_img)
35
  ax = plt.gca()
@@ -37,32 +31,29 @@ def get_figure(in_pil_img, in_results):
37
  for score, label, box in zip(in_results["scores"], in_results["labels"], in_results["boxes"]):
38
  selected_color = choice(COLORS)
39
 
40
- box_int = [i.item() for i in torch.round(box).to(torch.int32)]
41
- x, y, w, h = box_int[0], box_int[1], box_int[2]-box_int[0], box_int[3]-box_int[1]
42
- #x, y, w, h = torch.round(box[0]).item(), torch.round(box[1]).item(), torch.round(box[2]-box[0]).item(), torch.round(box[3]-box[1]).item()
43
 
44
  ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3, alpha=0.8))
45
- ax.text(x, y, 'MASFNet')
46
 
47
  plt.axis("off")
48
-
49
  return plt.gcf()
50
 
 
 
 
 
 
 
51
 
52
-
53
- with gr.Blocks(title="MASFNet Object Detection",
54
- css=".gradio-container {background:lightyellow;}"
55
- ) as demo:
56
- #sample_index = gr.State([])
57
-
58
-
59
- with gr.Row():
60
- input_image = gr.Image(label="Input image", type="pil")
61
- output_image = gr.Image(label="Output image with predicted instances", type="pil")
62
-
63
- gr.Examples(['img/1.png', 'img/2.png'], inputs=input_image)
64
-
65
- send_btn = gr.Button("Predict")
66
-
67
- #demo.queue()
68
- demo.launch(debug=True)
 
1
  import torch
2
  from PIL import Image
3
  import matplotlib.pyplot as plt
 
4
  import io
5
  from random import choice
6
  import gradio as gr
 
8
 
9
  yolo = YOLO()
10
 
11
+ # Colors for bounding boxes
 
 
 
 
 
 
12
  COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
13
+ "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
14
+ "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
15
 
16
+ # Font dictionary for text annotations
17
  fdic = {
18
+ "family": "DejaVu Serif",
19
+ "style": "normal",
20
+ "size": 18,
21
+ "color": "yellow",
22
+ "weight": "bold"
23
  }
24
 
 
25
  def get_figure(in_pil_img, in_results):
26
+ """ Function to generate figure with bounding boxes and labels """
27
  plt.figure(figsize=(16, 10))
28
  plt.imshow(in_pil_img)
29
  ax = plt.gca()
 
31
  for score, label, box in zip(in_results["scores"], in_results["labels"], in_results["boxes"]):
32
  selected_color = choice(COLORS)
33
 
34
+ box_int = [int(i.item()) for i in torch.round(box)]
35
+ x, y, w, h = box_int[0], box_int[1], box_int[2] - box_int[0], box_int[3] - box_int[1]
 
36
 
37
  ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3, alpha=0.8))
38
+ ax.text(x, y)
39
 
40
  plt.axis("off")
 
41
  return plt.gcf()
42
 
43
+ def predict(image):
44
+ try:
45
+ r_image = yolo.detect_image(image)
46
+ return r_image
47
+ except Exception as e:
48
+ return str(e)
49
 
50
+ # Define the Gradio interface
51
+ gr.Interface(
52
+ fn=predict,
53
+ inputs=gr.inputs.Image(label="Input image", type="pil"),
54
+ outputs=gr.outputs.Image(label="Output image with predicted instances", type="pil"),
55
+ examples=['img/1.png', 'img/2.png'],
56
+ title="MASFNet Object Detection",
57
+ theme="default",
58
+ debug=True
59
+ ).launch()