Exemple #1
0
    def train(self, training_data_path):
        faces = []
        labels = []
        #cv2.namedWindow('Training...', cv2.WND_PROP_FULLSCREEN)
        #cv2.setWindowProperty('Training...', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        #screen_width, screen_height = utils.get_screen_resolution()
        #window_width = int(screen_width * 0.8)
        #window_height = int(screen_height * 0.8)
        #cv2.namedWindow('Training...', cv2.WINDOW_NORMAL)
        #cv2.resizeWindow('Training...', window_width, window_height)
        cv2.namedWindow('Training...')
        cv2.moveWindow('Training...', 0, 0)

        dirs = os.listdir(training_data_path)
        print dirs

        label = 0
        for dir_name in dirs:
            if dir_name.startswith('.'):
                continue
            subject = dir_name
            print subject, label
            subject_path = os.path.join(training_data_path, dir_name)
            subject_image_files = os.listdir(subject_path)
            # Read each image, detect the face, add the detected face to the
            # subject's list of faces
            for image_name in subject_image_files:
                # Ignore system files like .DS_Store
                if image_name.startswith('.') or image_name == 'name.txt':
                    continue
                print image_name
                image_path = os.path.join(subject_path, image_name)
                image = cv2.imread(image_path)

                # Display an image window to show the image
                #cv2.imshow('Training...', image)
                small_image = cv2.resize(image, None, fx=0.1, fy=0.1)
                cv2.imshow('Training...', small_image)
                cv2.waitKey(100)

                # Detect and record face
                face, rect = self.face_detector.detect(image)
                if face is not None:
                    small_rectangle_coordinates = utils.scale_coordinates(
                        rect, 0.1)
                    utils.draw_rectangle(small_image,
                                         small_rectangle_coordinates)
                    cv2.imshow('Training...', small_image)
                    cv2.waitKey(100)
                    faces.append(face)
                    labels.append(label)

            label += 1

        # Clean up after ourselves
        cv2.destroyWindow('Training...')
        cv2.waitKey(1)
        cv2.destroyAllWindows()

        self.face_recogniser.train(faces, numpy.array(labels))
    def draw(self, app, cr):
        utils.draw_rectangle(
            cr, self._rect, self._fill_color, self._outline_color)

        node_data = [
            # Nodes, input, offset, text align
            (self._component.inputs, True,
             (NODE_RADIUS+2, 0), utils.TextHAlign.LEFT),
            (self._component.outputs, False,
             (-NODE_RADIUS-2, 0), utils.TextHAlign.RIGHT),
        ]

        for nodes, is_input, offset, text_align in node_data:
            for i, node in enumerate(nodes):
                position = self.node_pos(is_input, i)
                connected = node.is_connected()
                fill_color = _node_color(node.value, connected)
                utils.draw_circle(cr, position, NODE_RADIUS, fill_color, BLACK)
                utils.draw_text(
                    cr, node.label, position + offset, size=10,
                    bold=True, h_align=text_align)

        name = self._component.name
        if name:
            position = self.center - (0, self._rect.height/2+8)
            utils.draw_text(cr, name, position, bold=True)

        self._component.on_draw(app, cr)
def process(img_folder, label_boxes, hog, clf):
    for f in file_iterator(img_folder, 'jpg'):
        img = cv2.cvtColor(cv2.imread(f), cv2.COLOR_BGR2GRAY)
        f = os.path.basename(f)

        print ('Processing file %s'.format(f))
        windows = detect(img, hog, clf)
        label_box = label_boxes[f]
        img_with_box = img.copy()
        draw_rectangle(img_with_box, label_box, color=(255,55,0))

        if len(windows) == 0:
            cv2.imwrite(LOW_ACCURACY_IMG_PATH + f, img_with_box)
            with open(ACCURACY_FILE, 'a') as af:
                af.write(f + ',0\n')
            continue

        pred_box = get_corner(flattern(windows))
        accuracy = cpt_accuracy(label_box, pred_box)
        with open(ACCURACY_FILE, 'a') as af:
            af.write(f + ',' + str(accuracy) + '\n')

        draw_rectangle(img_with_box, pred_box, color=(0,55,255))
        cv2.imwrite(PREDICT_PATH + f, img_with_box)

        if accuracy < 0.5:
            cv2.imwrite(LOW_ACCURACY_IMG_PATH + f, img_with_box)
            apply_hard_negative(img, flattern(windows), label_box)
Exemple #4
0
    def web_identify():

        form = IdentifyForm(request.form)

        if request.method == 'POST':
            if len(request.files) > 0:
                file = request.files['file']
                if utils.is_picture(file.filename):
                    try:
                        pilimage = Image.open(file.stream, 'r').convert('RGB')
                    except Exception as e:
                        flash("Error: {}".format(e))
                        return render_template('identify.html', form=form)

                    try:
                        faces = dlib_api.detect_and_identify_faces(
                            np.array(pilimage))
                    except Exception as e:
                        flash("Error: {}".format(e))
                        return render_template('identify.html', form=form)

                    font = utils.get_font(1024)

                    draw = ImageDraw.Draw(pilimage)
                    for p, rect, shape in faces:
                        s = font.getsize(p.name)
                        utils.draw_rectangle(draw, rect, 5, color=128)
                        draw.text(
                            ((rect.right() - rect.left()) / 2 + rect.left() -
                             int(s[0] / 2), rect.top() - s[1] - 20),
                            p.name,
                            fill=128,
                            font=font)
                    del draw

                    tmpfile = os.path.join('static/tmp',
                                           '{}.jpg'.format(uuid.uuid4()))

                    # resize
                    pilimage = utils.resize_image(pilimage, 1024)

                    with open(tmpfile, 'wb+') as fp:
                        pilimage.save(fp, 'JPEG')

                    flash('found {} faces... '.format(len(faces)) +
                          '\n'.join(("id: {}, name: {}".format(p.id, p.name)
                                     for p, _, _ in faces)))

                    return render_template('identify.html',
                                           form=form,
                                           proc_image=tmpfile)

                else:
                    flash('Error: Only images allowed!')
            else:
                flash('Error: All the form fields are required. ')

        return render_template('identify.html', form=form)
Exemple #5
0
    def draw(self, app, cr, mouse_pos):
        color = (0, 0, 1)
        for component in self._components:
            rect = component.display.rect
            rect = shapes.Rectangle(
                size=rect.size+(20, 20),
                position=rect.position-(10, 10))
            utils.draw_rectangle(
                cr, rect, fill_color=None, outline_color=color)

        if self._select_pos:
            rect = _get_rectangle(self._select_pos, mouse_pos)
            utils.draw_rectangle(
                cr, rect, fill_color=None, outline_color=color)
	def _capture_image(self, img):
		img = cv2.resize(img, (self.widthImg, self.heightImg))  # RESIZE IMAGE
		imgBlank = np.zeros((self.heightImg, self.widthImg, 3),
		                    np.uint8)  # CREATE A BLANK IMAGE FOR TESTING DEBUGING IF REQUIRED
		imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # CONVERT IMAGE TO GRAY SCALE
		imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1)  # ADD GAUSSIAN BLUR
		thres = utils.get_track_bar_values()  # GET TRACK BAR VALUES FOR THRESHOLDS
		imgThreshold = cv2.Canny(imgBlur, thres[0], thres[1])  # APPLY CANNY BLUR
		kernel = np.ones((5, 5))
		imgDial = cv2.dilate(imgThreshold, kernel, iterations = 2)  # APPLY DILATION
		imgThreshold = cv2.erode(imgDial, kernel, iterations = 1)  # APPLY EROSION

		## FIND ALL COUNTOURS
		imgContours = img.copy()  # COPY IMAGE FOR DISPLAY PURPOSES
		imgBigContour = img.copy()  # COPY IMAGE FOR DISPLAY PURPOSES
		contours, hierarchy = cv2.findContours(imgThreshold, cv2.RETR_EXTERNAL,
		                                       cv2.CHAIN_APPROX_SIMPLE)  # FIND ALL CONTOURS
		cv2.drawContours(imgContours, contours, -1, (0, 255, 0), 10)  # DRAW ALL DETECTED CONTOURS

		# FIND THE BIGGEST COUNTOUR
		biggest, maxArea = utils.calc_biggest_contour(contours)  # FIND THE BIGGEST CONTOUR

		# If connected corners found -> capture image.
		if biggest.size != 0:
			biggest = utils.reorder_points(biggest)
			cv2.drawContours(imgBigContour, biggest, -1, (0, 255, 0), 20)  # DRAW THE BIGGEST CONTOUR
			imgBigContour = utils.draw_rectangle(imgBigContour, biggest, 2)
			pts1 = np.float32(biggest)  # PREPARE POINTS FOR WARP
			pts2 = np.float32(
				[[0, 0], [self.widthImg, 0], [0, self.heightImg], [self.widthImg, self.heightImg]])  # PREPARE POINTS FOR WARP
			matrix = cv2.getPerspectiveTransform(pts1, pts2)
			imgWarpColored = cv2.warpPerspective(img, matrix, (self.widthImg, self.heightImg))

			# REMOVE 20 PIXELS FORM EACH SIDE
			imgWarpColored = imgWarpColored[20:imgWarpColored.shape[0] - 20, 20:imgWarpColored.shape[1] - 20]
			imgWarpColored = cv2.resize(imgWarpColored, (self.widthImg, self.heightImg))

			# APPLY ADAPTIVE THRESHOLD
			imgWarpGray = cv2.cvtColor(imgWarpColored, cv2.COLOR_BGR2GRAY)
			imgAdaptiveThre = cv2.adaptiveThreshold(imgWarpGray, 255, 1, 1, 9, 2)
			imgAdaptiveThre = cv2.bitwise_not(imgAdaptiveThre)
			imgAdaptiveThre = cv2.medianBlur(imgAdaptiveThre, 3)
			# print("Done")


			# Image Array for Display
			img_all = ([img, imgGray, imgThreshold, imgContours],
			           [imgBigContour, imgWarpColored, imgWarpGray, imgAdaptiveThre])

			return imgAdaptiveThre, img_all
		else:
			img_all = ([img, imgGray, imgThreshold, imgContours],
			                  [imgBlank, imgBlank, imgBlank, imgBlank])
			return None, img_all
Exemple #7
0
 def on_draw(component, app, cr):
     lines = list(itertools.chain.from_iterable(
         textwrap.wrap(line, width=max_width, tabsize=4)
         for line in component.data['text'].split('\n')
     ))
     if lines and not lines[-1]:
         lines.pop()
     lines = lines[-max_height:]
     grid_size = app.grid_size
     position = (
         component.display.rect.bottom_left +
         (grid_size//2, -grid_size//2)
     )
     rect = shapes.Rectangle(
         position=position-(4, 12*max_height),
         size=(max_width*7+8, max_height*12+4))
     utils.draw_rectangle(cr, rect, (0.9, 0.9, 0.9), (0, 0, 0))
     for i, line in enumerate(reversed(lines)):
         pos = position - (0, i*12)
         utils.draw_text(
             cr, line, pos,
             h_align=utils.TextHAlign.LEFT,
             v_align=utils.TextVAlign.BOTTOM)
Exemple #8
0
def cf(x):
    return sv.predict(x)[0] in target_index


def score_cf(imag):
    scores = np.array(sv.predict_softmax_score(imag))
    return (np.argmax(scores)
            in target_index) and (np.max(scores) > param['score_bottom_line'])


for i, img in enumerate(data.test_img):
    print("{}th image in progress".format(i))
    temp_img, matrix = sw.sliding_window(
        img,
        score_cf,
        window_size=param['ss_dic']['window_size'],
        stride=16,
        boundary=param['sw_dic']['boundary'])
    temp_img.save('./test_result/sw' + str(i) + '.jpg', 'jpeg')
    img.save('./Images/sw' + str(i) + '.jpg', 'jpeg')
    box_set = sw.make_box(matrix)
    for box in box_set:
        img = utils.draw_rectangle(img, box, label='block')
    xml = utils.box_to_xml('sw' + str(i) + '.xml', path + str(i) + '.jpg',
                           img.size + (3, ), box_set)
    ElementTree(xml).write('./Annotation/sw' + str(i) + '.xml')
    txt = utils.box_to_txt([0 for _ in range(len(box_set))], box_set, img.size)
    with open('./labels/sw' + str(i) + '.txt', 'w') as f:
        f.write(txt)
    img.save('./test_result/sw_box' + str(i) + '.jpg', 'jpeg')
Exemple #9
0
def debug_box(webcam, X, Y):
    img = load_webcam(webcam)
    color = average_color(img, X, Y)
    draw_rectangle(img, X, Y, color=color)
    return send_file(img_to_io(img), mimetype='image/jpeg')
Exemple #10
0
import cv2
import numpy as np
from utils import draw_rectangle,paint_landmark_points

location = "outputs\\part1_with_rectangle_"
image = cv2.imread("images\\aydemirakbas.png")
draw_rectangle(image,location+"aydemirakbas.png")
image = cv2.imread("images\\deniro.jpg")
draw_rectangle(image,location+"deniro.png")
image = cv2.imread("images\\kimbodnia.png")
draw_rectangle(image,location+"kimbodnia.png")

location = "outputs\\part1_with_landmarks_collage.png"
image = cv2.imread("images\\aydemirakbas.png")
image1 = paint_landmark_points(image)
image = cv2.imread("images\\deniro.jpg")
image2 = paint_landmark_points(image)
image = cv2.imread("images\\kimbodnia.png")
image3 = paint_landmark_points(image)

image = cv2.imread("images\\cat.jpg")
image4 = paint_landmark_points(image,np.load("images\\cat_landmarks.npy"))
image = cv2.imread("images\\gorilla.jpg")
image5 = paint_landmark_points(image,np.load("images\\gorilla_landmarks.npy"))
image = cv2.imread("images\\panda.jpg")
image6 = paint_landmark_points(image,np.load("images\\panda_landmarks.npy"))

top_image = cv2.hconcat([image6, image4, image5]) #Concatenate animal images horizontally
bottom_image = cv2.hconcat([image2, image1, image3]) #Concatenate human images horizontally
collage = cv2.vconcat([top_image, bottom_image]) # Concatenate top_image and bottom_image vertically
cv2.imwrite(location, collage) #Write image on specified place with "location"
Exemple #11
0
        RET, FRAME = VID_FEED.read()
        if not RET:
            print("Unable to capture video")
            sys.exit()

        if DRAW_HARRIS:
            FRAME = draw_harris_kps(FRAME)

        # MATCH_DATA = find_homographies(REF_DSC, FRAME)
        MATCH_DATA, _ = get_homographies_contour(FRAME, REF_IMAGES, MATCH_DATA,
                                                 None)

        for ind, homography in enumerate(MATCH_DATA):
            # homography = match_tuple[0]
            if homography is not None:
                projection_matrix, R, T = get_matrix(CAM_PARAMS, homography)
                print(R, T)
                # FRAME = draw_corners(FRAME, homography)
                FRAME = render(FRAME, OBJ, projection_matrix, REF_IMAGES[ind],
                               False)
                if RECTANGLE:
                    FRAME, _ = draw_rectangle(homography, REF_IMAGES[ind],
                                              FRAME)

        cv2.imshow('frame', FRAME)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    VID_FEED.release()
    cv2.destroyAllWindows()
Exemple #12
0
def doc_scan_pipeline(input=PATH, output="./img/scanned_doc.jpg"):
    img = cv2.imread(input)

    # 0. Convert given image from BGR to RGB format
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    h, w, _ = img.shape
    img = cv2.resize(img, (width, height))

    # 1. Convert to grayscale
    img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

    # 2. Add Gaussian blur
    img_blur = cv2.GaussianBlur(img_gray, (5, 5), 1)

    # 3. Add Canny edge detection
    img_threshold = cv2.Canny(img_blur, 100, 200, L2gradient=True)

    # 3.1 Apply dilation
    kernel = np.ones((3, 3))
    img_threshold = cv2.dilate(img_threshold, kernel, iterations=2)

    # 4. Find all the contours
    img_contours = img.copy()
    img_big_contour = img.copy()
    contours, hierarchy = cv2.findContours(img_threshold, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_SIMPLE)
    cv2.drawContours(image=img_contours,
                     contours=contours,
                     contourIdx=-1,
                     color=(0, 255, 0),
                     thickness=5)

    # 5. Find the biggest contour
    biggest, maxArea = biggest_contour(contours)
    biggest = reorder(biggest)
    cv2.drawContours(image=img_big_contour,
                     contours=biggest,
                     contourIdx=-1,
                     color=(0, 255, 0),
                     thickness=10)

    # 5.1 Draw a rectangle, i.e., 4 lines connecting the 4 dots corresponding to the largest contour
    img_big_contour = draw_rectangle(img_big_contour, biggest, thickness=2)

    pts1 = np.float32(biggest)
    pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])

    # 6. Image Warp
    # 6.1 Calculate a 3x3 perspective transform matrix
    matrix = cv2.getPerspectiveTransform(pts1, pts2)

    # 6.2 Apply the perspective matrix to the image
    img_warp_coloured = cv2.warpPerspective(img, matrix, (width, height))

    # 7. Adaptive thresholding
    img_warp_gray = cv2.cvtColor(img_warp_coloured, cv2.COLOR_BGR2GRAY)
    img_adaptive_th = cv2.adaptiveThreshold(img_warp_gray, 255, 1,
                                            cv2.THRESH_BINARY, 5, 2)

    # 7.1 Apply median blurring to remove tiny speckles of noise
    img_adaptive_th = cv2.medianBlur(img_adaptive_th, 3)

    # Save the document to disk
    cv2.imwrite(output, img_adaptive_th)

    # Add labels to each image
    img = draw_text(img, "Original")
    img_gray = draw_text(img_gray, "Grayscale")
    img_blur = draw_text(img_blur, "Gaussian Blur", pos=(int(width / 4), 50))
    img_threshold = draw_text(img_threshold,
                              "Canny Edge",
                              pos=(int(width / 4), 50))
    img_contours = draw_text(img_contours, "Contours")
    img_big_contour = draw_text(img_big_contour,
                                "Largest Contour",
                                pos=(int(width / 7), 50))
    img_warp_coloured = draw_text(img_warp_coloured,
                                  "Warp",
                                  pos=(int(width / 3), 50))
    img_adaptive_th = draw_text(img_adaptive_th,
                                "Adaptive Thresholding",
                                pos=(int(width / 7), 50),
                                font_scale=2,
                                font_thickness=6)

    blank_img = np.zeros((height, width, 3), dtype=np.uint8)
    image_list = [
        img, img_gray, img_blur, img_threshold, img_contours, img_big_contour,
        img_warp_coloured, img_adaptive_th
    ]

    # Combine the images into a grid
    # image_grid returns PIL image, np.asarray() can be used to convert it back to cv2 compatible format
    grid = np.asarray(image_grid(image_list, width, height))
Exemple #13
0
Runner script to test during development.
"""

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

from augment import Pipeline
from horizontal import RandomHorizontalFlip
from scale import RandomScale, Scale
from translate import RandomTranslate, Translate
from rotate import RandomRotate
from utils import draw_rectangle

image = cv.imread("styrofoam cup8.jpg")[:, :, ::-1]
bbox = np.array([[402, 0, 799, 270, 1]]).astype('float64')

plt.imshow(draw_rectangle(image, bbox))
plt.show()

print(bbox, type(bbox))

transforms = Pipeline(
    [RandomHorizontalFlip(1),
     Translate(x=0.2, y=0.2),
     RandomRotate((-2, 2))])
image, bbox = transforms(image, bbox)
print(bbox, type(bbox))

plt.imshow(draw_rectangle(image, bbox))
plt.show()