Skip to content

Commit c2305e5

Browse files
committed
add yolov8 section to the object detection tutorial
1 parent 483e50b commit c2305e5

File tree

6 files changed

+229
-7
lines changed

6 files changed

+229
-7
lines changed
6.28 MB
Binary file not shown.
+5-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
# [How to Perform YOLO Object Detection using OpenCV and PyTorch in Python](https://www.thepythoncode.com/article/yolo-object-detection-with-opencv-and-pytorch-in-python)
22
To run this:
33
- `pip3 install -r requirements.txt`
4-
- Download the [model weights](https://pjreddie.com/media/files/yolov3.weights) and put them in `weights` folder.
54
- To generate a object detection image on `images/dog.jpg`:
65
```
7-
python yolo_opencv.py images/dog.jpg
6+
python yolov8_opencv.py images/dog.jpg
87
```
9-
A new image `dog_yolo3.jpg` will appear which has the bounding boxes of different objects in the image.
8+
A new image `dog_yolo8.jpg` will appear which has the bounding boxes of different objects in the image.
109
- For live object detection:
1110
```
12-
python live_yolo_opencv.py
11+
python live_yolov8_opencv.py
1312
```
1413
- If you want to read from a video file and make predictions:
1514
```
16-
python read_video.py video.avi
15+
python read_video_yolov8.py 1.mp4
1716
```
1817
This will start detecting objects in that video, in the end, it'll save the resulting video to `output.avi`
19-
- If you wish to use PyTorch for GPU acceleration, please install PyTorch CUDA [here](https://pytorch.org/get-started) and use `yolo.py` file.
18+
- Old files for YOLOv3: `yolo_opencv.py`, `live_yolo_opencv.py`, `read_video.py`
2019
- Feel free to edit the codes for your needs!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import cv2
2+
import numpy as np
3+
4+
import time
5+
import sys
6+
7+
from ultralytics import YOLO
8+
9+
10+
CONFIDENCE = 0.5
11+
font_scale = 1
12+
thickness = 1
13+
labels = open("data/coco.names").read().strip().split("\n")
14+
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")
15+
16+
model = YOLO("yolov8n.pt")
17+
18+
cap = cv2.VideoCapture(0)
19+
_, image = cap.read()
20+
h, w = image.shape[:2]
21+
fourcc = cv2.VideoWriter_fourcc(*"XVID")
22+
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (w, h))
23+
while True:
24+
_, image = cap.read()
25+
26+
start = time.perf_counter()
27+
# run inference on the image
28+
# see: https://docs.ultralytics.com/modes/predict/#arguments for full list of arguments
29+
results = model.predict(image, conf=CONFIDENCE)[0]
30+
time_took = time.perf_counter() - start
31+
print("Time took:", time_took)
32+
33+
# loop over the detections
34+
for data in results.boxes.data.tolist():
35+
# get the bounding box coordinates, confidence, and class id
36+
xmin, ymin, xmax, ymax, confidence, class_id = data
37+
# converting the coordinates and the class id to integers
38+
xmin = int(xmin)
39+
ymin = int(ymin)
40+
xmax = int(xmax)
41+
ymax = int(ymax)
42+
class_id = int(class_id)
43+
44+
# draw a bounding box rectangle and label on the image
45+
color = [int(c) for c in colors[class_id]]
46+
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color=color, thickness=thickness)
47+
text = f"{labels[class_id]}: {confidence:.2f}"
48+
# calculate text width & height to draw the transparent boxes as background of the text
49+
(text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=thickness)[0]
50+
text_offset_x = xmin
51+
text_offset_y = ymin - 5
52+
box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height))
53+
overlay = image.copy()
54+
cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED)
55+
# add opacity (transparency to the box)
56+
image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0)
57+
# now put the text (label: confidence %)
58+
cv2.putText(image, text, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
59+
fontScale=font_scale, color=(0, 0, 0), thickness=thickness)
60+
61+
# end time to compute the fps
62+
end = time.perf_counter()
63+
# calculate the frame per second and draw it on the frame
64+
fps = f"FPS: {1 / (end - start):.2f}"
65+
cv2.putText(image, fps, (50, 50),
66+
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 6)
67+
out.write(image)
68+
cv2.imshow("image", image)
69+
70+
if ord("q") == cv2.waitKey(1):
71+
break
72+
73+
74+
cap.release()
75+
cv2.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import cv2
2+
import numpy as np
3+
4+
import time
5+
import sys
6+
7+
from ultralytics import YOLO
8+
9+
# define some parameters
10+
CONFIDENCE = 0.5
11+
font_scale = 1
12+
thickness = 1
13+
labels = open("data/coco.names").read().strip().split("\n")
14+
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")
15+
16+
# loading the YOLOv8 model with the default weight file
17+
model = YOLO("yolov8n.pt")
18+
19+
# read the file from the command line
20+
video_file = sys.argv[1]
21+
cap = cv2.VideoCapture(video_file)
22+
_, image = cap.read()
23+
h, w = image.shape[:2]
24+
fourcc = cv2.VideoWriter_fourcc(*"XVID")
25+
out = cv2.VideoWriter("output.avi", fourcc, 20.0, (w, h))
26+
while True:
27+
_, image = cap.read()
28+
29+
start = time.perf_counter()
30+
results = model.predict(image, conf=CONFIDENCE)[0]
31+
time_took = time.perf_counter() - start
32+
print("Time took:", time_took)
33+
34+
# loop over the detections
35+
for data in results.boxes.data.tolist():
36+
# get the bounding box coordinates, confidence, and class id
37+
xmin, ymin, xmax, ymax, confidence, class_id = data
38+
# converting the coordinates and the class id to integers
39+
xmin = int(xmin)
40+
ymin = int(ymin)
41+
xmax = int(xmax)
42+
ymax = int(ymax)
43+
class_id = int(class_id)
44+
45+
# draw a bounding box rectangle and label on the image
46+
color = [int(c) for c in colors[class_id]]
47+
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color=color, thickness=thickness)
48+
text = f"{labels[class_id]}: {confidence:.2f}"
49+
# calculate text width & height to draw the transparent boxes as background of the text
50+
(text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=thickness)[0]
51+
text_offset_x = xmin
52+
text_offset_y = ymin - 5
53+
box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height))
54+
try:
55+
overlay = image.copy()
56+
except:
57+
break
58+
cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED)
59+
# add opacity (transparency to the box)
60+
image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0)
61+
# now put the text (label: confidence %)
62+
cv2.putText(image, text, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
63+
fontScale=font_scale, color=(0, 0, 0), thickness=thickness)
64+
65+
# end time to compute the fps
66+
end = time.perf_counter()
67+
# calculate the frame per second and draw it on the frame
68+
fps = f"FPS: {1 / (end - start):.2f}"
69+
cv2.putText(image, fps, (50, 50),
70+
cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 6)
71+
out.write(image)
72+
cv2.imshow("image", image)
73+
74+
if ord("q") == cv2.waitKey(1):
75+
break
76+
77+
78+
cap.release()
79+
cv2.destroyAllWindows()
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
opencv-python
22
numpy
3-
matplotlib
3+
matplotlib
4+
ultralytics
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import numpy as np
2+
import os
3+
import cv2
4+
import time
5+
import sys
6+
from ultralytics import YOLO
7+
8+
# define some parameters
9+
CONFIDENCE = 0.5
10+
font_scale = 1
11+
thickness = 1
12+
13+
# loading the YOLOv8 model with the default weight file
14+
model = YOLO("yolov8n.pt")
15+
16+
# loading all the class labels (objects)
17+
labels = open("data/coco.names").read().strip().split("\n")
18+
19+
# generating colors for each object for later plotting
20+
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")
21+
22+
path_name = sys.argv[1]
23+
image = cv2.imread(path_name)
24+
file_name = os.path.basename(path_name) # "dog.jpg"
25+
filename, ext = file_name.split(".") # "dog", "jpg"
26+
27+
# measure how much it took in seconds
28+
start = time.perf_counter()
29+
# run inference on the image
30+
# see: https://docs.ultralytics.com/modes/predict/#arguments for full list of arguments
31+
results = model.predict(image, conf=CONFIDENCE)[0]
32+
time_took = time.perf_counter() - start
33+
print(f"Time took: {time_took:.2f}s")
34+
print(results.boxes.data)
35+
36+
# loop over the detections
37+
for data in results.boxes.data.tolist():
38+
# get the bounding box coordinates, confidence, and class id
39+
xmin, ymin, xmax, ymax, confidence, class_id = data
40+
# converting the coordinates and the class id to integers
41+
xmin = int(xmin)
42+
ymin = int(ymin)
43+
xmax = int(xmax)
44+
ymax = int(ymax)
45+
class_id = int(class_id)
46+
47+
# draw a bounding box rectangle and label on the image
48+
color = [int(c) for c in colors[class_id]]
49+
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), color=color, thickness=thickness)
50+
text = f"{labels[class_id]}: {confidence:.2f}"
51+
# calculate text width & height to draw the transparent boxes as background of the text
52+
(text_width, text_height) = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, fontScale=font_scale, thickness=thickness)[0]
53+
text_offset_x = xmin
54+
text_offset_y = ymin - 5
55+
box_coords = ((text_offset_x, text_offset_y), (text_offset_x + text_width + 2, text_offset_y - text_height))
56+
overlay = image.copy()
57+
cv2.rectangle(overlay, box_coords[0], box_coords[1], color=color, thickness=cv2.FILLED)
58+
# add opacity (transparency to the box)
59+
image = cv2.addWeighted(overlay, 0.6, image, 0.4, 0)
60+
# now put the text (label: confidence %)
61+
cv2.putText(image, text, (xmin, ymin - 5), cv2.FONT_HERSHEY_SIMPLEX,
62+
fontScale=font_scale, color=(0, 0, 0), thickness=thickness)
63+
64+
# display output image
65+
cv2.imshow("Image", image)
66+
cv2.waitKey(0)
67+
# save output image to disk
68+
cv2.imwrite(filename + "_yolo8." + ext, image)

0 commit comments

Comments
 (0)