Faizan Azizahmed Shaikh commited on
Commit
59e57cd
·
1 Parent(s): 67c544d

Upload 6 files

Browse files
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
+
requirements.txt CHANGED
@@ -1,2 +0,0 @@
1
- opencv_python==4.8.0.74
2
- ultralytics==8.0.150
 
 
 
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/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/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/yolov8n.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:31e20dde3def09e2cf938c7be6fe23d9150bbbe503982af13345706515f2ef95
3
+ size 6534387