{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Setup for web service" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import ultralytics\n", "from ultralytics import YOLO\n", "import cv2\n", "import numpy as np\n", "from PIL import Image, ImageFont, ImageDraw\n", "from IPython.display import display\n", "import onnx\n", "import onnxruntime\n", "import cv2\n", "import numpy as np\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "model = YOLO(r\"runs\\detect\\train\\weights\\best.pt\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "image 1/1 e:\\tomAIto_git\\tomAIto\\tomato_model\\val\\images\\IMG_1024.jpg: 640x480 2 b_fully_ripeneds, 1305.3ms\n", "Speed: 14.9ms preprocess, 1305.3ms inference, 15.3ms postprocess per image at shape (1, 3, 640, 480)\n" ] } ], "source": [ "results = model.predict(r\"val\\images\\IMG_1024.jpg\")\n", "result = results[0]\n", "#box = result.boxes[0]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Object type: b_fully_ripened\n", "Coordinates: [747, 275, 2211, 1872]\n", "Probability: 0.98\n", "---\n", "Object type: b_fully_ripened\n", "Coordinates: [12, 931, 1052, 1949]\n", "Probability: 0.95\n", "---\n" ] }, { "data": { "text/plain": [ "[None, None]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def print_box(box):\n", " class_id, cords, conf = box\n", " print(\"Object type:\", class_id)\n", " print(\"Coordinates:\", cords)\n", " print(\"Probability:\", conf)\n", " print(\"---\")\n", "\n", "[\n", " print_box([\n", " result.names[box.cls[0].item()],\n", " [round(x) for x in box.xyxy[0].tolist()],\n", " round(box.conf[0].item(), 2)\n", " ]) for box in result.boxes\n", "]" ] } ], "metadata": { "kernelspec": { "display_name": "ml_env", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 2 }