File size: 1,372 Bytes
d3dc09c
 
 
 
 
d8e67ae
d3dc09c
 
 
 
 
 
d4507d2
d3dc09c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)