PolarisFTL commited on
Commit
138acfe
·
verified ·
1 Parent(s): 3609cb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -80
app.py CHANGED
@@ -1,86 +1,30 @@
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
7
  from yolo import YOLO
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()
30
-
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
- with gr.Blocks(title="MASFNet Object Detection",
51
- css=".gradio-container {background:lightyellow;}"
52
- ) as demo:
53
- #sample_index = gr.State([])
54
-
55
- gr.HTML("""<div style="font-family:'Times New Roman', 'Serif'; font-size:16pt; font-weight:bold; text-align:center; color:royalblue;">MASFNet Object Detection</div>""")
56
-
57
- gr.HTML("""<br/>""")
58
- gr.HTML("""<h4 style="color:navy;">a. Select an example by clicking a thumbnail below.</h4>""")
59
- gr.HTML("""<h4 style="color:navy;">b. Or upload an image by clicking on the canvas.</h4>""")
60
-
61
- with gr.Row():
62
- input_image = gr.Image(label="Input image", type="pil")
63
- output_image = gr.Image(label="Output image with predicted instances", type="pil")
64
-
65
- gr.Examples(['img/1.png', 'img/2.png'], inputs=input_image)
66
-
67
- gr.HTML("""<br/>""")
68
- gr.HTML("""<h4 style="color:navy;">3. Set a threshold value (default to 0.9)</h4>""")
69
-
70
- # threshold = gr.Slider(0, 1.0, value=0.9, label='threshold')
71
-
72
- gr.HTML("""<br/>""")
73
- gr.HTML("""<h4 style="color:navy;">4. Then, click "Infer" button to predict object instances. It will take about 10 seconds (yolos-tiny) or 20 seconds (yolos-small).</h4>""")
74
-
75
- send_btn = gr.Button("Infer")
76
- send_btn.click(fn=predict, inputs=[input_image], outputs=[output_image])
77
-
78
- gr.HTML("""<br/>""")
79
- gr.HTML("""<h4 style="color:navy;">Reference</h4>""")
80
- gr.HTML("""<ul>""")
81
- gr.HTML("""<li><a href="https://huggingface.co/docs/transformers/model_doc/yolos" target="_blank">Hugging Face Transformers - YOLOS</a>""")
82
- gr.HTML("""</ul>""")
83
-
84
-
85
- #demo.queue()
86
- demo.launch(share=True)
 
 
 
 
 
 
1
  import gradio as gr
2
  from yolo import YOLO
3
 
4
  yolo = YOLO()
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def predict(image):
7
+ r_image = yolo.detect_image(image)
8
+ return r_image
9
+
10
+
11
+ title = "MASFNet: Multi-scale Adaptive Sampling Fusion Network for Object Detection in Adverse Weather"
12
+ description = """
13
+ The bot was trained to answer questions based on Rick and Morty dialogues. Ask Rick anything!
14
+ <img src="https://huggingface.co/spaces/course-demos/Rick_and_Morty_QA/resolve/main/rick.png" width=200px>
15
+ """
16
+
17
+ article = "Check out [the original Rick and Morty Bot](https://huggingface.co/spaces/kingabzpro/Rick_and_Morty_Bot) that this demo is based off of."
18
+
19
+ gr.Interface(
20
+ fn=predict,
21
+ inputs=gr.inputs.Image(type="file", label="Upload an Image"),
22
+ outputs=gr.outputs.Image("Prediction Result"),
23
+ title=title,
24
+ description=description,
25
+ article=article,
26
+ examples=[
27
+ ["img/1.png"],
28
+ ["img/2.png"]
29
+ ]
30
+ ).launch()