ppicazo commited on
Commit
5c00058
·
verified ·
1 Parent(s): f413349

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -1,38 +1,39 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- from PIL import Image, ImageDraw
4
 
5
  # Load object detection pipeline
6
  model_pipeline = pipeline(
7
  task="object-detection",
8
- model="bortle/autotrain-ap-obj-detector-1"
9
  )
10
 
11
  def predict(image):
12
- # Resize the image to width 1080, maintaining aspect ratio
13
  width = 1080
14
  ratio = width / image.width
15
  height = int(image.height * ratio)
16
- resized_image = image.resize((width, height))
17
 
18
- # Run object detection
19
- detections = model_pipeline(resized_image)
20
 
21
- # Draw detections on image
22
- draw = ImageDraw.Draw(resized_image)
23
  for det in detections:
24
  box = det["box"]
25
- label = f'{det["label"]}: {det["score"]:.2f}'
 
 
 
26
  draw.rectangle(
27
  [(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
28
  outline="red",
29
  width=3
30
  )
31
- draw.text((box["xmin"], box["ymin"] - 10), label, fill="red")
 
32
 
33
- return resized_image
34
 
35
- # Gradio Interface
36
  gr.Interface(
37
  fn=predict,
38
  inputs=gr.Image(type="pil", label="Upload Astrophotography Image"),
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ from PIL import Image, ImageDraw, ImageFont
4
 
5
  # Load object detection pipeline
6
  model_pipeline = pipeline(
7
  task="object-detection",
8
+ model="bortle/autotrain-ap-obj-detector-2"
9
  )
10
 
11
  def predict(image):
 
12
  width = 1080
13
  ratio = width / image.width
14
  height = int(image.height * ratio)
15
+ image = image.resize((width, height))
16
 
17
+ detections = model_pipeline(image)
 
18
 
19
+ # Draw boxes
20
+ draw = ImageDraw.Draw(image)
21
  for det in detections:
22
  box = det["box"]
23
+ label = f'{det["label"]} ({det["score"]:.2f})'
24
+ print(f"Drawing box: {box} Label: {label}") # Debug
25
+
26
+ # Draw rectangle
27
  draw.rectangle(
28
  [(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
29
  outline="red",
30
  width=3
31
  )
32
+ # Optional: draw label
33
+ draw.text((box["xmin"] + 2, box["ymin"] - 10), label, fill="red")
34
 
35
+ return image
36
 
 
37
  gr.Interface(
38
  fn=predict,
39
  inputs=gr.Image(type="pil", label="Upload Astrophotography Image"),