Exemple #1
0
def dl_images(source_domain='stylebook.de',
              text_filter='',
              dl_dir='/data/jeremy/image_dbs/golden/',
              in_docker=True,
              visual_output=False):
    '''
    dl everything in the images db, on the assumption that these are the  most relevant to test our answers to
    :return:
    '''

    if in_docker:
        db = pymongo.MongoClient('localhost', port=27017).mydb
    else:
        db = constants.db

    all = db.images.find({'domain': source_domain})
    for doc in all:
        url = doc['image_urls'][0]
        if text_filter in url[0]:
            print url
            Utils.get_cv2_img_array(url,
                                    convert_url_to_local_filename=True,
                                    download=True,
                                    download_directory=dl_dir)
        else:
            print('skipping ' + url)

    #move the images with more than one person
    imutils.do_for_all_files_in_dir(imutils.one_person_per_image,
                                    dl_dir,
                                    visual_output=False)
def get_pd_results(url=None, filename=None, img_arr=None):
    if url is not None:
        print('getting pd results for ' + url)
        image = Utils.get_cv2_img_array(url)
    elif filename is not None:
        print('getting pd results for ' + filename)
        image = cv2.imread(filename)
    elif img_arr is not None:
        print('getting pd results for img_arr ')
        image = img_arr
    if image is None:
        print('image came back none')
        return None
    try:
        seg_res = pd_falcon_client.pd(image)
    except:
        print('exception in pd_falcon_client.pd ' + str(sys.exc_info()[0]))
        return


#    print('segres:'+str(seg_res))
    if not 'mask' in seg_res:
        print('pd did not return mask')
        print seg_res
        return
    mask = seg_res['mask']
    label_dict = seg_res['label_dict']
    print('labels:' + str(label_dict))
    #    conversion_utils.count_values(mask)
    pose = seg_res['pose']
    mask_np = np.array(mask, dtype=np.uint8)
    print('masksize ' + str(mask_np.shape))
    pose_np = np.array(pose, dtype=np.uint8)
    print('posesize ' + str(pose_np.shape))
    return [mask, label_dict, pose_np]
def download_image(prod, feature_name, category_id, max_images):
    downloaded_images = 0
    directory = os.path.join(category_id, feature_name)
    try:
        downloaded_images = len([name for name in os.listdir(directory) if os.path.isfile(name)])
    except:
        pass
    if downloaded_images < max_images:
            xlarge_url = prod['image']['sizes']['XLarge']['url']

            img_arr = Utils.get_cv2_img_array(xlarge_url)
            if img_arr is None:
                logging.warning("Could not download image at url: {0}".format(xlarge_url))
                return

            relevance = background_removal.image_is_relevant(img_arr)
            if relevance.is_relevant:
                logging.info("Image is relevant...")

                filename = "{0}_{1}.jpg".format(feature_name, prod["id"])
                filepath = os.path.join(directory, filename)
                Utils.ensure_dir(directory)
                logging.info("Attempting to save to {0}...".format(filepath))
                success = cv2.imwrite(filepath, img_arr)
                if not success:
                    logging.info("!!!!!COULD NOT SAVE IMAGE!!!!!")
                    return 0
                # downloaded_images += 1
                logging.info("Saved... Downloaded approx. {0} images in this category/feature combination"
                             .format(downloaded_images))
                return 1
            else:
                # TODO: Count number of irrelevant images (for statistics)
                return 0
 def test_gender(self):
     for url in self.urls:
         img_arr = Utils.get_cv2_img_array(url)
         if img_arr is None:
             continue
         print('image size:' + str(img_arr.shape))
         face_dict = cropping.find_that_face(img_arr, 1)
         face = face_dict['faces'][0]
         print('face x,y,w,h: ' + str(face))
         result = classifier_client.get('gender', url, url=url, face=face)
         print('result for gender on {} is {}'.format(url, result))
def get_pd_results_and_save(url=None, filename=None):
    if url is not None:
        print('getting pd results for ' + url)
        image = Utils.get_cv2_img_array(url)
    elif filename is not None:
        print('getting pd results for ' + filename)
        image = cv2.imread(filename)
    if image is None:
        print('image came back none')
        return None
    try:
        seg_res = pd_falcon_client.pd(image)
    except:
        print('exception in pd_falcon_client.pd ' + str(sys.exc_info()[0]))
        return
#    print('segres:'+str(seg_res))
    if filename is not None:
        imgfilename = os.path.basename(
            filename
        )  #use the actual on-disk filename if thats what we started with, otherwise use the name generated by pd
        #this will et saved to /home/jeremy/pd_output/filename
    else:
        try:
            imgfilename = seg_res['filename'] + '.jpg'
        except:
            print('some error on imgfile name')
            imgfilename = str(int(time.time())) + '.jpg'
    print('filename ' + imgfilename)
    if not 'mask' in seg_res:
        print('pd did not return mask')
        print seg_res
        return
    mask = seg_res['mask']
    label_dict = seg_res['label_dict']
    print('labels:' + str(label_dict))
    conversion_utils.count_values(mask)
    pose = seg_res['pose']
    mask_np = np.array(mask, dtype=np.uint8)
    print('masksize ' + str(mask_np.shape))
    pose_np = np.array(pose, dtype=np.uint8)
    print('posesize ' + str(pose_np.shape))
    #    print('returned url '+seg_res['url'])
    converted_mask = convert_and_save_results(mask_np, label_dict, pose_np,
                                              imgfilename, image, url)
    dir = constants.pd_output_savedir
    Utils.ensure_dir(dir)
    full_name = os.path.join(dir, imgfilename)
    #            full_name = filename
    bmp_name = full_name.strip('.jpg') + (
        '.bmp')  #this bmp should get generated by convert_and_save_results
    #    imutils.show_mask_with_labels(bmp_name,constants.fashionista_categories_augmented_zero_based,original_image = full_name,save_images=False)
    return mask, label_dict, pose_np, converted_mask
def get_single_label_output(url_or_np_array,
                            required_image_size=(227, 227),
                            output_layer_name='prob'):
    '''
    CURRENTLY INOPERATIONAL
    :param url_or_np_array:
    :param required_image_size:
    :param output_layer_name:
    :return:
    '''
    if isinstance(url_or_np_array, basestring):
        print('infer_one working on url:' + url_or_np_array)
        image = Utils.get_cv2_img_array(url_or_np_array)
    elif type(url_or_np_array) == np.ndarray:
        image = url_or_np_array
# load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
#    im = Image.open(imagename)
#    im = im.resize(required_imagesize,Image.ANTIALIAS)
#    in_ = in_.astype(float)
    in_ = imutils.resize_keep_aspect(image,
                                     output_size=required_image_size,
                                     output_file=None)
    in_ = np.array(in_, dtype=np.float32)  #.astype(float)
    if len(in_.shape) != 3:  #h x w x channels, will be 2 if only h x w
        print('got 1-chan image, turning into 3 channel')
        #DEBUG THIS , ORDER MAY BE WRONG [what order? what was i thinking???]
        in_ = np.array([in_, in_, in_])
    elif in_.shape[
            2] != 3:  #for rgb/bgr, some imgages have 4 chan for alpha i guess
        print('got n-chan image, skipping - shape:' + str(in_.shape))
        return
#    in_ = in_[:,:,::-1]  for doing RGB -> BGR : since this is loaded nby cv2 its unecessary
#    cv2.imshow('test',in_)
    in_ -= np.array((104, 116, 122.0))
    in_ = in_.transpose((2, 0, 1))
    # shape for input (data blob is N x C x H x W), set data
    multilabel_net.blobs['data'].reshape(1, *in_.shape)
    multilabel_net.blobs['data'].data[...] = in_
    # run net and take argmax for prediction
    multilabel_net.forward()
    #    out = net.blobs['score'].data[0].argmax(axis=0) #for a parse with per-pixel max
    out = multilabel_net.blobs[output_layer_name].data[
        0]  #for the nth class layer #siggy is after sigmoid
    min = np.min(out)
    max = np.max(out)
    print('out  {}'.format(out))
Exemple #7
0
def create_training_set_with_grabcut(collection):
    coll = db[collection]
    i = 1
    total = db.training_images.count()
    start = time.time()
    for doc in coll.find():
        if not i % 10:
            print "did {0}/{1} documents in {2} seconds".format(
                i, total,
                time.time() - start)
            print "average time for image = {0}".format(
                (time.time() - start) / i)
        url = doc['url'].split('/')[-1]
        img_url = 'https://tg-training.storage.googleapis.com/tamara_berg_street2shop_dataset/images/' + url
        image = Utils.get_cv2_img_array(img_url)
        if image is None:
            print "{0} is a bad image".format(img_url)
            continue
        i += 1
        small_image, ratio = background_removal.standard_resize(image, 600)
        # skin_mask = kassper.skin_detection_with_grabcut(small_image, small_image, skin_or_clothes='skin')
        # mask = np.where(skin_mask == 255, 35, 0).astype(np.uint8)
        mask = np.zeros(small_image.shape[:2], dtype=np.uint8)
        for item in doc['items']:
            try:
                bb = [int(c / ratio) for c in item['bb']]
                item_bb = get_margined_bb(small_image, bb, 0)
                if item['category'] not in cats:
                    continue
                category_num = cats.index(item['category'])
                item_mask = background_removal.simple_mask_grabcut(
                    small_image, rect=item_bb)
            except:
                continue
            mask = np.where(item_mask == 255, category_num, mask)
        filename = 'tamara_berg_street2shop_dataset/masks/' + url[:-4] + '.txt'
        save_to_storage(bucket, mask, filename)
        coll.update_one({'_id': doc['_id']}, {
            '$set': {
                'mask_url':
                'https://tg-training.storage.googleapis.com/' + filename
            }
        })
    print "Done masking! took {0} seconds".format(time.time() - start)
def make_masks(images):
    img_arrs = []
    masks = []
    mask_items = []
    for url1 in images:
        max_retry = 5
        got_mask = False
        img_arr = Utils.get_cv2_img_array(url1)
        while max_retry and not got_mask:
            try:
                mask, labels, pose = paperdoll_parse_enqueue.paperdoll_enqueue(
                    img_arr, at_front=True, async=False).result[:3]
                got_mask = np.any(mask)  # condition for legal mask?
                print(str(got_mask))
                masks.append(mask)

                final_mask = paperdolls.after_pd_conclusions(
                    mask, labels)  #, person['face'])
                for num in np.unique(final_mask):
                    category = list(labels.keys())[list(
                        labels.values()).index(num)]
                    if category == 'dress' and category in constants.paperdoll_shopstyle_women.keys(
                    ):
                        print("Found dress!!")
                        mask_item = 255 * np.array(final_mask == num,
                                                   dtype=np.uint8)
                        mask_items.append(mask_item)

            except Exception as e:
                max_retry = max_retry - 1
                print("Failed to get mask with exception:")
            print e.message

            if not got_mask:
                print url1 + " failed."
                continue
            else:
                print "Success: " + url1


# np.save("yuli_masks.npy", masks)
# np.save(mfilename, mask_items)
    return
def save_img_at_url(url,savename=None):
    # download the image, convert it to a NumPy array, and then save
    # it into OpenCV format using last part of url (will overwrite imgs of same name at different url)
    if not savename:
        savename = url.split('?')[0]
    img_arr = Utils.get_cv2_img_array(url)
    print('name:'+savename)
    cv2.imwrite(savename,img_arr)
    return img_arr


    if url.count('jpg') > 1:
        print('np jpg here captain')
        return None
    resp = requests.get(url)
    print resp
#    resp = urllib.urlopen(url)
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    if image.size == 0:
        return None
    new_image = cv2.imdecode(image, cv2.IMREAD_COLOR)
Exemple #10
0
def get_hydra_output(url_or_image_arr,
                     out_dir='./',
                     orig_size=(256, 256),
                     crop_size=(224, 224),
                     mean=(104.0, 116.7, 122.7),
                     gpu=1,
                     save_data=True,
                     save_path='/data/jeremy/caffenets/hydra/production/saves',
                     detection_thresholds=constants.hydra_tg_thresholds,
                     url=None):
    '''
    start net, get a bunch of results. DONE: resize to e.g. 250x250 (whatever was done in training) and crop to dims
    :param url_or_image_arr_list:#
    :param prototxt:
    :param caffemodel:
    :param out_dir:
    :param dims:
    :param output_layers:
    :param mean:
    :return:
    '''
    detection_thresholds = None
    start_time = time.time()
    caffe.set_mode_gpu()
    caffe.set_device(gpu)
    #    print('params:'+str(hydra_net.params))
    out_layers = hydra_net.outputs
    out_layers = put_in_numeric_not_alphabetic_order(out_layers)
    #    print('out layers: '+str(out_layers))
    j = 0
    output_names = constants.hydra_tg_heads

    # load image, resize, crop, subtract mean, and make dims C x H x W for Caffe
    im = Utils.get_cv2_img_array(url_or_image_arr)
    if im is None:
        logging.warning('could not get image ' + str(url_or_image_arr))
        return None
    if isinstance(url_or_image_arr, basestring):
        print('get_hydra_output working on:' + url_or_image_arr)
    print('img  size:' + str(im.shape))
    im = imutils.resize_keep_aspect(im, output_size=orig_size)
    im = imutils.center_crop(im, crop_size)

    in_ = np.array(im, dtype=np.float32)
    if len(in_.shape) != 3:
        print('got 1-chan image, skipping')
        return
    elif in_.shape[2] != 3:
        print('got n-chan image, skipping - shape:' + str(in_.shape))
        return
    in_ -= mean
    in_ = in_.transpose((2, 0, 1))  #W,H,C -> C,W,H
    hydra_net.blobs['data'].reshape(1, *in_.shape)
    hydra_net.blobs['data'].data[...] = in_
    # run net and take argmax for prediction
    hydra_net.forward()
    out = {}
    i = 0
    for output_layer in out_layers:
        one_out = hydra_net.blobs[output_layer].data[
            0]  #not sure why the data is nested [1xN] matrix and not a flat [N] vector
        second_neuron = copy.copy(
            one_out[1]
        )  #the copy is required - if you dont do it then out gets over-written with each new one_out
        second_neuron = round(float(second_neuron), 3)
        #      print('type:'+str(type(second_neuron)))
        name = output_names[i]
        if detection_thresholds is None:
            out[name] = second_neuron
            print('{} for category {} {}'.format(second_neuron, i, name))
        elif second_neuron > detection_thresholds[i]:
            out[name] = second_neuron
            print('{} is past threshold {} for category {} {}'.format(
                second_neuron, detection_thresholds[i], i, name))
        logging.debug('output for {} {} is {}'.format(output_layer, name,
                                                      second_neuron))
        #        print('final till now:'+str(all_outs)+' '+str(all_outs2))
        i = i + 1
    logging.debug('all output:' + str(out))
    logging.debug('elapsed time:' + str(time.time() - start_time))

    if save_data:
        if isinstance(url_or_image_arr, basestring):
            filename = url_or_image_arr.replace('https://', '').replace(
                'http://', '').replace('/', '_')
            url = url_or_image_arr
        else:
            n_chars = 6
            filename = ''.join(
                random.choice(string.ascii_uppercase + string.digits)
                for _ in range(n_chars)) + '.jpg'
            if url is None:
                url = 'not_from_url'
        Utils.ensure_dir(save_path)
        imgname = os.path.join(save_path, filename)
        if imgname[:-4] != '.jpg':
            imgname = imgname + '.jpg'
        cv2.imwrite(imgname, im)
        #    out['imgname']=filename
        out['url'] = url
        textfile = os.path.join(save_path, 'output.txt')
        with open(textfile, 'a') as fp:
            json.dump(out, fp, indent=4)
            fp.close()
        print('wrote image to {} and output text to {}'.format(
            imgname, textfile))

    return out
    return mask #

if __name__ == "__main__":
    urls = ['https://s-media-cache-ak0.pinimg.com/236x/ce/64/a0/ce64a0dca7ad6d609c635432e9ae1413.jpg',  #bags
            'http://pinmakeuptips.com/wp-content/uploads/2015/02/1.4.jpg',
            'https://s-media-cache-ak0.pinimg.com/564x/9a/9d/f7/9a9df7455232035c6284ad1961816fd8.jpg',
            'http://2.bp.blogspot.com/-VmiQlqdliyE/U9nyto2L1II/AAAAAAAADZ8/g30i4j_YZeI/s1600/310714+awayfromblue+kameleon+shades+chambray+shift+dress+converse+louis+vuitton+neverfull+mbmj+scarf.png',
            'https://s-media-cache-ak0.pinimg.com/236x/1b/31/fd/1b31fd2182f0243ebc97ca115f04f131.jpg',
            'http://healthsupporters.com/wp-content/uploads/2013/10/belt_2689094b.jpg' ,
            'http://static1.businessinsider.com/image/53c96c90ecad04602086591e-480/man-fashion-jacket-fall-layers-belt.jpg', #belts
            'http://gunbelts.com/media/wysiwyg/best-gun-belt-width.jpg',
            'https://i.ytimg.com/vi/5-jWNWUQdFQ/maxresdefault.jpg'
            ]

    start_time=time.time()
    for url in urls:
        image = Utils.get_cv2_img_array(url)
        output = get_mlb_output(image)
#        output = get_mlb_output(url)
#        output1 = multilabel_from_binaries.get_multiple_single_label_outputs(url)
#        output2 = multilabel_from_binaries2.get_multiple_single_label_outputs(url)
        print('final output for {} : cat {} '.format(url,output))
#        print('final output for {} : cat {} {}'.format(url,output1,output2))
    elapsed_time = time.time()-start_time
    print('time per image:{}, {} elapsed for {} images'.format(elapsed_time/len(urls),elapsed_time,len(urls)))
#    cv2.imshow('output',output)

    raw_input('ret to cont')
    test_combine_neurodoll_and_multilabel(url)
    raw_input('ret to cont')
    test_combine_neurodoll_nonfalcon_and_multilabel_falcon(url)
Exemple #12
0
import argparse
import glob
import time

counter = 0
ext = [".jpg", ".png"]
mother_path = '/home/jeremy/image_dbs/colorful_fashion_parsing_data'

sets = {'images', 'labels', 'labels_200x150'}

for set in sets:
    path = os.path.join(mother_path, set)

    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(tuple(ext)):

                image = Utils.get_cv2_img_array(os.path.join(root, file))

                mirrored_image = cv2.flip(image, 1)

                words = file.split('.')

                cv2.imwrite(
                    os.path.join(root,
                                 str(words[0]) + '-mirrored.' + str(words[1])),
                    mirrored_image)

                counter += 1
                print counter
                print os.path.join(root, words[0] + '-mirrored.' + words[1])
Exemple #13
0
def check_db_speed(url, products_collection, category, annoy_list):
    image = Utils.get_cv2_img_array(url)
    if image is None:
        print "Couldn't download image.."
        return
    mask = np.random.rand(image.shape[0], image.shape[1])
    mask = np.where(mask < 0.1, 255, 0).astype(np.uint8)
    start = time.time()
    fp = fingerprint_core.dict_fp(image, mask, category)
    # find_similar_mongo.find_top_n_results(image=image, mask=mask, number_of_results=100, category_id=category,
    #                                       collection=products_collection, dibi=db)
    # entries = db[products_collection].find({'categories': category},
    #                                        {"id": 1, "fingerprint": 1, "images.XLarge": 1, "clickUrl": 1})
    entries = db[products_collection].find(
        {
            "AnnoyIndex": {
                "$in": annoy_list
            },
            'categories': category
        },
        {
            "id": 1,
            "fingerprint": 1,
            "images.XLarge": 1,
            "clickUrl": 1
        },
    ).hint([('AnnoyIndex', 1)])
    farthest_nearest = 1
    nearest_n = []
    # tt = 0
    i = 0
    # for i, entry in enumerate(entries):
    for entry in entries:
        i += 1
        # t1 = time()
        # tt += t1-t2
        ent = entry['fingerprint']
        d = NNSearch.distance(category, fp, ent, products_collection)
        if not d:
            # t2 = time()
            # tdif = t2 - t1
            # tt += tdif
            continue
        if i < number_of_matches:
            nearest_n.append((entry, d))
            farthest_nearest = 1
        else:
            if i == number_of_matches:
                # sort by distance
                nearest_n.sort(key=lambda tup: tup[1])
                # last item in the list (index -1, go python!)
                farthest_nearest = nearest_n[-1][1]

            # Loop through remaining entries, if one of them is better, insert it in the correct location and remove last item
            if d < farthest_nearest:
                insert_at = number_of_matches - 2
                while d < nearest_n[insert_at][1]:
                    insert_at -= 1
                    if insert_at == -1:
                        break
                nearest_n.insert(insert_at + 1, (entry, d))
                nearest_n.pop()
                farthest_nearest = nearest_n[-1][1]
        # t2 = time()
        # tdif = t2-t1
        # tt+=tdif
    # print tt

    # print "sorting entries took {0} secs".format(time()-start)
    # t3 = time()
    [result[0].pop('fingerprint') for result in nearest_n]
    [result[0].pop('_id') for result in nearest_n]
    nearest_n = [result[0] for result in nearest_n]
    # t4 = time()
    # print t4-t3
    # return nearest_n
    return time.time() - start
import os
from PIL import Image
import caffe
import numpy as np
from trendi import background_removal, Utils, constants
import cv2
import sys
import argparse
import glob
import time
import skimage
from PIL import Image


path = "/home/yonatan/test_set/female/Juljia_Vysotskij_0001.jpg"
image = Utils.get_cv2_img_array(path)


def cv2_image_to_caffe(image):
    return skimage.img_as_float(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)).astype(np.float32)


def find_face(image):
    gray = cv2.cvtColor(image, constants.BGR2GRAYCONST)
    face_cascades = [
        cv2.CascadeClassifier(os.path.join(constants.classifiers_folder, 'haarcascade_frontalface_alt2.xml')),
        cv2.CascadeClassifier(os.path.join(constants.classifiers_folder, 'haarcascade_frontalface_alt.xml')),
        cv2.CascadeClassifier(os.path.join(constants.classifiers_folder, 'haarcascade_frontalface_alt_tree.xml')),
        cv2.CascadeClassifier(os.path.join(constants.classifiers_folder, 'haarcascade_frontalface_default.xml'))]
    cascade_ok = False
    for cascade in face_cascades:
def deepfashion_to_db(
    attribute_file='/data/jeremy/image_dbs/deep_fashion/category_and_attribute_prediction/list_attr_img.txt',
    bbox_file='/data/jeremy/image_dbs/deep_fashion/category_and_attribute_prediction/list_bbox.txt',
    bucket='https://tg-training.storage.googleapis.com/deep_fashion/category_and_attribute_prediction/',
    #                       bucket = 'gs://tg-training/deep_fashion/',
    use_visual_output=True):
    '''
    takes deepfashion lists of bbs and attrs, and images in bucket.
    puts bb, attr, and link to file on bucket into db
    :param attribute_file:
    :param bbox_file:
    :param bucket:
    :param use_visual_output:
    :return:
    '''

    with open(attribute_file, 'r') as fp:
        attrlines = fp.readlines()
        attrlines = attrlines[
            2:]  #1st line is # of files, 2nd line describes fields
        fp.close()
    with open(bbox_file, 'r') as fp2:
        bboxlines = fp2.readlines()
        bboxlines = bboxlines[
            2:]  #1st line is # of files, 2nd line describes fields
        fp2.close()

    bbox_files = [bboxline.split()[0] for bboxline in bboxlines]

    #    hydra_cats_for_deepfashion_folders = create_nn_imagelsts.deepfashion_to_tg_hydra(folderpath='/data/jeremy/image_dbs/deep_fashion/category_and_attribute_prediction/img')
    with open('/data/jeremy/image_dbs/labels/deepfashion_to_hydra_map.txt',
              'r') as fpx:
        hydra_cats_for_deepfashion_folders = json.load(fpx)
        fpx.close()

    hydra_cats_dirsonly = [
        dummy[0] for dummy in hydra_cats_for_deepfashion_folders
    ]

    #    print(hydra_cats_for_deepfashion_folders[0])
    #    print(hydra_cats_dirsonly[0])
    #    print(attrlines[0])
    #    print(bboxlines[0])
    db = constants.db
    #    cursor = db.training_images.find()
    for line in attrlines:
        bbox = None
        hydra_cat = None
        info_dict = {}
        info_dict['items'] = []
        #raw_input('ret to cont')
        attribute_list = []
        #print line
        path = line.split()[0]
        vals = [int(i) + 1 for i in line.split()[1:]
                ]  #the vals are -1, 1 so add 1 to get 0, 2
        non_zero_idx = np.nonzero(vals)
        print('nonzero idx:' + str(non_zero_idx))
        for i in range(len(non_zero_idx[0])):
            #print yonatan_constants.attribute_type_dict[str(non_zero_idx[0][i])]
            attribute_list.append(yonatan_constants.attribute_type_dict[str(
                non_zero_idx[0][i])])
        print('attributes:' + str(attribute_list))
        url = bucket + path
        info_dict['items'].append({'attributes': attribute_list})
        print('url:' + str(url))
        try:
            bbox_index = bbox_files.index(
                path
            )  #there is prob a better way to search here than building another list
            bbox = [int(x) for x in bboxlines[bbox_index].split()[1:]]
            print('bbox ' + str(bbox) + ' line ' + str(bboxlines[bbox_index]))
            #deepfashion bbox is x1 x2 y1 y2, convert to x y w h
            bbox_tg = [bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]]
            info_dict['items'][0]['bb'] = bbox_tg
        except ValueError:
            print(path + ' is not in bboxfile list')
        try:
            folder_only = path.replace(
                'img/', ''
            )  #the paths from attr_file (and bbfile) have img/folder not just folder
            folder_only = os.path.dirname(folder_only)
            hydra_category_index = hydra_cats_dirsonly.index(folder_only)
            hydra_cat = hydra_cats_for_deepfashion_folders[
                hydra_category_index][1]
            print(
                'hydracat ' + str(hydra_cat) + ' line ' +
                str(hydra_cats_for_deepfashion_folders[hydra_category_index]))
            info_dict['items'][0]['category'] = hydra_cat
        except ValueError:
            print(folder_only + ' is not in hydracat list')
        img_arr = Utils.get_cv2_img_array(url)
        if img_arr is None:
            print('WARNING could not get ' + url)
        else:
            h, w = img_arr.shape[0:2]
            if use_visual_output:
                if bbox is not None:
                    cv2.rectangle(
                        img_arr, (bbox_tg[0], bbox_tg[1]),
                        (bbox_tg[0] + bbox_tg[2], bbox_tg[1] + bbox_tg[3]),
                        color=[255, 0, 0],
                        thickness=5)
                cv2.imshow('deepfashion', img_arr)
                cv2.waitKey()
            info_dict['image_width'] = w
            info_dict['image_height'] = h

        info_dict['url'] = url

        #        info_dict['items'] = items
        print('db entry:' + str(info_dict))
        ack = db.training_images_deepfashion.insert_one(info_dict)
        print('ack:' + str(ack.acknowledged))
Exemple #16
0
def get_single_label_output(url_or_np_array,
                            net,
                            required_image_size=(224, 224),
                            resize=(250, 250),
                            analog_output=True,
                            from_binary_net=True):
    '''
    gets the output of a single-label classifier.
    :param url_or_np_array:
    :param net:
    :param required_image_size: the size of the image the net wants (has been trained on), (WxH)
    :param resize: resize img to this dimension. if this is > required_image_size then take center crop.  pls dont make this < required_image_size
    :param analog_output: get the actual class activations not just the max
    :param binary_net: get the 2nd neuron output if this is a binary net (forget abt the first one since the neurons would be 'not item/item'
    :return:
    '''
    #the below could be replaced by a call to
    if isinstance(url_or_np_array, basestring):
        image = url_to_image(url_or_np_array)
    elif type(url_or_np_array) == np.ndarray:
        image = url_or_np_array
    image = Utils.get_cv2_img_array(url_or_np_array)
    if image is None:
        print('ug didnt manage to get an image...' + str(url_or_np_array))
        return
    print('multilabel working on image of shape:' + str(image.shape))

    #  save image to make sure no rgb/bgr funny business
    #    hash = hashlib.sha1()
    #    hash.update(str(time.time()))
    #    print hash.hexdigest()
    #    name=hash.hexdigest()[:10]+'.jpg'
    #    print('saving '+name)
    #    cv2.imwrite(name,image)

    # load image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
    #    im = Image.open(imagename)
    #    im = im.resize(required_imagesize,Image.ANTIALIAS)
    #    in_ = in_.astype(float)
    if resize:
        image = imutils.resize_keep_aspect(image,
                                           output_size=resize,
                                           output_file=None)
        #print('original resized to '+str(image.shape))
    height, width, channels = image.shape
    crop_dx = width - required_image_size[0]
    crop_dy = height - required_image_size[1]
    if crop_dx != 0:
        remove_x_left = crop_dx / 2
        remove_x_right = crop_dx - remove_x_left
        image = image[:,
                      remove_x_left:width - remove_x_right, :]  #crop center x
        #print('removing {} from left and {} from right leaving {}'.format(remove_x_left,remove_x_right,image.shape))
    if crop_dy != 0:
        remove_y_top = crop_dy / 2
        remove_y_bottom = crop_dy - remove_y_top
        image = image[remove_y_top:width -
                      remove_y_bottom, :, :]  #crop center y
        #print('removing {} from top and {} from bottom leaving {}'.format(remove_x_left,remove_x_right,image.shape))

    image = np.array(image, dtype=np.float32)  #.astype(float)
    if len(image.shape) != 3:  #h x w x channels, will be 2 if only h x w
        print('got 1-chan image, turning into 3 channel')
        #DEBUG THIS , ORDER MAY BE WRONG [what order? what was i thinking???]
        image = np.array([image, image, image])
    elif image.shape[
            2] != 3:  #for rgb/bgr, some imgages have 4 chan for alpha i guess
        print('got n-chan image, skipping - shape:' + str(image.shape))
        return
#    image = image[:,:,::-1]  for doing RGB -> BGR : since this is loaded nby cv2 its unecessary
#    cv2.imshow('test',image)
    image -= np.array((104.0, 116.0, 122.0))
    image = image.transpose((2, 0, 1))
    # shape for input (data blob is N x C x H x W), set data
    net.blobs['data'].reshape(1, *image.shape)
    net.blobs['data'].data[...] = image
    # run net and take argmax for prediction
    net.forward()
    #    out = net.blobs['score'].data[0].argmax(axis=0) #for a parse with per-pixel max
    out = net.blobs['prob'].data[
        0]  #for the nth class layer #siggy is after sigmoid
    the_chosen_one = out.argmax()
    min = np.min(out)
    max = np.max(out)
    print('net output:  {}  answer:class {}'.format(out, the_chosen_one))
    if analog_output:
        if from_binary_net:
            second_neuron_output = out[1]
            print('binary output:' + str(out[1]))
            return second_neuron_output
        return out
    return the_chosen_one
def pd_test_iou_and_cats(
    images_file='/home/jeremy/image_dbs/pixlevel/pixlevel_fullsize_test_labels_v3.txt',
    n_channels=len(constants.pixlevel_categories_v3),
    output_labels=constants.pixlevel_categories_v3,
    pd_to_output_converter=constants.
    fashionista_augmented_zero_based_to_pixlevel_v3):
    '''
    :param images_file: file w lines of imgfile,labelfile. fash.augmented.zerobased list at 'pixlevel_fullsize_test_labels_faz.txt'
    :param n_channels: number of categories
    :param output_labels: what labels for output (used in legends)
    :param pd_to_output_converter: convert pd to outputlabels using this mapping
    :return: results from histogram - iou etc
    '''
    if not (os.path.exists(images_file)):
        logging.warning('file {} does not exist, exiting'.format(images_file))
        return
    with open(images_file, 'r') as fp:
        lines = fp.readlines()
        if len(lines) == 0:
            logging.warning(
                'got no lines from {}, exiting'.format(images_file))
            return
        print('{} has lines like:\n{}'.format(images_file, lines[0]))
        imgfiles = [l.split()[0] for l in lines]
        labelfiles = [l.split()[1] for l in lines]
    hist = np.zeros((n_channels, n_channels))

    for image_file, labelfile in zip(imgfiles, labelfiles):
        image_arr = Utils.get_cv2_img_array(image_file)
        gt_arr = cv2.imread(labelfile)
        print('gt size {} img size {} for {} and {}'.format(
            gt_arr.shape, image_arr.shape, labelfile, image_file))
        result = get_pd_results(img_arr=image_arr)
        if result is not None:
            mask, labels, pose = result[:]
        else:
            logging.warning(
                'got None result from get_pd_results in pd_test_iou_and_cats')
            continue


#        mask, labels, pose = paperdoll_parse_enqueue.paperdoll_enqueue(image_arr, async=False)
        conversion_utils.count_values(mask)
        converted_mask = convert_results(
            mask, labels, pd_to_nd_label_converter=pd_to_output_converter)
        conversion_utils.count_values(converted_mask)
        print('mask uniques {} converted uniques {} gt uniques {}'.format(
            np.unique(mask), np.unique(converted_mask), np.unique(gt_arr)))
        final_mask = pipeline.after_pd_conclusions(mask, labels)
        print('mask, after conclusions')
        conversion_utils.count_values(final_mask)
        converted_final_mask = convert_results(
            final_mask,
            labels,
            pd_to_nd_label_converter=pd_to_output_converter)
        conversion_utils.count_values(converted_final_mask)
        print('final mask uniques {} gt uniques {}'.format(
            np.unique(converted_final_mask), np.unique(gt_arr)))
        #before conclusions
        savename = os.path.basename(image_file).replace('.jpg', '_legend.jpg')
        imutils.show_mask_with_labels(converted_mask,
                                      labels=output_labels,
                                      original_image=image_file,
                                      visual_output=False,
                                      savename=savename,
                                      save_images=True)
        #after conclusions
        savename_finalmask = os.path.basename(image_file).replace(
            '.jpg', '_afterpdconclusions_legend.jpg')
        imutils.show_mask_with_labels(converted_final_mask,
                                      labels=output_labels,
                                      original_image=image_file,
                                      visual_output=False,
                                      savename=savename_finalmask,
                                      save_images=True)
        #ground truth
        gtsavename = os.path.basename(image_file).replace(
            '.jpg', '_gt_legend.jpg')
        imutils.show_mask_with_labels(gt_arr,
                                      labels=output_labels,
                                      original_image=image_file,
                                      visual_output=False,
                                      savename=gtsavename,
                                      save_images=True)
        #mask (after conclusions)
        bmpname = os.path.basename(image_file).replace('.jpg', 'pd.bmp')
        cv2.imwrite(bmpname, converted_final_mask)
        print('saving naive legend to ' + savename +
              ' afterconclusions legend to ' + savename_finalmask +
              ' gt legend to ' + gtsavename + ', mask to ' + bmpname)

        hist += jrinfer.fast_hist(gt_arr, converted_final_mask, n_channels)

    results = jrinfer.results_from_hist(hist, labels=labels)
    return results
Exemple #18
0
def fashionista_to_ultimate_21(img_arr_or_url_or_file):
    ##########warning not finished #################3
    #also this is for images saved using the fashionista_categories_augmented labels, raw d output is with a dictionary of arbitrary labels
    #so use  get_pd_results_on_db_for_webtool.convert_and_save_results
    #actually this is a better place for that so now its copied here

    ultimate_21 = [
        'bgnd', 'bag', 'belt', 'blazer', 'coat', 'dress', 'eyewear', 'face',
        'hair', 'hat', 'jeans', 'legging', 'pants', 'shoe', 'shorts', 'skin',
        'skirt', 'stocking', 'suit', 'sweater', 'top'
    ]
    #tossed,'bodysuit', 'socks','bra'
    #tossed​, 'accessories', 'ring', 'necklace', 'bracelet', 'wallet', 'tie', 'earrings', 'gloves', 'watch']
    #scarf aded to shirt since it mostly shows up there

    # ## fashionista classes:
    fashionista_categories_augmented = [
        '', 'null', 'tights', 'shorts', 'blazer', 't-shirt', 'bag', 'shoes',
        'coat', 'skirt', 'purse', 'boots', 'blouse', 'jacket', 'bra', 'dress',
        'pants', 'sweater', 'shirt', 'jeans', 'leggings', 'scarf', 'hat',
        'top', 'cardigan', 'accessories', 'vest', 'sunglasses', 'belt',
        'socks', 'glasses', 'intimate', 'stockings', 'necklace', 'cape',
        'jumper', 'sweatshirt', 'suit', 'bracelet', 'heels', 'wedges', 'ring',
        'flats', 'tie', 'romper', 'sandals', 'earrings', 'gloves', 'sneakers',
        'clogs', 'watch', 'pumps', 'wallet', 'bodysuit', 'loafers', 'hair',
        'skin', 'face'
    ]  #0='',1='null'(bgnd) 57='face'

    #CONVERSION FROM FASH 57 TO ULTIMATE21
    conversion_dictionary_strings = {
        'bgnd': ['null'],
        'bag': ['bag', 'purse'],
        'belt': ['belt'],
        'blazer': ['blazer', 'jacket', 'vest'],
        'top': ['t-shirt', 'shirt', 'blouse', 'top', 'sweatshirt', 'scarf'],
        'coat': ['coat', 'cape'],
        'dress': ['dress', 'romper'],
        'suit': ['suit'],
        'face': ['face'],
        'hair': ['hair'],
        'hat': ['hat'],
        'jeans': ['jeans'],
        'legging': ['tights', 'leggings'],
        'pants': ['pants'],
        'shoe': [
            'shoes', 'boots', 'heels', 'wedges', 'pumps', 'loafers', 'flats',
            'sandals', 'sneakers', 'clogs'
        ],
        'shorts': ['shorts'],
        'skin': ['skin'],
        'skirt': ['skirt'],
        'stocking': ['intimate', 'stockings'],
        'eyewear': ['sunglasses', 'glasses'],
        'sweater': ['sweater', 'cardigan', 'jumper']
    }

    index_conversion = [
        -666 for i in range(len(fashionista_categories_augmented))
    ]
    for k, v in conversion_dictionary_strings.iteritems():
        ultimate_21_index = ultimate_21.index(k)
        for fash_cat in v:  #no need to check if the fashcat is in v since all the values here are in fashionista....
            fash_index = fashionista_categories_augmented.index(fash_cat)
            #            fash_index = constants.fashionista_categories_augmented_zero_based.index(fash_cat)
            logging.debug(
                'ultimate index {} cat {} fash index {} cat {}'.format(
                    ultimate_21_index, ultimate_21[ultimate_21_index],
                    fash_index, fashionista_categories_augmented[fash_index]))
            index_conversion[fash_index] = ultimate_21_index

    print(index_conversion)
    #    for i in range(len(index_conversion)):
    #        if index_conversion[i] == -666:
    #            print('unmapped fashcat:'+str(i)+fashionista_categories_augmented[i])

    if isinstance(img_arr_or_url_or_file, basestring):
        mask = Utils.get_cv2_img_array(img_arr_or_url_or_file)
    #todo - check why get_cv2_img_array doesnt like getting a  mask
    else:
        mask = img_arr_or_url_or_file
    #   mask=cv2.imread(file,cv2.IMREAD_GRAYSCALE)
    if mask is None:
        if isinstance(img_arr_or_url_or_file, basestring):
            logging.warning('could not get filename/url:' +
                            str(img_arr_or_url_or_file))
        else:
            logging.warning('could not get file/url')
        return None
    if len(mask.shape) == 3:
        logging.warning('multichannel mask, taking chan 0')
        mask = mask[:, :, 0]
    uniques = np.unique(mask)
    #    print('uniques:'+str(uniques))
    for u in uniques:
        newval = index_conversion[u]  #find indice(s) of vals matching unique
        if newval < 0:  #if you want to 'throw away' a cat just map it to background, otherwise
            newval = 0
        print('replacing index {} with newindex {}'.format(u, newval))
        mask[mask == u] = newval
    return mask
Exemple #19
0
def detect_with_scale_pyramid_and_sliding_window(image_filename_or_cv2_array,
                                                 prototxt,
                                                 caffemodel,
                                                 mean_B=128,
                                                 mean_G=128,
                                                 mean_R=128,
                                                 image_width=150,
                                                 image_height=200,
                                                 show_visual_output=False):
    caffe.set_mode_gpu()
    if host == 'jr-ThinkPad-X1-Carbon':
        caffe.set_mode_cpu()
    net = caffe.Net(prototxt, caffemodel, caffe.TEST)
    img_arr = Utils.get_cv2_img_array(image_filename_or_cv2_array)
    orig_img_arr = img_arr.copy()

    if (0):
        if mean_B is not None and mean_G is not None and mean_R is not None:
            img_arr[:, :, 0] = img_arr[:, :, 0] - mean_B
            img_arr[:, :, 1] = img_arr[:, :, 1] - mean_G
            img_arr[:, :, 2] = img_arr[:, :, 2] - mean_R
        img_arr = np.divide(img_arr, 255.0)
        transformed_image = img_arr.transpose((2, 0, 1))

        net.blobs['data'].data[...] = transformed_image
        ### perform classification
        output = net.forward()

        logging.debug('orig shape ' + str(img_arr.shape))
        h, w = img_arr.shape[0:2]


#    if h != image_height or w != image_width:
#        img_arr = cv2.resize(img_arr,(image_width,image_height))

# copy the image data into the memory allocated for the net

    i = 0
    # loop over the image pyramid
    for resized in pyramid(img_arr, scale=1.5):
        # loop over the sliding window for each layer of the pyramid
        for (x, y, window) in sliding_window(resized,
                                             stepSize=32,
                                             windowSize=(image_width,
                                                         image_height)):
            # if the window does not meet our desired window size, ignore it
            if window.shape[0] != image_height or window.shape[
                    1] != image_width:
                logging.debug('got bad window shape from sliding_window')
                continue

            # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
            # MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
            # WINDOW
            img_arr2 = window.copy()
            if mean_B is not None and mean_G is not None and mean_R is not None:
                img_arr2[:, :, 0] = img_arr2[:, :, 0] - mean_B
                img_arr2[:, :, 1] = img_arr2[:, :, 1] - mean_G
                img_arr2[:, :, 2] = img_arr2[:, :, 2] - mean_R
            img_arr2 = np.divide(img_arr2, 255.0)
            transformed_image = img_arr2.transpose((2, 0, 1))

            net.blobs['data'].data[...] = transformed_image
            ### perform classification
            output = net.forward()

            n = net.blobs
            print('net ' + str(n))
            output = n['output_layer'].data
            print('output ' + str(output))

            # since we do not have a classifier, we'll just draw the window
            clone = resized.copy()
            cv2.rectangle(clone, (x, y), (x + image_width, y + image_height),
                          (0, 255, 0), 2)
            if show_visual_output:
                cv2.imshow("sliding window", clone)
                cv2.imshow("window", window)
                cv2.waitKey(1)
                time.sleep(0.025)
            fname = 'output' + str(i) + '.jpg'
            #            cv2.imwrite(fname,clone)
            i = i + 1