File size: 1,767 Bytes
6fad636
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "81598ea8-8e97-4ad7-a45f-bd928d0ef416",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "from ultralytics import YOLO\n",
    "import cv2\n",
    "import os\n",
    "\n",
    "def predict_image(image_input):\n",
    "    image = cv2.imread(image_input)\n",
    "    # load model\n",
    "    model = YOLO(\"best.pt\")\n",
    "    #run predict\n",
    "    outputs = model.predict(source=image_input)\n",
    "    results = output[0].cpu().numpy()\n",
    "    for i, det in enumerate(results.boxes.xyxy):\n",
    "        cv2.rectangle(image, (int(det[0]), int(det[1]), int(det[2]), int(det[3]),\n",
    "                              color=(0, 0, 255), thickness=2, lineType=cv2.Line_AA)\n",
    "    return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
    "\n",
    "inputs_image = [gr.components.Image(type=\"filepath\", label=\"Input Image\")]\n",
    "outputs_image = [gr.components.Image(type=\"numpy\", label=\"Output Image\")]\n",
    "\n",
    "interface_image = gr.Interface(fn = predict_image, inputs=inputs_image, outputs=outputs_image, \n",
    "                               title=\"Fire & Smoke Detector\", cache_examples=False)\n",
    "\n",
    "interface_image.launch(Debug=True)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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": 5
}