Nicolasg2c commited on
Commit
d59dd3f
·
1 Parent(s): 59daa14

Refactor object detection output to return a formatted string of detected objects

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -35,27 +35,35 @@ def detect_objects(image):
35
  for score, label, box in zip(scores, labels, boxes):
36
  detected_objects.append(f"Objeto: {label}: {model.config.id2label[label.item()]}, Score: {score:.2f}, Box: {box.tolist()}")
37
 
38
- ##Plotear los resultados
39
- plt.figure(figsize=(10, 10))
40
- plt.imshow(image)
41
- ax = plt.gca()
42
- for score, label, box in zip(scores, labels, boxes):
43
- x_min, y_min, x_max, y_max = box
44
- rect = Rectangle((x_min, y_min), x_max - x_min, y_max - y_min,
45
- fill=False, color='red', linewidth=2)
46
- ax.add_patch(rect)
47
- ax.text(x_min, y_min, f"{model.config.id2label[label.item()]}: {score:.2f}",
48
- fontsize=12, color='red', verticalalignment='top')
49
- plt.axis('off')
50
- plt.tight_layout()
 
 
 
 
 
 
 
 
51
 
52
- #return image con los objetos detectados
53
- fig = plt.gcf()
54
- buf = io.BytesIO()
55
- fig.savefig(buf, format='png', bbox_inches='tight')
56
- buf.seek(0)
57
- plt.close()
58
- return Image.open(buf)
59
 
60
  # Crear la interfaz Gradio
61
  def create_interface():
 
35
  for score, label, box in zip(scores, labels, boxes):
36
  detected_objects.append(f"Objeto: {label}: {model.config.id2label[label.item()]}, Score: {score:.2f}, Box: {box.tolist()}")
37
 
38
+ # # Visualizar los resultados de la detección
39
+ # plt.figure(figsize=(10, 10))
40
+ # plt.imshow(image)
41
+ # ax = plt.gca()
42
+ # for score, label, box in zip(scores, labels, boxes):
43
+ # x_min, y_min, x_max, y_max = box
44
+ # rect = Rectangle((x_min, y_min), x_max - x_min, y_max - y_min,
45
+ # fill=False, color='red', linewidth=2)
46
+ # ax.add_patch(rect)
47
+ # ax.text(x_min, y_min, f"{model.config.id2label[label.item()]} | Total score: {score:.2f}",
48
+ # fontsize=12, color='red', verticalalignment='top')
49
+ # plt.axis('off')
50
+ # plt.tight_layout()
51
+ # fig = plt.gcf()
52
+ # buf = io.BytesIO()
53
+ # fig.savefig(buf, format='png', bbox_inches='tight')
54
+ # buf.seek(0)
55
+ # plt.close()
56
+
57
+ #Retornar la imagen con las detecciones
58
+ # return Image.open(buf)
59
 
60
+ # Mostrar los objetos detectados
61
+
62
+ detected_objects = []
63
+ for score, label, box in zip(scores, labels, boxes):
64
+ detected_objects.append(f"Objeto: {label}, Score: {score:.2f}, Box: {box.tolist()}")
65
+
66
+ return "\n".join(detected_objects)
67
 
68
  # Crear la interfaz Gradio
69
  def create_interface():