Skip to content
Snippets Groups Projects
Select Git revision
  • 881106aee61f6e431ae764d01f064bb4418427fa
  • master default protected
  • correlation
  • 24-non-negative-omp
  • 15-integration-sota
  • 20-coherence-des-arbres-de-predictions
  • 19-add-some-tests
  • 13-visualization
  • 17-adding-new-datasets
  • 12-experiment-pipeline
  • 14-correction-of-multiclass-classif
  • archive/10-gridsearching-of-the-base-forest
  • archive/farah_notation_and_related_work
  • archive/wip_clean_scripts
  • archive/4-implement-omp_forest_classifier
  • archive/5-add-plots-2
  • archive/Leo_Add_first_notebook
17 results

compute_hyperparameters.py

Blame
    • Charly Lamothe's avatar
      880ff78f
      - Add an option to not use the best hyperparameters file; · 880ff78f
      Charly Lamothe authored
      - Definitely use the correct forest size (either the one from best hyperparameters or the one specified in parameter);
      - Use a number of extracted forest sizes proportional as the forest size instead of fixed forest size;
      - Add an option to save the current command line name instead of using the unamed directory;
      - Add new california housing dataset best hyperparameters, and convert all value types that are number from string to int/float in other best hyperparameter files;
      - Remove useless code from compute_results.py in prevision of the changes;
      - Before best hyperparameters saving, save number as int or float instead of string;
      - Add job_number option for parallelisation in both train.py and compute_hyperparameters.py scripts;
      - Clean-up TODO list.
      880ff78f
      History
      - Add an option to not use the best hyperparameters file;
      Charly Lamothe authored
      - Definitely use the correct forest size (either the one from best hyperparameters or the one specified in parameter);
      - Use a number of extracted forest sizes proportional as the forest size instead of fixed forest size;
      - Add an option to save the current command line name instead of using the unamed directory;
      - Add new california housing dataset best hyperparameters, and convert all value types that are number from string to int/float in other best hyperparameter files;
      - Remove useless code from compute_results.py in prevision of the changes;
      - Before best hyperparameters saving, save number as int or float instead of string;
      - Add job_number option for parallelisation in both train.py and compute_hyperparameters.py scripts;
      - Clean-up TODO list.
    ObjectRecognition2.py 2.70 KiB
    import cv2
    import numpy as np
    from mypy.messages import best_matches
    
    images=[]
    for i in range(5):
        path = "image "+str(i+1)+".jpg"
        image = cv2.imread(path)
        images.append(image)
    def calculate_hu_moments(images):
        moments_list = []
        for image in images:
            smoothed_img = cv2.GaussianBlur(image, (3, 3), 0)
            resized_img = cv2.resize(smoothed_img, (318, 513))
            if len(smoothed_img.shape) == 3 and smoothed_img.shape[2] == 3:
                resized_img_gray = cv2.cvtColor(resized_img, cv2.COLOR_BGR2GRAY)
            else:
                resized_img_gray = smoothed_img
            mask = np.zeros_like(resized_img_gray, dtype=np.uint8)
            mask[-100:, :] = 255
            resized_img_gray = cv2.bitwise_or(resized_img_gray, mask)
            _, binary = cv2.threshold(resized_img_gray, 127, 255, cv2.THRESH_BINARY)
            moments = cv2.moments(binary)
            huMoments = cv2.HuMoments(moments)
    
            for i in range(len(huMoments)):
                if huMoments[i] != 0:
                    huMoments[i] = -1 * np.sign(huMoments[i]) * np.log10(abs(huMoments[i]))
                else:
                    huMoments[i] = 0
            moments_list.append(huMoments)
        return moments_list
    def compare_hu_moments(moment_known, moment_unknown):
        distances = {
            'Euclidean': [],
            'Manhattan': [],
            'Chebyshev': []
        }
        for moments1 in moment_known:
            euclidean_distance = np.sqrt(np.sum((moments1 - moment_unknown) ** 2))
            manhattan_distance = np.sum(np.abs(moments1 - moment_unknown))
            chebyshev_distance = np.max(np.abs(moments1 - moment_unknown))
            distances['Euclidean'].append(euclidean_distance)
            distances['Manhattan'].append(manhattan_distance)
            distances['Chebyshev'].append(chebyshev_distance)
        return distances
    def best_matching(moment_known, moment_unknown):
        distances = compare_hu_moments(moment_known, moment_unknown)
        best_matches = {}
        for metric, values in distances.items():
            best_index = np.argmin(values)
            min_distance = values[best_index]
            best_matches[metric] = {
                "index": best_index,
                "distance": min_distance,
                "moments": moment_known[best_index]
            }
            print(metric, best_index, "signe : ",best_index+1 )
        return best_matches
    moments_list = calculate_hu_moments(images)
    img = cv2.imread("image 2.jpg")
    moment_unknown = calculate_hu_moments(img)
    distances = compare_hu_moments(moments_list, moment_unknown)
    
    best_matches = best_matching(moments_list, moment_unknown)
    #cv2.putText(img, f"Number: {index}", (20, 20),
    #                    cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)
    
    cv2.imshow('image', img)
    cv2.imshow('image unkown',images[3])
    
    cv2.waitKey(0)
    cv2.destroyAllWindows()