Spaces:
Sleeping
Sleeping
File size: 5,769 Bytes
31638e5 |
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "d9904ec5-391d-4967-9357-c8779d677142",
"metadata": {},
"outputs": [],
"source": [
"# import required libraries\n",
"from ultralytics import YOLO\n",
"import gradio as gr\n",
"import cv2\n",
"import math\n",
"from items import classNames"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1dbb6ae7-c844-4933-9a5c-f778bb1dfa83",
"metadata": {},
"outputs": [],
"source": [
"# detection function\n",
"def yolo_detect(feed, vid):\n",
" video = vid\n",
" # Load a pretrained YOLOv8n model\n",
" model = YOLO('yolov8n.pt')\n",
" \n",
" # Run inference on the source\n",
" results = model(video, stream=True, verbose=False) \n",
" frames = list()\n",
" \n",
" # plot annotations\n",
" for frame in results:\n",
" boxes = frame.boxes\n",
" single = frame.orig_img\n",
" for box in boxes:\n",
" # bounding box\n",
" x1, y1, x2, y2 = box.xyxy[0]\n",
" x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values\n",
"\n",
" # put box in cam\n",
" cv2.rectangle(single, (x1, y1), (x2, y2), (255, 0, 255), 3)\n",
"\n",
" # object details\n",
" cv2.putText(single, classNames[int(box.cls[0])], (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)\n",
" \n",
" frames.append(single)\n",
" cv2.destroyAllWindows()\n",
" \n",
" h, w, c = frames[1].shape\n",
" \n",
" out_file = \"output.avi\"\n",
" fourcc=cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')\n",
" writer = out = cv2.VideoWriter(out_file, fourcc, 25.0, (w, h))\n",
" for i in range(len(frames)):\n",
" writer.write(frames[i])\n",
" writer.release()\n",
" return out_file"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "692f5c49-67cd-4c11-8ee9-03dc7cb98809",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\utils.py:833: UserWarning: Expected 1 arguments for function <function yolo_detect at 0x000001B002054860>, received 2.\n",
" warnings.warn(\n",
"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\utils.py:841: UserWarning: Expected maximum 1 arguments for function <function yolo_detect at 0x000001B002054860>, received 2.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7861\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\routes.py\", line 442, in run_predict\n",
" output = await app.get_blocks().process_api(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\blocks.py\", line 1392, in process_api\n",
" result = await self.call_function(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\blocks.py\", line 1097, in call_function\n",
" prediction = await anyio.to_thread.run_sync(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
" return await get_asynclib().run_sync_in_worker_thread(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
" return await future\n",
" ^^^^^^^^^^^^\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
" result = context.run(func, *args)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\utils.py\", line 703, in wrapper\n",
" response = f(*args, **kwargs)\n",
" ^^^^^^^^^^^^^^^^^^\n",
"TypeError: yolo_detect() takes 1 positional argument but 2 were given\n"
]
}
],
"source": [
"demo = gr.Interface(fn=yolo_detect, \n",
" inputs=[gr.PlayableVideo(source='webcam'), gr.Video(autoplay=True)],\n",
" outputs=[gr.Video(autoplay=True, format='avi')],\n",
" cache_examples=True, allow_flagging='never')\n",
"demo.queue()\n",
"demo.launch(inline=False, debug=True, show_api=False, quiet=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "120eca17-b44a-4cf9-86fc-651ddf791ffa",
"metadata": {},
"outputs": [],
"source": [
"# demo.close()"
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|