def get_pd_masks_for_dir(indir,outdir): filenames = [f for f in os.listdir(indir) if '.jpg' in f] print('found {} files in {}'.format(len(filenames),indir)) for f in filenames: print('sending img '+f) full_imgname = os.path.join(indir,f) im = cv2.imread(full_imgname) start_time = time.time() retval = paperdoll_parse_enqueue.paperdoll_enqueue(im) print('waiting',end='') while not retval.is_finished: time.sleep(1) print('.', end="") sys.stdout.flush() mask, labels = retval.result[:2] end_time = time.time() dt=end_time-start_time dts.append(dt) parse_name = f.split('.jpg')[0]+'_pdparse.bmp' full_parse_name = os.path.join(outdir,parse_name) cv2.imwrite(full_parse_name,mask) print('labels:'+str(labels)) sorted_labels=sorted(labels.items(),key=operator.itemgetter(1)) print('sorted labels :'+str(sorted_labels)) labs_only = [i[0] for i in sorted_labels] print('labsonly '+str(labs_only)) imutils.show_mask_with_labels(full_parse_name,labs_only,save_images=True) aftermask = pipeline.after_pd_conclusions(mask, labels, face=None) after_pd_conclusions_name = full_parse_name.split('_pdparse.bmp')[0]+'_pdconclusions.bmp' cv2.imwrite(after_pd_conclusions_name,aftermask) imutils.show_mask_with_labels(after_pd_conclusions_name,labs_only,save_images=True) if retval is not None: print('retval:' + str(retval.result)+' time:'+str(dt)) else: print('no return val (None)')
def convert_and_save_results(mask, label_names, pose, filename, img, url, forwebtool=True): ''' This saves the mask using the labelling fashionista_categories_augmented_zero_based :param mask: :param label_names: :param pose: :param filename: :param img: :param url: :return: ''' fashionista_ordered_categories = constants.fashionista_categories_augmented_zero_based #constants.fashionista_categories h, w = mask.shape[0:2] new_mask = np.ones( (h, w, 3), dtype=np.uint8 ) * 255 # anything left with 255 wasn't dealt with in the following conversion code print('new mask size:' + str(new_mask.shape)) success = True #assume innocence until proven guilty print('attempting convert and save, shapes:' + str(mask.shape) + ' new:' + str(new_mask.shape)) for label in label_names: # need these in order if label in fashionista_ordered_categories: fashionista_index = fashionista_ordered_categories.index( label) + 0 # number by 0=null, 55=skin , not 1=null,56=skin pd_index = label_names[label] pixlevel_v2_index = constants.fashionista_aug_zerobased_to_pixlevel_categories_v2[ fashionista_index] pixlevel_index = constants.fashionista_aug_zerobased_to_pixlevel_categories_v4_for_web[ fashionista_index] if pixlevel_index is None: pixlevel_index = 0 #map unused categories (used in fashionista but not pixlevel v2) to background # new_mask[mask==pd_index] = fashionista_index # print('old index '+str(pd_index)+' for '+str(label)+': gets new index:'+str(fashionista_index)+':' + fashionista_ordered_categories[fashionista_index]+ ' and newer index '+str(pixlevel_v2_index)+':'+constants.pixlevel_categories_v2[pixlevel_v2_index]) new_mask[mask == pd_index] = pixlevel_index else: print('label ' + str(label) + ' not found in regular cats') success = False if 255 in new_mask: print('didnt fully convert mask') success = False if success: try: #write orig file # conversion_utils.count_values(new_mask,labels=constants.pixlevel_categories_v2) conversion_utils.count_values( new_mask, labels=constants.pixlevel_categories_v4_for_web) dir = constants.pd_output_savedir Utils.ensure_dir(dir) full_name = os.path.join(dir, filename) # full_name = filename print('writing output img to ' + str(full_name)) cv2.imwrite(full_name, img) except: print('fail in try 1, ' + sys.exc_info()[0]) try: #write rgb mask # bmp_name = full_name.replace('.jpg','_pixv2.png') bmp_name = full_name.replace('.jpg', '_pixv4.png') print('writing mask bmp to ' + str(bmp_name)) cv2.imwrite(bmp_name, new_mask) imutils.show_mask_with_labels( new_mask, labels=constants.pixlevel_categories_v4, original_image=full_name, save_images=True) if socket.gethostname() != 'extremeli-evolution-1': command_string = 'scp ' + bmp_name + ' [email protected]:/var/www/js-segment-annotator/data/pd_output/pd/' subprocess.call(command_string, shell=True) command_string = 'scp ' + full_name + ' [email protected]:/var/www/js-segment-annotator/data/pd_output/pd/' subprocess.call(command_string, shell=True) except: print('fail in try 2, ' + str(sys.exc_info()[0])) try: #write webtool mask if forwebtool: webtool_mask = copy.copy(new_mask) webtool_mask[:, :, 0] = 0 #zero out the B,G for webtool - leave only R webtool_mask[:, :, 1] = 0 #zero out the B,G for webtool - leave only R # bmp_name=full_name.replace('.jpg','_pixv2_webtool.png') bmp_name = full_name.replace('.jpg', '_pixv4_webtool.png') print('writing mask bmp to ' + str(bmp_name)) cv2.imwrite(bmp_name, webtool_mask) command_string = 'scp ' + bmp_name + ' [email protected]:/var/www/js-segment-annotator/data/pd_output/pd' subprocess.call(command_string, shell=True) except: print('fail in try 3, ' + str(sys.exc_info()[0])) try: pose_name = full_name.strip('.jpg') + '.pose' with open(pose_name, "w+") as outfile: print('succesful open, attempting to write pose') poselist = pose[0].tolist() # json.dump([1,2,3], outfile, indent=4) json.dump(poselist, outfile, indent=4) if url is not None: url_name = full_name.strip('.jpg') + '.url' print('writing url to ' + str(url_name)) with open(url_name, "w+") as outfile2: print('succesful open, attempting to write:' + str(url)) outfile2.write(url) except: print('fail in convert_and_save_results dude, bummer') print(str(sys.exc_info()[0])) return else: print( 'didnt fully convert mask, or unkown label in convert_and_save_results' ) success = False return
t3,im3 = cv2.threshold(result,180,255,cv2.THRESH_BINARY) t4 = cv2.adaptiveThreshold(result,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2) t5,im5 = cv2.threshold(result,128,255,cv2.THRESH_BINARY|cv2.THRESH_OTSU) outmat[0:256,256*index_to_show:256*(index_to_show+1)] = result outmat[256:256*2,256*index_to_show:256*(index_to_show+1)] = im1 outmat[256*2:256*3,256*index_to_show:256*(index_to_show+1)] = t4 outmat[256*3:256*4,256*index_to_show:256*(index_to_show+1)] = im5 cv2.imwrite('output.png',outmat) # cv2.imshow('output layer'+str(index_to_show),result) # cv2.waitKey(0) cv2.imshow('output layers',outmat) cv2.waitKey(0) # labels=constants.ultimate_21 # imutils.show_mask_with_labels('output.png',labels,visual_output=True) else: url = 'http://diamondfilms.com.au/wp-content/uploads/2014/08/Fashion-Photography-Sydney-1.jpg' url = 'http://pinmakeuptips.com/wp-content/uploads/2015/02/1.4.jpg' result = infer_one(url,required_image_size=required_image_size) cv2.imwrite('output.png',result) labels=constants.ultimate_21 imutils.show_mask_with_labels('output.png',labels,visual_output=True) # after_nn_result = pipeline.after_nn_conclusions(result,constants.ultimate_21_dict) # cv2.imwrite('output_afternn.png',after_nn_result) # labels=constants.ultimate_21