if 1.4 * w < 80: width = 80 else: width = int(1.4 * w) resized_hat = imutils.resize(hat, width = width) if (y + 0.25 * resized_hat.shape[0]) > resized_hat.shape[0] and (x - 5 + resized_hat.shape[1]) < frameClone.shape[1]: fix = 1 x_offset = x - 5 y_offset = y - 0.75 * resized_hat.shape[0] new_hat[y_offset:y_offset + resized_hat.shape[0], x_offset:x_offset + resized_hat.shape[1]] = resized_hat gray_hat = cv2.cvtColor(new_hat, cv2.COLOR_BGR2GRAY) # blurred_hat = cv2.GaussianBlur(gray_hat, (3,3), 0) # edged_hat = cv2.Canny(gray_hat, 169, 255) edged_hat = auto_canny.auto_canny(gray_hat) (_, cnts, _) = cv2.findContours(edged_hat.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # create negative mask cv2.drawContours(frameClone, cnts, -1, (0, 0, 0), cv2.FILLED) #create positive mask and combine if fix == 1: mask = np.zeros(frameClone.shape[:2], dtype = "uint8") cv2.drawContours(mask, cnts, -1, (255, 255, 255), cv2.FILLED) final = cv2.bitwise_and(new_hat, new_hat, mask = mask) # show our detected faces, then clear the frame in # preparation for the next frame
# loop over the face bounding boxes and draw them for (x, y, w, h) in faceRects: # cv2.rectangle(frameClone, (x, y), (x + w, y + h), (0, 255, 0), 2) resized_beard = imutils.resize(beard, width = w) if (resized_beard.shape[0] + y + (3/5 * h)) < frameClone.shape[0]: fix = 1 x_offset = x y_offset = y + (3/5 * h) new_beard[y_offset:y_offset + resized_beard.shape[0], x_offset:x_offset + resized_beard.shape[1]] = resized_beard gray_beard = cv2.cvtColor(new_beard, cv2.COLOR_BGR2GRAY) # blurred_beard = cv2.GaussianBlur(gray_beard, (5,5), 0) # edged_beard = cv2.Canny(gray_beard, 169, 255) edged_beard = auto_canny.auto_canny(gray_beard) (_, cnts, _) = cv2.findContours(edged_beard.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # print("I count {} beard(s)".format(len(cnts))) # create negative mask cv2.drawContours(frameClone, cnts, -1, (0, 0, 0), cv2.FILLED) # create positive mask and combine if fix == 1: mask = np.zeros(frameClone.shape[:2], dtype = "uint8") cv2.drawContours(mask, cnts, -1, (255, 255, 255), cv2.FILLED) final = cv2.bitwise_and(new_beard, new_beard, mask = mask)