示例#1
0
def add_salt_pepper_noise(image):
    # Need to produce a copy as to not modify the original image
    X_imgs_copy = image.copy()
    row, col, _ = image.shape
    salt_vs_pepper = 0.2
    amount = 0.004
    num_salt = np.ceil(amount * X_imgs_copy.size * salt_vs_pepper)
    num_pepper = np.ceil(amount * X_imgs_copy.size * (1.0 - salt_vs_pepper))

    seed = random.random()
    if seed > 0.75:
        # Add Salt noise
        coords = [
            np.random.randint(0, i - 1, int(num_salt))
            for i in X_imgs_copy.shape
        ]
        X_imgs_copy[coords[0], coords[1], :] = 1
    elif seed > 0.5:
        # Add Pepper noise
        coords = [
            np.random.randint(0, i - 1, int(num_pepper))
            for i in X_imgs_copy.shape
        ]
        X_imgs_copy[coords[0], coords[1], :] = 0
    else:
        return image
    return X_imgs_copy
示例#2
0
def annotate_image(image, model):
    # copy to draw on
    draw = image.copy()

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)

    # process image
    start = time.time()
    _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)

    # compute predicted labels and scores
    predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
    scores = detections[0,
                        np.arange(detections.shape[1]), 4 + predicted_labels]

    # correct for image scale
    detections[0, :, :4] /= scale

    # visualize detections
    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.3:
            continue
        b = detections[0, idx, :4].astype(int)
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)

    return draw
def detectAlphabets(imageToRecognize):
    args = parse_args()
    args.val_path = imageToRecognize
    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    keras.backend.tensorflow_backend.set_session(get_session())
    model = keras.models.load_model(os.path.join(dir, '../snapshots/resnet50_csv_wtext.h5'), custom_objects=custom_objects)
    test_image_data_generator = keras.preprocessing.image.ImageDataGenerator()
    #
    # # create a generator for testing data
    test_generator = CSVGenerator(
        csv_data_file=args.annotations,
        csv_class_file=args.classes,
        image_data_generator=test_image_data_generator,
        batch_size=args.batch_size
    )
    # index = 0
    # load image
    image = read_image_bgr(args.val_path)

    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)

    # process image
    start = time.time()
    _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)
    print('detections:', detections)
    # compute predicted labels and scores
    predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
    scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_labels]
    print("label=", predicted_labels)
    # correct for image scale
    scaled_detection = detections[0, :, :4] / scale

    # visualize detections
    recognized = {}

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.35:
            continue
        b = scaled_detection[idx, :4].astype(int)
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 1)

        caption = test_generator.label_to_name(label)
        if caption == "equal":
            caption = "="
        cv2.putText(draw, caption, (b[0], b[1] - 1), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
        recognized[caption] = b
        print(caption + ", score=" + str(score))

    plt.imshow(draw)
    plt.show()
    return recognized, draw
示例#4
0
def logopreds(prediction_model,image,p):

	draw = image.copy()
	draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

	image = preprocess_image(image)
	image, scale = resize_image(image)
	
	_, _, detections = prediction_model.predict_on_batch(np.expand_dims(image, axis=0))

	predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
	scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_labels]

	detections[:, :4] /= scale

	for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
		if score < 0.5:
			continue
		b = detections[0, idx, :4].astype(int)

		cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
		caption = "{} {:.3f}".format(p['l2n'][label], score)
		cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
		cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)
		
	return draw
def predict_level(crop_image, original_image, trigger):

    # if(trigger == True):
    # 	isLevel1 = detect_level_1(graph, sess, crop_image)
    # 	if(isLevel1 == True):
    # 		prediction = 'Level 1'
    # 		return prediction
    # 	else:
    # 		prediction = 'Nothing detected'
    # 		return prediction

    image = cv2.imread(crop_image)
    original_image = cv2.imread(original_image)
    # cv2.imshow('ori', original_image)
    # cv2.imshow('crop', image)
    # cv2.waitKey(0)
    # detect level 4 ( black )
    isLevel1 = detect_level_1(crop_image)
    if (isLevel1 == True):
        prediction = 1
        return prediction
    isLevel4 = detect_level_4(image.copy())
    if (isLevel4 == True):
        prediction = 4
        return prediction

    n_cluster = 3
    img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    img = img.reshape((img.shape[0] * img.shape[1], 3))
    clt = KMeans(n_cluster)
    clt.fit(img)
    hist = find_histogram(clt)
    bar, color = plot_colors2(hist, clt.cluster_centers_)
    # plt.axis('off')
    # plt.imshow(bar)
    # plt.show()
    depth_score = verify_pothole(color)
    # print('depth: ', depth_score)
    # print('image size', image.size)
    # print('original size', original_image.size)
    ratio = image.size / original_image.size
    # print('ratio:', ratio)

    final_ratio = ratio + depth_score
    # print('final ratio', final_ratio)
    if (final_ratio <= 1.0):
        prediction = 2
    else:
        prediction = 3
    # print(prediction)
    return prediction


# graph = tf.Graph()
# sess = tf.Session(graph = graph)
# with graph.as_default():
# 	with sess.as_default():
# 		#predict._init_(graph, sess)
# 		_init_(graph, sess)
# 		predict_level(graph, sess, crop_image, original_image, trigger)
示例#6
0
def threaded_resize(image, y, x, patch_width, patch_height, resize=False):
    clone = image.copy()

    crop_img = clone[y:y + patch_width, x:x + patch_height]
    if resize:
        return cv2.resize(crop_img, (224, 224))
    else:
        return crop_img
示例#7
0
def run_avg(image, accumWeight):
    global bg
    if bg is None:
        bg = image.copy().astype("float")
        return
    cv2.accumulateWeighted(
        image, bg, accumWeight
    )  # compute weighted average, accumulate it and update the background
示例#8
0
def predict():
    # initialize the data dictionary that will be returned from the
    # view
    app_id = request.args.get('app-id')
    logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
    logging.info('app-id = ' + app_id)
    data = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format and prepare it for
            # classification
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))
            image = prepare_image(
                image, (settings.IMAGE_WIDTH, settings.IMAGE_HEIGHT))

            # ensure our NumPy array is C-contiguous as well,
            # otherwise we won't be able to serialize it
            image = image.copy(order="C")

            # generate an ID for the classification then add the
            # classification ID + image to the queue
            k = app_id + '.' + str(uuid.uuid4())
            print(k)
            image = helpers.base64_encode_image(image)
            d = {"id": k, "image": image}
            db.rpush(settings.IMAGE_QUEUE, json.dumps(d))

            # keep looping until our model server returns the output
            # predictions
            while True:
                # attempt to grab the output predictions
                output = db.get(k)

                # check to see if our model has classified the input
                # image
                if output is not None:
                    # add the output predictions to our data
                    # dictionary so we can return it to the client
                    output = output.decode("utf-8")
                    data["predictions"] = json.loads(output)

                    # delete the result from the database and break
                    # from the polling loop
                    db.delete(k)
                    break

                # sleep for a small amount to give the model a chance
                # to classify the input image
                time.sleep(settings.CLIENT_SLEEP)

            # indicate that the request was a success
            data["success"] = True

    # return the data dictionary as a JSON response
    return flask.jsonify(data)
示例#9
0
    def callback(self, image):
        self.step += 1
        self.styled_image = image.copy()

        if self.verbose:
            print('Optimization step: %d/%d' % (self.step, self.iteration))

        if self.step == 1 or self.step % self.save_every_n_steps == 0:
            self.save_image(image)
示例#10
0
def quick_composite(image, mask, offset):
    if mask.ndim < 3:
        mask = np.dstack([mask] * 3)
    fg = cv2.cvtColor((image.copy() * 255).astype(np.uint8), cv2.COLOR_RGB2Lab)
    fg[:, :, 0] = np.clip(fg[:, :, 0] * offset, 0, 255)
    fg = cv2.cvtColor(fg, cv2.COLOR_Lab2RGB) / 255.
    comp = fg * mask + image * (1 - mask)
    comp = np.clip(comp, 0.0, 1.0)
    return comp
示例#11
0
def mask_image_with_mean_background(mask_object_found, image):
    new_image = image.copy()
    size_image = np.shape(mask_object_found)
    for j in range(size_image[0]):
        for i in range(size_image[1]):
            if mask_object_found[j][i] == 1:
                new_image[j, i, 0] = 103.939
                new_image[j, i, 1] = 116.779
                new_image[j, i, 2] = 123.68
    return new_image
示例#12
0
def cv2pil(image):
    """ OpenCV型 -> PIL型 """
    new_image = image.copy()
    if new_image.ndim == 2:  # モノクロ
        pass
    elif new_image.shape[2] == 3:  # カラー
        new_image = cv2.cvtColor(new_image, cv2.COLOR_BGR2RGB)
    elif new_image.shape[2] == 4:  # 透過
        new_image = cv2.cvtColor(new_image, cv2.COLOR_BGRA2RGBA)
    new_image = Image.fromarray(new_image)
    return new_image
示例#13
0
def classify_gesture(image):
    pred_image = image.copy()
    pred_image = np.expand_dims(pred_image, axis=0)
    pred_image = pred_image.reshape((-1, 28, 28, 1))
    image_class = model.predict(pred_image)

    try:
        gesture = GESTURES[image_class[0].tolist().index(1.0)]
    except ValueError:
        gesture = "NA"

    return gesture
def mask_nth_digit_in_image(image, n, out=None):
    """Return a copy of `image` with all but the `n`th digit masked."""
    # If we sum the image vertically, a plot of the column sums will present
    # four humps, each corresponding to a digit. This masking routine detects
    # the humps and masks the image so only the pixels contributing to the n'th
    # hump are visible.
    colsum = np.sum(image, axis=0).ravel()

    # # Now we need to find the least t such that image > t obtains four separate
    # # contiguous 'True' regions. We'll scan up from t = 0...
    # thresholded = colsum > 0                   # These two lines are just for
    # labels = sp.ndimage.label(thresholded)[0]  # memory allocation.

    # for t in range(int(max(colsum))):
    #   np.greater(colsum, t, out=thresholded)
    #   if sp.ndimage.label(thresholded, output=labels) == 4: break
    # else:
    #   raise RuntimeError('mask_nth_digit could not find four distinct digits '
    #                      'in an image.')

    # # We can now mask off the columns not containing the n'th digit.
    # if out is None:
    #   out = image.copy()
    # else:
    #   np.copyto(out, image)
    # out[:, labels != (n + 1)] = 0
    # return out

    # These columns are not allowed to have image boundaries in them.
    colsum[:7] = 1000
    colsum[9:13] = 1000
    colsum[16:19] = 1000
    colsum[23:] = 1000
    # The columns containing the four digits are separated by the three smallest
    # minima of colsum not at the edges of the image.
    left_less = colsum[:-1] < colsum[1:]
    # True entries in this array are 4 pixels left of all local minima in colsum.
    # This 4-offset allows us to ignore pixels at the edges of the image.
    minima_mask = left_less[4:-3] & ~left_less[3:-4]
    # So these are the minima's indices:
    minima_inds = np.argwhere(minima_mask).ravel() + 4
    # Find which three indices are associated with the smallest minima.
    boundaries = np.sort(minima_inds[np.argsort(colsum[minima_inds])[:3]])

    # Perform the masking now.
    if out is None:
        out = image.copy()
    elif out is not image:
        np.copyto(out, image)

    if n > 0: out[:, :(boundaries[n - 1]), ...] = 0
    if n < 3: out[:, (boundaries[n] + 1):, ...] = 0
    return out
示例#15
0
def classify_gesture_alt(image):
    pred_image = image.copy()
    pred_image = pred_image / 255
    pred_image = pred_image.reshape(-1, 28, 28, 1)
    image_class = model.predict(pred_image)

    try:
        gesture = GESTURES[image_class[0].tolist().index(1.0)]
    except ValueError:
        gesture = "NA"

    return gesture
示例#16
0
    def Objectdetection_Predict_Click(self):
        print("Objectdetection predict proceeding.....")
        if self.model_changed:
            keras.backend.tensorflow_backend.set_session(self.get_session())
            self.obd_model = models.load_model(self.model_path,
                                               backbone_name='resnet50')
            self.model_changed = False

        # keras.backend.tensorflow_backend.set_session(self.get_session())
        # model = models.load_model(self.model_path, backbone_name='resnet50')

        labels_to_names = {}
        if self.classes_flag:
            for i in range(len(self.classes)):
                labels_to_names[i] = self.classes[i]

        # 여기에 labels_to_names = 추가
        image = read_image_bgr(self.img_path)
        self.draw = image.copy()
        self.draw = cv2.cvtColor(self.draw, cv2.COLOR_BGR2RGB)
        image = preprocess_image(image)
        image, scale = resize_image(image)
        start = time.time()
        boxes, scores, labels = self.obd_model.predict_on_batch(
            np.expand_dims(image, axis=0))
        print('Loaded in : ', round(time.time() - start, 2), ' sec')
        print('Threshold : ', 0.5)
        boxes /= scale
        cnt = 0
        for box, score, label in zip(boxes[0], scores[0], labels[0]):
            cnt += 1
            # scores are sorted so we can break
            if score < 0.5:
                break

            color = label_color(label)
            b = box.astype(int)
            draw_box(self.draw, b, color=color)

            if self.classes_flag:
                caption = "{} {:.2f}%".format(labels_to_names[label],
                                              score * 100)
                print('Box', cnt, ' : ', box, labels_to_names[label], score)
            else:
                caption = "{} {:.2f}%".format(label, score * 100)
                print('Box', cnt, ' : ', box, ', ', label, ', ', score)
            draw_caption(self.draw, b, caption)
        self.update_signal.run()

        self.save_button.setEnabled(True)
        self.model_path_button.setEnabled(True)
        self.change_button_signal.run()
示例#17
0
def Draw_out(path):
    # load image
    image = read_image_bgr(path)
    # copy to draw ondraw = image.copy()
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = preprocess_image(image)
    image, scale = resize_image(image)
    #print ('scale',scale)

    # process image
    start = time.time()
    boxes, scores, labels = model.predict_on_batch(
        np.expand_dims(image, axis=0))
    #boxes是检测到的可能目标的框框
    #print ('boxes',boxes)
    #是对应的分值
    #print ('scores',scores)
    #对应的第几个标签
    #print ('labels',labels)
    #print("processing time: ", time.time() - start)

    # correct for image scale
    boxes /= scale

    # visualize detections
    for box, score, label in zip(boxes[0], scores[0], labels[0]):
        # scores are sorted so we can break
        if score < 0.4:
            break

        color = label_color(label)
        #print ('label',label)
        b = box.astype(int)
        #b是box取整
        #print ('b ',b)
        draw_box(draw, b, color=color)

        caption = "{} {:.3f}".format(labels_to_names[label], score)
        #single 0.431 是标签和分值
        #print ('caption ',caption)
        draw_caption(draw, b, caption)
    plt.figure(figsize=(10, 10))
    plt.axis('on')
    plt.imshow(draw)
    #plt.show()
    plt.savefig("static/js/powerbank_out.png")
    return caption
示例#18
0
def mask_image_with_mean_background(mask_object_found, image, color):
    new_image = image.copy()
    size_image = np.shape(mask_object_found)
    for j in range(size_image[0]):
        for i in range(size_image[1]):
            if mask_object_found[j][i] == 1:
                if (j == 0 or mask_object_found[j - 1][i] == 0) or (
                        j == size_image[0] - 1 or mask_object_found[j + 1][i]
                        == 0) or (i == 0 or mask_object_found[j][i - 1]
                                  == 0) or (i == size_image[1] - 1 or
                                            mask_object_found[j][i + 1] == 0):
                    new_image[j, i, 0] = color[0]
                    new_image[j, i, 1] = color[1]
                    new_image[j, i, 2] = color[2]
    return new_image
示例#19
0
def showImageCV(preds, imagePath, letter):
    # find the class label index with the largest corresponding
    # probability
    i = preds.argmax(axis=1)[0]
    # label = lb.classes_[i]
    # draw the class label + probability on the output image
    text = "{}: {:.2f}%".format(letter, preds[0][i] * 100)

    image = cv2.imread(imagePath)
    output = image.copy()
    cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
                (0, 0, 255), 2)
    # show the output image
    cv2_imshow(output)
    cv2.waitKey(0)
示例#20
0
def analyse_images(model, val_generator):
    for index in val_generator.size():
        # for index in range(11, 12):
        # load image
        image = val_generator.load_image(index)

        # copy to draw on
        draw = image.copy()
        # draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

        # preprocess image for network
        image = val_generator.preprocess_image(image)
        height, width, channels = image.shape
        font_size = int(height / 1000)
        if font_size < 1:
            font_size = 1
        image, scale = val_generator.resize_image(image)

        start = time.time()
        _, _, detections = get_predictions(model, image)
        print("processing time: ", time.time() - start)

        # compute predicted labels and scores
        predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
        scores = detections[0,
                            np.arange(detections.shape[1]),
                            4 + predicted_labels]

        # correct for image scale
        detections[0, :, :4] /= scale

        # visualize detections
        for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
            if score < 0.5:
                continue

            b = detections[0, idx, :4].astype(int)
            cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 255, 0), 3)
            caption = "{} {:.3f}".format(val_generator.label_to_name(label),
                                         score)
            cv2.putText(draw, caption, (b[0] - 50, b[1]),
                        cv2.FONT_HERSHEY_DUPLEX, font_size, (0, 0, 0), 3)
            cv2.putText(draw, caption, (b[0] - 50, b[1]),
                        cv2.FONT_HERSHEY_DUPLEX, font_size, (255, 255, 255), 2)

        cv2.imwrite('c:/test/' + str(index) + '.png', draw)
示例#21
0
def region(rects):
    #number of region proposals to show
    while True:
        # create a copy of original image
        imOut = image.copy()
        # itereate over all the region proposals
        for i, rect in enumerate(rects):
            # draw rectangle for region proposal till numShowRec
            if (i < numShowRects):
                x, y, w, h = rect
                r = []
                r.append(union(rects[i], rects[i + 1]))
                for (startX, startY, endX, endY) in r:
                    cv2.rectangle(imOut, (startX, startY), (endX, endY),
                                  (0, 255, 0), 1)
            else:
                break
        return imOut
def  visualize_bboxes(img_path,bboxes):
    # load image
    image = read_image_bgr(img_path)

    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    #print(bboxes)
    labels_to_names = labelsToNames()
    for i in range(bboxes.shape[0]):
        b = bboxes[i,0:4].astype(int)
        label = bboxes[i,4]
        score = bboxes[i,5]
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
示例#23
0
def logopreds(prediction_model, image, p):
    draw = image.copy()

    # Masking을 위한 프레임 생성
    mask = np.zeros(image.shape, np.uint8)
    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)

    image = preprocess_image(image)
    image, scale = resize_image(image)

    _, _, detections = prediction_model.predict_on_batch(np.expand_dims(image, axis=0))

    predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
    scores = detections[0, np.arange(detections.shape[1]), 4 + predicted_labels]

    detections[:, :4] /= scale

    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        if score < 0.3:
            continue
        b = detections[0, idx, :4].astype(int)

        # 타원으로 마스킹 하기 위한 계산(center: 타원 중심, axes: 중심에서 가장 큰 거리와 작은 거리)
        center = ((int)((b[0] + b[2]) / 2), (int)((b[1] + b[3]) / 2))
        axes = ((int)(abs((b[0] - b[2]) / 2)), (int)(abs((b[1] - b[3]) / 2)))

        if p['erase'] == "erase":

            # Detect된 영역 Masking(사각형)
            # cv2.rectangle(mask, (b[0], b[1]), (b[2], b[3]), (255, 255, 255), -1)

            # Detect된 영역 Masking(타원)
            cv2.ellipse(mask, center, axes, 0, 0, 360, 255, -1)

            # cv2.INPAINT_TELEA / cv2.INPAINT_NS
            dst = cv2.inpaint(draw, mask, 0.01, cv2.INPAINT_NS)
            draw = dst
        else:
            cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
            caption = "{} {:.3f}".format(p['l2n'][label], score)
            cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
            cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)
    #cv2.imshow("mask", mask)
    return draw
def extractSkin(image):
    # Taking a copy of the image
    img = image.copy()
    # Converting from BGR Colours Space to HSV
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

    # Defining HSV Threadholds
    lower_threshold = np.array([0, 48, 80], dtype=np.uint8)
    upper_threshold = np.array([20, 255, 255], dtype=np.uint8)

    # Single Channel mask,denoting presence of colours in the about threshold
    skinMask = cv2.inRange(img, lower_threshold, upper_threshold)

    # Cleaning up mask using Gaussian Filter
    skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0)

    # Extracting skin from the threshold mask
    skin = cv2.bitwise_and(img, img, mask=skinMask)

    # Return the Skin image
    return cv2.cvtColor(skin, cv2.COLOR_HSV2BGR)
def _get_body_part_partitions(image, rows, cols):
    front_cols, side_cols, rows = _get_grid_axes(rows, cols)

    image = image.copy()
    image = [np.rot90(image[:, :, i]) for i in range(0, 16, 4)]
    image[1] = np.fliplr(image[1])
    image[2] = np.fliplr(image[2])

    ret = []
    for label in _get_body_part_partition_labels():
        images = []
        for angle, p1, p2 in label:
            r1, c1 = p1
            r2, c2 = p2
            cols = front_cols if angle in (0, 2) else side_cols
            images.append(
                _crop_image(image[angle][rows[r1]:rows[r2 + 1],
                                         cols[c1]:cols[c2 + 1]]))
        ret.append(skimage.transform.resize(_concat_images(images),
                                            (256, 256)))
    return ret
def visualize_pred(img_path,bboxes):
    # load image
    image = read_image_bgr(img_path)

    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)
    #print(bboxes)
    labels_to_names = labelsToNames()
    for i in range(bboxes.shape[0]):
        b = bboxes[i,0:4].astype(int)
        label = bboxes[i,4]
        score = bboxes[i,5]
        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
        caption = "{} {:.3f}".format(labels_to_names[label], score)
        cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 0, 0), 3)
        cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_PLAIN, 1.5, (255, 255, 255), 2)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()
示例#27
0
    def select_region(self, image):
        """
        select area manually
        """
        # first, define the polygon by vertices
        rows, cols = image.shape[:2]
        pt_1 = [cols * 0.05, rows * 0.90]
        pt_2 = [cols * 0.05, rows * 0.70]
        pt_3 = [cols * 0.30, rows * 0.55]
        pt_4 = [cols * 0.6, rows * 0.15]
        pt_5 = [cols * 0.90, rows * 0.15]
        pt_6 = [cols * 0.90, rows * 0.90]

        vertices = np.array([[pt_1, pt_2, pt_3, pt_4, pt_5, pt_6]],
                            dtype=np.int32)
        point_img = image.copy()
        point_img = cv2.cvtColor(point_img, cv2.COLOR_GRAY2RGB)
        for point in vertices[0]:
            cv2.circle(point_img, (point[0], point[1]), 10, (0, 0, 255), 4)
        self.cv_show('point_img', point_img)

        return self.filter_region(image, vertices)
# Set the width and height of the frame for video to be saved
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Create a writer object to save the frames
writer = cv2.VideoWriter(r'C:\Users\Sharan Babu\Desktop\trimmed_video.mp4',
                         cv2.VideoWriter_fourcc(*'XVID'), 25, (width, height))
final_video = []

while True:
    success, image = cap.read()

    if success == True:
        final_video.append(image)
        img = image.copy()
        # Draw a rectangle to indicate the region of interest
        img = cv2.flip(img, 1)

        cv2.rectangle(img,
                      pt1=(450, 100),
                      pt2=(620, 300),
                      color=(0, 255, 0),
                      thickness=3)
        cv2.imshow("Video", img)

        roi = img[102:298, 448:618]

        # Image pre-processing for making predictions of the image
        data = cv2.resize(roi, (224, 224))
        data = np.array(data, dtype=np.float32)
示例#29
0
li = []
odir = "parse_imgs"
for filename in glob.glob(odir + "/*.png"):
    x.append(filename[11:])

new_set = [int(s.replace('.png', '')) for s in x]

new_set.sort()
new_n_set = ["parse_imgs/" + str(s) + ".png" for s in new_set]
# print(new_n_set)

for filename in new_n_set:
    model = "abc.model"
    # load the image
    image = cv2.imread(filename)
    orig = image.copy()
    # pre-process the image for classification
    image = cv2.resize(image, (30, 30))
    image = image.astype("float") / 255.0
    image = keras.preprocessing.image.img_to_array(image)
    image = np.expand_dims(image, axis=0)
    # load the trained convolutional neural network
    print("[INFO] loading network...")
    model = load_model(model)

    a = model.predict(image)[0]
    # print(a)
    pred = str(np.argmax(a))
    li.append(pred)
    print(str(np.argmax(a)) + "\t" + filename)
示例#30
0
def detect_one(image=None, index=0, threshold=0.5):
    # load image if none given:
    if type(image) == type(None):
        image = val_generator.load_image(index)

    # copy to draw on
    draw = image.copy()
    draw = cv2.cvtColor(draw, cv2.COLOR_BGR2RGB)

    # preprocess image for network
    image = val_generator.preprocess_image(image)
    image, scale = val_generator.resize_image(image)
    #     annotations = val_generator.load_annotations(index)
    #     index += 1

    # process image
    start = time.time()
    _, _, detections = model.predict_on_batch(np.expand_dims(image, axis=0))
    print("processing time: ", time.time() - start)

    # compute predicted labels and scores
    predicted_labels = np.argmax(detections[0, :, 4:], axis=1)
    scores = detections[0,
                        np.arange(detections.shape[1]), 4 + predicted_labels]

    # correct for image scale
    detections[0, :, :4] /= scale

    # visualize detections
    count = 0
    max_score = -1

    from collections import OrderedDict

    #     dets = []

    #     max_score2 = -1

    caption = ""

    for idx, (label, score) in enumerate(zip(predicted_labels, scores)):
        max_score = max(max_score, score)
        if score < threshold:
            continue
        b = detections[0, idx, :4].astype(int)

        assert len(b) == 4

        cv2.rectangle(draw, (b[0], b[1]), (b[2], b[3]), (0, 0, 255), 3)
        caption = f'{val_generator.label_to_name(label)} {score:.3f}'
        cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    1.5, (0, 0, 0), 3, cv2.LINE_AA)
        cv2.putText(draw, caption, (b[0], b[1] - 10), cv2.FONT_HERSHEY_SIMPLEX,
                    1.5, (255, 255, 255), 2, cv2.LINE_AA)

    print(caption)

    plt.figure(figsize=(15, 15))
    plt.axis('off')
    plt.imshow(draw)
    plt.show()