Format: application/octet-stream
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 7,
6 "id": "f810581d",
7 "metadata": {},
8 "outputs": [],
9 "source": [
10 "import cv2\n",
11 "import numpy as np\n",
12 "\n",
13 "# Load Yolo\n",
14 "net = cv2.dnn.readNet(\"yolov3-tiny.weights\", \"yolov3-tiny.cfg\")\n",
15 "classes = []\n",
16 "with open(\"coco.names\", \"r\") as f:\n",
17 " classes = [line.strip() for line in f.readlines()]\n",
18 "layer_names = net.getLayerNames()\n",
19 "output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]\n",
20 "colors = np.random.uniform(0, 255, size=(len(classes), 3))\n",
21 "\n",
22 "cap = cv2.VideoCapture('data/sample3.mp4')\n",
23 "# Loading image\n",
24 "while True:\n",
25 " ret, img = cap.read()\n",
26 " key = cv2.waitKey(1)\n",
27 " if key == ord('q') or ret == False:\n",
28 " break\n",
29 " else:\n",
30 " img = cv2.resize(img, None, fx=0.7, fy=0.7)\n",
31 " height, width, channels = img.shape\n",
32 "\n",
33 " # Detecting objects\n",
34 " blob = cv2.dnn.blobFromImage(img, 1/255, (width, height), (0, 0, 0), True, crop=False)\n",
35 "\n",
36 " net.setInput(blob)\n",
37 " outs = net.forward(output_layers)\n",
38 "\n",
39 " # Showing informations on the screen\n",
40 " class_ids = []\n",
41 " confidences = []\n",
42 " boxes = []\n",
43 " for out in outs:\n",
44 " for detection in out:\n",
45 " scores = detection[5:]\n",
46 " class_id = np.argmax(scores)\n",
47 " confidence = scores[class_id]\n",
48 " if confidence > 0.2:\n",
49 " # Object detected\n",
50 " center_x = int(detection[0] * width)\n",
51 " center_y = int(detection[1] * height)\n",
52 " w = int(detection[2] * width)\n",
53 " h = int(detection[3] * height)\n",
54 "\n",
55 " # Rectangle coordinates\n",
56 " x = int(center_x - w / 2)\n",
57 " y = int(center_y - h / 2)\n",
58 "\n",
59 " boxes.append([x, y, w, h])\n",
60 " confidences.append(float(confidence))\n",
61 " class_ids.append(class_id)\n",
62 "\n",
63 " indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)\n",
64 " #print(indexes)\n",
65 " font = cv2.FONT_HERSHEY_PLAIN\n",
66 " for i in range(len(boxes)):\n",
67 " if i in indexes:\n",
68 " x, y, w, h = boxes[i]\n",
69 " label = str(classes[class_ids[i]])\n",
70 " color = colors[class_ids[i]]\n",
71 " cv2.rectangle(img, (x, y), (x + w, y + h), (0,255,0) , 2)\n",
72 " cv2.putText(img, 'p', (x, y + 10), font, 3, color, 2)\n",
73 "\n",
74 "\n",
75 " cv2.imshow(\"Image\", img)\n",
76 "cv2.destroyAllWindows()"
77 ]
78 },
79 {
80 "cell_type": "code",
81 "execution_count": null,
82 "id": "7c2e776a",
83 "metadata": {},
84 "outputs": [],
85 "source": []
86 }
87 ],
88 "metadata": {
89 "kernelspec": {
90 "display_name": "Python 3",
91 "language": "python",
92 "name": "python3"
93 },
94 "language_info": {
95 "codemirror_mode": {
96 "name": "ipython",
97 "version": 3
98 },
99 "file_extension": ".py",
100 "mimetype": "text/x-python",
101 "name": "python",
102 "nbconvert_exporter": "python",
103 "pygments_lexer": "ipython3",
104 "version": "3.8.8"
105 }
106 },
107 "nbformat": 4,
108 "nbformat_minor": 5
109 }
110