from NaiveBayes import NaiveBayes from DataParser import DataParser import random, util, copy, math from FeatureExtractor import FeatureExtractor import matplotlib.pyplot as plt import numpy as np featureExtractor = FeatureExtractor() keys = ['fan_count_log',\ 'questions', 'exclamation',\ 'pos', 'neg', 'neu', \ #'compound',\ #'original_message_len_sqrt',\ 'unigrams_score', \ 'bigrams_score', \ 'trigrams_score', \ 'reading_ease_log', \ 'smog_index_inverse', \ #'neg_and_reading_ease',\ 'neu_and_smog_inverse',\ #'not_neg_and_smog_inverse',\ #'not_pos_and_reading_ease',\ #'num_unique_stems_log_inverse', \ 'elapsed_hours_log',\ # 'hours_since_last_post', 'early_morning',\ 'morning','midday', \ #'afternoon',\ 'evening', 'night', 'late_night_or_early_early_morning']
def find_logo_in_roi(rows=8, cols=8): # Create randomized train and test sets ImLo = ImageLoader() ImLo.create_random_set(train_name="train.pickle", test_name="test.pickle") # Load image set ImLo.load_set("train.pickle") # Load image img = ImLo.load_image() ref = ImLo.load_reference("reference_checker_large.png") FE = FeatureExtractor() FE.load_image(ref) FE.set_method("SIFT") kp_ref = FE.get_kp() des_ref = FE.get_des() rois = [] stitched = np.zeros(img.shape, np.uint8) y_corner_min = img.shape[0] y_corner_max = 0 x_corner_min = img.shape[1] x_corner_max = 0 for row in range(0, rows): for col in range(0, cols): y_min = int(row * img.shape[0] / rows) y_max = int(row * img.shape[0] / rows + img.shape[0] / rows) x_min = int(col * img.shape[1] / cols) x_max = int(col * img.shape[1] / cols + img.shape[1] / cols) roi = img[y_min:y_max, x_min:x_max] # cv.imshow("roi", roi) # cv.waitKey() FE.load_image(roi) FE.set_method("SIFT") kp = FE.get_kp() des = FE.get_des() if kp is not None: if len(kp) >= 2: print(len(kp)) FLANN_INDEX_KDTREE = 1 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) # or pass empty dictionary flann = cv.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des_ref, des, k=2) # Apply ratio test good = [] for m, n in matches: if m.distance < 0.7 * n.distance: good.append(m) if len(good) >= 4: print( "asd", np.float32([kp[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)) print(np.float32([kp[m.trainIdx].pt for m in good])) pts = np.int32([kp[m.trainIdx].pt for m in good]) for pt in pts: # Elso x, masodik y, 0,0 bal alul if pt[0] + x_min < x_corner_min: x_corner_min = pt[0] + x_min if pt[0] + x_min > x_corner_max: x_corner_max = pt[0] + x_min if pt[1] + y_min < y_corner_min: y_corner_min = pt[1] + y_min if pt[1] + y_min > y_corner_max: y_corner_max = pt[1] + y_min rois.append(((y_min, y_max), (x_min, x_max))) img3 = cv.drawMatches( ref, kp_ref, roi, kp, good, None, matchColor=(0, 0, 255), flags=cv.DRAW_MATCHES_FLAGS_NOT_DRAW_SINGLE_POINTS) # cv.imshow("matches", img3) # cv.waitKey() stitched[y_min:y_max, x_min:x_max] = roi cv.imshow("done", stitched) cv.circle(img, (x_corner_min, y_corner_min), radius=4, color=(0, 0, 255), thickness=-1) cv.circle(img, (x_corner_min, y_corner_max), radius=4, color=(0, 0, 255), thickness=-1) cv.circle(img, (x_corner_max, y_corner_max), radius=4, color=(0, 0, 255), thickness=-1) cv.circle(img, (x_corner_max, y_corner_min), radius=4, color=(0, 0, 255), thickness=-1) cv.imshow("rect", img) print(x_corner_min, y_corner_min, x_corner_max, y_corner_max) cv.waitKey()
def main_full_side(rows=8, cols=8, ratio_test=0.65, min_matches=10): # Create randomized train and test sets ImLo = ImageLoader() ImLo.create_random_set(train_name="train.pickle", test_name="test.pickle") # Load image set ImLo.load_set("train.pickle") FE = FeatureExtractor() # Load reference ref = ImLo.load_reference("reference_bot.png") FE.load_image(ref) FE.set_method("SIFT") kp_ref_bot = FE.get_kp() des_ref_bot = FE.get_des() ref = ImLo.load_reference("reference_left.png") FE.load_image(ref) FE.set_method("SIFT") kp_ref_left = FE.get_kp() des_ref_left = FE.get_des() total_good_corners = 0 total_tests = 0 while True: # Load image img = ImLo.load_image() if img is None or total_tests >= 20: print(total_tests, total_good_corners) print("Detection rate:", total_good_corners / (total_tests * 4)) break rois = [] stitched = np.zeros(img.shape, np.uint8) y_corner_min = img.shape[0] y_corner_max = 0 x_corner_min = img.shape[1] x_corner_max = 0 for row in range(0, rows): for col in range(0, cols): y_min = int(row * img.shape[0] / rows) y_max = int(row * img.shape[0] / rows + img.shape[0] / rows) x_min = int(col * img.shape[1] / cols) x_max = int(col * img.shape[1] / cols + img.shape[1] / cols) roi = img[y_min:y_max, x_min:x_max] # cv.imshow("roi", roi) # cv.waitKey() FE.load_image(roi) FE.set_method("SIFT") kp = FE.get_kp() des = FE.get_des() if kp is not None: if len(kp) >= 2: FLANN_INDEX_KDTREE = 1 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict( checks=50) # or pass empty dictionary flann = cv.FlannBasedMatcher(index_params, search_params) matches_bot = flann.knnMatch(des_ref_bot, des, k=2) matches_left = flann.knnMatch(des_ref_left, des, k=2) # Apply ratio test good = [] for m, n in matches_bot: if m.distance < ratio_test * n.distance: good.append(m) for m, n in matches_left: if m.distance < ratio_test * n.distance: good.append(m) print(len(good)) if len(good) >= min_matches: pts = np.int32([kp[m.trainIdx].pt for m in good]) important_roi = False for pt in pts: # Elso x, masodik y, 0,0 bal alul if pt[0] + x_min < x_corner_min: x_corner_min = pt[0] + x_min important_roi = True if pt[0] + x_min > x_corner_max: x_corner_max = pt[0] + x_min important_roi = True if pt[1] + y_min < y_corner_min: y_corner_min = pt[1] + y_min important_roi = True if pt[1] + y_min > y_corner_max: y_corner_max = pt[1] + y_min important_roi = True if important_roi: rois.append(((y_min, y_max), (x_min, x_max))) stitched[y_min:y_max, x_min:x_max] = roi if len(rois) > 0: cv.imshow("done", stitched) cv.circle(img, (x_corner_min, y_corner_min), radius=4, color=(0, 0, 255), thickness=-1) cv.circle(img, (x_corner_min, y_corner_max), radius=4, color=(0, 0, 255), thickness=-1) cv.circle(img, (x_corner_max, y_corner_max), radius=4, color=(0, 0, 255), thickness=-1) cv.circle(img, (x_corner_max, y_corner_min), radius=4, color=(0, 0, 255), thickness=-1) cv.imshow("rect", img) cv.waitKey() print(x_corner_min, y_corner_min, x_corner_max, y_corner_max) total_good_corners += int(input("How many good corners? ")) total_tests += 1
def __init__(self, path): # total posts self.postCount = 0 # maps a POST_ID to its feature_vector # e.g. feature_vectors['POST_ID'] gives a feature vector for a post self.feature_vectors = collections.defaultdict(lambda: {}) # map from post_IDs to reactions mapping; example:: # POST_ID : {num_like: 300, num_love: 12, num_wow: 0, num_sad: 0, num_angry: 0} self.post_results = {} # map between account IDs and their names # map between account IDs and their profile objects (JSON) self.Names = collections.defaultdict(lambda: '') self.Profiles = collections.defaultdict(lambda: '') # map between post IDs and their Post objects self.Posts = {} # Porter Stemmer, Feature Extractor, Stop Words self.stemmer = PorterStemmer() self.featureExtractor = FeatureExtractor() self.stopwords = set(stopwords.words('english')) # uni/bi/tri-gram frequencies across posts self.wordFrequency = collections.defaultdict(lambda: 0.0) self.bigramFrequency = collections.defaultdict( lambda: collections.defaultdict(lambda: 0)) self.trigramFrequency = collections.defaultdict( lambda: collections.defaultdict(lambda: collections.defaultdict( lambda: 0))) #uni/bi/tri-gram scores self.wordScores = collections.defaultdict(lambda: 0.0) self.bigramScores = collections.defaultdict( lambda: collections.defaultdict(lambda: 0.0)) self.trigramScores = collections.defaultdict( lambda: collections.defaultdict(lambda: collections.defaultdict( lambda: 0.0))) #total uni/bi/tri-gram counts processed self.wordCount = 0 self.bigramCount = 0 self.trigramCount = 0 # Heaps used to determine most popular uni/bi/tri-grams self.wordsHeap = None self.bigramsHeap = None self.trigramsHeap = None self.heapified = False # internal fields, shouldn't need to be called by other classes self.relevant_fields = ["id", "created_time", "type", "message"] self.num_reaction_keys = [ 'num_likes', 'num_loves', 'num_wows', 'num_sads', 'num_hahas', 'num_angrys' ] # parses train data self.parseTrainData(path) self.processParsedData()
def alignLyricsSection(self, extractedPitchList, listNonVocalFragments, tokenLevelAlignedSuffix, currSectionLink): ''' align @param: lyrics for one section ''' # read from file result URIRecordingChunkResynthesizedNoExt = currSectionLink.URIRecordingChunk detectedAlignedfileName = currSectionLink.URIRecordingChunk + tokenLevelAlignedSuffix fe = FeatureExtractor(self.path_to_hcopy, currSectionLink) onsetDetector = OnsetDetector(currSectionLink) detectedPath = '' phiOptPath = '' detectedTokenList = [] if not os.path.isfile(detectedAlignedfileName): fromTsTextGrid = -1 toTsTextGrid = -1 if ParametersAlgo.WITH_ORACLE_PHONEMES: # oracle phonemes raw_input( 'implemented only for Kimseye...! Continue only if working with Kimseye' ) if ParametersAlgo.FOR_MAKAM: fromTsTextGrid = 0 toTsTextGrid = 20.88 # for kimseye etmem fromSyllableIdx = 0 toSyllableIdx = 10 currSectionLink.loadSmallAudioFragmentOracle( self.model, fromSyllableIdx, toSyllableIdx) fe.featureVectors = currSectionLink.lyricsWithModels # featureVectors is alias for LyricsWithModelsOracle else: ###### extract audio features fe.featureVectors = currSectionLink.loadSmallAudioFragment( fe, extractedPitchList, self.recording.recordingNoExtURI, self.model) # sectionLink.lyricsWithModels.printWordsAndStates() #################### decode decoder = Decoder(currSectionLink, currSectionLink.lyricsWithModels, URIRecordingChunkResynthesizedNoExt) ##### prepare note onsets. result stored in files, which are used in decoding ############################ if ParametersAlgo.WITH_ORACLE_ONSETS == 1: URIrecOnsets = os.path.join( os.path.dirname(self.recording.recordingNoExtURI), ParametersAlgo.ANNOTATION_RULES_ONSETS_EXT) onsetDetector.parseNoteOnsetsGrTruth(URIrecOnsets) elif ParametersAlgo.WITH_ORACLE_ONSETS == 0: onsetDetector.extractNoteOnsets( URIRecordingChunkResynthesizedNoExt + '.wav') ############################################### detectedTokenList = decoder.decodeAudio(fe, onsetDetector, listNonVocalFragments, fromTsTextGrid, toTsTextGrid) detectedTokenList = addTimeShift(detectedTokenList, currSectionLink.beginTs) detectedPath = decoder.path.pathRaw # ##### write all decoded output persistently to files if ParametersAlgo.WRITE_TO_FILE: self.write_decoded_to_file( tokenLevelAlignedSuffix, URIRecordingChunkResynthesizedNoExt, decoder.path.phiPathLikelihood, detectedTokenList) ### VISUALIZE result # decoder.lyricsWithModels.printWordsAndStatesAndDurations(decoder.path) else: # do not decode, read form file detectedTokenList, phiOptPath, detectedPath = self.read_decoded( URIRecordingChunkResynthesizedNoExt, detectedAlignedfileName) return detectedTokenList, detectedPath, phiOptPath
print(original_df[60]) print(reviews_2[60]) print(lda_model.get_document_topics(doc_term_matrix[60])) if __name__ == '__main__': # Usage example(no database involved) # 0. get reviews & users # if read from database # parser = Parser() # reviews = parser.get_reviews() # reviews = parser.get_users() reviews = pd.read_csv("reviews.csv", sep=';') users = pd.read_csv("users.csv", sep=';') # print first 10 reviews & users # print(reviews.head(10)) # print(users.head(10)) extractor = FeatureExtractor(save_to_db=False) extractor.load_reviews(reviews.values.tolist()) extractor.load_users(users.values.tolist()) # 1. detect languages extractor.extract_languages() # 2.check sentiments extractor.extract_sentiments() # 3.gender classification extractor.extract_genders() # 4.topic modeling - it will take up to 3 minutes, after that open TopicModeling.html in your browser apply_topic_modeling(reviews)
def algoANN(self, trainingPath, testPath, featureExtraType, epochs): ''' Using Artificial Neural Network algorithm to classify insect images :param trainingPath: the path of training images :param testPath: the path of testing images :param featureExtraType: the feature type:sift or surf :param epochs: the numbre of training for neural network :return: the classification results ''' loadImage = LoadImage() featureExtra = FeatureExtractor() bow = BOW() # get the species,the name of all insects, the path of all insect images insectNames, names, trainingPaths = loadImage.loadTrainImage(trainingPath) insectSpecies = len(insectNames) print "insect species:", insectSpecies #get all descriptos of training images trainDescriptors = bow.getDescriptors(trainingPaths, featureExtraType) #get the BoW dictionary of trianing images trainBowDictionary = bow.getBowDictionary(insectSpecies, trainDescriptors, featureExtraType) #initialize a Neural Network net = buildNetwork(insectSpecies, 100, 100, insectSpecies) #initialize a data set ds = SupervisedDataSet(insectSpecies, insectSpecies) species = 0 #add all datas in data set for p in insectNames: trainingPaths = os.listdir(trainingPath + "\\" + p) for j in trainingPaths: #add data ds.addSample(featureExtra.getSingleFeature(trainingPath + "\\" + p + "\\" + j, trainBowDictionary, featureExtraType)[0], (species,)) species += 1 #initialize a trainer trainer = BackpropTrainer(net, ds, learningrate=0.01, momentum=0.1, weightdecay=0.01) #training for i in range(1, epochs): traError = trainer.train() print 'after %d epochs,train error:%f' % (i, traError) testInsectNames, testNames, testingPaths = loadImage.loadTrainImage(testPath) testDescriptors = bow.getDescriptors(testingPaths, featureExtraType) testBowDictionary = bow.getBowDictionary(insectSpecies, testDescriptors, featureExtraType) # Initializes a zero matrix to save the classification results result = np.zeros((insectSpecies, insectSpecies)) count = 0 # classify all the test immages for m in testInsectNames: testPaths = os.listdir(testPath + "\\" + m) for n in testPaths: test = net.activate(featureExtra.getSingleFeature(testPath + "\\" + m + "\\" + n, testBowDictionary, featureExtraType)[0]) target = map(lambda x: (x), test) # numpy.array to list result[count, target.index(max(target))] += 1 count += 1 return result
# del(contents[19]) # print('total number of images for {}: {}'.format(category, len(contents))) # idx_s = np.random.permutation(len(contents))[0:img_per_cat] # image_path += [os.path.join(img_dir, '{}.JPEG'.format(contents[nn].strip())) for nn in idx_s] ff = Dict['cache_path'] + '0.pickle' with open(ff, 'rb') as fh: _, _, image_path = pickle.load(fh) img_num = len(image_path) print('total number of images for all: {}'.format(img_num)) extractor = FeatureExtractor(cache_folder=model_cache_folder, which_net='vgg16', which_layer=VC['layer'], which_snapshot=0) res = np.zeros((featDim, 0)) loc_set = np.zeros((5, 0)) for ii in range(5000, img_num): assert (os.path.exists(image_path[ii])) img = cv2.imread(image_path[ii]) # img = cv2.resize(img, (scale_size, scale_size)) # img = myresize(img, scale_size, 'short') assert (np.min(img.shape[0:2]) == 224) tmp = extractor.extract_feature_image(img)[0] assert (tmp.shape[2] == featDim) height, width = tmp.shape[0:2]
start = time.time() if show_images: image_viewer = ImageViewer() dataloader = DataLoader(path='Dataset/') training_images, training_labels = dataloader.get_training_set( shuffle=True) validation_images, validation_labels = dataloader.get_validation_set( shuffle=True) if show_images: image_viewer.add_to_plot(training_images[0], training_labels[0]) feature_extractor = FeatureExtractor(training_images) training_keypoints = feature_extractor.get_keypoints( method=feature_extractor_method) if show_images: keypoint_image = training_images[0].copy() cv2.drawKeypoints(training_images[0], training_keypoints[0], keypoint_image) image_viewer.add_to_plot( keypoint_image, feature_extractor_method + " " + training_labels[0]) feature_descriptor = FeatureDescriptor(training_images, training_keypoints) training_descriptors = feature_descriptor.get_SIFT_descriptors() BOF = BagOfFeatures(training_descriptors, training_labels,
cols_num = parameters.boardSize streak_size = parameters.streakSize X_mark = parameters.X_mark O_mark = parameters.O_mark assert streak_size <= rows_num ''' For now, these are the supported features. Adding more features requires modifying the FeatureExtractor class ''' features_names = ["density", "linear", "nonlinear", "interaction", "blocking"] ''' Run code ''' #Read board from file and extract relevant data np_board = read_board_from_json(json_path, json_board_key, rows_num, cols_num) X_locations, O_locations = extract_X_O_locations(np_board, X_mark, O_mark) #Create score boards for our features fe = FeatureExtractor(streak_size=streak_size) features_scores_boards = extract_features_scores(np_board, features_names, fe) #Create score board for AlphaZero results az_board = convert_to_AlphaZeroBoard(np_board, X_locations, O_locations) actions_scores, board_score = get_AlphaZero_actions_scores( model_file, az_board) alphaZero_score_board = create_AlphaZero_prob_matrix(actions_scores, X_locations, O_locations, rows_num, cols_num) print("Done")
import Structures as str2 from keras.models import Model from keras.models import Sequential from keras.layers import Dense, LSTM, Bidirectional from keras.utils import to_categorical import keras from keras.layers.merge import Concatenate from keras.layers import Bidirectional, Dropout, Flatten from keras.layers.merge import Dot import keras from keras_self_attention import SeqSelfAttention from pydub import AudioSegment graph = tf.get_default_graph() ft = FeatureExtractor('mean_std.csv', BIT_PRECISION=16, sampling_rate=8000) #em = EmotionExtractor_tf.EmotionExtractor('baseline_context5_conv_simple2.weights', 'mean_std.csv') sequences_length = 256 classes = 4 context_len = 5 structure = str2.Structures(context_len, 136, classes, 256) my_attention_network = structure.structure_11_LCA_attention_dot3() my_attention_network.load_weights("vera_4_classes_lca_attention.npy") intermediate_model = Model(inputs=my_attention_network.input, outputs=my_attention_network.layers[-3].output) from keras.models import load_model
import pandas as pd import csv import logging from sklearn.linear_model import LogisticRegression from sklearn import preprocessing from FeatureExtractor import FeatureExtractor if __name__ == '__main__': logger = logging.Logger("MainLogger") digit_recogniser = FeatureExtractor(logger, 28, 28,2,True) pixel_training_data_df = pd.read_csv("Data/train.csv") training_feature_df = digit_recogniser.extract_features(pixel_training_data_df) print(training_feature_df.shape) x_data = training_feature_df.iloc[:, 1:] y_data = training_feature_df.iloc[:, [0]] scaler = preprocessing.StandardScaler().fit(x_data) x_data_scaled = scaler.transform(x_data) logistic_model = LogisticRegression(random_state=0, penalty='l1', solver='saga', tol=0.1).fit(x_data_scaled, y_data.values[:, 0]) accuracy = logistic_model.score(x_data_scaled, y_data.values[:, 0]) print(f"Training completed. Accuracy Rate: {accuracy * 100}%") # create predictions for the test data now pixel_test_data_df = pd.read_csv("Data/test.csv") digit_recogniser = FeatureExtractor(logger, 28, 28, False)
if not os.path.exists(args.extractor): print "Path to extractor '%s' not found" % args.extractor sys.exit(-1) if args.path_to_audio is None: args.path_to_audio = "audio/" + args.collection_name if args.path_to_audio.endswith("/"): args.path_to_audio = args.path_to_audio[:-1] if not os.path.exists(args.path_to_audio): print "Path to audio '%s' not found" % args.path_to_audio sys.exit(-1) if args.path_to_features is None: if not os.path.exists("features"): os.mkdir("features") args.path_to_features = "features/" + args.collection_name if args.path_to_features.endswith("/"): args.path_to_features = args.path_to_features[:-1] if not os.path.exists( args.path_to_features[:args.path_to_features.rfind("/")]): print "Path to features '%s' not found" % args.path_to_features sys.exit(-1) print args extractor = FeatureExtractor(args.extractor) extractor.extract(args.path_to_audio, args.path_to_features, args.audio_filetype, args.replace_features)
image_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ) ]) raw_image = cv2.imread(args.image_path)[..., ::-1] image = image_transform(raw_image.copy()).unsqueeze(0) raw_template = cv2.imread(args.template_path)[..., ::-1] template = image_transform(raw_template.copy()).unsqueeze(0) vgg_feature = models.vgg13(pretrained=True).features FE = FeatureExtractor(vgg_feature, use_cuda=args.use_cuda, padding=True) boxes, centers, scores = FE( template, image, threshold=args.threshold, use_cython=args.use_cython) d_img = raw_image.astype(np.uint8).copy() if len(boxes) > 0: nms_res = nms(np.array(boxes), np.array(scores), thresh=args.nms) print("detected objects: {}".format(len(nms_res))) for i in nms_res: d_img = cv2.rectangle(d_img, boxes[i][0], boxes[i][1], (255, 0, 0), 3) d_img = cv2.circle(d_img, centers[i], int( (boxes[i][1][0] - boxes[i][0][0])*0.2), (0, 0, 255), 2) if cv2.imwrite("result.png", d_img[..., ::-1]): print("result.png was generated")
args = parser.parse_args() image_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225], ) ]) raw_image = cv2.imread( "./sample/06b6fc71-7743-5aa0-9ff0-1ac1a20499e2.jpg")[..., ::-1] image = image_transform(raw_image.copy()).unsqueeze(0) vgg_feature = models.vgg19(pretrained=True).features FE = FeatureExtractor(vgg_feature, use_cuda=True, padding=True) counter = 1 for template_path in os.listdir( "C:\\Users\\Bhushan\\Desktop\\RGIT_Hackathon\\robustTemplateMatching\\template" ): raw_template = cv2.imread( os.path.join( "C:\\Users\\Bhushan\\Desktop\\RGIT_Hackathon\\robustTemplateMatching\\template", template_path))[..., ::-1] template = image_transform(raw_template.copy()).unsqueeze(0) try: boxes, centers, scores = FE(template,
def extractEntities(self, path=None): featureExtractor = FeatureExtractor(path) self.applyToRows('text', featureExtractor.entities, 'entities')
ret.append(e[mask]) return ret # for i in range(len(args)): # args[i] = args[i][mask] def create_color(pts2, im): pts2 = np.asarray(pts2, dtype='uint8') color = im[pts2[:, 0], pts2[:, 1]] / 255. return color if __name__ == "__main__": im_list = sorted(os.listdir(dir)) feate = FeatureExtractor(K) f1 = 0 RTprev = np.eye(4) origins = [] cameras = [] D4prev = np.array([0, 0, 0, 0]).reshape(4, 1) col_prev = np.array([0, 0, 0]).reshape(1, 3) q = Queue() mapa = Map() p = Process(target=draw_process, args=(q,)) p.start() ### INIT FIRST FRAME im = cv2.imread(dir + im_list[0]) Fr = Frame(im)
res_frame = cv2.circle(res_frame, (u1, v1), 1, (0, 255, 0), 2) #Draw line for matching keypoints for m in matches: u2, v2 = map(lambda x: (int(x.pt[0]), int(x.pt[1])), m) res_frame = cv2.line(res_frame, u2, v2, (255, 0, 0), 2) #res_frame = cv2.drawKeypoints(res_frame,kps,None,color=(0,255,0)) return res_frame def video_Init(): cap = cv2.VideoCapture("production ID_4608595.mp4") h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) // 4) w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) // 4) cv2.namedWindow("test") cv2.moveWindow("test", 0, 0) while 1: success, img = cap.read() frame = process_frame(img, w, h) if (success): cv2.imshow("test", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() fe = FeatureExtractor() video_Init()
def algoSVM(self,trainingPath, testPath, featureExtraType): ''' Using Support Vector Machine algorithm to classify insect images :param trainingPath: the path of training images :param testPath: he path of testing images :param featureExtraType: the feature type : sift or surf :return: ''' loadImage = LoadImage() featureExtra = FeatureExtractor() bow = BOW() #get the species,the name of all insects, the path of all insect images insectSpecies, names, trainingPaths = loadImage.loadTrainImage(trainingPath) print insectSpecies print "Le bombre d'espece :", len(insectSpecies) dictionarySize = len(insectSpecies) insect = {} num = 1 for name in insectSpecies: insect[name] = num num += 1 #get the descriptors of all training images descriptors = bow.getDescriptors(trainingPaths, featureExtraType) #get Bag of Words dictionary bowDictionary = bow.getBowDictionary(dictionarySize, descriptors, featureExtraType) print "bow dictionary" #train data trainDesc = [] #train response trainLabels = [] i = 0 #initialize train datas and train responses for p in trainingPaths: trainDesc.extend(featureExtra.getSingleFeature(p, bowDictionary, featureExtraType)) trainLabels.append(insect[names[i]]) i = i + 1 svm = cv2.SVM() #training svm.train(np.array(trainDesc), np.array(trainLabels)) testInsectNames = os.listdir(testPath) # Initialize a zero matrix to save the classification results result = np.zeros((dictionarySize, dictionarySize)) print "result zero" count = 0 #classify all the test immages for test in testInsectNames: testingImage = os.listdir(testPath + "\\" + test) for p in testingImage: #get feature from a test image feature = featureExtra.getSingleFeature(testPath + "\\" + test + "\\" + p, bowDictionary, featureExtraType) #predict p = svm.predict(feature) #save the result in the result matrix result[count, p - 1] += 1 count += 1 return result
from sklearn.cluster import KMeans import matplotlib.pylab as plt from FeatureExtractor import FeatureExtractor weights = io.loadmat('multi_data/TrainedWeightsNoFinetuning.mat') params2 = [weights['W1'], weights['b1'][0,:], weights['W2'], weights['b2'][0,:], weights['W3'], weights['b3'][0,:]] hypercube = io.loadmat('multi_data/hypercube.mat') fe_input = hypercube['Hypercube'] fe_input = fe_input.reshape((-1,103)) fe = FeatureExtractor(params2, fe_input, T.nnet.sigmoid) fe.ExtractFeatures() features = fe.output print 'Clustering has started...' kmeans = KMeans(init='k-means++', n_clusters=9, n_init=5, precompute_distances=False) kmeans.fit(features) centroids = kmeans.cluster_centers_ labels = kmeans.labels_ pred_img = labels.reshape((610, 340)) plt.imshow(pred_img)
train_list, "data_path": train_path, "dst_feats_path": dst_train_feats_path, "extracted_feats_dataset_name": extracted_train_feats_dataset_name, "artifact_output_path": args.output_path_train_feats_tar_path }) for dataset in datasets: # init the train dataset feature extractor and run it with FeatureExtractor( dataset["list_path"], dataset["data_path"], dataset["dst_feats_path"], feature_extractor_fn, num_threads=args.num_threads) as feature_extractor: print(f"Starting feature extraction for " f"{dataset['data_path']}:{dataset['list_path']}") feature_extractor.run() # write arg parse params to metadata.txt metadata_file_path = os.path.join( args.save_tmp_data_to, dataset["extracted_feats_dataset_name"], 'metadata.txt') # ensure exists Path(os.path.dirname(metadata_file_path)).mkdir(parents=True, exist_ok=True) with open(metadata_file_path, "w") as f:
def AIIR_search(): # Create randomized train and test sets ImLo = ImageLoader() ImLo.create_random_set(train_name="train.pickle", test_name="test.pickle") # Load image set ImLo.load_set("train.pickle") # Load image img = ImLo.load_image() ref = ImLo.load_reference("reference_logo.png") FEref = FeatureExtractor() FEref.load_image(ref) FEref.set_method("SIFT") kp_ref = FEref.get_kp() des_ref = FEref.get_des() FE = FeatureExtractor() FE.load_image(img) FE.set_method("SIFT") kp = FE.get_kp() des = FE.get_des() # img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # ref = cv.cvtColor(ref, cv.COLOR_BGR2GRAY) # FLANN parameters FLANN_INDEX_KDTREE = 1 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) # or pass empty dictionary flann = cv.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(des_ref, des, k=2) # Apply ratio test good = [] for m, n in matches: if m.distance < 0.75 * n.distance: good.append(m) print(len(good)) MIN_MATCH_COUNT = 5 if len(good) > MIN_MATCH_COUNT: src_pts = np.float32([kp_ref[m.queryIdx].pt for m in good]).reshape(-1, 1, 2) dst_pts = np.float32([kp[m.trainIdx].pt for m in good]).reshape(-1, 1, 2) M, mask = cv.findHomography(src_pts, dst_pts, cv.LMEDS) matchesMask = mask.ravel().tolist() h, w, d = img.shape pts = np.float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2) dst = cv.perspectiveTransform(pts, M) img = cv.polylines(img, [np.int32(dst)], True, [0, 0, 255], 3, cv.LINE_AA) else: print("Not enough matches are found - {}/{}".format( len(good), MIN_MATCH_COUNT)) matchesMask = None draw_params = dict( matchColor=(0, 255, 0), # draw matches in green color singlePointColor=None, matchesMask=matchesMask, # draw only inliers flags=2) img3 = cv.drawMatches(ref, kp_ref, img, kp, good, None, **draw_params) cv.imshow("matches", img3) cv.waitKey()
if args.verbosity >= 1: print 'Initializing networks... ', if args.verbosity >= 2: print 'G & D, ', if args.dual_discrim: generator = BasicGenerator(z_dim = 100, use_bias = True, dual_input = True, output_channels = 3) discriminator = BasicDiscriminator(1, use_bias = True, dual_output=True, input_channels = 3) if args.class_output: generator = BasicGenerator(z_dim = 100, use_bias = True, dual_input = True, output_channels = 3) discriminator = BasicDiscriminator(11, use_bias = True, input_channels = 3) else: generator = BasicGenerator(z_dim = 100, use_bias = True, output_channels = 3) discriminator = BasicDiscriminator(1, use_bias = True, input_channels = 3) if args.verbosity >= 2: print 'FE' extractor = FeatureExtractor(11, use_bias = True, input_channels = 3) if args.verbosity >= 2: print 'apply weights' generator.apply(weights_init) discriminator.apply(weights_init) w = list(discriminator.parameters()) we = list(extractor.parameters()) #print [len(w), len(we)] #for param in w: # print [type(param.data),param.size()] we = w start_epoch = 0 best_G_loss = 1000
def test_AutoEncoder(learning_rate=0.1, training_epochs=15, batch_size=20): """ :type learning_rate: float :param learning_rate: learning rate used for training the DeNosing AutoEncoder :type training_epochs: int :param training_epochs: number of epochs used for training """ x_scipySparse = None train_set_x = None numInstances = 0 numFeatures = 0 if ((os.path.exists("input_scipySparse.obj"))): print "loading sparse data from pickled file..." f = open("input_scipySparse.obj", 'r') x_scipySparse = cPickle.load(f) f.close() numInstances, numFeatures = x_scipySparse.shape else: print "extracting features and building sparse data..." fe = FeatureExtractor() fe.extractFeatures() train_set_x = fe.instanceList featureDict = fe.featDict numInstances = len(train_set_x) numFeatures = len(featureDict) x_lil = sp.lil_matrix( (numInstances, numFeatures), dtype='float32') # the data is presented as a sparse matrix i = -1 v = -1 try: for i, instance in enumerate(train_set_x): for v in instance.input: x_lil[i, v] = 1 except: print "i=", i, " v=", v x_scipySparse = x_lil.tocsc() f = open("input_scipySparse.obj", 'w') cPickle.dump(x_scipySparse, f, protocol=cPickle.HIGHEST_PROTOCOL) f.close() # compute number of mini-batches for training, validation and testing n_train_batches = numInstances / batch_size # allocate symbolic variables for the data index = T.lscalar() # index to a [mini]batch #x = sparse.basic.as_sparse_variable(x_scipySparse, 'x') x = theano.shared(x_scipySparse, borrow=True) #################################### # BUILDING THE MODEL # #################################### print "building the model..." rng = numpy.random.RandomState(123) ae = AutoEncoder(numpy_rng=rng, input=x, n_visible=numFeatures, n_hidden=10, n_trainExs=numInstances) cost, updates = ae.get_cost_updates(corruption_level=0., learning_rate=learning_rate) train_ae = theano.function( [index], cost, updates=updates, givens={x: train_set_x[index * batch_size:(index + 1) * batch_size]}) start_time = time.clock() ############ # TRAINING # ############ # go through training epochs print "starting training..." for epoch in xrange(training_epochs): # go through training set c = [] for batch_index in xrange(n_train_batches): c.append(train_ae(batch_index)) print 'Training epoch %d, cost ' % epoch, numpy.mean(c) end_time = time.clock() training_time = (end_time - start_time) print "training completed in : ", training_time