Remove print statements to optimise code

This commit is contained in:
DSTO\pivatom
2018-12-10 16:51:44 +10:30
parent 94a527377a
commit f8db12b5b6

View File

@@ -8,14 +8,15 @@ Created on Thu Nov 22 10:51:21 2018
import numpy as np import numpy as np
import cv2 import cv2
img = cv2.imread('H:\car\GestureRecognition\IMG_0818.png', 1) img = cv2.imread('H:\car\GestureRecognition\IMG_0825.jpg', 1)
img = cv2.imread('H:\car\GestureRecognition\IMG_0818.jpg', 1)
# Downscale the image # Downscale the image
img = cv2.resize(img, None, fx=0.1, fy=0.1, interpolation = cv2.INTER_AREA) img = cv2.resize(img, None, fx=0.1, fy=0.1, interpolation = cv2.INTER_AREA)
e1 = cv2.getTickCount() e1 = cv2.getTickCount()
# Hand Localization... # Hand Localization... possibly with YOLOv3? v2 is faster though...
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
@@ -27,7 +28,7 @@ img_hsv[:,:,0] = np.where(img_hsv[:,:,0] > 179, img_hsv[:,:,0] - 179, img_hsv[:,
img_hsv = cv2.GaussianBlur(img_hsv,(5,5),0) img_hsv = cv2.GaussianBlur(img_hsv,(5,5),0)
lower_skin = (0, 0, 153) lower_skin = (0, 0, 153)
upper_skin = (50, 153, 255) upper_skin = (45, 153, 255)
# Only need mask, as we can just use this to do the hand segmentation. # Only need mask, as we can just use this to do the hand segmentation.
mask = cv2.inRange(img_hsv, lower_skin, upper_skin) mask = cv2.inRange(img_hsv, lower_skin, upper_skin)
@@ -36,7 +37,8 @@ mask = cv2.inRange(img_hsv, lower_skin, upper_skin)
blur = cv2.GaussianBlur(mask,(5,5),0) blur = cv2.GaussianBlur(mask,(5,5),0)
ret, img_thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY) ret, img_thresh = cv2.threshold(blur, 50, 255, cv2.THRESH_BINARY)
img_thresh = mask # Uncomment if not using blur and threshold.
# img_thresh = mask
k = np.sum(img_thresh) / 255 k = np.sum(img_thresh) / 255
@@ -76,17 +78,14 @@ for pt in candidate_pts:
if new_distance > radius: if new_distance > radius:
radius = new_distance radius = new_distance
radius = int(radius * 0.55) radius = int(radius * 0.52)
# 140 needs to be replaced with a predicted value. i.e. not be a magic number. # 140 needs to be replaced with a predicted value. i.e. not be a magic number.
cv2.circle(img_thresh, centre, 140, (120,0,0), 3) # cv2.circle(img_thresh, centre, radius, (120,0,0), 3)
def calc_pos_y(x): def calc_pos_y(x):
return int((radius**2 - (x - centre[0])**2)**(1/2) + centre[1]) return int((radius**2 - (x - centre[0])**2)**(1/2) + centre[1])
print(img_thresh.shape)
print(centre)
print(radius)
# Now go around the circle to calculate num of times going 0->255 or vice-versa. # Now go around the circle to calculate num of times going 0->255 or vice-versa.
# First just do it the naive way with loops. # First just do it the naive way with loops.
# Equation of the circle: # Equation of the circle:
@@ -94,15 +93,14 @@ print(radius)
# Will just increment x to check, no need to loop y as well. # Will just increment x to check, no need to loop y as well.
# This is extremely slow, need to speed it up by removing for loop. # This is extremely slow, need to speed it up by removing for loop.
# Brings speed down to 20 fps. # Brings speed down to 20 fps.
# This is actually fast, it was just the print debug statements that made it slow, takes just 6ms...
# Could try a kerel method? # Could try a kerel method?
prev_x = centre[0] - radius prev_x = centre[0] - radius
prev_y = [calc_pos_y(centre[0] - radius), calc_pos_y(centre[0] - radius)] prev_y = [calc_pos_y(centre[0] - radius), calc_pos_y(centre[0] - radius)]
print(prev_y)
num_change = 0 num_change = 0
for x in range(centre[0] - radius + 1, centre[0] + radius): for x in range(centre[0] - radius + 1, centre[0] + radius):
ypos = calc_pos_y(x) ypos = calc_pos_y(x)
y = [ypos, centre[1] - (ypos-centre[1])] y = [ypos, centre[1] - (ypos-centre[1])]
print(y)
if(img_thresh[y[0], x] != img_thresh[prev_y[0], prev_x]): if(img_thresh[y[0], x] != img_thresh[prev_y[0], prev_x]):
num_change += 1 num_change += 1
if img_thresh[y[1], x] != img_thresh[prev_y[1], prev_x] and y[0] != y[1]: if img_thresh[y[1], x] != img_thresh[prev_y[1], prev_x] and y[0] != y[1]: