Rehman1603's picture
Update app.py
d8e67ae verified
raw
history blame
1.37 kB
from ultralytics.utils.plotting import Annotator
from PIL import Image
from ultralytics import YOLO
import gradio as gr
model=YOLO('Organ_detection.pt')
def Predict(img):
objects_name = []
cropped_images = []
# Object detection
results = model.predict(img, conf=0.40, iou=0.45,imgsz=640)
for r in results:
annotator = Annotator(img)
boxes = r.boxes
for box in boxes:
b = box.xyxy[0] # get box coordinates in (left, top, right, bottom) format
c = box.cls
annotator.box_label(b, model.names[int(c)], color=(255, 255, 0), txt_color=(255, 0, 255))
print(model.names[int(c)])
# Append the class name to the list
objects_name.append(model.names[int(c)])
# Crop the image using the bounding box coordinates
left, top, right, bottom = int(b[0]), int(b[1]), int(b[2]), int(b[3])
cropped_img = img[top:bottom, left:right]
# Store the cropped image
cropped_img_pil = Image.fromarray(cropped_img)
result_img = annotator.result()
return result_img, objects_name, cropped_img_pil
interface=gr.Interface(fn=Predict,inputs=["image"],
outputs=["image","text","image"])
interface.launch(debug=True)