oldgarden21 commited on
Commit
3de8d4c
ยท
verified ยท
1 Parent(s): 0bfe488

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -14
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import gradio as gr
2
- from PIL import Image
3
- import requests
4
  from transformers import DetrForObjectDetection, DetrFeatureExtractor
5
- import torch
6
 
7
  # ๋ชจ๋ธ๊ณผ ํ”ผ์ฒ˜ ์ถ”์ถœ๊ธฐ ๋กœ๋“œ
8
  model_id = "facebook/detr-resnet-50"
@@ -10,24 +8,23 @@ model = DetrForObjectDetection.from_pretrained(model_id)
10
  feature_extractor = DetrFeatureExtractor.from_pretrained(model_id)
11
 
12
  def object_detection(image):
13
- # ์ด๋ฏธ์ง€์—์„œ ํ”ผ์ฒ˜ ์ถ”์ถœ
14
  inputs = feature_extractor(images=image, return_tensors="pt")
15
- # ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•œ ์˜ˆ์ธก
16
  outputs = model(**inputs)
17
 
18
- # ์˜ˆ์ธก ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ
19
- # outputs๋Š” logits์™€ ๊ธฐํƒ€ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋ฏ€๋กœ, ๊ฒฐ๊ณผ๋ฅผ ํ•ด์„ํ•˜์—ฌ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ค„ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
20
- # ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ด๋ฏธ์ง€ ์œ„์— ๋ฐ•์Šค๋กœ ๊ทธ๋ฆฌ๋Š” ๋“ฑ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
21
- result = feature_extractor.post_process_object_detection(outputs, threshold=0.9)[0]
22
- return result
23
 
24
- # Gradio ์•ฑ ์ •์˜
25
  iface = gr.Interface(
26
  fn=object_detection,
27
- inputs=gr.inputs.Image(type="pil"),
28
- outputs=[gr.outputs.Image(type="pil"), gr.outputs.Label(num_top_classes=3)],
29
  title="Object Detection with DETR",
30
- description="Upload an image and the model will detect objects in the image."
31
  )
32
 
33
  iface.launch()
 
1
  import gradio as gr
 
 
2
  from transformers import DetrForObjectDetection, DetrFeatureExtractor
3
+ from PIL import Image
4
 
5
  # ๋ชจ๋ธ๊ณผ ํ”ผ์ฒ˜ ์ถ”์ถœ๊ธฐ ๋กœ๋“œ
6
  model_id = "facebook/detr-resnet-50"
 
8
  feature_extractor = DetrFeatureExtractor.from_pretrained(model_id)
9
 
10
  def object_detection(image):
11
+ # ์ด๋ฏธ์ง€๋ฅผ ๋ชจ๋ธ์ด ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜
12
  inputs = feature_extractor(images=image, return_tensors="pt")
 
13
  outputs = model(**inputs)
14
 
15
+ # ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.
16
+ # ์˜ˆ์ œ ์ฝ”๋“œ๋Š” ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ๋ถ€๋ถ„์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
17
+ # ์‹ค์ œ๋กœ๋Š” ํƒ์ง€๋œ ๊ฐ์ฒด๋ฅผ ์ด๋ฏธ์ง€ ์œ„์— ๋ฐ•์Šค๋กœ ๊ทธ๋ฆฌ๋Š” ๋“ฑ์˜ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
18
+ # ์—ฌ๊ธฐ์„œ๋Š” outputs์˜ ์š”์•ฝ๋œ ์ •๋ณด๋งŒ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
19
+ return outputs.logits.tolist()
20
 
21
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์„ค์ • ๋ฐ ์•ฑ ์‹คํ–‰
22
  iface = gr.Interface(
23
  fn=object_detection,
24
+ inputs=gr.Image(type="pil", tool="editor"),
25
+ outputs=gr.Text(),
26
  title="Object Detection with DETR",
27
+ description="Upload an image, and the model will detect objects in the image."
28
  )
29
 
30
  iface.launch()