from PIL import Image
import requests
import io
from ultralytics import YOLO

model = YOLO('yolov8n.pt')

while True:
    #   GET IMAGE
    img_raw = requests.get("http://192.168.199.143:8080/photoaf.jpg").content
    img = Image.open(io.BytesIO(img_raw)).resize((1280, 720))
    #   RECOGNITION
    results = model.predict(source=img, stream=True, conf=0.5, show=True)
    for result in results:
        detections = []
        for box in result.boxes:
            class_id = result.names[box.cls[0].item()]
            cords = box.xyxy[0].tolist()
            cords = [round(x) for x in cords]
            confi = round(box.conf[0].item(), 2)
            print("Object type:", class_id)
            print("Coordinates:", cords)
            print("Probability:", confi)
            print("---")
            detections.append({"name" : class_id, "cords" : cords, "conf" : confi})
