Spaces:
Sleeping
Sleeping
Faizan Azizahmed Shaikh
commited on
Commit
·
31638e5
1
Parent(s):
cc89d71
Upload 16 files
Browse files- .gitattributes +5 -0
- data/sample.mp4 +3 -0
- data/sample2.mp4 +3 -0
- data/sample3.mp4 +3 -0
- data/sample4.mp4 +3 -0
- requirements.txt +2 -0
- src/.ipynb_checkpoints/Webcam_Object_Detection-checkpoint.ipynb +158 -0
- src/.ipynb_checkpoints/realtime-checkpoint.py +42 -0
- src/.ipynb_checkpoints/requirements-checkpoint.txt +0 -0
- src/Webcam_Object_Detection.ipynb +101 -0
- src/__pycache__/items.cpython-311.pyc +0 -0
- src/app.py +66 -0
- src/items.py +12 -0
- src/output.avi +3 -0
- src/realtime.py +42 -0
- src/requirements.txt +0 -0
- src/yolov8n.pt +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,8 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
data/sample.mp4 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
data/sample2.mp4 filter=lfs diff=lfs merge=lfs -text
|
38 |
+
data/sample3.mp4 filter=lfs diff=lfs merge=lfs -text
|
39 |
+
data/sample4.mp4 filter=lfs diff=lfs merge=lfs -text
|
40 |
+
src/output.avi filter=lfs diff=lfs merge=lfs -text
|
data/sample.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bd9fe82cd88878238735a734b58e7880e2715ee0d62aa41a1cb649fe5a5a39e0
|
3 |
+
size 3943053
|
data/sample2.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9213c4d259427d1b9a570bd92f9a386e6ae2608c9176d99d6aa8eb8217b0f58b
|
3 |
+
size 3655442
|
data/sample3.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dce5920af1079c528410e4f316123693bb99d59fe70bae0eaf7b5d8f4013be5e
|
3 |
+
size 15876660
|
data/sample4.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1dead5a35f132329a89e3483fc143fe287aef10f0adff99886bb5294aee518f0
|
3 |
+
size 14572962
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
opencv_python==4.8.0.74
|
2 |
+
ultralytics==8.0.150
|
src/.ipynb_checkpoints/Webcam_Object_Detection-checkpoint.ipynb
ADDED
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "d9904ec5-391d-4967-9357-c8779d677142",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"# import required libraries\n",
|
11 |
+
"from ultralytics import YOLO\n",
|
12 |
+
"import gradio as gr\n",
|
13 |
+
"import cv2\n",
|
14 |
+
"import math\n",
|
15 |
+
"from items import classNames"
|
16 |
+
]
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"cell_type": "code",
|
20 |
+
"execution_count": 2,
|
21 |
+
"id": "1dbb6ae7-c844-4933-9a5c-f778bb1dfa83",
|
22 |
+
"metadata": {},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"# detection function\n",
|
26 |
+
"def yolo_detect(feed, vid):\n",
|
27 |
+
" video = vid\n",
|
28 |
+
" # Load a pretrained YOLOv8n model\n",
|
29 |
+
" model = YOLO('yolov8n.pt')\n",
|
30 |
+
" \n",
|
31 |
+
" # Run inference on the source\n",
|
32 |
+
" results = model(video, stream=True, verbose=False) \n",
|
33 |
+
" frames = list()\n",
|
34 |
+
" \n",
|
35 |
+
" # plot annotations\n",
|
36 |
+
" for frame in results:\n",
|
37 |
+
" boxes = frame.boxes\n",
|
38 |
+
" single = frame.orig_img\n",
|
39 |
+
" for box in boxes:\n",
|
40 |
+
" # bounding box\n",
|
41 |
+
" x1, y1, x2, y2 = box.xyxy[0]\n",
|
42 |
+
" x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values\n",
|
43 |
+
"\n",
|
44 |
+
" # put box in cam\n",
|
45 |
+
" cv2.rectangle(single, (x1, y1), (x2, y2), (255, 0, 255), 3)\n",
|
46 |
+
"\n",
|
47 |
+
" # object details\n",
|
48 |
+
" cv2.putText(single, classNames[int(box.cls[0])], (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)\n",
|
49 |
+
" \n",
|
50 |
+
" frames.append(single)\n",
|
51 |
+
" cv2.destroyAllWindows()\n",
|
52 |
+
" \n",
|
53 |
+
" h, w, c = frames[1].shape\n",
|
54 |
+
" \n",
|
55 |
+
" out_file = \"output.avi\"\n",
|
56 |
+
" fourcc=cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')\n",
|
57 |
+
" writer = out = cv2.VideoWriter(out_file, fourcc, 25.0, (w, h))\n",
|
58 |
+
" for i in range(len(frames)):\n",
|
59 |
+
" writer.write(frames[i])\n",
|
60 |
+
" writer.release()\n",
|
61 |
+
" return out_file"
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "code",
|
66 |
+
"execution_count": null,
|
67 |
+
"id": "692f5c49-67cd-4c11-8ee9-03dc7cb98809",
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [
|
70 |
+
{
|
71 |
+
"name": "stderr",
|
72 |
+
"output_type": "stream",
|
73 |
+
"text": [
|
74 |
+
"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",
|
75 |
+
" warnings.warn(\n",
|
76 |
+
"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",
|
77 |
+
" warnings.warn(\n"
|
78 |
+
]
|
79 |
+
},
|
80 |
+
{
|
81 |
+
"name": "stdout",
|
82 |
+
"output_type": "stream",
|
83 |
+
"text": [
|
84 |
+
"Running on local URL: http://127.0.0.1:7861\n"
|
85 |
+
]
|
86 |
+
},
|
87 |
+
{
|
88 |
+
"name": "stderr",
|
89 |
+
"output_type": "stream",
|
90 |
+
"text": [
|
91 |
+
"Traceback (most recent call last):\n",
|
92 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\routes.py\", line 442, in run_predict\n",
|
93 |
+
" output = await app.get_blocks().process_api(\n",
|
94 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
95 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\blocks.py\", line 1392, in process_api\n",
|
96 |
+
" result = await self.call_function(\n",
|
97 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
98 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\blocks.py\", line 1097, in call_function\n",
|
99 |
+
" prediction = await anyio.to_thread.run_sync(\n",
|
100 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
101 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\anyio\\to_thread.py\", line 33, in run_sync\n",
|
102 |
+
" return await get_asynclib().run_sync_in_worker_thread(\n",
|
103 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
104 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 877, in run_sync_in_worker_thread\n",
|
105 |
+
" return await future\n",
|
106 |
+
" ^^^^^^^^^^^^\n",
|
107 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 807, in run\n",
|
108 |
+
" result = context.run(func, *args)\n",
|
109 |
+
" ^^^^^^^^^^^^^^^^^^^^^^^^\n",
|
110 |
+
" File \"C:\\Users\\faiza\\anaconda3\\envs\\hgace\\Lib\\site-packages\\gradio\\utils.py\", line 703, in wrapper\n",
|
111 |
+
" response = f(*args, **kwargs)\n",
|
112 |
+
" ^^^^^^^^^^^^^^^^^^\n",
|
113 |
+
"TypeError: yolo_detect() takes 1 positional argument but 2 were given\n"
|
114 |
+
]
|
115 |
+
}
|
116 |
+
],
|
117 |
+
"source": [
|
118 |
+
"demo = gr.Interface(fn=yolo_detect, \n",
|
119 |
+
" inputs=[gr.PlayableVideo(source='webcam'), gr.Video(autoplay=True)],\n",
|
120 |
+
" outputs=[gr.Video(autoplay=True, format='avi')],\n",
|
121 |
+
" cache_examples=True, allow_flagging='never')\n",
|
122 |
+
"demo.queue()\n",
|
123 |
+
"demo.launch(inline=False, debug=True, show_api=False, quiet=True)"
|
124 |
+
]
|
125 |
+
},
|
126 |
+
{
|
127 |
+
"cell_type": "code",
|
128 |
+
"execution_count": null,
|
129 |
+
"id": "120eca17-b44a-4cf9-86fc-651ddf791ffa",
|
130 |
+
"metadata": {},
|
131 |
+
"outputs": [],
|
132 |
+
"source": [
|
133 |
+
"# demo.close()"
|
134 |
+
]
|
135 |
+
}
|
136 |
+
],
|
137 |
+
"metadata": {
|
138 |
+
"kernelspec": {
|
139 |
+
"display_name": "Python 3 (ipykernel)",
|
140 |
+
"language": "python",
|
141 |
+
"name": "python3"
|
142 |
+
},
|
143 |
+
"language_info": {
|
144 |
+
"codemirror_mode": {
|
145 |
+
"name": "ipython",
|
146 |
+
"version": 3
|
147 |
+
},
|
148 |
+
"file_extension": ".py",
|
149 |
+
"mimetype": "text/x-python",
|
150 |
+
"name": "python",
|
151 |
+
"nbconvert_exporter": "python",
|
152 |
+
"pygments_lexer": "ipython3",
|
153 |
+
"version": "3.11.4"
|
154 |
+
}
|
155 |
+
},
|
156 |
+
"nbformat": 4,
|
157 |
+
"nbformat_minor": 5
|
158 |
+
}
|
src/.ipynb_checkpoints/realtime-checkpoint.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import libraries
|
2 |
+
from ultralytics import YOLO
|
3 |
+
import cv2
|
4 |
+
import sys
|
5 |
+
|
6 |
+
def realtime(video):
|
7 |
+
# Load the YOLOv8 model
|
8 |
+
model = YOLO('yolov8n.pt')
|
9 |
+
|
10 |
+
# Open the video file
|
11 |
+
video_path = video
|
12 |
+
cap = cv2.VideoCapture(video_path)
|
13 |
+
cap.set(3, 720)
|
14 |
+
cap.set(4, 1280)
|
15 |
+
# Loop through the video frames
|
16 |
+
while cap.isOpened():
|
17 |
+
# Read a frame from the video
|
18 |
+
success, frame = cap.read()
|
19 |
+
|
20 |
+
if success:
|
21 |
+
# Run YOLOv8 inference on the frame
|
22 |
+
results = model(frame, verbose=False)
|
23 |
+
|
24 |
+
# Visualize the results on the frame
|
25 |
+
annotated_frame = results[0].plot()
|
26 |
+
|
27 |
+
# Display the annotated frame
|
28 |
+
cv2.imshow("YOLOv8 Inference", annotated_frame)
|
29 |
+
|
30 |
+
# Break the loop if 'q' is pressed
|
31 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
32 |
+
break
|
33 |
+
else:
|
34 |
+
# Break the loop if the end of the video is reached
|
35 |
+
break
|
36 |
+
|
37 |
+
# Release the video capture object and close the display window
|
38 |
+
cap.release()
|
39 |
+
cv2.destroyAllWindows()
|
40 |
+
|
41 |
+
if __name__ == '__main__':
|
42 |
+
realtime(sys.argv[1])
|
src/.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
File without changes
|
src/Webcam_Object_Detection.ipynb
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "d9904ec5-391d-4967-9357-c8779d677142",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"# import required libraries\n",
|
11 |
+
"from ultralytics import YOLO\n",
|
12 |
+
"import gradio as gr\n",
|
13 |
+
"import cv2\n",
|
14 |
+
"import math\n",
|
15 |
+
"from items import classNames"
|
16 |
+
]
|
17 |
+
},
|
18 |
+
{
|
19 |
+
"cell_type": "code",
|
20 |
+
"execution_count": null,
|
21 |
+
"id": "1dbb6ae7-c844-4933-9a5c-f778bb1dfa83",
|
22 |
+
"metadata": {},
|
23 |
+
"outputs": [],
|
24 |
+
"source": [
|
25 |
+
"# detection function\n",
|
26 |
+
"def yolo_detect(feed, vid):\n",
|
27 |
+
" video = vid\n",
|
28 |
+
" # Load a pretrained YOLOv8n model\n",
|
29 |
+
" model = YOLO('yolov8n.pt')\n",
|
30 |
+
" \n",
|
31 |
+
" # Run inference on the source\n",
|
32 |
+
" results = model(video, stream=True, verbose=False) \n",
|
33 |
+
" frames = list()\n",
|
34 |
+
" \n",
|
35 |
+
" # plot annotations\n",
|
36 |
+
" for frame in results:\n",
|
37 |
+
" boxes = frame.boxes\n",
|
38 |
+
" single = frame.orig_img\n",
|
39 |
+
" for box in boxes:\n",
|
40 |
+
" # bounding box\n",
|
41 |
+
" x1, y1, x2, y2 = box.xyxy[0]\n",
|
42 |
+
" x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values\n",
|
43 |
+
"\n",
|
44 |
+
" # put box in cam\n",
|
45 |
+
" cv2.rectangle(single, (x1, y1), (x2, y2), (255, 0, 255), 3)\n",
|
46 |
+
"\n",
|
47 |
+
" # object details\n",
|
48 |
+
" cv2.putText(single, classNames[int(box.cls[0])], (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)\n",
|
49 |
+
" \n",
|
50 |
+
" frames.append(single)\n",
|
51 |
+
" cv2.destroyAllWindows()\n",
|
52 |
+
" \n",
|
53 |
+
" h, w, c = frames[1].shape\n",
|
54 |
+
" \n",
|
55 |
+
" out_file = \"output.avi\"\n",
|
56 |
+
" fourcc=cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')\n",
|
57 |
+
" writer = out = cv2.VideoWriter(out_file, fourcc, 25.0, (w, h))\n",
|
58 |
+
" for i in range(len(frames)):\n",
|
59 |
+
" writer.write(frames[i])\n",
|
60 |
+
" writer.release()\n",
|
61 |
+
" return out_file"
|
62 |
+
]
|
63 |
+
},
|
64 |
+
{
|
65 |
+
"cell_type": "code",
|
66 |
+
"execution_count": null,
|
67 |
+
"id": "692f5c49-67cd-4c11-8ee9-03dc7cb98809",
|
68 |
+
"metadata": {},
|
69 |
+
"outputs": [],
|
70 |
+
"source": [
|
71 |
+
"demo = gr.Interface(fn=yolo_detect, \n",
|
72 |
+
" inputs=[gr.PlayableVideo(source='webcam'), gr.Video(autoplay=True)],\n",
|
73 |
+
" outputs=[gr.PlayableVideo(autoplay=True, format='avi')],\n",
|
74 |
+
" cache_examples=True, allow_flagging='never')\n",
|
75 |
+
"demo.queue()\n",
|
76 |
+
"demo.launch(inline=False, debug=True, show_api=False, quiet=True)"
|
77 |
+
]
|
78 |
+
}
|
79 |
+
],
|
80 |
+
"metadata": {
|
81 |
+
"kernelspec": {
|
82 |
+
"display_name": "Python 3 (ipykernel)",
|
83 |
+
"language": "python",
|
84 |
+
"name": "python3"
|
85 |
+
},
|
86 |
+
"language_info": {
|
87 |
+
"codemirror_mode": {
|
88 |
+
"name": "ipython",
|
89 |
+
"version": 3
|
90 |
+
},
|
91 |
+
"file_extension": ".py",
|
92 |
+
"mimetype": "text/x-python",
|
93 |
+
"name": "python",
|
94 |
+
"nbconvert_exporter": "python",
|
95 |
+
"pygments_lexer": "ipython3",
|
96 |
+
"version": "3.11.4"
|
97 |
+
}
|
98 |
+
},
|
99 |
+
"nbformat": 4,
|
100 |
+
"nbformat_minor": 5
|
101 |
+
}
|
src/__pycache__/items.cpython-311.pyc
ADDED
Binary file (899 Bytes). View file
|
|
src/app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[ ]:
|
5 |
+
|
6 |
+
|
7 |
+
# import required libraries
|
8 |
+
from ultralytics import YOLO
|
9 |
+
import gradio as gr
|
10 |
+
import cv2
|
11 |
+
import math
|
12 |
+
from items import classNames
|
13 |
+
|
14 |
+
|
15 |
+
# In[ ]:
|
16 |
+
|
17 |
+
|
18 |
+
# detection function
|
19 |
+
def yolo_detect(feed, vid):
|
20 |
+
video = vid
|
21 |
+
# Load a pretrained YOLOv8n model
|
22 |
+
model = YOLO('yolov8n.pt')
|
23 |
+
|
24 |
+
# Run inference on the source
|
25 |
+
results = model(video, stream=True, verbose=False)
|
26 |
+
frames = list()
|
27 |
+
|
28 |
+
# plot annotations
|
29 |
+
for frame in results:
|
30 |
+
boxes = frame.boxes
|
31 |
+
single = frame.orig_img
|
32 |
+
for box in boxes:
|
33 |
+
# bounding box
|
34 |
+
x1, y1, x2, y2 = box.xyxy[0]
|
35 |
+
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
|
36 |
+
|
37 |
+
# put box in cam
|
38 |
+
cv2.rectangle(single, (x1, y1), (x2, y2), (255, 0, 255), 3)
|
39 |
+
|
40 |
+
# object details
|
41 |
+
cv2.putText(single, classNames[int(box.cls[0])], (x1,y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)
|
42 |
+
|
43 |
+
frames.append(single)
|
44 |
+
cv2.destroyAllWindows()
|
45 |
+
|
46 |
+
h, w, c = frames[1].shape
|
47 |
+
|
48 |
+
out_file = "output.avi"
|
49 |
+
fourcc=cv2.VideoWriter_fourcc('X', 'V', 'I', 'D')
|
50 |
+
writer = out = cv2.VideoWriter(out_file, fourcc, 25.0, (w, h))
|
51 |
+
for i in range(len(frames)):
|
52 |
+
writer.write(frames[i])
|
53 |
+
writer.release()
|
54 |
+
return out_file
|
55 |
+
|
56 |
+
|
57 |
+
# In[ ]:
|
58 |
+
|
59 |
+
|
60 |
+
demo = gr.Interface(fn=yolo_detect,
|
61 |
+
inputs=[gr.PlayableVideo(source='webcam'), gr.Video(autoplay=True)],
|
62 |
+
outputs=[gr.PlayableVideo(autoplay=True, format='avi')],
|
63 |
+
cache_examples=True, allow_flagging='never')
|
64 |
+
demo.queue()
|
65 |
+
demo.launch(inline=False, debug=True, show_api=False, quiet=True)
|
66 |
+
|
src/items.py
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# object classes
|
2 |
+
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",
|
3 |
+
"traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",
|
4 |
+
"dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",
|
5 |
+
"handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",
|
6 |
+
"baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",
|
7 |
+
"fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
|
8 |
+
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",
|
9 |
+
"diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",
|
10 |
+
"microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",
|
11 |
+
"teddy bear", "hair drier", "toothbrush"
|
12 |
+
]
|
src/output.avi
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:12a1475d91cadf7ae8c048c1fb342c8c4ebfe806cfb4de08705c3f53fb07f1d2
|
3 |
+
size 88264158
|
src/realtime.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import libraries
|
2 |
+
from ultralytics import YOLO
|
3 |
+
import cv2
|
4 |
+
import sys
|
5 |
+
|
6 |
+
def realtime(video):
|
7 |
+
# Load the YOLOv8 model
|
8 |
+
model = YOLO('yolov8n.pt')
|
9 |
+
|
10 |
+
# Open the video file
|
11 |
+
video_path = video
|
12 |
+
cap = cv2.VideoCapture(video_path)
|
13 |
+
cap.set(3, 720)
|
14 |
+
cap.set(4, 1280)
|
15 |
+
# Loop through the video frames
|
16 |
+
while cap.isOpened():
|
17 |
+
# Read a frame from the video
|
18 |
+
success, frame = cap.read()
|
19 |
+
|
20 |
+
if success:
|
21 |
+
# Run YOLOv8 inference on the frame
|
22 |
+
results = model(frame, verbose=False)
|
23 |
+
|
24 |
+
# Visualize the results on the frame
|
25 |
+
annotated_frame = results[0].plot()
|
26 |
+
|
27 |
+
# Display the annotated frame
|
28 |
+
cv2.imshow("YOLOv8 Inference", annotated_frame)
|
29 |
+
|
30 |
+
# Break the loop if 'q' is pressed
|
31 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
32 |
+
break
|
33 |
+
else:
|
34 |
+
# Break the loop if the end of the video is reached
|
35 |
+
break
|
36 |
+
|
37 |
+
# Release the video capture object and close the display window
|
38 |
+
cap.release()
|
39 |
+
cv2.destroyAllWindows()
|
40 |
+
|
41 |
+
if __name__ == '__main__':
|
42 |
+
realtime(sys.argv[1])
|
src/requirements.txt
ADDED
File without changes
|
src/yolov8n.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:31e20dde3def09e2cf938c7be6fe23d9150bbbe503982af13345706515f2ef95
|
3 |
+
size 6534387
|