Skip to content

Commit f7278bb

Browse files
committed
kids detection
1 parent c6ea6fc commit f7278bb

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

misc/kids_detection.png

1.32 MB
Loading

misc/kids_detection_output.png

398 KB
Loading

python/yolo-tiny.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77

88
# step 2 - feed a 640x640 image to get predictions
99

10-
image = cv2.imread('misc/car.jpg')
11-
blob = cv2.dnn.blobFromImage(image, 1/255.0, (640, 640), swapRB=True)
10+
def format_yolov5(frame):
11+
12+
row, col, _ = frame.shape
13+
_max = max(col, row)
14+
result = np.zeros((_max, _max, 3), np.uint8)
15+
result[0:row, 0:col] = frame
16+
return result
17+
18+
image = cv2.imread('misc/corrida-crianças-1280x720.jpg')
19+
input_image = format_yolov5(image) # making the image square
20+
blob = cv2.dnn.blobFromImage(input_image , 1/255.0, (640, 640), swapRB=True)
1221
net.setInput(blob)
1322
predictions = net.forward()
1423

@@ -20,6 +29,10 @@
2029

2130
output_data = predictions[0]
2231

32+
image_width, image_height, _ = input_image.shape
33+
x_factor = image_width / 640
34+
y_factor = image_height / 640
35+
2336
for r in range(25200):
2437
row = output_data[r]
2538
confidence = row[4]
@@ -35,23 +48,37 @@
3548
class_ids.append(class_id)
3649

3750
x, y, w, h = row[0].item(), row[1].item(), row[2].item(), row[3].item()
38-
left = int(x - 0.5 * w)
39-
top = int(y - 0.5 * h)
40-
box = np.array([left, top, int(w), int(h)])
51+
left = int((x - 0.5 * w) * x_factor)
52+
top = int((y - 0.5 * h) * y_factor)
53+
width = int(w * x_factor)
54+
height = int(h * y_factor)
55+
box = np.array([left, top, width, height])
4156
boxes.append(box)
4257

4358
class_list = []
4459
with open("config_files/classes.txt", "r") as f:
4560
class_list = [cname.strip() for cname in f.readlines()]
4661

47-
for i in range(len(class_ids)):
62+
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
63+
64+
result_class_ids = []
65+
result_confidences = []
66+
result_boxes = []
67+
68+
for i in indexes:
69+
result_confidences.append(confidences[i])
70+
result_class_ids.append(class_ids[i])
71+
result_boxes.append(boxes[i])
72+
73+
for i in range(len(result_class_ids)):
4874

49-
box = boxes[i]
50-
class_id = class_ids[i]
75+
box = result_boxes[i]
76+
class_id = result_class_ids[i]
5177

5278
cv2.rectangle(image, box, (0, 255, 255), 2)
5379
cv2.rectangle(image, (box[0], box[1] - 20), (box[0] + box[2], box[1]), (0, 255, 255), -1)
5480
cv2.putText(image, class_list[class_id], (box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, .5, (0,0,0))
5581

82+
cv2.imwrite("misc/kids_detection.png", image)
5683
cv2.imshow("output", image)
5784
cv2.waitKey()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy