-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from fernaper/development
v2.0.2
- Loading branch information
Showing
15 changed files
with
331 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
# Images and videos | ||
*.jpg | ||
*.jpeg | ||
*.png | ||
*.mp4 | ||
|
||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# MIT License | ||
# Copyright (c) 2019 Fernando Perez | ||
import time | ||
import cv2 | ||
|
||
# TODO: Document ManagerCV2 | ||
class ManagerCV2(): | ||
|
||
_tries_reconnect_stream = 10 | ||
|
||
def __init__(self, video, is_stream=False, keystroke=-1, wait_key=-1, fps_limit=0): | ||
self.video = video | ||
self.is_stream = is_stream | ||
self.stream = video | ||
self.fps_limit = fps_limit | ||
|
||
self.keystroke = keystroke | ||
self.wait_key = wait_key | ||
|
||
self.last_keystroke = -1 | ||
self.initial_time = None | ||
self.final_time = None | ||
self.count_frames = 0 | ||
|
||
|
||
def __iter__(self): | ||
self.initial_time = time.time() | ||
self.last_frame_time = self.initial_time | ||
self.count_frames = 0 | ||
self.last_keystroke = -1 | ||
return self | ||
|
||
|
||
def __next__(self): | ||
ret, frame = self.video.read() | ||
self.final_time = time.time() | ||
|
||
if self.is_stream: | ||
for i in range(ManagerCV2._tries_reconnect_stream): | ||
ret, frame = self.video.read() | ||
if ret: | ||
break | ||
if i+1 == ManagerCV2._tries_reconnect_stream: | ||
self.end_iteration() | ||
elif not ret: | ||
self.end_iteration() | ||
|
||
if self.wait_key != -1: | ||
self.last_keystroke = cv2.waitKey(self.wait_key) | ||
if self.last_keystroke == self.keystroke: | ||
self.end_iteration() | ||
|
||
self.count_frames += 1 | ||
|
||
# Here we limit the speed (if we want constant frames) | ||
if self.fps_limit: | ||
time_to_sleep = (1 / self.fps_limit) - (time.time() - self.last_frame_time) | ||
if time_to_sleep > 0: | ||
time.sleep(time_to_sleep) | ||
|
||
self.last_frame_time = time.time() | ||
return frame | ||
|
||
|
||
def get_last_keystroke(self): | ||
return self.last_keystroke | ||
|
||
|
||
def end_iteration(self): | ||
self.video.release() | ||
raise StopIteration | ||
|
||
|
||
def get_fps(self): | ||
return round(self.count_frames / (self.final_time - self.initial_time),3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../README.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# MIT License | ||
# Copyright (c) 2019 Fernando Perez | ||
import cv2 | ||
|
||
from cv2_tools.utils import * | ||
|
||
|
||
# TODO: Document SelectorCV2 | ||
class SelectorCV2(): | ||
|
||
|
||
def __init__(self, alpha=0.9, color=(110,70,45), normalized=False, thickness=2, | ||
filled=False, peephole=True, margin=5, closed_polygon=False): | ||
self.zones = [] | ||
self.polygon_zones = [] | ||
self.all_tags = [] | ||
# Visual parameters | ||
self.alpha = alpha | ||
self.color = color | ||
self.normalized = normalized | ||
self.thickness = thickness | ||
self.filled = filled | ||
self.peephole = peephole | ||
self.margin = margin | ||
# Polygon | ||
self.closed_polygon = closed_polygon | ||
|
||
|
||
def set_properties(self, alpha=None, color=None, normalized=None, | ||
thickness=None, filled=None, peephole=None, | ||
margin=None): | ||
if alpha is not None: | ||
self.alpha = alpha | ||
if color is not None: | ||
self.color = color | ||
if normalized is not None: | ||
self.normalized = normalized | ||
if thickness is not None: | ||
self.thickness = thickness | ||
if filled is not None: | ||
self.filled = filled | ||
if peephole is not None: | ||
self.peephole = peephole | ||
if margin is not None: | ||
self.margin = margin | ||
|
||
|
||
def add_zone(self, zone, tags=None): | ||
self.zones.append(zone) | ||
if tags and type(tags) is not list: | ||
tags = [tags] | ||
elif not tags: | ||
tags = [] | ||
self.all_tags.append(tags) | ||
|
||
|
||
def add_polygon(self, polygon, surrounding_box=False, tags=None): | ||
if not polygon: | ||
return | ||
|
||
self.polygon_zones.append(polygon) | ||
|
||
if surrounding_box: | ||
min_x, min_y, max_x, max_y = polygon[0][0], polygon[0][1], 0, 0 | ||
for position in polygon: | ||
if position[0] < min_x: | ||
min_x = position[0] | ||
if position[0] > max_x: | ||
max_x = position[0] | ||
if position[1] < min_y: | ||
min_y = position[1] | ||
if position[1] > max_y: | ||
max_y = position[1] | ||
|
||
self.zones.append((min_x, min_y, max_x, max_y)) | ||
|
||
if tags and type(tags) is not list: | ||
tags = [tags] | ||
elif not tags: | ||
tags = [] | ||
self.all_tags.append(tags) | ||
|
||
|
||
def set_range_valid_rectangles(self, origin, destination): | ||
self.zones = self.zones[origin:destination] | ||
self.all_tags = self.all_tags[origin:destination] | ||
|
||
|
||
def set_valid_rectangles(self, indexes): | ||
# This if is just for efficiency | ||
if not indexes: | ||
self.zones = [] | ||
self.all_tags = [] | ||
return | ||
|
||
for i in range(len(self.zones)): | ||
if i not in indexes: | ||
self.zones.pop(i) | ||
self.all_tags.pop(i) | ||
|
||
|
||
def draw(self, frame, fx=1, fy=1, interpolation=cv2.INTER_LINEAR): | ||
next_frame = select_multiple_zones( | ||
frame.copy(), | ||
self.zones, | ||
all_tags=self.all_tags, | ||
alpha=self.alpha, | ||
color=self.color, | ||
normalized=self.normalized, | ||
thickness=self.thickness, | ||
filled=self.filled, | ||
peephole=self.peephole, | ||
margin=self.margin) | ||
|
||
next_frame = select_polygon( | ||
next_frame, | ||
all_vertexes=self.polygon_zones, | ||
color=self.color, | ||
thickness=self.thickness, | ||
closed=self.closed_polygon | ||
) | ||
|
||
return cv2.resize(next_frame, (0,0), fx=fx, fy=fy, interpolation=interpolation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.