Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Алдаров Артем Константинович
ИИММ-2024
Commits
6c856209
Commit
6c856209
authored
5 months ago
by
Алдаров Артем Константинович
Browse files
Options
Download
Patches
Plain Diff
Update АлдаровАК_БИВ211_ПР1.ipynb
parent
a713764f
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
АлдаровАК_БИВ211_ПР1.ipynb
+19
-33
АлдаровАК_БИВ211_ПР1.ipynb
with
19 additions
and
33 deletions
+19
-33
АлдаровАК_БИВ211_ПР1.ipynb
+
19
−
33
View file @
6c856209
...
...
@@ -16,7 +16,7 @@
"cells": [
{
"cell_type": "code",
"execution_count":
72
,
"execution_count":
1
,
"metadata": {
"id": "rkL66VFAv45A"
},
...
...
@@ -38,15 +38,15 @@
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "
ada8ee6d-c7f7-4d51-e19f-de5e6fbc299a
"
"outputId": "
5f793442-2a13-4fae-e8a1-b4654dac0389
"
},
"execution_count":
73
,
"execution_count":
2
,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"
Drive already m
ounted at /content/drive
; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).
\n"
"
M
ounted at /content/drive\n"
]
}
]
...
...
@@ -70,7 +70,7 @@
"metadata": {
"id": "9b9vy_GWwtDE"
},
"execution_count":
7
4,
"execution_count": 4
6
,
"outputs": []
},
{
...
...
@@ -95,7 +95,7 @@
"metadata": {
"id": "NcGSNZoG8sSw"
},
"execution_count": 7
5
,
"execution_count":
4
7,
"outputs": []
},
{
...
...
@@ -106,24 +106,24 @@
" for cx, cy in points:\n",
" cv2.drawMarker(frame, (cx, cy), color=color, thickness=2, \\\n",
" markerType=cv2.MARKER_TILTED_CROSS, line_type=cv2.LINE_AA, \\\n",
" markerSize=1
5
)"
" markerSize=1
0
)"
],
"metadata": {
"id": "uBNLkUpK_K_U"
},
"execution_count":
76
,
"execution_count":
48
,
"outputs": []
},
{
"cell_type": "code",
"source": [
"high_color = (
87,86,104
)\n",
"lower_color = (
35
,
3
0,
4
0)"
"high_color = (
255,160,80
)\n",
"lower_color = (
0
, 0, 0)"
],
"metadata": {
"id": "
nakl8vS_C0zM
"
"id": "
8UMhTvFQ395R
"
},
"execution_count":
77
,
"execution_count":
49
,
"outputs": []
},
{
...
...
@@ -140,8 +140,8 @@
" ret, frame = cap.read()\n",
"\n",
" if ret:\n",
" frame = cv2.cvtColor(frame, cv2.COLOR_BGR2
RGB
) # BGR ->
RGB
\n",
" frame_thres = cv2.inRange(frame, lower_color, high_color) # Image thresholding\n",
" frame
_hsv
= cv2.cvtColor(frame, cv2.COLOR_BGR2
HSV
) # BGR ->
HSV
\n",
" frame_thres = cv2.inRange(frame
_hsv
, lower_color, high_color) # Image thresholding\n",
"\n",
" coordinates = find_centroid(frame_thres)\n",
" if coordinates is not None:\n",
...
...
@@ -149,30 +149,15 @@
"\n",
" draw_cross(frame, points)\n",
"\n",
" frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)\n",
"\n",
" writer.write(frame)\n",
"\n",
"writer.release()\n",
"print('Done!')"
"writer.release()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rpr4YJZN4R0s",
"outputId": "48afa7f2-0ac3-4880-cf7e-5ba51d2d7eaf"
"id": "rpr4YJZN4R0s"
},
"execution_count": 78,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Done!\n"
]
}
]
"execution_count": 50,
"outputs": []
}
]
}
\ No newline at end of file
%% Cell type:code id: tags:
```
import cv2
import numpy as np
import matplotlib.pyplot as plt
```
%% Cell type:code id: tags:
```
from google.colab import drive
drive.mount('/content/drive')
```
%% Output
Drive already m
ounted at /content/drive
; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
M
ounted at /content/drive
%% Cell type:code id: tags:
```
# Path to video
path = '/content/drive/MyDrive/walking_man.mp4'
# Capturing video
cap = cv2.VideoCapture(path)
# Creating video writer
writer = cv2.VideoWriter('walking_man_result.mp4', # video name
cv2.VideoWriter_fourcc(*'MP4V'), # codec
25, # FPS
(int(cap.get(3)),int(cap.get(4))) # dimentions X:Y
)
```
%% Cell type:code id: tags:
```
# Function to find the center of the biggest blob of colored pixels on image
def find_centroid(img):
contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Finding blobs
if len(contours) > 0:
largest_contour = max(contours, key=cv2.contourArea) # Finding the biggest blob
moments = cv2.moments(largest_contour) # Finding contour moments
if moments["m00"] != 0:
cx = int(moments["m10"] / moments["m00"])
cy = int(moments["m01"] / moments["m00"])
else:
cx, cy = 0, 0
return (cx, cy)
```
%% Cell type:code id: tags:
```
# Function to draw crosses from all previous frames
def draw_cross(frame, points, color=(150,200,255)):
for cx, cy in points:
cv2.drawMarker(frame, (cx, cy), color=color, thickness=2, \
markerType=cv2.MARKER_TILTED_CROSS, line_type=cv2.LINE_AA, \
markerSize=1
5
)
markerSize=1
0
)
```
%% Cell type:code id: tags:
```
high_color = (
87,86,104
)
lower_color = (
35
,
3
0,
4
0)
high_color = (
255,160,80
)
lower_color = (
0
, 0, 0)
```
%% Cell type:code id: tags:
```
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
ret = True
# List of coordinates of all centroids
points = []
while ret:
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2
RGB
) # BGR ->
RGB
frame_thres = cv2.inRange(frame, lower_color, high_color) # Image thresholding
frame
_hsv
= cv2.cvtColor(frame, cv2.COLOR_BGR2
HSV
) # BGR ->
HSV
frame_thres = cv2.inRange(frame
_hsv
, lower_color, high_color) # Image thresholding
coordinates = find_centroid(frame_thres)
if coordinates is not None:
points.append(coordinates)
draw_cross(frame, points)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
writer.write(frame)
writer.release()
print('Done!')
```
%% Output
Done!
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets