danielHora commited on
Commit
ed4a7a1
1 Parent(s): c2f6332

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -10
app.py CHANGED
@@ -1,23 +1,45 @@
1
  from transformers import DetrFeatureExtractor, DetrForObjectDetection
2
- from PIL import Image
3
  import requests
4
  import gradio as gr
5
 
 
 
 
 
 
 
 
 
 
 
 
6
  def object_classify(img):
7
  feature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-50')
8
  model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-50')
9
 
10
- inputs = feature_extractor(images=img, return_tensors="pt")
11
- outputs = model(**inputs)
12
- # model predicts bounding boxes and corresponding COCO classes
13
- logits = outputs.logits
14
- bboxes = outputs.pred_boxes
15
 
16
- probas = logits.softmax(-1)[0, :, :-1]
17
- keep = probas.max(-1).values > 0.7
18
- return keep
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  interface=gr.Interface(fn=object_classify,
21
- inputs=gr.inputs.Image(shape=(224,224),label='Insert Image'),
22
  examples = [],description='??',allow_flagging="never")
23
  interface.launch()
 
1
  from transformers import DetrFeatureExtractor, DetrForObjectDetection
2
+ from PIL import Image, ImageDraw, ImageFont
3
  import requests
4
  import gradio as gr
5
 
6
+ # Draw bounding box definition
7
+ def draw_bounding_box(im, score, label, xmin, ymin, xmax, ymax, index, num_boxes):
8
+ """ Draw a bounding box. """
9
+ # Draw the actual bounding box
10
+ outline = 'blue'
11
+ im_with_rectangle = ImageDraw.Draw(im)
12
+ im_with_rectangle.rounded_rectangle((xmin, ymin, xmax, ymax), outline = outline, width = 3, radius = 10)
13
+
14
+ # Return the result
15
+ return im
16
+
17
  def object_classify(img):
18
  feature_extractor = DetrFeatureExtractor.from_pretrained('facebook/detr-resnet-50')
19
  model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-50')
20
 
21
+ object_detector = pipeline("object-detection", model = model, feature_extractor = feature_extractor)
22
+ bboxes = object_detector(img)
23
+
 
 
24
 
25
+ # Iteration elements
26
+ num_boxes = len(bounding_boxes)
27
+ index = 0
28
+
29
+ # Draw bounding box for each result
30
+ for bounding_box in bounding_boxes:
31
+ box = bounding_box['box']
32
+ #Draw the bounding box
33
+ output_image = draw_bounding_box(img, bounding_box['score'],
34
+ bounding_box['label'],
35
+ box['xmin'], box['ymin'],
36
+ box['xmax'], box['ymax'],
37
+ index, num_boxes)
38
+ index += 1
39
+ return output_image
40
+
41
 
42
  interface=gr.Interface(fn=object_classify,
43
+ inputs=gr.inputs.Image(shape=(224,224),label='Insert Image'),outputs = gr.outputs.Image(),
44
  examples = [],description='??',allow_flagging="never")
45
  interface.launch()