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
591cb916
Commit
591cb916
authored
5 months ago
by
Алдаров Артем Константинович
Browse files
Options
Download
Plain Diff
Merge branch 'master' of
https://git.miem.hse.ru/akaldarov/aimm2024
parents
f358850d
1284e284
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
АлдаровАК_БИВ211_ПР1.ipynb
+0
-164
АлдаровАК_БИВ211_ПР1.ipynb
with
0 additions
and
164 deletions
+0
-164
АлдаровАК_БИВ211_ПР1.ipynb
deleted
100644 → 0
+
0
−
164
View file @
f358850d
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "rkL66VFAv45A"
},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
],
"metadata": {
"id": "Rr1YMcU711VQ",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5f793442-2a13-4fae-e8a1-b4654dac0389"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Mounted at /content/drive\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Path to video\n",
"path = '/content/drive/MyDrive/walking_man.mp4'\n",
"\n",
"# Capturing video\n",
"cap = cv2.VideoCapture(path)\n",
"\n",
"# Creating video writer\n",
"writer = cv2.VideoWriter('walking_man_result.mp4', # video name\n",
" cv2.VideoWriter_fourcc(*'MP4V'), # codec\n",
" 25, # FPS\n",
" (int(cap.get(3)),int(cap.get(4))) # dimentions X:Y\n",
" )"
],
"metadata": {
"id": "9b9vy_GWwtDE"
},
"execution_count": 46,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Function to find the center of the biggest blob of colored pixels on image\n",
"def find_centroid(img):\n",
" contours, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Finding blobs\n",
" if len(contours) > 0:\n",
" largest_contour = max(contours, key=cv2.contourArea) # Finding the biggest blob\n",
"\n",
" moments = cv2.moments(largest_contour) # Finding contour moments\n",
"\n",
" if moments[\"m00\"] != 0:\n",
" cx = int(moments[\"m10\"] / moments[\"m00\"])\n",
" cy = int(moments[\"m01\"] / moments[\"m00\"])\n",
" else:\n",
" cx, cy = 0, 0\n",
"\n",
" return (cx, cy)"
],
"metadata": {
"id": "NcGSNZoG8sSw"
},
"execution_count": 47,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Function to draw crosses from all previous frames\n",
"def draw_cross(frame, points, color=(150,200,255)):\n",
" 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=10)"
],
"metadata": {
"id": "uBNLkUpK_K_U"
},
"execution_count": 48,
"outputs": []
},
{
"cell_type": "code",
"source": [
"high_color = (255,160,80)\n",
"lower_color = (0, 0, 0)"
],
"metadata": {
"id": "8UMhTvFQ395R"
},
"execution_count": 49,
"outputs": []
},
{
"cell_type": "code",
"source": [
"cap.set(cv2.CAP_PROP_POS_FRAMES, 0)\n",
"\n",
"ret = True\n",
"\n",
"# List of coordinates of all centroids\n",
"points = []\n",
"\n",
"while ret:\n",
" ret, frame = cap.read()\n",
"\n",
" if ret:\n",
" frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 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",
" points.append(coordinates)\n",
"\n",
" draw_cross(frame, points)\n",
"\n",
" writer.write(frame)\n",
"\n",
"writer.release()"
],
"metadata": {
"id": "rpr4YJZN4R0s"
},
"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
Mounted 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=10)
```
%% Cell type:code id: tags:
```
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_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # 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)
writer.write(frame)
writer.release()
```
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