Пример #1
0
    def process(self, cascade, nested_cascade, img_file):
        cascade = cv2.CascadeClassifier(self.current_dir + cascade)
        nested = cv2.CascadeClassifier(self.current_dir + nested_cascade)

        gray = cv2.imread(self.current_dir + img_file, 0)
        # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # gray = cv2.equalizeHist(gray)

        face_rects = self.detect(gray, cascade)
        vis = gray.copy()
        self.draw_rects(vis, face_rects, (0, 255, 0))

        eye_count = 0
        if not nested.empty():
            for x1, y1, x2, y2 in face_rects:
                face_roi = gray[y1:y2, x1:x2]
                face_vis = vis[y1:y2, x1:x2]

                eyes_rects = self.detect(face_roi.copy(), nested)
                self.draw_rects(face_vis, eyes_rects, (255, 0, 0))

                for x1, y1, x2, y2 in eyes_rects:
                    eyes_roi = face_roi[y1:y2, x1:x2]

                    edge_eyes_roi = edge_detect.edge_detect(eyes_roi)
                    helper.save_image(
                        edge_eyes_roi, self.current_dir +
                        '/../resources/eyes_roi%s.png' % (eye_count))

                    # edge_eyes_roi = self.noise_removal.median(edge_eyes_roi)
                    edge_eyes_roi = self.noise_removal.opening(edge_eyes_roi)
                    helper.save_image(
                        edge_eyes_roi, self.current_dir +
                        '/../resources/eyes_roi_noise_removal%s.png' %
                        (eye_count))
                    eye_count += 1

                    edge_eyes_roi = np.array(edge_eyes_roi, dtype=np.uint8)
                    eyes_vis = face_vis[y1:y2, x1:x2]

                    circles = cv2.HoughCircles(edge_eyes_roi,
                                               cv2.cv.CV_HOUGH_GRADIENT,
                                               1,
                                               20,
                                               param1=50,
                                               param2=30,
                                               minRadius=0,
                                               maxRadius=0)
                    if circles is not None:
                        circles = np.uint16(np.around(circles))
                        for i in circles[0, :]:
                            cv2.circle(eyes_vis, (i[0], i[1]), i[2],
                                       (0, 255, 0), 2)

        cv2.imshow('face detect', vis)
Пример #2
0
    def process(self, cascade, nested_cascade, img_file):
        cascade = cv2.CascadeClassifier(self.current_dir + cascade)
        nested = cv2.CascadeClassifier(self.current_dir + nested_cascade)

        gray = cv2.imread(self.current_dir + img_file, 0)
        # gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        # gray = cv2.equalizeHist(gray)

        face_rects = self.detect(gray, cascade)
        vis = gray.copy()
        self.draw_rects(vis, face_rects, (0, 255, 0))

        eye_count = 0
        if not nested.empty():
            for x1, y1, x2, y2 in face_rects:
                face_roi = gray[y1:y2, x1:x2]
                face_vis = vis[y1:y2, x1:x2]

                eyes_rects = self.detect(face_roi.copy(), nested)
                self.draw_rects(face_vis, eyes_rects, (255, 0, 0))

                for x1, y1, x2, y2 in eyes_rects:
                    eyes_roi = face_roi[y1:y2, x1:x2]

                    edge_eyes_roi = edge_detect.edge_detect(eyes_roi)
                    helper.save_image(edge_eyes_roi, self.current_dir + "/../resources/eyes_roi%s.png" % (eye_count))

                    # edge_eyes_roi = self.noise_removal.median(edge_eyes_roi)
                    edge_eyes_roi = self.noise_removal.opening(edge_eyes_roi)
                    helper.save_image(
                        edge_eyes_roi, self.current_dir + "/../resources/eyes_roi_noise_removal%s.png" % (eye_count)
                    )
                    eye_count += 1

                    edge_eyes_roi = np.array(edge_eyes_roi, dtype=np.uint8)
                    eyes_vis = face_vis[y1:y2, x1:x2]

                    circles = cv2.HoughCircles(
                        edge_eyes_roi, cv2.cv.CV_HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0
                    )
                    if circles is not None:
                        circles = np.uint16(np.around(circles))
                        for i in circles[0, :]:
                            cv2.circle(eyes_vis, (i[0], i[1]), i[2], (0, 255, 0), 2)

        cv2.imshow("face detect", vis)
Пример #3
0
                    os.makedirs(epoch_dir)
                helper.write_loss_in_txt(g_loss, epoch)
                saver.save(sess, config.TRAIN.out_dir + "/model.ckpt")
            except:
                time.sleep(1)

        ## ------------ validation loop -------------------------
        for ind in range(len(val_file_list)):
            A_file_name_val = config.base_dir + config.VAL.A_data_dir + '/' + val_file_list[ind]
            if not os.path.isfile(A_file_name_val):  # test label
                continue
            A_image_val = helper.read_image(A_file_name_val)  # training image A
            # B_image_val = helper.read_image(B_file_name_val)  # training image A
            output = sess.run(generator_test, feed_dict={input_A_test: A_image_val})
            output = np.concatenate([A_image_val, output, B_image], axis=2)
            helper.save_image(output, config.TRAIN.out_dir + "/%04d/" % epoch + val_file_list[ind].replace('.jpg', '_out.jpg'))



# ---------------------------------------------------
#                      test
# ---------------------------------------------------
if config.TEST.is_test:
    test_file_list = os.listdir(config.base_dir + config.TEST.A_data_dir)
    if not os.path.isdir(config.TEST.out_dir + config.TEST.out_dir_postfix):
        os.makedirs(config.TEST.out_dir + config.TEST.out_dir_postfix)
    time_list = np.zeros(len(test_file_list), dtype=float)
    for ind in range(len(test_file_list)):
        A_file_name_val = config.base_dir + config.TEST.A_data_dir + '/' + test_file_list[ind]
        if not os.path.isfile(A_file_name_val):
            continue
Пример #4
0

def _prewitt(self):
    return ([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]],
            [[-1, -1, -1], [0, 0, 0], [1, 1, 1]])


def bounding_box(img, range_w=None, range_h=None):
    img = histogram_equalization.adaptive_mean(img)
    cv2.imshow('binary', img)

    contours, hierarchy = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_KCOS)
    boundings = []
    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        if (not range_w or (w >= range_w[0] and w <= range_w[1])) and \
                (not range_h or (h >= range_h[0] and h <= range_h[1])):
            boundings.append((x, y, w, h))
            cv2.rectangle(img, (x, y), (x + w, y + h), (255), 2)

    return img, boundings


if __name__ == '__main__':
    pix = helper.read_image('../resources/fp01.jpg')
    pix = helper.convert_gray(pix)
    helper.save_image('../resources/gray.png', True)
    pix = basic(pix)
    # noise_removal = NoiseRemoval()
    # pix = noise_removal.opening(pix)
    helper.save_image(pix, '../resources/edge.png')