oldgarden21 commited on
Commit
262dac0
ยท
verified ยท
1 Parent(s): 8a80b35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -1,31 +1,32 @@
1
  import gradio as gr
2
  from transformers import DetrForObjectDetection, DetrFeatureExtractor
 
3
  import torch
4
 
5
- # ๋ชจ๋ธ ID ์ง€์ •
6
  model_id = "facebook/detr-resnet-50"
7
-
8
- # ๋ชจ๋ธ๊ณผ ํ”ผ์ฒ˜ ์ถ”์ถœ๊ธฐ ๋กœ๋“œ
9
  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
  outputs = model(**inputs)
16
 
17
- # ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ
18
- # ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํžˆ ๋ชจ๋ธ์˜ ์ถœ๋ ฅ logits๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
19
- # ์‹ค์ œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋Š” ์ถœ๋ ฅ์„ ์‚ฌ์šฉ์ž๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๊ฐ€๊ณตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
20
- return outputs.logits.tolist()
 
 
 
 
 
 
 
21
 
22
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ ๋ฐ ์‹คํ–‰
23
  iface = gr.Interface(
24
  fn=object_detection,
25
- inputs=gr.Image(),
26
- outputs="json",
27
  title="Object Detection with DETR",
28
- description="Upload an image and the model will detect objects in the image."
29
- )
30
-
31
- iface.launch()
 
1
  import gradio as gr
2
  from transformers import DetrForObjectDetection, DetrFeatureExtractor
3
+ from PIL import Image, ImageDraw
4
  import torch
5
 
 
6
  model_id = "facebook/detr-resnet-50"
 
 
7
  model = DetrForObjectDetection.from_pretrained(model_id)
8
  feature_extractor = DetrFeatureExtractor.from_pretrained(model_id)
9
 
10
  def object_detection(image):
 
11
  inputs = feature_extractor(images=image, return_tensors="pt")
12
  outputs = model(**inputs)
13
 
14
+ # ํ›„์ฒ˜๋ฆฌ ๊ณผ์ •
15
+ target_sizes = torch.tensor([image.size[::-1]])
16
+ results = feature_extractor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
17
+
18
+ draw = ImageDraw.Draw(image)
19
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
20
+ box = [round(i, 2) for i in box.tolist()]
21
+ draw.rectangle(box, outline="red", width=3)
22
+ draw.text((box[0], box[1]), f"{model.config.id2label[label.item()]}: {round(score.item(), 3)}", fill="red")
23
+
24
+ return image
25
 
 
26
  iface = gr.Interface(
27
  fn=object_detection,
28
+ inputs=gr.Image(type="pil"),
29
+ outputs=gr.Image(type="pil"),
30
  title="Object Detection with DETR",
31
+ description="Upload an image, and the model will detect objects in the image. Detected objects will be highlighted with bounding boxes."
32
+ ).launch()