def attempt_image_load(in_file, out_file): try: return gl.Image(out_file) except Exception: pass for fuzz in [8, 6, 4, 2, 1]: try: subprocess.check_call('./make_conservative_image.sh %s %s %d' % (in_file, out_file, fuzz), shell=True) return gl.Image(out_file) except Exception: pass return None
def classify(self, path, h=48, w=48, channels=1): """Classify the image :param path: path to image file :param h: image height :param w: image width :param channels: number of channels for the image :return: classifications """ image = gl.Image(path) data = image.pixel_data.copy() image, face = self._image_processor.process_image(data) if face is None: return None face = face.flatten() face = face - np.mean(face) face /= np.std(face) fmin = np.min(face) fmax = np.max(face) face = np.floor(255 * (face - fmin) / (fmax - fmin)) face_arr = gl.SArray([face.tolist()]) clf_image = face_arr.pixel_array_to_image(h, w, channels, allow_rounding=True) x = gl.SFrame({'images': clf_image}) classifications = self._classifier(x) return image, clf_image[0], classifications
def load_perturbed_image(d): in_file = d['path'] seed = d['row_number'] out_file = join(out_file_base, image_path) subprocess.call('./make_perturbed_image.sh %s %s %d' % (in_file, out_file, seed), shell=True) return gl.Image(out_file)
def import_train_data(filename): data = {} document_dir, label_list = get_filename(filename) image_list = [] for i in xrange(len(document_dir)): file_locate = filename + '/' + document_dir[i] image_list.append(gl.Image(file_locate)) # value_dic.setdefault(document_dir[i].strip().split('_')[0],label_list[i]) data.setdefault('label', label_list) data.setdefault('image', image_list) df = pd.DataFrame(data=data) return df
def import_test_data(filename): document_dir, label_list = get_filename(filename) data = {} image_list = [] label_list = [] for i in xrange(len(document_dir)): file_locate = filename + '/' + document_dir[i] image_list.append(gl.Image(file_locate)) label_list.append(-1) data.setdefault('label', label_list) data.setdefault('image', image_list) df = pd.DataFrame(data=data) return df
def upload(): file = request.files['user_upload'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) #---------------- BEGIN TO RUN TRANSFORMATIONS ON IMAGE INPUTS ---------------- # pic = gl.Image("./uploads/"+filename) #loading in dummy image since deep feature extractor only works in dataframe pic = [gl.image_analysis.resize(pic, 100, 100, 3, decode=True), gl.Image("../image_data_dump/1.jpg")] pic = gl.SFrame(pic) #extract features extractor = gl.feature_engineering.DeepFeatureExtractor(features = "X1", model='auto') extractor_g = extractor.fit(pic) features_g = extractor_g.transform(pic) #predict probabilities d_values = svm_d_model.predict_proba(features_g[0]["deep_features.X1"]) d_keys = svm_d_model.classes_ g_values = svm_g_model.predict_proba(features_g[0]["deep_features.X1"]) g_keys = svm_g_model.classes_ #dictionary these d_values = d_values.tolist() d_values = [round(item,3) for sublist in d_values for item in sublist] d_dict = dict(zip(d_keys, d_values)) g_values = g_values.tolist() g_values = [round(item,3) for sublist in g_values for item in sublist] g_dict = dict(zip(g_keys, g_values)) #return results result = {'decade': d_dict, 'genre': g_dict} return jsonify(result)
def graph_lab(url, format = 'auto', flip_img = False, zoom = False): """Extracts the graphlab features""" if format == 'auto': extension = url.split('.')[-1] img = preprocess(url) if flip_img: img = flip(img) if zoom: img = middle(img) h,w,_ = img.shape img_bytes = bytearray(img) image_data_size = len(img_bytes) img = graphlab.Image(_image_data=img_bytes, _width=w, _height=h, _channels=3, _format_enum=2, _image_data_size=image_data_size) return SFrame({'image': [img]})
def append_images(json_file): # we fill an SFrame with all the given metadata of the dogs meta = SFrame.read_json(json_file, orient='records') # this is the SFrame that we will fill with the data plus the image, which will be saved in the final file image_list = SFrame(data=None) # for each image in the images column in the meta SFrame, we add one line in the final SF with one image per line for i in range(0, len(meta) - 1): dogo = meta[i:i + 1] for image in dogo['images'][0]: # print image dogo_clone = dogo.copy() dogo_clone.add_column(SArray([(graphlab.Image(images_path + image)) ]), name='image') dogo_clone.add_column(SArray([image]), name='image_filename') image_list = image_list.append(SFrame(dogo_clone)) image_list.save(filename='prepared_data/')
def from_pil_image(pil_img): """ Convert a PIL image to a Graphlab Image. """ height = pil_img.size[1] width = pil_img.size[0] if pil_img.mode == 'L': image_data = bytearray([z for z in pil_img.getdata()]) channels = 1 elif pil_img.mode == 'RGB': image_data = bytearray([z for l in pil_img.getdata() for z in l]) channels = 3 else: image_data = bytearray([z for l in pil_img.getdata() for z in l]) channels = 4 image_data_size = len(image_data) return gl.Image(_image_data=image_data, _width=width, _height=height, _channels=channels, _format_enum=2, _image_data_size=image_data_size)
def from_pil_image(pil_img): height = pil_img.size[1] width = pil_img.size[0] if pil_img.mode == 'L': image_data = bytearray([z for z in pil_img.getdata()]) channels = 1 elif pil_img.mode == 'RGB': image_data = bytearray( [z for l in pil_img.getdata() for z in l]) channels = 3 else: image_data = bytearray( [z for l in pil_img.getdata() for z in l]) channels = 4 format_enum = _format['RAW'] image_data_size = len(image_data) img = _gl.Image(_image_data=image_data, _width=width, _height=height, _channels=channels, _format_enum=format_enum, _image_data_size=image_data_size) return img
graphlab.canvas.show() graphlab.canvas.set_target('ipynb') # Download the training data gl_img = graphlab.SFrame('http://s3.amazonaws.com/dato-datasets/coursera/deep_learning/image_train_data') gl_img # Display the first five images gl_img['image'][0:5].show() # Resize to view it large graphlab.image_analysis.resize(gl_img['image'][2:3], 96,96).show() # Load our input image img = graphlab.Image('C:\Users\Dell\Desktop\pexels-photo.jpg') ppsf = graphlab.SArray([img]) ppsf = graphlab.image_analysis.resize(ppsf, 32,32) ppsf.show() # Create the SFrame of the image to extract its features ppsf = graphlab.SFrame(ppsf).rename({'X1': 'image'}) # Loading the deep learning model from the GraphLab website deep_learning_model = graphlab.load_model('https://static.turi.com/products/graphlab-create/resources/models/python2.7/imagenet_model_iter45') # Extract the deep features of our image ppsf['deep_features'] = deep_learning_model.extract_features(ppsf) # Give a label and id to the input image ppsf['label'] = 'me' gl_img['id'].max()
import graphlab as gl # Construct list of URLS url_list = [ 'http://s3.amazonaws.com/gl-testdata/images/sample.jpg', 'http://s3.amazonaws.com/gl-testdata/images/sample.png' ] # Construct SArry of urls url_sarray = gl.SArray(url_list) # Construct SArray of Images image_sarray = url_sarray.apply(lambda x: gl.Image(x))
def load_image(url): return gl.image_analysis.resize(gl.Image(url), 256, 256, 3)
gl_img : graphlab.Image A graphlab.Image that is to be converted to a PIL Image Returns ------- out : PIL.Image The input converted to a PIL Image """ if gl_img._format_enum == _format['RAW']: # Read in Image, switch based on number of channels. if gl_img.channels == 1: img = PIL.Image.frombytes('L', (gl_img._width, gl_img._height), str(gl_img._image_data)) elif gl_img.channels == 3: img = PIL.Image.frombytes('RGB', (gl_img._width, gl_img._height), str(gl_img._image_data)) elif gl_img.channels == 4: img = PIL.Image.frombytes('RGBA', (gl_img._width, gl_img._height), str(gl_img._image_data)) else: raise ValueError('Unsupported channel size: ' + str(gl_img.channels)) else: img = PIL.Image.open(_StringIO.StringIO(gl_img._image_data)) return img # Sample conversion gl_img = gl.Image('http://s3.amazonaws.com/gl-testdata/images/sample.jpg') pil_img = to_pil_image(gl_img)
def predict_image_url(self, path, imageSize, topk): image_sf = gl.SFrame({'image': [gl.Image(path)]}) image_sf['image'] = gl.image_analysis.resize(image_sf['image'], imageSize, imageSize) top_labels = self.model.predict_topk(image_sf, k=min(topk, 5)) return top_labels
def image_deep_features(file_path, deep_learning_model): img = graphlab.Image(file_path) image_sf = graphlab.SFrame({'image': [img]}) image_sf['deep_features'] = deep_learning_model.extract_features(image_sf) return image_sf
def visualize_detection(self, gl_im, dets): """ Visualize detection result Parameters ---------- gl_im: gl.Image The image to be visualized dets: SFrame detection result in sframe Returns ------- out: gl.Image Image with bounding boxes drawn. """ try: from PIL import ImageDraw,ImageFont except ImportError: raise ImportError('PIL or pillow package is required for this') try: import graphlab as _gl except ImportError: import sframe as _gl except ImportError: raise ImportError('Requires GraphLab Create or SFrame') _format = {'JPG': 0, 'PNG': 1, 'RAW': 2, 'UNDEFINED': 3} assert(type(gl_im) == _gl.Image) pil_img = gl_im._to_pil_image() draw = ImageDraw.Draw(pil_img) fnt = ImageFont.truetype(_os.path.join(_os.path.dirname(__file__),'Lato-Regular.ttf'), 12) assert len(dets['id'].unique()) == 1, "only support visualize single image" for row in dets: bbox = row['box'] cls = str(row['class']) score = str(row['score']) draw.rectangle(list(bbox),outline="red") draw.rectangle([bbox[0] + 1, bbox[1] + 1, bbox[2] - 1, bbox[3] - 1], outline="red") draw.rectangle([bbox[0] + 2, bbox[1] + 2, bbox[2] - 2, bbox[3] - 2], outline="red") draw.text((bbox[0] + 2, bbox[1] - 16),cls + ":" + score, font=fnt, fill="red") height = pil_img.size[1] width = pil_img.size[0] if pil_img.mode == 'L': image_data = bytearray([z for z in pil_img.getdata()]) channels = 1 elif pil_img.mode == 'RGB': image_data = bytearray([z for l in pil_img.getdata() for z in l ]) channels = 3 else: image_data = bytearray([z for l in pil_img.getdata() for z in l]) channels = 4 format_enum = _format['RAW'] image_data_size = len(image_data) # Construct a graphlab.Image return _gl.Image(_image_data=image_data, _width=width, _height=height, _channels=channels, _format_enum=format_enum, _image_data_size=image_data_size)