text:
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 18 23:19:54 2020

@author: arthurd
"""

import osmnx as ox
import numpy as np
import time

# Visualize the data
import matplotlib.pyplot as plt
from matplotlib import collections as mc

# Test the import
from noiseplanet.matcher import model
from noiseplanet.matcher.model import route

def test_nearest():
    print('Test match_nearest_edge(graph, track)')
    track = np.array( [[45.75815477,  4.83579607],
                       [45.75815501,  4.83578568],
                       [45.75813165,  4.83577836],
                       [45.75811917,  4.83577826],
                       [45.75811917,  4.83577826],
                       [45.75810508,  4.83574771],
                       [45.75807665,  4.83572188],
                       [45.75807665,  4.83572188],
                       [45.75806175,  4.83570647],
                       [45.75805712,  4.8356987 ],
                       [45.75804746,  4.83569629],
                       [45.75803752,  4.83568949],
                       [45.75802591,  4.83566137],
                       [45.75802629,  4.83565147],
                       [45.75802629,  4.83565147],
                       [45.75801363,  4.83562952],
                       [45.75800574,  4.83562477],
                       [45.75800574,  4.83562477],
                       [45.75799968,  4.83560606],
                       [45.75800028,  4.83559067],
                       [45.75800028,  4.83559067],
                       [45.75799686,  4.83558285],
                       [45.75797956,  4.83556543],
                       [45.75797466,  4.83555272],
                       [45.75797466,  4.83555272],
                       [45.75796105,  4.83554806],
                       [45.75793787,  4.83553723],
                       [45.75793787,  4.83553723],
                       [45.75793882,  4.83553601],
                       [45.75792874,  4.83550413],
                       [45.75793143,  4.83549635],
                       [45.75793143,  4.83549635],
                       [45.75791593,  4.83548718],
                       [45.75788795,  4.83546475],
                       [45.75788795,  4.83546475],
                       [45.75786658,  4.83547026],
                       [45.75783632,  4.83546862],
                       [45.75783632,  4.83546862],
                       [45.75782964,  4.83547046],
                       [45.75780487,  4.83546808],
                       [45.7577991 ,  4.83545974]])

    # Create graph
    graph = model.graph_from_track(track, network='all')

    # Compute the path
    start = time.time()
    track_corr, route_corr, edgeid, stats = model.match_nearest_edge(graph, track)
    print('Map Matching nearest in {0}s'.format(round(time.time() - start, 5)))
    # Visualization Nearest
    fig, ax = ox.plot_graph(graph, node_color="skyblue", node_alpha=.5, node_size=15, show=False, close=False, annotate=False)
    plt.title("Map Matching to the closest edge", color="#999999")

    plt.scatter(track[:, 1], track[:, 0], s=30, marker='.', color="black", zorder=2, label='Original Point')
    plt.plot(track[:, 1], track[:, 0], linewidth=2, alpha=.7, color="black")
    plt.scatter(track_corr[:, 1], track_corr[:, 0], s=30, marker='.', color="darkcyan", zorder=2, label='Projected Point')
    plt.plot(route_corr[:, 1], route_corr[:, 0], linewidth=2, alpha=.7, color="darkcyan")

    # projection between the two tracks
    lines = [[(track[i, 1], track[i, 0]), (track_corr[i, 1], track_corr[i, 0])] for i in range(len(track))]
    lc = mc.LineCollection(lines, linestyle='--', colors='skyblue', alpha=1, linewidths=1, zorder=1, label='Projection')
    ax.add_collection(lc)

    ax.legend(loc=1, frameon=True, facecolor='w')

def test_leuven():
    print('Test match_leuven(graph, track)')
    track = np.array( [[45.75815477,  4.83579607],
                       [45.75815501,  4.83578568],
                       [45.75813165,  4.83577836],
                       [45.75811917,  4.83577826],
                       [45.75811917,  4.83577826],
                       [45.75810508,  4.83574771],
                       [45.75807665,  4.83572188],
                       [45.75807665,  4.83572188],
                       [45.75806175,  4.83570647],
                       [45.75805712,  4.8356987 ],
                       [45.75804746,  4.83569629],
                       [45.75803752,  4.83568949],
                       [45.75802591,  4.83566137],
                       [45.75802629,  4.83565147],
                       [45.75802629,  4.83565147],
                       [45.75801363,  4.83562952],
                       [45.75800574,  4.83562477],
                       [45.75800574,  4.83562477],
                       [45.75799968,  4.83560606],
                       [45.75800028,  4.83559067],
                       [45.75800028,  4.83559067],
                       [45.75799686,  4.83558285],
                       [45.75797956,  4.83556543],
                       [45.75797466,  4.83555272],
                       [45.75797466,  4.83555272],
                       [45.75796105,  4.83554806],
                       [45.75793787,  4.83553723],
                       [45.75793787,  4.83553723],
                       [45.75793882,  4.83553601],
                       [45.75792874,  4.83550413],
                       [45.75793143,  4.83549635],
                       [45.75793143,  4.83549635],
                       [45.75791593,  4.83548718],
                       [45.75788795,  4.83546475],
                       [45.75788795,  4.83546475],
                       [45.75786658,  4.83547026],
                       [45.75783632,  4.83546862],
                       [45.75783632,  4.83546862],
                       [45.75782964,  4.83547046],
                       [45.75780487,  4.83546808],
                       [45.7577991 ,  4.83545974]])

    # Create graph
    graph = model.graph_from_track(track, network='all')

    # Compute the path
    start = time.time()
    track_corr, route_corr, edgeid, stats = model.match_leuven(graph, track)
    print('Map Matching hmm in {0}s'.format(round(time.time() - start, 5)))
    # Visualization Nearest
    fig, ax = ox.plot_graph(graph, node_color="skyblue", node_alpha=.5, node_size=15, show=False, close=False, annotate=False)
    plt.title("Map Matching to the closest edge", color="#999999")

    plt.scatter(track[:, 1], track[:, 0], s=30, marker='.', color="black", zorder=2, label='Original Point')
    plt.plot(track[:, 1], track[:, 0], linewidth=2, alpha=.7, color="black")
    plt.scatter(track_corr[:, 1], track_corr[:, 0], s=30, marker='.', color="darkcyan", zorder=2, label='Projected Point')
    plt.plot(route_corr[:, 1], route_corr[:, 0], linewidth=2, alpha=.7, color="darkcyan")

    # projection between the two tracks
    lines = [[(track[i, 1], track[i, 0]), (track_corr[i, 1], track_corr[i, 0])] for i in range(len(track))]
    lc = mc.LineCollection(lines, linestyle='--', colors='skyblue', alpha=1, linewidths=1, zorder=1, label='Projection')
    ax.add_collection(lc)

    ax.legend(loc=1, frameon=True, facecolor='w')

def test_route_from_track():
    print('Test route_from_track(track)')
    track = np.array(  [[45.75809136,  4.83577159],
                        [45.7580932 ,  4.83576182],
                        [45.7580929 ,  4.8357634 ],
                        [45.75809207,  4.8357678 ],
                        [45.75809207,  4.8357678 ],
                        [45.75809647,  4.83574439],
                        [45.75809908,  4.83573054],
                        [45.75809908,  4.83573054],
                        [45.75810077,  4.83572153],
                        [45.75810182,  4.83571596],
                        [45.75810159,  4.83571719],
                        [45.7581021 ,  4.83571442],
                        [45.7580448 ,  4.83558152],
                        [45.75804304,  4.83558066],
                        [45.75804304,  4.83558066],
                        [45.75802703,  4.83557288],
                        [45.75801895,  4.83556895],
                        [45.75801895,  4.83556895],
                        [45.75800954,  4.83556438],
                        [45.75800681,  4.83556305],
                        [45.75800681,  4.83556305],
                        [45.75800209,  4.83556076],
                        [45.75798288,  4.83555142],
                        [45.75797578,  4.83554797],
                        [45.75797578,  4.83554797],
                        [45.75796259,  4.83554156],
                        [45.7579395 ,  4.83553033],
                        [45.7579395 ,  4.83553033],
                        [45.75794009,  4.83553062],
                        [45.75792429,  4.83552294],
                        [45.75792505,  4.83552331],
                        [45.75792505,  4.83552331],
                        [45.7579092 ,  4.83551561],
                        [45.75787935,  4.8355011 ],
                        [45.75787935,  4.8355011 ],
                        [45.75786135,  4.83549235],
                        [45.75783387,  4.83547899],
                        [45.75783387,  4.83547899],
                        [45.75782827,  4.83547626],
                        [45.75780555,  4.83546522],
                        [45.7577986 ,  4.83546184]])

    edgeid = np.array([ [6135818902, 6135818901],
                        [6135818901, 6135818902],
                        [6135818902, 6135818901],
                        [6135818901, 6135818902],
                        [6135818901, 6135818902],
                        [6135818901, 6135818902],
                        [6135818901, 6135818902],
                        [6135818901, 6135818902],
                        [6135818902, 6135818901],
                        [6135818902, 6135818901],
                        [6135818901, 6135818902],
                        [6135818902, 6135818901],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765],
                        [ 192313667, 1777581765]])
    graph = route.graph_from_track(track)
    start = time.time()
    route_corr, stats = route.route_from_track(graph, track, edgeid=None)
    print('Route without edgeid computed in : {0}s'.format(round(time.time() - start, 5)))
    start = time.time()
    route_corr, stats = route.route_from_track(graph, track, edgeid=edgeid)
    print('Route with edgeid computed in : {0}s'.format(round(time.time() - start, 5)))

    # Visualization leuven - Leuven Map Matching
    fig, ax = ox.plot_graph(graph, node_color="skyblue", node_alpha=.5, node_size=15, show=False, close=False, annotate=False)
    plt.title("Map Matching with Viterbi's algorithm (leuven)", color="#999999")
    plt.scatter(track[:, 1], track[:, 0], s=30, marker='.', color="darkcyan", zorder=2, label='Original Point')
    plt.plot(route_corr[:, 1], route_corr[:, 0], linewidth=2, alpha=.7, color="darkcyan")

if __name__ == "__main__":

    # Testing the different method of Map Matching
    test_nearest()
    test_leuven()

    # Testing route functions
    test_route_from_track()

max_stars_repo_name: jks-liu/noiseplanet

max_issues_repo_name: voodooed/noiseplanet

max_forks_repo_name: voodooed/noiseplanet


text:
#!/usr/bin/env python
from __future__ import print_function
import roslib
roslib.load_manifest('mct_transform_2d')
import rospy
import numpy
import cv
import cv2
import sys
import threading
import mct_active_target
import functools
import scipy.optimize
from mct_blob_finder import BlobFinder
from mct_utilities import file_tools
from cv_bridge.cv_bridge import CvBridge

# Services
from mct_msg_and_srv.srv import GetBool
from mct_msg_and_srv.srv import GetBoolResponse
from mct_msg_and_srv.srv import GetFlagAndMessage
from mct_msg_and_srv.srv import GetFlagAndMessageResponse
from mct_msg_and_srv.srv import GetTransform2d
from mct_msg_and_srv.srv import GetTransform2dResponse

# Messages
from sensor_msgs.msg import Image

WAITING = 0
WORKING = 1
FINISHED= 2

STATE_MESSAGE = {
        WAITING  : "Waiting",
        WORKING  : "Calibrating",
        FINISHED : "Finished",
        }

STATE_COLOR = {
        WAITING  : (0,255,255),
        WORKING  : (0,0,255),
        FINISHED : (0,255,0) ,
        }

class Transform_2D_Calibrator(object):

    def __init__(self, topic_0, topic_1):

        self.ready = False
        self.state = WAITING  # There are 3 allowed states WAITING, WORKING, FINISHED
        #self.state = WORKING
        self.topics = topic_0, topic_1
        self.bridge = CvBridge()
        self.lock = threading.Lock()

        rospy.init_node('transform_2d_calibrator')
        self.node_name = rospy.get_name()

        # Initialize data lists
        self.blobs = {
                self.topics[0]: [],
                self.topics[1]: [],
                }
        self.index_to_image = {
                self.topics[0]: {},
                self.topics[1]: {},
                }
        self.overlap_indices = []

        # Set active target information and turn off leds
        target_info = mct_active_target.active_target_info()
        self.led_n_max = target_info[0]
        self.led_m_max = target_info[1]
        self.number_of_leds = self.led_n_max*self.led_m_max
        self.led_max_power = target_info[2]
        self.led_space_x = target_info[3]
        self.led_space_y = target_info[3]
        mct_active_target.off()

        # Current led indices
        self.led_n = 0
        self.led_m = 0

        # Wait counter for image acquisition

        self.image_wait_cnt = {
                self.topics[0]: 0,
                self.topics[1]: 0,
                }

        # Sleep periods for idle and wait count loops
        self.idle_sleep_dt = 0.25
        self.wait_sleep_dt = 0.005

        # Led power setting
        params_namespace = '/transform_2d_calibrator_params'
        self.led_power = rospy.get_param(
                '{0}/target/led_power'.format(params_namespace),
                10
                )

        # Wait count for image acquisition
        self.image_wait_number = rospy.get_param(
                '{0}/image_wait_number'.format(params_namespace),
                4
                )

        # Initialize blob finder
        self.blobFinder = BlobFinder()
        self.blobFinder.threshold = rospy.get_param(
                '{0}/blob_finder/threshold'.format(params_namespace),
               150
                )
        self.blobFinder.filter_by_area = rospy.get_param(
                '{0}/blob_finder/filter_by_area'.format(params_namespace),
                False
                )
        self.blobFinder.min_area = rospy.get_param(
                '{0}/blob_finder/min_area'.format(params_namespace),
                0
                )
        self.blobFinder.max_area = rospy.get_param(
                '{0}/blob_finder/max_area'.format(params_namespace),
                200
                )

        # Number of points required
        self.num_points_required = rospy.get_param(
                '{0}/num_points_required'.format(params_namespace),
                10
                )
        self.transform = None
        self.transform_error = None

        # Get homography matrices
        self.homography_dict = {}
        for topic in self.topics:
            try:
                # Get the data from the parameter server
                rows = rospy.get_param('{0}/homography_matrix/rows'.format(topic))
                cols = rospy.get_param('{0}/homography_matrix/cols'.format(topic))
                data = rospy.get_param('{0}/homography_matrix/data'.format(topic))
            except KeyError:
                # Data is not on the parameter server - try getting it by reading the file
                camera = get_camera_from_topic(topic)
                homography = file_tools.read_homography_calibration(camera)
                rows = homography['rows']
                cols = homography['cols']
                data = homography['data']

            # Rearrange into numpy matrix
            homography_matrix = numpy.array(homography['data'])
            homography_matrix = homography_matrix.reshape((rows,cols))
            self.homography_dict[topic] = homography_matrix

        # Set font and initial image information
        self.cv_text_font = cv.InitFont(cv.CV_FONT_HERSHEY_TRIPLEX, 0.8, 0.8,thickness=1)
        self.image_info= 'no data'

        # Subscription to image topic
        image_callback_0 = functools.partial(self.image_callback, self.topics[0])
        image_callback_1 = functools.partial(self.image_callback, self.topics[1])
        self.image_sub = {
                self.topics[0] : rospy.Subscriber(self.topics[0], Image, image_callback_0),
                self.topics[1] : rospy.Subscriber(self.topics[1], Image, image_callback_1),
                }

        # Publications - bcalibration progress images for topics 0 and 1
        self.image_pub = {
                self.topics[0] : rospy.Publisher('image_transform_calibration_0', Image),
                self.topics[1] : rospy.Publisher('image_transform_calibration_1', Image),
                }

        # Services
        self.start_srv = rospy.Service(
                '{0}/start'.format(self.node_name),
                GetFlagAndMessage,
                self.handle_start_srv
                )

        self.is_calibrated_srv = rospy.Service(
                '{0}/is_calibrated'.format(self.node_name),
                GetBool,
                self.handle_is_calibrated_srv
                )

        self.get_transform_2d_srv = rospy.Service(
                '{0}/get_transform_2d'.format(self.node_name),
                GetTransform2d,
                self.handle_get_transform_2d_srv
                )

        self.ready = True

    def handle_get_transform_2d_srv(self,req):
        """
        Handles requests to get the transform found by the
        """
        if self.transform is None:
            rotation = 0.0
            translation_x = 0.0
            translation_y = 0.1
        else:
            rotation = self.transform['rotation']
            translation_x = self.transform['translation_x']
            translation_y = self.transform['translation_y']
        return GetTransform2dResponse(rotation, translation_x, translation_y)

    def handle_start_srv(self,req):
        """
        Handles to start/restart the homography calibration procedure.
        """
        flag = True
        message = ''
        with self.lock:
            flag, message = mct_active_target.lock(self.node_name)
            if flag:
                self.image_info = ''
                self.state = WORKING
                self.led_n = 0
                self.led_m = 0
                self.blobs = {topic_0: [], topic_1: []}
                self.index_to_image = {topic_0: {}, topic_1: {}}
                self.overlap_indices = []
                self.transform = None
                self.transform_error = None
        return GetFlagAndMessageResponse(flag,message)

    def handle_is_calibrated_srv(self,req):
        """
        Handles requests for whether or not the transform calibration has been completed.
        """
        if self.transform is not None:
            value = True
        else:
            value = False
        return GetBoolResponse(value)

    def image_callback(self,topic,data):
        """
        Image topic callback function. Uses the blobFinder to find the blobs
        (leds) in the image. Publishes images showing the calibration progress.
        """
        # Find blobs
        with self.lock:

            if not self.ready:
                return

            if self.state == WORKING:
                self.blobs[topic], blobs_image = self.blobFinder.findBlobs(data)
                blobs_rosimage = self.bridge.cv_to_imgmsg(blobs_image,encoding="passthrough")
            else:
                self.blobs[topic] = []

            if self.image_wait_cnt[topic] < self.image_wait_number:
                self.image_wait_cnt[topic] += 1

        # Create calibration data  image
        cv_image = self.bridge.imgmsg_to_cv(data,desired_encoding="passthrough")
        ipl_image = cv.GetImage(cv_image)
        calib_image = cv.CreateImage(cv.GetSize(ipl_image), cv.IPL_DEPTH_8U, 3)
        cv.CvtColor(ipl_image,calib_image,cv.CV_GRAY2BGR)

        # Add calibration markers to image
        color = STATE_COLOR[self.state]
        if self.state == WORKING:
            for image_point in self.index_to_image[topic].values():
                x,y = image_point
                cv.Circle(calib_image, (int(x),int(y)),3, color)
        else:
            for index in self.overlap_indices:
                x,y = self.index_to_image[topic][index]
                cv.Circle(calib_image, (int(x),int(y)),3, color)

        ## Add text to image
        message = [STATE_MESSAGE[self.state]]
        if self.state == WORKING or self.state==FINISHED:
            if self.state == WORKING:
                num_points_found = len(self.index_to_image[topic])
            else:
                num_points_found = len(self.overlap_indices)
            message.append('{0}/{1} pts'.format(num_points_found,self.get_led_count()))
        if self.image_info:
            message.append('- {0}'.format(self.image_info))
        if (self.state == FINISHED) and (self.transform_error is not None):
                message.append('- error {0:1.2f} mm'.format(1e3*self.transform_error))
        message = ' '.join(message)
        cv.PutText(calib_image,message,(10,25),self.cv_text_font,color)

        # Publish calibration progress image
        calib_rosimage = self.bridge.cv_to_imgmsg(calib_image,encoding="passthrough")
        self.image_pub[topic].publish(calib_rosimage)

    def wait_for_images(self):
        """
        Wait for 'image_wait_number' of images to be acquired.
        """
        with self.lock:
            for topic in self.topics:
                self.image_wait_cnt[topic] = 0
        done = False
        while not done:
            with self.lock:
                image_wait_cnt = min([self.image_wait_cnt[t] for t in self.topics])
            if image_wait_cnt >= self.image_wait_number:
                done = True
            rospy.sleep(self.wait_sleep_dt)

    def increment_led(self):
        """
        Increments the led array indices. When the last led is reached check to see if
        sufficient points have been captured for the homography calibratoin. If so, then
        the state is changed to FINISHED.  If insufficient points have been gathered then
        the state is changed back to WAITING.
        """

        if self.led_m < self.led_m_max-1 or self.led_n < self.led_n_max-1:
            if self.led_n < self.led_n_max-1:
                self.led_n += 1
            else:
                self.led_n  = 0
                self.led_m += 1
        else:

            # Turn off led and unlock active target
            mct_active_target.off()
            mct_active_target.unlock(self.node_name)

            # Need to check that we got enough done otherwise return to idle.
            self.state = FINISHED
            self.overlap_indices = self.get_overlap_indices()
            if len(self.overlap_indices) >= self.num_points_required:
                self.state = FINISHED
                self.image_info = ''
            else:
                self.state = WAITING
                self.image_info = 'not enough data'
                self.transform = None

    def get_overlap_indices(self):
        """
        Returns the overlaping points
        """
        indices_0 = self.index_to_image[self.topics[0]]
        indices_1 = self.index_to_image[self.topics[1]]
        overlap_indices = set(indices_0)
        overlap_indices.intersection_update(indices_1)
        return list(overlap_indices)

    def get_led_count(self):
        return self.led_n + self.led_m*self.led_n_max + 1

    def save_blob_data(self):
        """
        Save blob data found in images
        """
        with self.lock:
            for topic in self.topics:
                if len(self.blobs[topic]) == 1:
                    blob = self.blobs[topic][0]
                    image_x = blob['centroid_x']
                    image_y = blob['centroid_y']
                    self.index_to_image[topic][ (self.led_n, self.led_m) ] = (image_x, image_y)

    def find_transform(self):
        """
        Find 2d transform (rotation + translation) from the world points found
        in topic[0] and topic[1] and thier respective homographies.
        """
        # Get world points for both topics for all points in overlap
        world_points_dict = {}

        for topic in self.topics:

            # Get homography from image coordinates to world coordinates
            homography_matrix = self.homography_dict[topic]
            homography_matrix_t = homography_matrix.transpose()

            # Get image points for indices in overlap
            image_points = []
            for index in self.overlap_indices:
                image_points.append(self.index_to_image[topic][index])
            image_points = numpy.array(image_points)

            # Convert to homogenious coordinates and compute world coordinates
            image_points_hg = numpy.ones((image_points.shape[0],3))
            image_points_hg[:,:2] = image_points
            world_points_hg = numpy.dot(image_points_hg, homography_matrix_t)
            world_points = numpy.array(world_points_hg[:,:2])
            denom = numpy.zeros((world_points.shape[0],2))
            denom[:,0] = world_points_hg[:,2]
            denom[:,1] = world_points_hg[:,2]
            world_points = world_points/denom
            world_points_dict[topic] = world_points

        # Estimate translate as mean difference in point positions
        points_0 = world_points_dict[self.topics[0]]
        points_1 = world_points_dict[self.topics[1]]
        rotation_est, translation_est = estimate_transform_2d(points_0, points_1)

        rotation, translation, error = fit_transform_2d(
                points_0,
                points_1,
                rotation_est,
                translation_est
                )

        self.transform = {
                'rotation'      : rotation,
                'translation_x' : translation[0],
                'translation_y' : translation[1],
                }

        self.transform_error = error

    def run(self):
        """
        Node main function. When the state is WORKING this function activates
        the leds on the active calibration target one at a time. When all of
        the leds have been activated and if sufficient number of points have
        been collected the homography matrix is calculated.
        """

        while not rospy.is_shutdown():

            if self.state == WORKING:
                # Turn on current led and wait for images
                mct_active_target.set_led(self.led_n, self.led_m, self.led_power)
                self.wait_for_images()
                self.save_blob_data()
                self.increment_led()

            elif self.state == FINISHED and self.transform is None:
                # Calculate 2d transforms
                self.find_transform()
            else:
                rospy.sleep(self.idle_sleep_dt)

# ----------------------------------------------------------------------------------

def fit_transform_2d(points_0, points_1, rotation_est, translation_est):
    """
    Fits the 2d transformation from points
    """
    def error_func(x):
        dx, dy, angle = x[0], x[1], x[2]
        rot_matrix = get_rot_matrix(angle)
        rot_matrix_t = rot_matrix.transpose()
        points_1_pred = numpy.dot(points_0,rot_matrix_t) + numpy.array([dx,dy])
        error = (points_1_pred - points_1)**2
        error = numpy.sqrt(error.sum(axis=1))
        return error.mean()

    # Find best least squares fit using downhill simplex
    x0 = numpy.array([translation_est[0], translation_est[1], rotation_est])
    result = scipy.optimize.fmin(error_func, x0,disp=False, full_output=True)
    sol, error = result[0], result[1]
    rotation, translation = sol[2], sol[:2]
    return rotation, translation, error

def estimate_transform_2d(points_0, points_1):
    """
    Estimates 2d transformation from corresponding points_0 and points_1
    """

    # Estimate rotation - as mean angle between a and b vectors
    a = points_0 - points_0[0,:]
    b = points_1 - points_1[0,:]
    a = a[1:,:]
    b = b[1:,:]
    a_dot_b = a[:,0]*b[:,0] + a[:,1]*b[:,1]
    a_cross_b = a[:,0]*b[:,1] - a[:,1]*b[:,0]
    rotation_est = numpy.arctan2(a_cross_b, a_dot_b)
    rotation_est = rotation_est.mean()

    # Un-rotate and estimate translation
    rot_matrix = get_rot_matrix(rotation_est)
    rot_matrix_t = rot_matrix.transpose()
    points_0_rot = numpy.dot(points_0,rot_matrix_t)
    translation_est = points_1 - points_0_rot
    translation_est = translation_est.mean(axis=0)
    return rotation_est, translation_est

def get_rot_matrix(angle):
    """
    Returns 2d rotation matrix given the rotation angle
    """
    rot_matrix = numpy.array([
        [numpy.cos(angle), -numpy.sin(angle)],
        [numpy.sin(angle),  numpy.cos(angle)],
        ])
    return rot_matrix

def get_camera_from_topic(topic):
    """
    Gets the camera name from the image topic. Note, assumes that topic is
    of the form /<machine>/<camera>/...

    """
    topic_split = topic.split('/')
    camera = topic_split[2]
    return camera

# -----------------------------------------------------------------------------
if __name__ == '__main__':
    topic_0 = sys.argv[1]
    topic_1 = sys.argv[2]
    node = Transform_2D_Calibrator(topic_0, topic_1)
    node.run()

ext: py

max_stars_repo_name: iorodeo/mct

max_issues_repo_name: iorodeo/mct

max_forks_repo_name: iorodeo/mct

reason: import numpy,import scipy


text:
#!/usr/bin/env python
"""Run gammapy benchmarks"""
import getpass
import importlib
import logging
import platform
import shutil
import subprocess
import sys
import tempfile
import warnings
from pathlib import Path

import click
import numpy as np
import yaml
from psrecord.main import monitor

log = logging.getLogger(__name__)

THIS_REPO = Path(__file__).parent

AVAILABLE_BENCHMARKS = {
    "io": "io.py",
    "analysis_3d": "analysis_3d.py",
    "analysis_3d_joint": "analysis_3d_joint.py",
    "lightcurve_1d": "lightcurve_1d.py",
    "lightcurve_3d": "lightcurve_3d.py",
    "spectrum_1d": "spectrum_1d.py",
    "spectrum_1d_joint": "spectrum_1d_joint.py",
}

MONITOR_OPTIONS = {"duration": None, "interval": 0.5, "include_children": True}

def get_provenance():
    """Compute provenance info about software and data used."""
    data = {
        "env": {
            "user": getpass.getuser(),
            "machine": platform.machine(),
            "system": platform.system(),
        },
        "software": {},
    }

    data["software"]["python_executable"] = sys.executable
    data["software"]["python_version"] = platform.python_version()
    data["software"]["numpy"] = importlib.import_module("numpy").__version__
    data["software"]["scipy"] = importlib.import_module("scipy").__version__
    data["software"]["astropy"] = importlib.import_module("astropy").__version__
    data["software"]["gammapy"] = importlib.import_module("gammapy").__version__

    return data

@click.group()
@click.option(
    "--log-level",
    default="info",
    type=click.Choice(["debug", "info", "warning", "error", "critical"]),
)
@click.option("--show-warnings", is_flag=True, help="Show warnings?")
def cli(log_level, show_warnings):
    """
    Run and manage Gammapy benchmarks.
    """
    levels = dict(
        debug=logging.DEBUG,
        info=logging.INFO,
        warning=logging.WARNING,
        error=logging.ERROR,
        critical=logging.CRITICAL,
    )
    logging.basicConfig(level=levels[log_level])
    log.setLevel(level=levels[log_level])

    if not show_warnings:
        warnings.simplefilter("ignore")

@cli.command("run-benchmark", help="Run Gammapy benchmarks")
@click.argument("benchmarks", type=click.Choice(list(AVAILABLE_BENCHMARKS) + ["all"]))
def run_benchmarks(benchmarks):
    info = get_provenance()

    if benchmarks == "all":
        benchmarks = list(AVAILABLE_BENCHMARKS)
    else:
        benchmarks = [benchmarks]

    result = {}  # condenses the results from each benchmark

    for benchmark in benchmarks:
        results_folder = THIS_REPO / f"results/{benchmark}"
        results_folder.mkdir(exist_ok=True, parents=True)

        results_filename = results_folder / "results.txt"
        plot_filename = results_folder / "results.png"
        provenance_filename = results_folder / "provenance.yaml"

        run_single_benchmark(
            benchmark,
            logfile=str(results_filename),
            plot=str(plot_filename),
            **MONITOR_OPTIONS,
        )

        log.info("Writing {}".format(results_filename))
        log.info("Writing {}".format(plot_filename))

        with provenance_filename.open("w") as fh:
            log.info("Writing {}".format(provenance_filename))
            yaml.dump(info, fh, default_flow_style=False)

        dict = {}
        t, cpu, _, memory = np.loadtxt(results_filename, unpack=True)
        dict["total_time"] = float(max(t))
        dict["CPU_max"] = float(max(cpu[2:]))
        dict["CPU_mean"] = float(np.mean(cpu[2:]))
        dict["memory_peak"] = float(np.max(memory))

        result[benchmark] = dict

    yaml_filename = THIS_REPO / "results/results.yaml"
    with yaml_filename.open("w") as fh:
        log.info("Writing {}".format(yaml_filename))
        yaml.dump(result, fh, default_flow_style=False)

def run_single_benchmark(benchmark, **kwargs):
    script_path = Path(AVAILABLE_BENCHMARKS[benchmark]).absolute()
    cmd = "python {}".format(script_path)
    log.info(f"Executing command: {cmd}")

    with tempfile.TemporaryDirectory() as path:
        process = subprocess.Popen(cmd, shell=True, cwd=str(path))
        pid = process.pid
        monitor(pid, **kwargs)
        shutil.copyfile(Path(path) / "bench.yaml", f"results/{benchmark}/bench.yaml")

if __name__ == "__main__":
    cli()

max_stars_repo_name: adonath/gammapy-benchmarks

max_issues_repo_name: adonath/gammapy-benchmarks

max_forks_repo_name: adonath/gammapy-benchmarks

reason: import numpy


text:
# Dataset from UCI dataset: https://archive.ics.uci.edu/ml/datasets/Breast+Cancer+Wisconsin+%28Original%29
import numpy as np
from sklearn import preprocessing, cross_validation, neighbors
import pandas as pd

df = pd.read_csv('breast-cancer-wisconsin.data.txt')
df.replace('?', -99999, inplace=True)
# drop useless data
df.drop(['id'], 1, inplace=True)

X = np.array(df.drop(['class'], 1))
y = np.array(df['class'])

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2)

clf = neighbors.KNeighborsClassifier()
clf.fit(X_train, y_train)

accuracy = clf.score(X_test, y_test)

print (accuracy)

example_measures = np.array([[4, 2, 1, 1, 1, 2, 3, 2, 1], [4, 2, 1, 1, 1, 2, 3, 2, 1]])
example_measures = example_measures.reshape(len(example_measures), -1)

prediction = clf.predict(example_measures)

print (prediction)


text:
# encoding: utf-8
"""
Train a spacy or sklearn model and pickle it
"""
from scipy import sparse as sp
import numpy as np

from pathlib import Path
import pickle
import os.path
import json
import ast

from sklearn.metrics import f1_score, classification_report
from sklearn.preprocessing import MultiLabelBinarizer

from grants_tagger.models import create_model
from grants_tagger.utils import load_train_test_data, yield_tags

from tensorflow.random import set_seed

# TODO: Remove when WellcomeML implements setting random_seed inside models
# replace with param in configs then
set_seed(41)

def create_label_binarizer(data_path, label_binarizer_path, sparse=False):
    """Creates, saves and returns a multilabel binarizer for targets Y"""
    label_binarizer = MultiLabelBinarizer(sparse_output=sparse)
    # TODO: pass Y_train here which can be generator or list
    label_binarizer.fit(yield_tags(data_path))

    with open(label_binarizer_path, 'wb') as f:
        f.write(pickle.dumps(label_binarizer))

    return label_binarizer

def train_and_evaluate(
        train_data_path, label_binarizer_path, approach,
        parameters=None, model_path=None, test_data_path=None,
        threshold=None, test_size=0.25, sparse_labels=False,
        cache_path=None, data_format="list", verbose=True):
    """
    train_data_path: path. path to JSONL data that contains "text" and "tags" fields.
    label_binarizer_path: path. path to load or store label_binarizer.
    approach: str. approach to use for modelling e.g. tfidf-svm or bert.
    parameters: str. a stringified dict that contains params that get passed to the model.
    model_path: path. path to save the model.
    test_data_path: path, default None. path to test data, if not provided train data would be split.
    threshold: float, default 0.5. Probability on top of which a tag is assigned.
    test_size: float, default 0.25. when test_data_path not provided this is the split used for test data
    sparse_labels: bool, default False. whether tags (labels) would be sparse for memory efficiency.
    cache_path: path, default None. path to use for caching data transformations for speed.
    data_format: str, default list. one of list, generator. generator used for memory efficiency.
    """

    if os.path.exists(label_binarizer_path):
        print(f"{label_binarizer_path} exists. Loading existing")
        with open(label_binarizer_path, "rb") as f:
            label_binarizer = pickle.loads(f.read())
    else:
        label_binarizer = create_label_binarizer(train_data_path, label_binarizer_path, sparse_labels)

    model = create_model(approach, parameters)

    # X can be (numpy arrays, lists) or generators
    X_train, X_test, Y_train, Y_test = load_train_test_data(
        train_data_path, label_binarizer, test_data_path=test_data_path,
        test_size=test_size, data_format=data_format)
    model.fit(X_train, Y_train)

    # TODO: Can we handle it better?
    if data_format == "generator":
        Y_test_gen = Y_test()
        if sparse_labels:
            Y_test_batches = []
            for Y_test_batch in Y_test_gen:
                Y_test_batch = sp.csr_matrix(Y_test_batch)
                Y_test_batches.append(Y_test_batch)
            Y_test = sp.vstack(Y_test_batches)
        else:
            Y_test = list(Y_test_gen)

    if threshold:
        Y_pred_prob = model.predict_proba(X_test)
        Y_pred_test = Y_pred_prob > threshold
    else:
        Y_pred_test = model.predict(X_test)

    f1 = f1_score(Y_test, Y_pred_test, average='micro')
    if verbose:
        report = classification_report(Y_test, Y_pred_test, target_names=label_binarizer.classes_)
        print(report)

    if model_path:
        if str(model_path).endswith('pkl') or str(model_path).endswith('pickle'):
            with open(model_path, 'wb') as f:
                f.write(pickle.dumps(model))
        else:
            model.save(model_path)

    return f1

if __name__ == "__main__":
    # Note that this CLI is purely for SageMaker so it is quite minimal
    import argparse

    argparser = argparse.ArgumentParser()
    argparser.add_argument("--data_path", type=Path)
    argparser.add_argument("--model_path", type=Path)
    argparser.add_argument("--label_binarizer_path", type=Path)
    argparser.add_argument("--approach", type=str)
    argparser.add_argument("--parameters", type=str)
    argparser.add_argument("--test_data_path", type=Path)
    argparser.add_argument("--threshold", type=float)
    argparser.add_argument("--data_format", type=str)
    argparser.add_argument("--test_size", type=float)
    argparser.add_argument("--sparse_labels", type=bool)
    argparser.add_argument("--cache_path", type=Path)
    args = argparser.parse_args()

    train_and_evaluate(
        args.data_path, args.label_binarizer_path, args.approach,
        args.parameters, test_data_path=args.test_data_path,
        threshold=args.threshold, model_path=args.model_path,
        data_format=args.data_format, test_size=args.test_size,
        sparse_labels=args.sparse_labels)

reason: import numpy,from scipy


text:
import os
import pickle

import numpy as np
import keras

class MyPredictor(object):
    """An example Predictor for an AI Platform custom prediction routine."""

    def __init__(self, model): #, preprocessor
        """Stores artifacts for prediction. Only initialized via `from_path`.
        """
        self._model = model
        #self._preprocessor = preprocessor

    def predict(self, instances, **kwargs):
        """Performs custom prediction.

        Preprocesses inputs, then performs prediction using the trained Keras
        model.

        Args:
            instances: A list of prediction input instances.
            **kwargs: A dictionary of keyword args provided as additional
                fields on the predict request body.

        Returns:
            A list of outputs containing the prediction results.
        """

        #inputs = instances['input']
        #preprocessed_inputs = self._preprocessor.preprocess(inputs)

        outputs = self._model.predict(instances[0]['input'])
        logval = outputs.tolist()[0][0]
        transformedval = np.exp(logval)
        return str(transformedval)

    @classmethod
    def from_path(cls, model_dir):
        """Creates an instance of MyPredictor using the given path.

        This loads artifacts that have been copied from your model directory in
        Cloud Storage. MyPredictor uses them during prediction.

        Args:
            model_dir: The local directory that contains the trained Keras
                model and the pickled preprocessor instance. These are copied
                from the Cloud Storage model directory you provide when you
                deploy a version resource.

        Returns:
            An instance of `MyPredictor`.
        """
        model_path = os.path.join(model_dir, 'census.hdf5')
        model = keras.models.load_model(model_path)

        """
        preprocessor_path = os.path.join(model_dir, 'preprocessor.pkl')
        with open(preprocessor_path, 'rb') as f:
            preprocessor = pickle.load(f)
        """

        return cls(model) #, preprocessor

max_issues_repo_name: brianray/chicago-taxi

max_forks_repo_name: jonrossclaytor/chicago-taxi


text:
import gensim
# for preprocessing
from gensim.utils import simple_preprocess
from gensim.parsing.preprocessing import STOPWORDS
from nltk.stem import WordNetLemmatizer, SnowballStemmer
# for bigrams
from gensim.models import Phrases
from gensim.models.phrases import Phraser
from nltk.stem.porter import *
import nltk
# nltk.download('wordnet')
from nltk.corpus import wordnet
from gensim import corpora, models
from pprint import pprint
# auxiliary
import numpy as np
import pandas as pd

stemmer = SnowballStemmer('english')

class Preprocessor:
	def __init__(self, text):
		"""
		Initialize NLP preprocessor.

		------PARAMETERS------
		text: a dataframe column of str text

		------FUNCTIONS------
		preprocess: preprocesses text
		create_nlp_items_from_preprocessed: processes preprocessed text to produce nlp items
		"""
		self.text_ = text

	def preprocess(self, lemmatize=True, stopwords=[], min_token_length=3):
		"""
		Takes a dataframe's text column and preprocesses it by:
			* Removing typical English stop words and removing stop words designated by user
			* Lemmatizing words

		------PARAMETERS------
		text: dataframe column
		stopwords: list of *lower case* stop words to remove
		min_token_length: minimum character length of a word (e.g, if it is 3, 'an' is removed)

		------ATTRIBUTES STORED------
		preprocessed_text_: a list of lists for of words preprocessed
		"""
		self.preprocessed_text_ = self.text_.map(
			lambda p: self._preprocess_row(p, lemmatize, stopwords=stopwords, min_token_length=min_token_length)
		).tolist()
		self.stopwords_ = stopwords
		self.min_token_length_ = min_token_length

	def _preprocess_row(self, text, lemmatize=True, stopwords=[], min_token_length=3):
		"""
		Takes a dataframe's text column and preprocesses it by:
			* Removing typical English stop words and removing stop words designated by user
			* Lemmatizing words

		------PARAMETERS------
		text: dataframe column
		stopwords: list of *lower case* stop words to remove
		min_token_length: minimum character length of a word (e.g, if it is 3, 'an' is removed)

		------OUTPUT------
		result: a list of words preprocessed. Use .map() functionality in pandas to compute
			dataframe column
		"""
		# Lemmatize and stem preprocessing steps on the data set.
		def lemmatize_stemming(text):
			return stemmer.stem(WordNetLemmatizer().lemmatize(text, pos='v'))

		result = []
		for token in gensim.utils.simple_preprocess(text):
			if token not in gensim.parsing.preprocessing.STOPWORDS and len(token) > min_token_length:
				if lemmatize:
					result.append(lemmatize_stemming(token))
				else:
					result.append(token)
		result = [item for item in result if item not in stopwords]

		return result

	def get_bigrams_from_preprocessed(self, min_count=0.1, threshold=10., scoring='default'):
		"""
        Computes bigrams after preprocessing. NOTE: overwrites preprocessed_text_ attribute.

        ------PARAMETERS------
        min_count: minimum count of bigrams to be included
        threshold: scoring threshold  for bigrams for inclusion
        scoring: gensim Phrases scoring function to evaluate bigrams for threshold
		"""
		x = Phrases(self.preprocessed_text_, min_count=min_count, threshold=threshold, scoring=scoring)
		x = Phraser(x)

		bigram_token = []
		for sent in self.preprocessed_text_:
			bigram_token.append(x[sent])

		self.preprocessed_text_ = bigram_token

	def create_nlp_items_from_preprocessed_df(self,
											  no_below=2,
											  no_above=1.0,
											  keep_n=None,
											  verbose=True):
		"""
		Creates key NLP items from a pandas dataframe column that's already been preprocessed ('preprocess' function).
		------PARAMETERS------
		no_below: int, only include words that appear in this many documents
		no_above: float, exclude all words that appear in this proportion of documents (1.0 = 100%)
		keep_n: maximum number of terms to keep
		verbose: show progress with output previews and etc.

		------ATTRIBUTES STORED------
		dictionary_: A dictionary
		bow_corpus_: The bag of words corpus corresponding with the dictionary
		tfidf_: A tfidf 'model' for gensim
		tfidf_corpus_: A tfidf in corpus form
		tfidf_sparse_: A sparse tfidf *matrix*, readily usable for further calculations.
		"""

		# Create dictionary
		dictionary = gensim.corpora.Dictionary(self.preprocessed_text_)
		if verbose:
			count = 0
			for k, v in dictionary.iteritems():
				print(k, v)
				count += 1
				if count > 10:
					break
			print('\n ' + str(len(dictionary)) + ' unique words. \n')

		# Filter dictionary's extremes
		if keep_n == None:
			keep_n = len(dictionary)
		dictionary.filter_extremes(no_below=no_below, no_above=no_above, keep_n=keep_n)

		if verbose:
			print(str(len(dictionary)) + ' unique words after extremes filtered. \n')
			print('')

		# Create bow_corpus
		bow_corpus = [dictionary.doc2bow(doc) for doc in self.preprocessed_text_]

		if verbose:
			# Preview the corpus
			bow_doc_n = bow_corpus[np.random.choice(list(range(len(self.preprocessed_text_))))]
			for i in range(len(bow_doc_n)):
				print("Word {} (\"{}\") appears {} time.".format(bow_doc_n[i][0],
														   dictionary[bow_doc_n[i][0]],
						bow_doc_n[i][1]))

		# Create tfidf
		tfidf = models.TfidfModel(bow_corpus)
		corpus_tfidf = tfidf[bow_corpus]
		if verbose:
			print('\n Corpus TF-IDF preview:')
			for doc in corpus_tfidf:
				pprint(doc)
				break

		# Make sparse tfidf
		# .T to transpose since it's originally a word-doc matrix, not doc-word matrix (we want rows as docs)
		tfidf_sparse = gensim.matutils.corpus2csc(corpus_tfidf, printprogress=500).T

		# Store attributes
		self.dictionary_ = dictionary
		self.bow_corpus_ = bow_corpus
		self.tfidf_ = tfidf
		self.corpus_tfidf_ = corpus_tfidf
		self.tfidf_sparse_ = tfidf_sparse

		self.dictionary_no_above_ = no_above
		self.dictionary_no_below_ = no_below


text:
# sum of the first 100 natural numbers decimal square root

import mpmath
import timeit

start = timeit.default_timer()

def euler_80():
    mpmath.mp.dps = 102
    total = 0
    roots = [i for i in xrange(1, 100) if i**0.5 % 1 != 0]
    for i in roots:
        total += sum([int(v) for v in str(mpmath.sqrt(i)) if v != '.'][:100])
    return total

print "Answer: %s" % euler_80()
stop = timeit.default_timer()
print "Time: %f" % (stop - start)

max_stars_repo_name: tijko/Project-Euler

max_issues_repo_name: tijko/Project-Euler

max_forks_repo_name: tijko/Project-Euler

reason: import mpmath


text:
import cv2
import numpy as np
#import matplotlib.pyplot as plt
import datetime

cap = cv2.VideoCapture(0)

################################################

##### taking input point ######################

_, inp_img = cap.read()
inp_img = cv2.flip(inp_img, 1)
inp_img = cv2.blur(inp_img, (4,4))
gray_inp_img = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)

##############################################

########## tracking starts here ###########

old_pts = np.array([[350, 180], [350, 350]], dtype=np.float32).reshape(-1,1,2)

backup = old_pts.copy()
backup_img = gray_inp_img.copy()

#### text o/p window
outp = np.zeros((480,640,3))

#### variable ####
ytest_pos = 40
###############

while True:
    _, new_inp_img = cap.read()
    new_inp_img = cv2.flip(new_inp_img, 1)
    new_inp_img = cv2.blur(new_inp_img, (4,4))
    new_gray = cv2.cvtColor(new_inp_img, cv2.COLOR_BGR2GRAY)
    new_pts,status,err = cv2.calcOpticalFlowPyrLK(gray_inp_img,
                         new_gray,
                         old_pts,
                         None, maxLevel=1,
                         criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT,
                                                         15, 0.08))
    #### boundries
    if new_pts.ravel()[0]  >= 600:
        new_pts.ravel()[0] = 600
    if new_pts.ravel()[1] >= 350:
        new_pts.ravel()[1] = 350
    if new_pts.ravel()[0]  <= 20:
        new_pts.ravel()[0] = 20
    if new_pts.ravel()[1] <= 150:
        new_pts.ravel()[1] = 150
    if new_pts.ravel()[2]  >= 600:
        new_pts.ravel()[2] = 600
    if new_pts.ravel()[3] >= 350:
        new_pts.ravel()[3] = 350
    if new_pts.ravel()[2]  <= 20:
        new_pts.ravel()[2] = 20
    if new_pts.ravel()[3] <= 150:
        new_pts.ravel()[3] = 150
    ###############

    ##### drawing line
    x,y = new_pts[0,:,:].ravel()
    a,b = new_pts[1,:,:].ravel()
    cv2.line(new_inp_img, (int(x),int(y)), (int(a),int(b)), (0,0,255), 15)

    cv2.imshow("ouput", new_inp_img)

    if new_pts.ravel()[0]  > 400 or new_pts.ravel()[2]  > 400:
        if new_pts.ravel()[0] > 550 or new_pts.ravel()[2]  > 550:
            new_pts = backup.copy()
            new_inp_img = backup_img.copy()
            ytest_pos += 40
            cv2.putText(outp, "gone at {}".format(datetime.datetime.now().strftime("%H:%M")), (10,ytest_pos),
                cv2.FONT_HERSHEY_PLAIN, 3, (0,255,0))

    elif new_pts.ravel()[0]  < 200 or new_pts.ravel()[2]  < 200:
        if new_pts.ravel()[0] < 50 or new_pts.ravel()[2]  < 50:
            new_pts = backup.copy()
            new_inp_img = backup_img.copy()
            ytest_pos += 40
            cv2.putText(outp, "came at {}".format(datetime.datetime.now().strftime("%H:%M")), (10,ytest_pos),
                cv2.FONT_HERSHEY_PLAIN, 3, (0,0,255))

    cv2.imshow('final', outp)
    gray_inp_img = new_gray.copy()
    old_pts = new_pts.reshape(-1,1,2)

    if cv2.waitKey(1) & 0xff == 27:
        break

#### thank you for this video

cv2.destroyAllWindows()
cap.release()

max_stars_repo_name: Pawandeep-prog/track-people-in-hall

max_issues_repo_name: Pawandeep-prog/track-people-in-hall

max_forks_repo_name: Pawandeep-prog/track-people-in-hall


text:
# 参考にしたサイト
# https://code-graffiti.com/blending-images-with-opencv-in-python/
# https://www.mathpython.com/ja/opencv-image-add/
# https://qiita.com/Gyutan/items/5e62420cc8f6bb106bed

# 画像合成関連のimport
import cv2
import matplotlib.pyplot as plt
import numpy as np

# pdf変換関連のimport
from pathlib import Path
from pdf2image import convert_from_path

# PDFファイルのパス
pdf_path = Path("pdf-PDF-Invoice3.pdf")

#この1文で変換されたjpegファイルが、imageホルダー内に作られます。
#convert_from_path(pdf_path, output_folder='./' ,fmt='jpeg',output_file='henkan_gazou.jpg')

# PDFをImage に変換(pdf2imageの関数)
pages = convert_from_path(pdf_path)

# 画像ファイルが１ページだけある前提で保存
for i, page in enumerate(pages):
    page.save("henkan_gazou.jpg", fmt='jpeg')

# 画像の読み込み
img1 = cv2.imread('henkan_gazou.jpg') # 元画像
img2 = cv2.imread('gazou_add.png') # 合成したい小さい画像

# 画像のサイズを確認
print(img1.shape)
print(img2.shape)

# 画像のサイズを変更（お好みで）
img2 =cv2.resize(img2,(300,300))

'''
yt:yb,xl:xr
yt ... y 座標の上
yb ... y 座標の下
xl ... x 座標の左
xr ... x 座標の右
'''
height, width = img2.shape[:2]
img1[250:height+250, 1000:width+1000] = img2

cv2.imwrite('new.jpg', img1)


text:
#!/usr/bin/env python
#
# This code implements the segment mining in Entropy/IP
#   @1 finds frequency outliers (like constants, enums, etc.)
#   @2 finds highly dense ranges of values (like close /32 prefixes)
#   @3 finds uniformly distributed ranges of values (like counters)
#   @4 prints what didn't get into @1-@3
#
# Note that not every piece of the code was mentioned in the paper, as the
# description would be too detailed - this is a heuristics algorithm.
#
# Mind the different terminology vs. the paper.
# Runs in python2. Requires numpy and scikit-learn.
#
# Copyright (c) 2015-2016 Akamai Technologies, Inc.
# See file "LICENSE" for licensing information.
# Author: Pawel Foremski
#

import sys
from collections import defaultdict
import argparse
import math
import numpy as np
from sklearn.cluster import DBSCAN

# segment labels
SL = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", \
      "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]

# parse arguments ASAP
p = argparse.ArgumentParser(description='Entropy/IP: segment mining')
p.add_argument('ips', help='file with IPv6 addresses in full hex form')
p.add_argument('segments', help='file with segments (output of a1-segments.py)')
p.add_argument('--segment', help='alternative segment definition')
args = p.parse_args()

###################################### helper functions

def read_segment(segment):
	d = segment.split('-')
	return [{"start":int(d[0]), "stop":int(d[1])}]

def read_segments(src):
	segments = []
	for line in src:
		if line[0:10] != "# segment\t": continue
		d = line[0:-1].split('\t')
		row = {"start":int(d[2]), "stop":int(d[3])}
		segments.append(row)
	return segments

def read_ips(src, segments):
	db = []
	N = 0

	for segment in segments:
		cl = segment.copy()
		cl["vals"] = []
		db.append(cl)

	for line in src:
		if len(line) < 32: continue
		line = line[:-1].split()[0].lower()
		if len(line) > 32: continue
		N += 1
		for cl in db:
			cl["vals"].append(int(line[cl["start"]/4:cl["stop"]/4], 16))

	return db, N

###################################### pretty-printers

def ppcnt(pcnt): return "%7.2f%%" % (pcnt)

def pp(vals, counts, N):
	if len(counts) == 0: return False
	rv = False

	# print starting from top-freq
	indexer = counts.argsort()[::-1]
	for u,c in zip(vals[indexer], counts[indexer]):
		pcnt = 100.0*c/N
		if pcnt < 0.005: continue

		fmt1 = "%0" + str(L/4) + "x"
		fmt2 = " "*(2+32-L/4) + "%" + "s"
		print ("  " + fmt1 + fmt2) % (u, ppcnt(100.0*c/N))
		rv = True
	return rv

def rpp(vals, counts, L, N):
	if len(counts) == 0: return False
	rv = False

	# print heavy-hitters
	if len(counts) > 4:
		q1, q3 = np.percentile(counts, [25, 75])
		T = min(0.1*N, max(q3 + 1.5*(q1 - q1), 0.02*N))
		hhs = counts > T
		rv = pp(vals[hhs], counts[hhs], N)
		vals = vals[~hhs]
		counts = counts[~hhs]

	pcnt = 100.0 * sum(counts)/N
	if pcnt < 0.05: return rv

	if len(vals) < 5:
		rv |= pp(vals, counts, N)
	else:
		fmt1 = "* %0" + str(L/4) + "x" + "-%0" + str(L/4) + "x"
		fmt2 = " "*(1+32-L/4-L/4-1) + " %" + "s"
		print (fmt1 + fmt2) % (vals.min(), vals.max(), ppcnt(pcnt))
		rv = True

	return rv

###################################### custom DBSCAN metrics

def metric(p1,p2):
	bdiff = math.fabs(p2[0] - p1[0])
	pdiff = math.fabs(math.log(p2[2],13) - math.log(p1[2],13))
	return bdiff*0.25 + pdiff*50.0

###################################### main

## prepare: read segments and IPs
if args.segment:
	segments = read_segment(args.segment)
else:
	segments = read_segments(open(args.segments))

db,N = read_ips(open(args.ips), segments)

## for each segment...
for cn,cl in enumerate(db):
	L = cl["stop"] - cl["start"]
	P = 1.0/2**L
	print "%s: bits %d-%d (hex chars %2d-%2d)" \
		% (SL[cn], cl["start"], cl["stop"], cl["start"]/4+1, cl["stop"]/4)

	### sample IPs if dataset too large?
	if len(cl["vals"]) > 50000:
		vals = np.random.choice(cl["vals"], size=50000)
	else:
		vals = np.asarray(cl["vals"])

	N = len(vals)
	unique,counts = np.unique(vals, return_counts=True)

	### detect frequency top-outliers (@1)
	if len(counts) > 10:
		q1, q3 = np.percentile(counts, [25, 75])
		iqr = q3 - q1
		T = min(0.1*N, max(q3 + 1.5*iqr, 1.0*P*N))
		hhs = counts > T
		nhhs = ~hhs

		# too many? use top 10th as *threshold*
		if sum(hhs) > 10:
			indexer = counts.argsort()[::-1]
			t10 = max(2, counts[indexer[9]])
			hhs = counts >= t10
			nhhs = ~hhs

			# still too many? just use the top 10
			if sum(hhs) > 10:
				hhs = indexer[0:10]
				nhhs = indexer[10:]
	else: # frequency table very short: take all >0.1%
		hhs = counts > max(2, 0.001*N)
		nhhs = ~hhs

	# divide into outliers vs. non-outliers
	hhunique = unique[hhs]
	hhcounts = counts[hhs]
	unique2 = unique[nhhs]
	counts2 = counts[nhhs]

	# present (sorted by counts)
	pp(hhunique, hhcounts, N)

	# anything significant left?
	if sum(counts2) < 0.001*N:
		continue
	elif len(counts2) < 5:
		pp(unique2, counts2, N)
		continue

	### find dense regions (@2)
	if L >= 8:
		dbscan = DBSCAN(eps=(L/4.0)**3.0, min_samples=5)
		regions = dbscan.fit_predict(unique2.reshape(-1,1))
		labels = set(regions)
		left = sum(counts2)

		for label in labels:
			rvals = unique2[regions == label]
			rcounts = counts2[regions == label]

			# anything significant?
			if label == -1:
				continue
			elif sum(rcounts) < 0.001*N:
				regions[regions == label] = -1
				continue

			# find density
			observedc = float(sum(rcounts))
			expectedc = float(rvals.max() - rvals.min()) / (2**L-1) * left
			density = observedc / expectedc
			if density < 100.0:
				regions[regions == label] = -1
				continue

			rpp(rvals, rcounts, L, N)

		unique3 = unique2[regions == -1]
		counts3 = counts2[regions == -1]
	else:
		unique3 = unique2
		counts3 = counts2

	### find continuous regions of similar probability (@3)
	if L >= 8 and len(counts3) > 1:
		bincount = min(256, 2**(cl["stop"]-cl["start"]))
		hist,bins = np.histogram(unique3, weights=counts3, bins=bincount)
		step = bins[1] - bins[0]

		data = np.asarray((range(0,len(hist)), bins[:-1], 1.0*hist/N)).T
		data = data[data[:,2] > 0.0]

		dbscan = DBSCAN(eps=5.0, min_samples=5, metric=metric)
		regions = dbscan.fit_predict(data)
		labels = set(regions)

		cregions = []
		for label in labels:
			rbins  = data[regions == label,1]
			rfreqs = data[regions == label,2]

			# unlabeled: just background
			if label == -1: continue

			# anything significant?
			if len(rbins) < 5 or sum(rfreqs) < 0.1:
				regions[regions == label] = -1
				continue

			start = rbins.min()
			stop  = rbins.max() + step
			avg   = rfreqs.mean()
			cregions.append((start, stop, avg))

		# convert clusters to ranges
		if len(cregions) > 0:
			cregions = np.array(cregions)
			cregions = list(cregions[np.argsort(cregions[:,0])])

			i = 0
			while i+1 < len(cregions):
				cur = cregions[i]
				nxt = cregions[i+1]

				if nxt[0] < cur[1]:
					if nxt[2] > cur[2]:
						if cur[1] > nxt[1]:
							cregions.insert(i+2, np.array([nxt[1], cur[1], cur[2]]))
						cur[1] = nxt[0]
					else:
						nxt[0] = cur[1]
				i += 1

			# print real ranges + probability
			for cregion in cregions:
				indexer = (unique3 >= cregion[0]) & (unique3 <= cregion[1])
				rvals = unique3[indexer]
				rcounts = counts3[indexer]
				if rpp(rvals, rcounts, L, N):
					unique3 = unique3[~indexer]
					counts3 = counts3[~indexer]

	### print the rest (@4)
	pcnt = 100.0 * counts3.sum() / N
	rpp(unique3, counts3, L, N)

max_stars_repo_name: akamai/entropy-ip

max_issues_repo_name: akamai/entropy-ip

max_forks_repo_name: akamai/entropy-ip


text:
from matplotlib import pyplot as plt
import cv2
import numpy as np

def load_img(img_path):
    img = cv2.imread(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    return hsv_img

def mask_color(img, lower_color, upper_color):
    mask = cv2.inRange(img, lower_color, upper_color)
    kernel_open = np.ones((5,5))
    kernel_close = np.ones((20,20))
    mask_open = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_open)
    mask_close = cv2.morphologyEx(mask_open, cv2.MORPH_CLOSE, kernel_close)
    masked = cv2.bitwise_and(img, img, mask=mask_close)
    return masked

def detect_bounds(img):
    mins = [240, 320] # y,x
    maxes = [0, 0]
	for y in range(masked.shape[0]):
		for x in range(masked.shape[1]):
		    if not masked[y][x][0] == 0:
		        if y < mins[0]:
		            mins[0] = y
		        if y > maxes[0]:
		            maxes[0] = y
		        if x < mins[1]:
		            mins[1] = x
		        if x > maxes[1]:
		            maxes[1] = x
    mins.reverse()
	maxes.reverse()
	return [(mins[0], maxes[1]), maxes, (maxes[0], mins[1]), (mins)]

max_stars_repo_name: pzmi/celldetector-tf

max_issues_repo_name: pzmi/celldetector-tf

max_forks_repo_name: pzmi/celldetector-tf


text:
#!/usr/bin/env python3

import os.path
import subprocess as sp
import numpy as np

# General comment regarding format: cannot use full-scale string interpolation
# because Nersc does not have the latest python3.
# Otherwise clumsy format(name1=name1, name2=name2, ..) wouldn't be necessary.

current_dir = os.path.dirname(os.path.abspath(__file__))

execname_si = "./../../../../build/examples/amr-merge-tree/amr_merge_tree_simple_double"
execname_sc = "./../../../../build/examples/amr-merge-tree/amr_merge_tree_send_components_double"
execname_mt = "./../../../../build/examples/local-global/mt-lg-ghosts-double"
execname_pi = "./../../../../build/examples/local-global/persistent-integral-lg-double"

nblocks = "64"

executables_found = (os.path.exists(execname_si) and
                     os.path.exists(execname_sc) and
                     os.path.exists(execname_mt) and
                     os.path.exists(execname_pi))

if not executables_found:
    raise FileNotFoundError("One of the executables is missing. Rebuild the project or edit paths in script")

beta_params = [(0.5, 0.5), (5, 1), (1, 3), (2,2), (2,5)]

fnames = []

def generate_data():
    global fnames
    fnames = []
    np.random.seed(1)
    fname = "a-64-64-64-normal-double.npy"
    fnames.append(fname)
    if not os.path.exists(fname):
        a = np.random.randn(64,64,64)
        np.save(fname, a)
    for (alpha, beta) in beta_params:
        fname = "alpha-{}-beta-{}-size-64-double.npy".format(alpha, beta)
        fnames.append(fname)
        if not os.path.exists(fname):
            beta = np.random.beta(alpha, beta, size=(64,64,64))
            np.save(fname, beta)

def save_correct_tree(input_npy_fname, tree_fname, log_file):
    if not os.path.exists(tree_fname):
        sp.call([execname_mt, "-b", nblocks, "-w", "-n", input_npy_fname, tree_fname], stdout=log_file, stderr=log_file)

def save_correct_integral(input_npy_fname, out_int_fname, rho, theta, log_file):
    tree_fname = current_dir + "/" + input_npy_fname[:-4] + ".tree"
    save_correct_tree(input_npy_fname, tree_fname, log_file)
    if not os.path.exists(out_int_fname):
        exec_str = "./get_intl.sh {tree_fname} {out_int_fname} {rho} {theta} {execname_pi}".format(tree_fname=tree_fname,
                out_int_fname=out_int_fname, rho=rho, theta=theta, execname_pi=execname_pi)
        sp.call(exec_str, shell = True)
        # sp.call(["./get_intl.sh",  tree_fname, out_int_fname, rho, theta, execname_pi], stdout=log_file, stderr=log_file)

def read_integral_file(fname):
    integral = {}
    with open(fname, 'r') as f:
        for line in f:
            x,y,z,v = line.split()
            x = int(x)
            y = int(y)
            z = int(z)
            v = float(v)
            integral[(x,y,z)] = v
    return integral

def compare_integrals(fname1, fname2):
    integral1 = read_integral_file(fname1)
    integral2 = read_integral_file(fname2)
    if (len(integral1) != len(integral2)):
        return False
    for vertex in integral1:
        v1 = integral1[vertex]
        try:
            v2 = integral2[vertex]
            if (v1 - v2) / v1  > 0.01:
                return False
        except KeyError:
            return False
    return True

n_failed = 0
n_passed = 0
bounds = [("0.5", "0.8"), ("0.2", "0.4"), ("0.5", "0.7"), ("0.8", "0.99"), ("0.3", "0.6")]

generate_data()

for fname in fnames:
    for (rho, theta) in bounds:
        out_tree_simple_fname = "none"
        # out_diagram_simple_fname = "{current_dir}/{fname}-simple-rho_{rho}-diagram.txt".format(current_dir=current_dir,fname=fname, rho=rho)
        out_diagram_simple_fname = "none"
        out_integral_simple_fname = "{current_dir}/{fname}-b-{nblocks}-simple-integral-rho-{rho}-theta-{theta}.txt".format(nblocks=nblocks, current_dir=current_dir,fname=fname, rho=rho, theta=theta)

        out_tree_sc_fname = "none"
        # out_diagram_sc_fname = "{current_dir}/{fname}-sc-rho_{rho}-diagram.txt".format(current_dir=current_dir,fname=fname, rho=rho)
        out_diagram_sc_fname = "none"
        out_integral_sc_fname = "{current_dir}/{fname}-b-{nblocks}-sc-integral-rho-{rho}-theta-{theta}.txt".format(nblocks = nblocks, current_dir=current_dir,fname=fname, rho=rho, theta=theta)

        out_integral_correct_fname = "{current_dir}/{fname}-correct-integral-rho-{rho}-theta-{theta}.txt".format(current_dir=current_dir,fname=fname, rho=rho, theta=theta)

        with open("compare-integrals-log.txt", 'a') as log_file:
            save_correct_integral(fname, out_integral_correct_fname, rho, theta, log_file)
            retcode = sp.call([execname_si, "-b", nblocks, "-w", "-n", "-a", "-i", rho, "-x", theta, fname, out_tree_simple_fname, out_diagram_simple_fname, out_integral_simple_fname], stdout=log_file, stderr=log_file)
            if retcode != 0:
                print("Execution failed: {} {} {}-{}".format(execname_si, fname, rho, theta))
                n_failed += 1
            # retcode = sp.call([execname_sc, "-b", nblocks, "-w", "-n", "-a", "-i", rho, "-x", theta, fname, out_tree_sc_fname, out_diagram_sc_fname, out_integral_sc_fname], stdout=log_file, stderr=log_file)
            # if retcode != 0:
            #     print("Execution failed: {} {} {}-{}".format(execname_sc, fname, rho, theta))
            #     n_failed += 1

        if not compare_integrals(out_integral_simple_fname, out_integral_correct_fname):
            print("Test failed: {} {} {}-{}".format("simple", fname, rho, theta))
            n_failed += 1
        else:
            n_passed += 1

        # if not compare_integrals(out_integral_sc_fname, out_integral_correct_fname):
        #     print("Test failed: {} {} {}-{}".format("send_components", fname, rho, theta))
        #     n_failed += 1
        # else:
        #     n_passed += 1

        print("Processed {}".format(fname))

if n_failed == 0:
    print("All {} tests passed".format(n_passed))
else:
    print("Failed tests: {}".format(n_failed))


text:
import pickle
from os import path, makedirs
import numpy as np
from sklearn import neighbors, svm, ensemble, neural_network
from sklearn.metrics import make_scorer, classification_report, f1_score, precision_score, roc_auc_score
from sklearn.model_selection import StratifiedKFold, train_test_split, cross_validate
from sklearn.tree import DecisionTreeClassifier
from skopt import BayesSearchCV
from skopt.space import Integer, Real, Categorical

BASE_DIR = path.dirname(__file__)
PATH_TO_PKL = 'trained_classifier.pkl'

class FaceClassifier:
    def __init__(self, model_path=None):

        self.model = None
        if model_path is None:
            return
        elif model_path == 'default':
            model_path = path.join(BASE_DIR, PATH_TO_PKL)

        # Load models
        with open(model_path, 'rb') as f:
            self.model = pickle.load(f)

    def parameter_tuning(self, model, cv, images_per_person, X, y):
        print("Parameter tuning")
        if model == "svm":
            parameter_space = {
                'C': (0.001, 1000000.0),
                'gamma': (0.00001, 1.0),
                'degree': (1, 9),
                'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
                'tol': (0.00001, 0.1)
            }
            clf = svm.SVC(probability=True)

        elif model == "knn":
            parameter_space = {
                'n_neighbors': (1, 2 * images_per_person),
                'weights': ['uniform', 'distance'],
                'algorithm': ['auto', 'ball_tree', 'kd_tree', 'brute'],
                'leaf_size': (5, 150),
                'p': (1, 9)
            }
            clf = neighbors.KNeighborsClassifier()

        elif model == "mlp":
            parameter_space = {
                # 'layer1': Integer(5, 100),
                # 'layer2': Integer(0, 100),
                # 'layer3': Integer(0, 100),
                'hidden_layer_sizes': Integer(10, 150),
                # numpy.arange(0.005, 0.1, 0.005)
                'activation': Categorical(['relu', 'tanh', 'logistic']),
                'solver': Categorical(['adam', 'sgd', 'lbfgs']),
                'alpha': Real(0.01, 0.5),
                'learning_rate': Categorical(['constant', 'adaptive']),
                'learning_rate_init': Real(0.002, 0.2),
                'max_iter': Integer(9999, 10001)
            }
            clf = neural_network.MLPClassifier()

        elif model == "dtree":
            parameter_space = {
                'criterion': ['gini', 'entropy'],
                'splitter': ['best', 'random'],
                'min_samples_split': (2, 50),
                'min_samples_leaf': (1, 20),
                # 'max_depth': (50, 500),
                # 'max_leaf_nodes': (50, 500),
                # 'min_impurity_decrease': (1e-10, 1e-6)
                'max_features': ["auto", "sqrt", "log2", None],
                # 'max_features': (0.1, 0.9)
            }
            clf = DecisionTreeClassifier()
        else:
            parameter_space = {
                'n_estimators': (50, 200),
                'criterion': ['gini', 'entropy'],
                # 'min_samples_split': (2, 20),
                # 'min_samples_leaf': (1, 20),
                # 'max_depth': (2, 150)
                # 'max_features': (0.1, 0.9)
            }
            clf = ensemble.RandomForestClassifier()
        score = make_scorer(f1_score, average='weighted')
        clf = BayesSearchCV(estimator=clf, search_spaces=parameter_space, n_iter=50, cv=cv,
                            scoring=score, verbose=True, n_jobs=-1)
        clf.fit(X, y)
        self.model = clf.best_estimator_
        return clf.best_score_

    def choose_model(self, model):
        if model == 'knn':
            self.model = neighbors.KNeighborsClassifier(n_neighbors=1, weights='distance', p=2, leaf_size=110)
        elif model == "dtree":
            self.model = DecisionTreeClassifier(criterion='entropy', max_depth=140)
        elif model == 'random_forest':
            self.model = ensemble.RandomForestClassifier(n_estimators=150, criterion='entropy', max_depth=140)
        elif model == 'mlp':
            self.model = neural_network.MLPClassifier(activation='tanh', hidden_layer_sizes=60,
                                                      learning_rate='adaptive', learning_rate_init=0.08,
                                                      max_iter=10000, alpha=0.093, solver='adam')
        elif model == 'svm':
            self.model = svm.SVC(kernel='rbf', C=10000.0, probability=True)
        else:  # svm
            self.model = svm.SVC(kernel='linear', probability=True)

    def train(self, X, y, model='knn', num_sets=10, k_fold=False, parameter_tuning=False, save_model_path=None,
              images_per_person=10, num_people=None):

        if parameter_tuning is True:
            cv = StratifiedKFold(n_splits=num_sets, shuffle=True)
            score = self.parameter_tuning(model, cv, images_per_person, X, y)

        elif k_fold is True:
            raise NotImplementedError
            cv = StratifiedKFold(n_splits=num_sets, shuffle=True)
            self.choose_model()
            # TODO: selecionar melhores metricas, e armazena-las em csv separado, com os respectivos parametros usados
            scoring = {'balanced_accuracy_score': 'balanced_accuracy_score',
                       'precision_weighted': 'precision_weighted',
                       'f1_weighted': 'f1_weighted,',
                       'recall_weighted': 'recall_weighted',
                       'roc_auc_score': 'roc_auc_score',
                       'weighted_roc_auc_score': make_scorer(roc_auc_score, average='weighted'),
                       'fit_time': 'fit_time',
                       'score_time': 'score_time'
                       }
            score = cross_validate(self.model, X, y, cv, scoring=scoring)
            print(f"Scores:\n{score}")

        else:
            if num_sets > 1:
                X, X_test, y, y_test = train_test_split(X, y, stratify=y, test_size=1 / num_sets)

            self.choose_model()

            self.model.fit(X, y)

            if num_sets > 1:
                y_pred = self.model.predict(X_test)
                # y_prob = self.model.predict_proba(X_test)
                print(classification_report(y_test, y_pred))
                score = f1_score(y_test, y_pred, average='weighted')

        if save_model_path is not None:
            folder = path.dirname(save_model_path)
            makedirs(folder, exist_ok=True)

            with open(save_model_path, 'wb') as f:
                pickle.dump(self.model, f)

            parameters_csv = path.join(path.dirname(save_model_path), f"{model}_parameters.csv")
            parameters = self.model.get_params()
            if not path.exists(parameters_csv):
                file = open(parameters_csv, "w")
                file.write(';'.join(param_name for param_name in parameters.keys()) +
                           ";images_per_person;number_people;number_sets;score\n")
            else:
                file = open(parameters_csv, 'a')
            file.write(';'.join(str(param_value) for param_value in list(parameters.values())) +
                       f";{images_per_person};{num_people};{num_sets};{score}\n")
            file.close()

    def classify(self, descriptor):
        if self.model is None:
            print('Train the model before doing classifications.')
            return

        pred = self.model.predict([descriptor])
        # if len(pred) > 1:
        #     print("Houston, we have a problem")

        # Para knn, a probabilidade so deve ser diferente de 1 para maiores valores de k,
        # mas melhor reconhecimento tem ocorrido com k=1
        prob = np.ravel(self.model.predict_proba([descriptor]))
        # selecionar maiores probabilidades para 2o classificador
        # prob[::-1].sort()

        return pred[0], round(np.amax(prob), 2)