-
Notifications
You must be signed in to change notification settings - Fork 0
/
extraction.py
67 lines (55 loc) · 1.72 KB
/
extraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import h5py
import numpy as np
import argparse
import time
from PIL import Image
import skimage.io
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
# Local Import
from server.database.database import DAO
from retrieval.detector import Detector
from retrieval.extractor import Extractor
from server.database.model import Product
IMAGE_PATH = "server/image/database/"
if __name__ == '__main__':
database = DAO()
# Get data produk
products = database.getAll()
# Feature Extraction
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
print("--------------------------------------------------")
print(" feature extraction starts")
print("--------------------------------------------------")
start = time.time()
# Array
feats = []
ids = []
# Extractor using VGG16
modelVGG = Extractor()
# Detector
image_detector = Detector("weight/mask_rcnn_fashion.h5")
for product in products:
# Image
image = skimage.io.imread(IMAGE_PATH+product.image)
image_detection = image_detector.detection(image)
# Objek Dominan
big_box, big_ix = image_detector.get_biggest_box(image_detection['rois'])
# Crop Image
image = image_detector.crop_object(image, big_box)
norm_feat = modelVGG.extract_feat(image)
feats.append(norm_feat)
ids.append(product.id)
feats = np.array(feats)
# directory for storing extracted features
output = "featureCNN_map.h5"
print("--------------------------------------------------")
print(" writing feature extraction results ...")
print("--------------------------------------------------")
h5f = h5py.File(output, 'w')
h5f.create_dataset('feats', data = feats)
h5f.create_dataset('id', data = ids)
h5f.close()
end = time.time()
print("Cost time: ",end-start," (s)")