SeoJunn commited on
Commit
bb78b14
1 Parent(s): 47d299d

Refactor : detection 코드 주석

Browse files
Files changed (1) hide show
  1. app.py +41 -39
app.py CHANGED
@@ -9,6 +9,8 @@ import torch
9
  import tensorflow as tf
10
  from PIL import ImageDraw
11
 
 
 
12
  # image segmentation 모델
13
  feature_extractor = SegformerFeatureExtractor.from_pretrained(
14
  "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
@@ -18,12 +20,12 @@ model_segmentation = TFSegformerForSemanticSegmentation.from_pretrained(
18
  )
19
 
20
  # image detection 모델
21
- processor_detection = DetrImageProcessor.from_pretrained(
22
- "facebook/detr-resnet-50"
23
- )
24
- model_detection = DetrForObjectDetection.from_pretrained(
25
- "facebook/detr-resnet-50"
26
- )
27
 
28
 
29
  def ade_palette():
@@ -100,36 +102,36 @@ def sepia(inputs, button_text):
100
  """객체 검출 또는 세그멘테이션을 수행하고 결과를 반환하는 함수입니다."""
101
 
102
  input_img = Image.fromarray(inputs)
103
- if button_text == "detection":
104
- inputs_detection = processor_detection(images=input_img, return_tensors="pt")
105
- outputs_detection = model_detection(**inputs_detection)
106
-
107
- target_sizes = torch.tensor([input_img.size[::-1]])
108
- results_detection = processor_detection.post_process_object_detection(
109
- outputs_detection, target_sizes=target_sizes, threshold=0.9
110
- )[0]
111
-
112
- draw = ImageDraw.Draw(input_img)
113
- for score, label, box in zip(
114
- results_detection["scores"],
115
- results_detection["labels"],
116
- results_detection["boxes"],
117
- ):
118
- box = [round(i, 2) for i in box.tolist()]
119
- label_name = model_detection.config.id2label[label.item()]
120
- print(
121
- f"Detected {label_name} with confidence "
122
- f"{round(score.item(), 3)} at location {box}"
123
- )
124
- draw.rectangle(box, outline="red", width=3)
125
- draw.text((box[0], box[1]), label_name, fill="red", font=None)
126
-
127
- fig = plt.figure(figsize=(20, 15))
128
- plt.imshow(input_img)
129
- plt.axis("off")
130
- return fig
131
-
132
- elif button_text == "segmentation":
133
  inputs_segmentation = feature_extractor(images=input_img, return_tensors="tf")
134
  outputs_segmentation = model_segmentation(**inputs_segmentation)
135
  logits_segmentation = outputs_segmentation.logits
@@ -155,13 +157,13 @@ def on_button_click(inputs):
155
  image_path, selected_option = inputs
156
  if selected_option == "dropout":
157
  # 'dropout'이면 두 가지 중에 하나를 랜덤으로 선택
158
- selected_option = np.random.choice(["detection", "segmentation"])
159
 
160
  return sepia(image_path, selected_option)
161
 
162
  # Gr.Dropdown을 사용하여 옵션을 선택할 수 있도록 변경
163
  dropdown = gr.Dropdown(
164
- ["detection", "segmentation"], label="Menu", info="Select One!"
165
  )
166
 
167
  demo = gr.Interface(
@@ -177,4 +179,4 @@ demo = gr.Interface(
177
  allow_flagging="never",
178
  )
179
 
180
- demo.launch(share=False)
 
9
  import tensorflow as tf
10
  from PIL import ImageDraw
11
 
12
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
+
14
  # image segmentation 모델
15
  feature_extractor = SegformerFeatureExtractor.from_pretrained(
16
  "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
 
20
  )
21
 
22
  # image detection 모델
23
+ # processor_detection = DetrImageProcessor.from_pretrained(
24
+ # "facebook/detr-resnet-50", revision="no_timm"
25
+ # )
26
+ # model_detection = DetrForObjectDetection.from_pretrained(
27
+ # "facebook/detr-resnet-50", revision="no_timm"
28
+ # )
29
 
30
 
31
  def ade_palette():
 
102
  """객체 검출 또는 세그멘테이션을 수행하고 결과를 반환하는 함수입니다."""
103
 
104
  input_img = Image.fromarray(inputs)
105
+ # if button_text == "detection":
106
+ # inputs_detection = processor_detection(images=input_img, return_tensors="pt")
107
+ # outputs_detection = model_detection(**inputs_detection)
108
+
109
+ # target_sizes = torch.tensor([input_img.size[::-1]])
110
+ # results_detection = processor_detection.post_process_object_detection(
111
+ # outputs_detection, target_sizes=target_sizes, threshold=0.9
112
+ # )[0]
113
+
114
+ # draw = ImageDraw.Draw(input_img)
115
+ # for score, label, box in zip(
116
+ # results_detection["scores"],
117
+ # results_detection["labels"],
118
+ # results_detection["boxes"],
119
+ # ):
120
+ # box = [round(i, 2) for i in box.tolist()]
121
+ # label_name = model_detection.config.id2label[label.item()]
122
+ # print(
123
+ # f"Detected {label_name} with confidence "
124
+ # f"{round(score.item(), 3)} at location {box}"
125
+ # )
126
+ # draw.rectangle(box, outline="red", width=3)
127
+ # draw.text((box[0], box[1]), label_name, fill="red", font=None)
128
+
129
+ # fig = plt.figure(figsize=(20, 15))
130
+ # plt.imshow(input_img)
131
+ # plt.axis("off")
132
+ # return fig
133
+
134
+ if button_text == "segmentation":
135
  inputs_segmentation = feature_extractor(images=input_img, return_tensors="tf")
136
  outputs_segmentation = model_segmentation(**inputs_segmentation)
137
  logits_segmentation = outputs_segmentation.logits
 
157
  image_path, selected_option = inputs
158
  if selected_option == "dropout":
159
  # 'dropout'이면 두 가지 중에 하나를 랜덤으로 선택
160
+ selected_option = np.random.choice(["segmentation"])
161
 
162
  return sepia(image_path, selected_option)
163
 
164
  # Gr.Dropdown을 사용하여 옵션을 선택할 수 있도록 변경
165
  dropdown = gr.Dropdown(
166
+ ["segmentation"], label="Menu", info="Chose Segmentation!"
167
  )
168
 
169
  demo = gr.Interface(
 
179
  allow_flagging="never",
180
  )
181
 
182
+ demo.launch()