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
3b0df469
Commit
3b0df469
authored
6 months ago
by
Алдаров Артем Константинович
Browse files
Options
Download
Patches
Plain Diff
Upload ПР1 .ipynb
parent
e04b5d06
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
АлдаровАК_БИВ211_ПР1.ipynb
+178
-0
АлдаровАК_БИВ211_ПР1.ipynb
with
178 additions
and
0 deletions
+178
-0
АлдаровАК_БИВ211_ПР1.ipynb
0 → 100644
+
178
−
0
View file @
3b0df469
{
"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": 129,
"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": "2fa59ab5-c20d-46d8-a976-4bb3002ac0cd"
},
"execution_count": 130,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Path to video\n",
"path = '/content/drive/MyDrive/prybakov_walks.mp4'\n",
"\n",
"# Capturing video\n",
"cap = cv2.VideoCapture(path)\n",
"\n",
"# Creating video writer\n",
"writer = cv2.VideoWriter('prybakov_walks_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": 131,
"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": 132,
"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=1, \\\n",
" markerType=cv2.MARKER_TILTED_CROSS, line_type=cv2.LINE_AA, \\\n",
" markerSize=25)"
],
"metadata": {
"id": "uBNLkUpK_K_U"
},
"execution_count": 139,
"outputs": []
},
{
"cell_type": "code",
"source": [
"lower_color = (160,0,25)\n",
"high_color = (200, 255, 255)"
],
"metadata": {
"id": "nakl8vS_C0zM"
},
"execution_count": 134,
"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",
" if ret:\n",
" frame_temp = frame.copy() # Copying the image for manipulations\n",
"\n",
" frame_hsv = cv2.cvtColor(frame_temp, 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()\n",
"print('Done!')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "rpr4YJZN4R0s",
"outputId": "651f0cab-89c8-4438-ea2b-e5f3e2b7ddc1"
},
"execution_count": 140,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Done!\n"
]
}
]
}
]
}
\ 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 mounted at /content/drive; to attempt to forcibly remount, call drive.mount("/content/drive", force_remount=True).
%% Cell type:code id: tags:
```
# Path to video
path = '/content/drive/MyDrive/prybakov_walks.mp4'
# Capturing video
cap = cv2.VideoCapture(path)
# Creating video writer
writer = cv2.VideoWriter('prybakov_walks_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=1, \
markerType=cv2.MARKER_TILTED_CROSS, line_type=cv2.LINE_AA, \
markerSize=25)
```
%% Cell type:code id: tags:
```
lower_color = (160,0,25)
high_color = (200, 255, 255)
```
%% 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_temp = frame.copy() # Copying the image for manipulations
frame_hsv = cv2.cvtColor(frame_temp, 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()
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