-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversion to Python 3.6 #5
Open
alizahidraja
wants to merge
57
commits into
sashagaz:master
Choose a base branch
from
orensbruli:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
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
…ructure to contains the usefull information of the detected hand
Added a new substract method using first frames to get the background
…ent (lighting, camera, distances, etc)
Added multiple hands tracking. Added tracking methods. Added ID to the hand structure.
Fixed several bugs Implemented some functions to remove failed matches.
…ding on the number of frames, the detections and the traking.
Sumarized some duplicated code on a new generic method contour_to_hand
Created new method to manually add new hands to the tracking. This makes the percentage of false positives much lower than before. Fixed some bugs. Implemented a new masking method by diff with black pixel percentage checking.
Fixed some typos in some method names Created a method to downscale bounding rects Created method extract_contour_inside_rect Added debug attribute for the HandDetector class Added method addHand2 to look for hands on a roi ignorin the contours Added method compute_overlayed_frame to get an image with the hands information over the initial frame Fixed a bug with the position history of the hands Added an option to the follow method to use camshift instead meanshift for the tracking Added a method to reset the background and some conditions in the method to get the mask to reset if needed. Added a new method to "detect" fist based on the convex hull and defects points and taking the bigges of two clusters created with kmeans
…of the hand. Added code to update the attributes of the hand when a fist is detected
Removed the default call to the method draw_contour_features Fixed a bug with the detecction of the fist when no hand contour is found.
…unction. Fixed the implementation of the depth mask.
Added some DEBUG info to the windows.
Added some comments and removed unneded code for debug only
Sorry, here is the code in a text file |
Created new roi class Created new Hand class
…with a side and percentage. Added some test to main.
… simplify some methods.
Fixed a bug when extending the roi.
Added method to Roi to apply a mask on a frame. Added method to Roi to draw a it on a frame Added method to Roi to extract it from a frame
…, top_right, bottom_left, bottom_right. Added method to get a rate of the intersection of two rois.
…ing_roi and extended_roi.
… with the same letter.
Added some comments and TODOs.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
'''
Converted to Python 3.6
by Ali Zahid Raja
19/6/2019
'''
#!/usr/bin/env python
-- coding: utf-8 --
import copy
import math
import os
import random
from collections import deque
from datetime import datetime
import cv2
import numpy as np
def get_random_color(n=1):
ret = []
r = int(random.random() * 256)
g = int(random.random() * 256)
b = int(random.random() * 256)
step = 256 / n
for i in range(n):
r += step
g += step
b += step
r = int(r) % 256
g = int(g) % 256
b = int(b) % 256
ret.append((r, g, b))
return ret
Function to find angle between two vectors
def calculate_angle(v1, v2):
dot = np.dot(v1, v2)
x_modulus = np.sqrt((v1 * v1).sum())
y_modulus = np.sqrt((v2 * v2).sum())
cos_angle = dot / x_modulus / y_modulus
angle = np.degrees(np.arccos(cos_angle))
return angle
Function to find distance between two points in a list of lists
def find_distance(point_a, point_b):
return np.sqrt(np.power((point_a[0][0] - point_b[0][0]), 2) + np.power((point_a[0][1] - point_b[0][1]), 2))
# Creating a window for HSV track bars
cv2.namedWindow('HSV_TrackBar')
# Starting with 100's to prevent error while masking
h, s, v = 100, 100, 100
# Creating track bar
cv2.createTrackbar('h', 'HSV_TrackBar', 0, 179, nothing)
cv2.createTrackbar('s', 'HSV_TrackBar', 0, 255, nothing)
cv2.createTrackbar('v', 'HSV_TrackBar', 0, 255, nothing)
MAX_UNDETECTED_FRAMES = 30 * 10 # 30 FPS * 10 seconds
MAX_UNDETECTED_SECONDS = 10
DETECTION_TRUTH_FACTOR = 2. # points of life to recover if detected in one iteration
TRACKING_TRUTH_FACTOR = .1 # points of life to recover if tracked in one iteration
UNDETECTION_TRUTH_FACTOR = 3. # points of life to recover if detected in one iteration
UNTRACKING_TRUTH_FACTOR = 2 # points of life to recover if tracked in one iteration
MAX_TRUTH_VALUE = 100.
class Hand:
def init(self):
self.id = None
self.fingertips = []
self.intertips = []
self.center_of_mass = None
self.finger_distances = []
self.average_defect_distance = []
self.contour = None
self.bounding_rect = None
self.tracking_fails = 0
self.detection_fails = 0
self.frame_count = 0
self.tracking_window = None
self.tracked = False
self.detected = True
self.position_history = []
self.color = get_random_color()[0]
self.truth_value = 100
def clean_mask_noise(mask, blur=5):
# Kernel matrices for morphological transformation
kernel_square = np.ones((11, 11), np.uint8)
kernel_ellipse = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
def get_color_mask(image):
# Blur the image
blur_radius = 5
blurred = cv2.GaussianBlur(image, (blur_radius, blur_radius), 0)
# Convert to HSV color space
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, np.array([2, 50, 50]), np.array([15, 255, 255]))
return mask
def upscale_bounding_rect(bounding_rect, frame_shape, upscaled_pixels):
x, y, w, h = bounding_rect
new_x = max(x - int(upscaled_pixels / 2), 0)
def downscale_bounding_rect(bounding_rect, frame_shape, downscaled_pixels):
x, y, w, h = bounding_rect
new_x = min(x + int(downscaled_pixels / 2), frame_shape[1])
def extract_contour_inside_circle(full_contour, circle):
center, radius = circle
new_contour = []
for point in full_contour:
if (point[0][0] - center[0]) ** 2 + (point[0][1] - center[1]) ** 2 < radius ** 2:
new_contour.append(point)
return np.array(new_contour)
def extract_contour_inside_rect(full_contour, rect):
x1, y1, w, h = rect
x2 = x1 + w
y2 = y1 + h
new_contour = []
for point in full_contour:
if x1 < point[0][0] < x2 and y1 < point[0][1] < y2:
new_contour.append(point)
return np.array(new_contour)
class HandDetector:
def init(self, source=0):
# Open Camera object
# self.capture = cv2.VideoCapture(0)
# TODO: For testing only
if source != -1:
self.capture = cv2.VideoCapture(source)
else:
self.capture = None
def main():
hand_detector = HandDetector()
hand_detector.debug = True
# hand_detector = HandDetector('./resources/testing_hand_video2.mp4')
hand_detector.capture_and_compute2()
hand_detector.exit()
if name == "main":
main()