def mask_image(self, img): #blur_k_size = (11, 11) #img = cv2.blur(img, blur_k_size) w, h, c = img.shape skin_mask = sd.mask_skin(img) # determine candidates for classification using mask skin_mask_reshaped = skin_mask.reshape(-1, 1) candidates = (np.nonzero(skin_mask_reshaped))[0] # reshaped image for collecting pixels reshaped_img = img.reshape(-1, 3) out = np.zeros(w*h, np.uint8) # classify start_time = time.time() logits = self.classifier.classify(reshaped_img[candidates]) # collect skin pixels skin_idx = np.nonzero(np.array(logits) == 1) out[candidates[skin_idx]] = 255 out = out.reshape(w, h) kernel= cv2.getStructuringElement(\ cv2.MORPH_ELLIPSE,(5,5)) out = cv2.erode(out, kernel, iterations = 1) return out
def collect_data(self, img1, img2): mask1 = sd.mask_skin(img1) mask2 = sd.mask_skin(img2) and_mask = cv2.bitwise_and(mask1, mask2) # mask neg image of min_image neg = self.collect_neg_data(img1, and_mask) # mask pos image of min_image pos = self.collect_pos_data(img1, mask1, and_mask) # mask pos image of max_image pos = np.append(pos, \ self.collect_pos_data(img2, \ mask2, and_mask), axis=0) return pos, neg
def shake_detect(self, prev_frame, curr_frame): h, w, c = curr_frame.shape blur_size = 2 * round(w * 5 / 320) + 1 threshold = 0.8 * 255 copy = curr_frame curr_frame = cv2.medianBlur(curr_frame, blur_size) prev_frame = cv2.medianBlur(prev_frame, blur_size) mask = sd.mask_skin(curr_frame) diff = abs_diff(curr_frame, prev_frame) diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) #diff[diff < threshold] = 0 diff = cv2.bitwise_and(diff, diff, mask=mask) diff = erode_frame(diff) cv2.imshow('diff', diff) k = cv2.waitKey(5) & 0xFF return diff, self.shaker.shake_detect(diff, copy)
if __name__ == '__main__': # test data folder_dir = "../test-data/" img1_dir = folder_dir + "img_max.png" img2_dir = folder_dir + "img_min.png" test_dir = folder_dir + "img_test.png" ''' img1_dir = "../test-data/classifier_test1.png" img2_dir = "../test-data/classifier_test2.png" test_dir = "../test-data/classifier_test3.png" ''' min_img = cv2.imread(img1_dir) min_img = cv2.resize(min_img, None, fx=0.1, fy=0.1, interpolation=cv2.INTER_AREA) max_img = cv2.imread(img2_dir) max_img = cv2.resize(max_img, None, fx=0.1, fy=0.1, interpolation=cv2.INTER_AREA) test_img = cv2.imread(test_dir) test_img = cv2.resize(test_img, None, fx=0.1, fy=0.1, interpolation=cv2.INTER_AREA) copy = test_img start_time = time.time() scc = SkinColorClassifier(min_img, max_img) out = scc.mask_image(test_img) print("Whole Time duration: " + str(time.time() - start_time)) print("Test Image Size: " + str(test_img.shape)) cv2.imshow('fixed mask', sd.mask_skin(copy)) cv2.imshow('rf mask', out) cv2.waitKey(0)
import cv2 import os import skin_detection as sd from random import * i = 1 cam = cv2.VideoCapture(0) BIG_NUM = 1000000000 while True: ret_val, img = cam.read() frame_size = (240, 180) img = cv2.resize(img, frame_size) img = sd.mask_skin(img) cv2.imshow('my webcam', img) k = cv2.waitKey(1) out_dir = "../data/train-data/" folder = out_dir + "R/" if k == 27: break elif k == 49: i = 1 print("Changed to R") folder = out_dir + "R/" elif k == 50: print("Changed to P") folder = out_dir + "P/" elif k == 51:
ret, prev_frame = cap.read() prev_frame = render_frame(prev_frame) frame_cnt = 0 threshold = 0.8 * 255 yhistory1 = [] yhistory2 = [] while cap.isOpened(): ret, curr_frame = cap.read() if ret: curr_frame = render_frame(curr_frame) mask = sd.mask_skin(curr_frame) diff = frame_diff(curr_frame, prev_frame) diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY) diff[diff < threshold] = 0 diff = cv2.bitwise_and(diff, diff, mask=mask) diff = erode_frame(diff) diff = cv2.cvtColor(diff, cv2.COLOR_GRAY2BGR) idx1 = np.argwhere(diff > 0) cnt1 = len(idx1) y_sum1 = 0 for i in idx1: y_sum1 += i[0] if cnt1 is not 0: yhistory1.append(y_sum1/cnt1) elif len(yhistory1) != 0: