Commit 591cb916 authored by Алдаров Артем Константинович's avatar Алдаров Артем Константинович
Browse files
No related merge requests found
Showing with 0 additions and 164 deletions
+0 -164
%% 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()
```
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment