Ejemplo n.º 1
0
    def get(self, image=None, region=None, hog_color_space_convert=None, hog_channels=None,
            hog_cell_per_block=None, hog_orient=None, hog_pix_per_cell=None):
        # key = repr((image, region))
        key = str(zlib.crc32(image.data.tobytes())) + str(hog_pix_per_cell[0]) + str(hog_pix_per_cell[1])
        # key = zlib.crc32(bytes(repr([image_key, region['top_left']['x'], region['top_left']['y'], region['bottom_right']['x'],
        #             region['bottom_right']['y'], hog_color_space_convert, hog_channels, hog_cell_per_block, hog_orient,
        #             hog_pix_per_cell]), encoding='ascii'))
        feature_extractor = FeatureExtractor(
            region=region,
            hog_color_space_convert=hog_color_space_convert,
            hog_channels=hog_channels,
            hog_pix_per_cell=hog_pix_per_cell,
            hog_cell_per_block=hog_cell_per_block,
            hog_orient=hog_orient
        )

        if key in self.cache:
            feature_extractor.features = self.cache[key]
        else:
            feature_extractor.extract(image)
        self.cache[key] = feature_extractor.features
        return feature_extractor
Ejemplo n.º 2
0
 def __preprocess(self):
     feature_extractor = FeatureExtractor(self.dataset_path,
                                          self.feature_groups)
     for valid_dir in ["valid{}".format(str(n)) for n in range(5)]:
         print("\t\t=== {} : {:^10} ===".format(valid_dir, "train"))
         feature_extractor.extract(part="train", valid_dir=valid_dir)
         print("\t\t=== {} : {:^10} ===".format(valid_dir, "validate"))
         feature_extractor.extract(part="validate", valid_dir=valid_dir)
         if need_prediction() or only_prediction():
             print("\t\t=== {} : {:^10} ===".format(valid_dir, "test"))
             feature_extractor.extract(part="test", valid_dir=valid_dir)
     return feature_extractor.get_feature_names()
def send_frames(size, host_ip):

    extractor = FeatureExtractor('../ncs/ncs_models/ucf101-resnet18.bin',
                                 '../ncs/ncs_models/ucf101-resnet18.xml')

    video_capture = cv2.VideoCapture(0)
    sender = imagezmq.ImageSender(connect_to='tcp://%s:5555' % host_ip)

    while True:
        ret, frame = video_capture.read()
        # frame = np.copy(f.array)
        sender.send_image(socket.gethostname(), frame)
        frame = extractor.preprocess_frame(frame)
        features = extractor.extract(frame)
        sender.send_image(socket.gethostname(), features['218'])

        if cv2.waitKey(1) == ord('q'):
            break

    video_capture.release()
    cv2.destroyAllWindows()
Ejemplo n.º 4
0
    def extract_features(self, image):
        # Create a list to append feature vectors to
        region = {
            'top_left': {
                'x': 0,
                'y': 0,
            },
            'bottom_right': {
                'x': image.shape[1],
                'y': image.shape[0],
            }
        }
        feature_extractor = FeatureExtractor(
            hog_color_space_convert=self.hog_color_space_convert,
            region=region,
            hog_channels=self.hog_channels,
            hog_pix_per_cell=self.hog_pix_per_cell,
            hog_cell_per_block=self.hog_cell_per_block,
            hog_orient=self.hog_orient
        )

        return feature_extractor.extract(image)
Ejemplo n.º 5
0
from PIL import Image
from feature_extractor import FeatureExtractor
from pathlib import Path
import numpy as np
import pickle

if __name__ == '__main__':
    fe = FeatureExtractor()
    img_paths=[]
    features=[]
    for img_path in sorted(Path("./static/img").glob("*.jpg")):
        print(img_path)  # e.g., ./static/img/xxx.jpg
        feature = fe.extract(img=Image.open(img_path))
        features.append(feature)
        img_paths.append(img_path)
    features = np.array(features)
    print(len(img_paths),len(features))
    img_paths_Pickle = open('./static/img_paths_Pickle', 'wb') 
    pickle.dump(img_paths, img_paths_Pickle)
    features_Pickle = open('./static/feature/features_Pickle', 'wb') 
    pickle.dump(features, features_Pickle)
from pathlib import Path

import numpy as np
from PIL import UnidentifiedImageError
from tensorflow.keras.preprocessing.image import load_img

from feature_extractor import FeatureExtractor
from PIL.Image import open

if __name__ == '__main__':
    fe = FeatureExtractor()

    for img_path in sorted(Path("./static/img").glob("*.jpg")):
        try:
            feature_path = Path("./static/feature") / (
                img_path.stem + ".npy")  # e.g., ./static/feature2/xxx.npy
            if feature_path.exists():
                continue
            print(img_path)  # e.g., ./static/img/xxx.jpg
            feature = fe.extract(load_img(img_path, color_mode='rgb'))
            np.save(feature_path, feature)
        except UnidentifiedImageError as e:
            print("Wrong Image" + str(e))
        while True:
            ## Read in a frame
            ret, frame = video_stream.read()
            if frame is None: break
            height, width, _ = frame.shape
            frame_id += 1

            ## Buffer to annotate the original video
            frame_buffer.append(frame)

            ## Collect and predict action for 60 frames
            if len(frame_buffer) == 60:
                processed_frames = extractor.preprocess_clip_stream(
                    frame_buffer)
                prediction, score = detector.detect(
                    extractor.extract(processed_frames))

                ## Annotate 60 frames and write it out to the output file
                for f in frame_buffer:
                    cv2.putText(f, "%s: %f" % (prediction, score), (30, 50),
                                cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 2,
                                cv2.LINE_AA)
                    if not LOG:
                        out.write(f)

                if LOG:
                    print("%d %s %f" % (frame_id, prediction, score))
                ## Reset the buffer
                frame_buffer = []

            ## If the 'q' key is pressed, break from the loop

if __name__ == '__main__':
    args = sys.argv
    if not len(args) == 2:
        print('Usage: cmd modelpath')
        exit()

    modelpath = args[1]

    print('Loading feature extractor...')
    start = time.time()
    fe = FeatureExtractor(modelpath)
    fe.loadModel()
    print('Model Load time: {:.4f}s'.format(time.time() - start))
    # Change to use appropriate models
    feMap = dict()
    for k, _ in LABELS.items():
        feMap[k] = fe

    print('Warming up model...')
    fe.extract(
        np.random.randint(
            0, 255, (fe.INPUT_HEIGHT, fe.INPUT_HEIGHT, 3)).astype(np.uint8))

    fedb = FeatureVectorDatabase()
    idAssociator = IdAssociator(feMap, fedb)

    server = CentralServer(MQTT_SERVER, idAssociator)
    server.run()
Ejemplo n.º 9
0
	frame_id, frame_buffer = 0, []
	with torch.no_grad():
		while True:
			ret, frame = video_stream.read()
			if frame is None: break
			height, width, _ = frame.shape
			frame_id+=1

			## Buffer to annotate the original video
			frame_buffer.append(frame)

			## Collect and predict action for CLIP_LEN frames
			if len(frame_buffer) == CLIP_LEN:				
				processed_frames = extractor.preprocess_clip_stream(frame_buffer)
				prediction, score = detector.detect(extractor.extract(processed_frames))

				## Annotate CLIP_LEN frames and write it out to the output file
				for f in frame_buffer:
					cv2.putText(f,"%s: %f"%(prediction, score),(30,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),2,cv2.LINE_AA)
					if not LOG:
						out.write(f)

				if LOG:
					print("%d %s %f"%(frame_id, prediction, score))
				## Reset the buffer
				frame_buffer = []

			## If the 'q' key is pressed, break from the loop
			key = cv2.waitKey(1) & 0xFF
			if key == ord("q"):
Ejemplo n.º 10
0
    .avg_count_synsets() \
    .difference_in_length() \
    .similarity_diff_to_target() \
    .max_dependency_tree_depth() \
    .target_word_synset_count() \
    .token_count_norm_diff()\
    .elmo_similarity()

model_trainer = ModelTrainer(german_config, german_config.logger)

german_classifier = WordSenseAlignmentClassifier(german_config,
                                                 feature_extractor,
                                                 model_trainer)
data = german_classifier.load_data().get_preprocessed_data()

feats = feature_extractor.extract(data,
                                  feats_to_scale=['len_diff', 'pos_diff'])
feats = feature_extractor.keep_feats(['pos_diff', 'lemma_match_normalized', 'NOUN', 'CCONJ', 'PUNCT',
                                      'synsets_count_diff', 'len_diff', 'simdiff_to_target',
                                      'target_word_synset_count', 'token_count_norm_diff', 'elmo_sim']) \

x_trainset, x_testset = model_trainer.split_data(feats, 0.0)

with open(
        'models/en_nuig_split_biggestRandomForestClassifier20200331-0117.pickle',
        'rb') as pickle_file:
    clf = pickle.load(pickle_file)
    predicted = clf.predict(x_trainset)
    print(predicted)
    predicted_series = pd.Series(predicted)
    data['relation'] = predicted_series
    german_predicted = data[['word', 'pos', 'def1', 'def2', 'relation']]
Ejemplo n.º 11
0
import glob
import os
import pickle
from PIL import Image
from feature_extractor import FeatureExtractor

fe = FeatureExtractor()

for img_path in sorted(glob.glob('static/img/*.jpg')):
    print(img_path)
    img = Image.open(img_path)  # PIL image
    print(img)
    feature = fe.extract(img)
    feature_path = 'static/feature/' + os.path.splitext(os.path.basename(img_path))[0] + '.pkl'
    pickle.dump(feature, open(feature_path, 'wb'))
Ejemplo n.º 12
0
            continue
        if db_object.is_doc_type:
            """document"""
            if db_object.contains_images:
                images_array = []
                images_id = []
                for image in db_object.files:
                    pdf_image = str(uuid.uuid4()) + ".jpg"
                    with open(pdf_image, 'wb') as file_to_save:
                        file_to_save.write(image.file.read())
                    images_array.append(pdf_image)
                    images_id.append(image.file)
                to_save = list()
                for image, image_id in zip(images_array, images_id):
                    try:
                        image_feature = fe.extract(image)
                        feature_to_save = pickle.dumps(image_feature)
                        save_to_db(db_object, feature_to_save, file=image_id)
                    except Exception as e:
                        print(str(e) + "Exception in predict")
                        err_logger(str(e) + "Exception in predict")
                        continue
                print(".....................FINISHED PROCESSING FILE.....................")
                update_state(file_name)
            else:
                print(".....................FINISHED PROCESSING FILE.....................")

        else:
            """image"""
            with open(file_name, 'wb') as file_to_save:
                file_to_save.write(db_object.file.read())
    .difference_in_length() \
    .similarity_diff_to_target() \
    .max_dependency_tree_depth() \
    .target_word_synset_count()

model_trainer = ModelTrainer(german_config, german_config.logger)

german_classifier = WordSenseAlignmentClassifier(german_config,
                                                 feature_extractor,
                                                 model_trainer)
data = german_classifier.load_data().get_preprocessed_data()

feats = feature_extractor.extract(data,
                                  feats_to_scale=[
                                      'similarities', 'len_diff', 'pos_diff',
                                      'max_depth_deptree_1',
                                      'max_depth_deptree_2', 'synset_count_1',
                                      'synset_count_2',
                                      'target_word_synset_count'
                                  ])
x_trainset, x_testset = model_trainer.split_data(feats, 0.0)

with open('models/en_nuig_svmVotingClassifier20200325-1321.pickle',
          'rb') as pickle_file:
    clf = pickle.load(pickle_file)
    predicted = clf.predict(x_trainset)
    print(predicted)
    predicted_series = pd.Series(predicted)
    data['relation'] = predicted_series
    german_predicted = data[['word', 'pos', 'def1', 'def2', 'relation']]
    german_predicted.to_csv('english_nuig_svm_20200325.csv',
                            sep='\t',
Ejemplo n.º 14
0
def make_training_data(feature_funcs, annotations):
    """
    Given the FrameNet annotations, return the training instances in terms of the tree nodes

    >>> from annotation import parse_fulltext
    >>> annotations = parse_fulltext("test_data/annotation.xml")
    >>> from features import DummyNodeFeature
    >>> instances = make_training_data([DummyNodeFeature], annotations)
    >>> len(instances) # 27 nodes times 2 annotations
    52
    >>> [i for i in instances if i[1] == 'Recipient'][0][0]
    {'node_dummy': ([u'to', u'Goodwill'], u'PP')}
    >>> [i for i in instances if i[1] == 'Donor'][0][0]
    {'node_dummy': ([u'Your'], u'PRP$')}
    >>> [i for i in instances if i[1] == 'Means'][0][0]
    {'node_dummy': ([u'Your', u'contribution', u'to', u'Goodwill'], u'NP')}
    >>> [i for i in instances if i[1] == 'Value'][0][0]
    {'node_dummy': ([u'more', u'than', u'you', u'may', u'know'], u'ADVP')}

    >>> from features import PathToFrame
    >>> annotations = parse_fulltext("test_data/annotation3.xml")
    >>> instances = make_training_data([PathToFrame], annotations)
    """
    extractor = FeatureExtractor(feature_funcs)

    training_instances = []

    for sent_str, anns in annotations:
        tree = parser.raw_parse(sent_str).next()
        tree = convert_brackets(tree)
        # print tree
        # some preprocessing, align the positions and
        # also use the sentence string given the parse tree
        anns = align_annotation_with_sentence(sent_str,
                                              ' '.join(tree.leaves()), anns)
        sent_str = ' '.join(tree.leaves())
        for ann in anns:
            frame_name = ann.frame_name
            start, end = ann.target.start, ann.target.end
            frame = Frame(start, end, frame_name)
            frame_node = find_node_by_positions(tree, start, end)

            # TODO: bug here
            if frame_node is None:
                sys.stderr.write(
                    "Warning: %r does not correspond to any tree node in sentence \"%s\"\nSkip it\n "
                    % (frame, sent_str))
                continue

            for node, (node_start_pos, node_end_pos) in collect_nodes(tree):
                node_pos = NodePosition(node_start_pos, node_end_pos)
                context = Context(sent_str, tree, frame, node_pos)

                feature_values = extractor.extract(node, context)

                # try to see the it has some semantic role
                found_matching_node = False
                for fe in ann.FE:
                    other_node = find_node_by_positions(tree, fe.start, fe.end)
                    if node == other_node:
                        training_instances.append((feature_values, fe.name))
                        found_matching_node = True
                        break

                # semantic role => NULL
                if not found_matching_node:
                    training_instances.append((feature_values, 'NULL'))

    return training_instances
Ejemplo n.º 15
0
##        return render_template('index.html')
##
##if __name__=="__main__":
##    app.run()
##@app.route('/', methods=['GET', 'POST'])

#file = request.files['query_img']
#file="D:/tensorflow/sis-master/static/img/pic_559.png"
file = sys.argv[1]
img = Image.open(file)  # PIL image

#img = Image.open(file.stream)  # PIL image
#uploaded_img_path = "static/uploaded/" + datetime.now().isoformat() + "_" + file.filename
#img.save(uploaded_img_path)

query = fe.extract(img)
dist = np.linalg.norm(features - query, axis=1)  # Do search
ids = np.argsort(dist)[:3]
dist = [dist[id] for id in ids]
retrieved_img_paths = [img_paths[id] for id in ids]
#print(retrieved_img_paths)
file = open("image_path.txt", "w")
print(dist)
for score, x in zip(dist, retrieved_img_paths):
    img = Image.open(x)
    img.show()
    print(x + ',' + str(score))
    file.write(x + "|" + str(score) + "\n")
file.close()
##    cv2.imshow("Results 1-5", x)
##    cv2.waitKey(0)
Ejemplo n.º 16
0
    if not os.path.isfile(srcfile):
        print(srcfile + " not exist!")
    else:
        fpath, fname = os.path.split(dstfile)  #分离文件名和路径
        if not os.path.exists(fpath):
            os.makedirs(fpath)  #创建路径
        shutil.move(srcfile, dstfile)  #移动文件
        print("move " + srcfile + " -> " + dstfile + "\n")


fe = FeatureExtractor()
while True:

    for img_path in sorted(glob.glob('static/processing-image/*.jpg')):
        try:
            print(img_path)
            img = Image.open(img_path)  # PIL image
            feature = fe.extract(img)
            feature_path_tmp = 'static/feature/' + os.path.splitext(
                os.path.basename(img_path))[0] + '.tmp'
            feature_path = 'static/feature/' + os.path.splitext(
                os.path.basename(img_path))[0] + '.pkl'
            pickle.dump(feature, open(feature_path_tmp, 'wb'))
            movefile(img_path,
                     img_path.replace("static/processing-image", "static/img"))
            movefile(feature_path_tmp, feature_path)
        except:
            pass

    time.sleep(5)
Ejemplo n.º 17
0
# Convert arguments to description of features.
size = args.char_n_gram_size
character_grams = list(map(lambda x: (x, size), args.char_n_gram))

size = args.special_n_gram_size
special_grams = list(map(lambda x: (x, size), args.special_n_gram))

word_frequencies = args.word_frequencies

size = args.postag_n_gram_size
postag_grams = list(map(lambda x: (x, size), args.postag_n_gram))

size = args.word_n_gram_size
word_grams = list(map(lambda x: (x, size), args.word_n_gram))

# Get authors from input folder.
authors = analyse_input_folder(args.data_folder)

feature_extractor = FeatureExtractor(authors,
                                     character_grams=character_grams,
                                     special_character_grams=special_grams,
                                     word_frequencies=word_frequencies,
                                     postag_grams=postag_grams,
                                     word_grams=word_grams,
                                     corpus=args.corpus,
                                     normalize=args.normalize,
                                     feature_header=args.feature_header)

feature_extractor.extract(args.outfile, args.master_file)
    folder = args[1]
    modelpath = args[2]

    md_file = os.path.join(folder, 'normalized_class_mapping.txt')
    if not os.path.exists(md_file):
        md_file = os.path.join(folder, 'class_mapping.txt')
        if not os.path.exists(md_file):
            print('No metadata found in folder {}'.format(folder))
            exit()

    fe = FeatureExtractor(modelpath)
    print('Loading Model...')
    start = time.time()
    fe.loadModel()
    print('Warming up model...')
    fe.extract(cv2.imread('amur_small/002019.jpg'))
    print('Total time to load model: {:.4f}s'.format(time.time() - start))

    x_train_names, y_train, x_val_names, y_val = getTrainValData(folder, md_file)
    print('Train classes: {}, Valid Classes: {}'.format(len(set(y_train)), len(set(y_val))))
    print('Total Train images: {}, Total validation images: {}'.format(len(x_train_names), len(x_val_names)))

    print('Extracting features...This could take a while...')
    start = time.time()
# Extract feature for each image in training set
    x_train = np.asarray([fe.extract(cv2.imread(x)) for x in x_train_names])
    x_val = np.asarray([fe.extract(cv2.imread(x)) for x in x_val_names])
    total_time = time.time() - start

    print('Total Feature extraction time: {:.4f}s, Average time per feature extraction: {:.4f}s'.format(total_time, total_time / (len(x_train) + len(x_val))))
Ejemplo n.º 19
0
from log_parser import LogParser
from feature_extractor import FeatureExtractor
from clustering import Clustering
from idf import IDF

parser = LogParser('../../data/HDFS_2K.log')
tagged_events, log_sequences = parser.parse()

extractor = FeatureExtractor(log_sequences, list(tagged_events.keys()))
log_sequences = extractor.extract()

clustering = Clustering(log_sequences, tagged_events)
cluster_ids, cluster_values, silhouette = clustering.cluster()

for cluster_value in cluster_values:
    if cluster_value['num_possible_abnormal_events'] != 0:
        print(cluster_value)
        print()

#print ("El coeficiente de silueta es =", silhouette)

Ejemplo n.º 20
0
    .tfidf() \
    .ont_hot_pos() \
    .matching_lemma() \
    .count_each_pos() \
    .cosine() \
    .jaccard() \
    .difference_in_length()

model_trainer = ModelTrainer(german_config, german_config.logger)

german_classifier = WordSenseAlignmentClassifier(german_config,
                                                 feature_extractor,
                                                 model_trainer)
data = german_classifier.load_data().get_preprocessed_data()

feats = feature_extractor.extract(
    data, feats_to_scale=['similarities', 'len_diff', 'pos_diff'])
feats = feature_extractor.keep_feats([
    'similarities', 'cos_tfidf', 'ADP', 'DET', 'pos_diff', 'len_diff', 'PRON',
    'CONJ', 'X', 'PROPN', 'NOUN', 'cos', 'ADJ', 'VERB', 'jaccard', 'PUNCT',
    'noun', 'ADV', 'adjective'
])
x_trainset, x_testset = model_trainer.split_data(feats, 0.0)

with open(
        'models/dutch_all_features_nonebalanceRandomForestClassifier20200329-1354.pickle',
        'rb') as pickle_file:
    clf = pickle.load(pickle_file)
    predicted = clf.predict(x_trainset)
    print(predicted)
    predicted_series = pd.Series(predicted)
    data['relation'] = predicted_series
import os
from feature_extractor import FeatureExtractor
import feature_extractor as fe
from PIL import Image
import glob
import numpy as np

dir_path = os.path.dirname(os.path.realpath(__file__))
data_dir = dir_path + '/' + "static/data"
feature_dir = dir_path + '/' + "features"
os.makedirs(data_dir, exist_ok=True)
os.makedirs(feature_dir, exist_ok=True)

f = FeatureExtractor()
#f.store_features(data_dir , feature_dir)
#fe.store_features(data_dir , feature_dir)

data_path = data_dir
storing_path = feature_dir
for each in glob.glob(data_path + '/*'):
    image_name = (each.rsplit('/', 1)[-1]).rsplit('.jpg')[0]
    img = Image.open(each)
    feature = f.extract(img)
    np.save(storing_path + '/' + image_name, feature)