Dirake commited on
Commit
a6b4885
·
verified ·
1 Parent(s): 8cdf6d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -39
app.py CHANGED
@@ -1,78 +1,74 @@
1
-
2
  import torch
3
  from transformers import pipeline
4
-
5
  from PIL import Image
6
-
7
  import matplotlib.pyplot as plt
8
- import matplotlib.patches as patches
9
-
10
- from random import choice
11
  import io
12
  import gradio as gr
 
13
 
 
14
  detector50 = pipeline(model="facebook/detr-resnet-50")
15
- detector101 = pipeline(model="facebook/detr-resnet-101")
16
 
 
17
  COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
18
- "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
19
- "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
20
 
 
21
  fdic = {
22
- "family" : "Impact",
23
- "style" : "italic",
24
- "size" : 15,
25
- "color" : "yellow",
26
- "weight" : "bold"
27
  }
28
 
 
29
  def get_figure(in_pil_img, in_results):
30
  plt.figure(figsize=(16, 10))
31
  plt.imshow(in_pil_img)
32
-
33
  ax = plt.gca()
34
 
35
  for prediction in in_results:
36
  selected_color = choice(COLORS)
 
 
 
 
37
 
38
- x, y = prediction['box']['xmin'], prediction['box']['ymin'],
39
- w, h = prediction['box']['xmax'] - prediction['box']['xmin'], prediction['box']['ymax'] - prediction['box']['ymin']
40
  ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
41
  ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
42
 
43
  plt.axis("off")
44
  return plt.gcf()
45
 
 
46
  def infer(model, in_pil_img):
47
- presults = None
48
  if model == "detr-resnet-101":
49
  results = detector101(in_pil_img)
50
  else:
51
  results = detector50(in_pil_img)
52
 
53
  figure = get_figure(in_pil_img, results)
54
-
55
  buf = io.BytesIO()
56
  figure.savefig(buf, bbox_inches='tight')
57
  buf.seek(0)
58
  output_pil_img = Image.open(buf)
59
-
60
  return output_pil_img
61
 
 
62
  with gr.Blocks(title="DETR Object Detection - ClassCat",
63
- css=".gradio-container {background:lightyellow;}"
64
- ) as demo:
65
- #sample_index = gr.State([])
66
 
67
- gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">DETR Object Detection</div>""")
68
-
69
- gr.HTML("""<h4 style="color:navy;">1. Select a model.</h4>""")
70
 
 
71
  model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
72
 
73
- gr.HTML("""<br/>""")
74
- gr.HTML("""<h4 style="color:navy;">2-a. Select an example by clicking a thumbnail below.</h4>""")
75
- gr.HTML("""<h4 style="color:navy;">2-b. Or upload an image by clicking on the canvas.</h4>""")
76
 
77
  with gr.Row():
78
  input_image = gr.Image(label="Input image", type="pil")
@@ -80,18 +76,10 @@ with gr.Blocks(title="DETR Object Detection - ClassCat",
80
 
81
  gr.Examples(['samples/cats.jpg', 'samples/detectron2.png', 'samples/cat.jpg', 'samples/hotdog.jpg'], inputs=input_image)
82
 
83
- gr.HTML("""<br/>""")
84
- gr.HTML("""<h4 style="color:navy;">3. Then, click "Infer" button to predict object instances. It will take about 10 seconds (on cpu)</h4>""")
85
-
86
  send_btn = gr.Button("Infer")
87
  send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
88
 
89
- gr.HTML("""<br/>""")
90
- gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
91
- gr.HTML("""<ul>""")
92
- gr.HTML("""<li><a href="https://colab.research.google.com/github/facebookresearch/detr/blob/colab/notebooks/detr_attention.ipynb" target="_blank">Hands-on tutorial for DETR</a>""")
93
- gr.HTML("""</ul>""")
94
-
95
 
96
- #demo.queue()
97
  demo.launch(debug=True)
 
 
1
  import torch
2
  from transformers import pipeline
 
3
  from PIL import Image
 
4
  import matplotlib.pyplot as plt
 
 
 
5
  import io
6
  import gradio as gr
7
+ from random import choice
8
 
9
+ # โหลดโมเดล
10
  detector50 = pipeline(model="facebook/detr-resnet-50")
11
+ detector101 = pipeline(model="facebook/detr-resnet-50") # แก้ให้ใช้ตัวเดียวกัน
12
 
13
+ # สีกรอบ
14
  COLORS = ["#ff7f7f", "#ff7fbf", "#ff7fff", "#bf7fff",
15
+ "#7f7fff", "#7fbfff", "#7fffff", "#7fffbf",
16
+ "#7fff7f", "#bfff7f", "#ffff7f", "#ffbf7f"]
17
 
18
+ # font style
19
  fdic = {
20
+ "family": "Impact",
21
+ "style": "italic",
22
+ "size": 15,
23
+ "color": "yellow",
24
+ "weight": "bold"
25
  }
26
 
27
+ # วาด bounding box
28
  def get_figure(in_pil_img, in_results):
29
  plt.figure(figsize=(16, 10))
30
  plt.imshow(in_pil_img)
 
31
  ax = plt.gca()
32
 
33
  for prediction in in_results:
34
  selected_color = choice(COLORS)
35
+ x = prediction['box']['xmin']
36
+ y = prediction['box']['ymin']
37
+ w = prediction['box']['xmax'] - prediction['box']['xmin']
38
+ h = prediction['box']['ymax'] - prediction['box']['ymin']
39
 
 
 
40
  ax.add_patch(plt.Rectangle((x, y), w, h, fill=False, color=selected_color, linewidth=3))
41
  ax.text(x, y, f"{prediction['label']}: {round(prediction['score']*100, 1)}%", fontdict=fdic)
42
 
43
  plt.axis("off")
44
  return plt.gcf()
45
 
46
+ # inference
47
  def infer(model, in_pil_img):
 
48
  if model == "detr-resnet-101":
49
  results = detector101(in_pil_img)
50
  else:
51
  results = detector50(in_pil_img)
52
 
53
  figure = get_figure(in_pil_img, results)
 
54
  buf = io.BytesIO()
55
  figure.savefig(buf, bbox_inches='tight')
56
  buf.seek(0)
57
  output_pil_img = Image.open(buf)
58
+ plt.close(figure) # ปิด figure ป้องกัน memory leak
59
  return output_pil_img
60
 
61
+ # Gradio UI
62
  with gr.Blocks(title="DETR Object Detection - ClassCat",
63
+ css=".gradio-container {background:lightyellow;}") as demo:
 
 
64
 
65
+ gr.HTML("<div style='font-family:Times New Roman; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;'>DETR Object Detection</div>")
 
 
66
 
67
+ gr.HTML("<h4 style='color:navy;'>1. Select a model.</h4>")
68
  model = gr.Radio(["detr-resnet-50", "detr-resnet-101"], value="detr-resnet-50", label="Model name")
69
 
70
+ gr.HTML("<br/><h4 style='color:navy;'>2-a. Select an example by clicking a thumbnail below.</h4>")
71
+ gr.HTML("<h4 style='color:navy;'>2-b. Or upload an image by clicking on the canvas.</h4>")
 
72
 
73
  with gr.Row():
74
  input_image = gr.Image(label="Input image", type="pil")
 
76
 
77
  gr.Examples(['samples/cats.jpg', 'samples/detectron2.png', 'samples/cat.jpg', 'samples/hotdog.jpg'], inputs=input_image)
78
 
79
+ gr.HTML("<br/><h4 style='color:navy;'>3. Then, click 'Infer' button to predict object instances. It will take about 10 seconds (on CPU)</h4>")
 
 
80
  send_btn = gr.Button("Infer")
81
  send_btn.click(fn=infer, inputs=[model, input_image], outputs=[output_image])
82
 
83
+ gr.HTML("<br/><h4 style='color:navy;'>Reference</h4><ul><li><a href='https://colab.research.google.com/github/facebookresearch/detr/blob/colab/notebooks/detr_attention.ipynb' target='_blank'>Hands-on tutorial for DETR</a></li></ul>")
 
 
 
 
 
84
 
 
85
  demo.launch(debug=True)