def webfile_prep(fwebname): robo.whereami(sys._getframe().f_code.co_name) try: webfile_prepped = open(fwebname, "r") return webfile_prepped except: print cfg.color.magenta print "Hmm, Unable to load WEBSTAGRAM response: "+fwebname print "(usually, the tag has no images. so check the txt file, and also give it a look online.)" print cfg.color.white robo.goodbye() sys.exit(1) #shouldnt get here, but for safety
def webfile_prep(fwebname): robo.whereami(sys._getframe().f_code.co_name) with open(fwebname, "r") as webfile_local: try: webfile_prepped = json.load(webfile_local) return webfile_prepped except: print cfg.color.magenta print "Hmm, Unable to load JSON from IMGUR API response." print "(usually, the tag has no images. so check the txt file above, and give it a look online.)" print cfg.color.white robo.goodbye() sys.exit(1) #shouldnt get here, but for safety
def getwebfile(webfileurl): robo.whereami(sys._getframe().f_code.co_name) print "get data from:", webfileurl try: webfile = urlopen(webfileurl) return webfile except: print cfg.color.magenta print "doh, didnt get file from "+webfileurl+"!" print "usually this is random. good next steps:" print "1. maybe check the url in a browser or\n2. wait like 15 seconds and try again." print cfg.color.white robo.goodbye() sys.exit(1) #shouldnt get here, but for safety
def classify_image(testimg, model_data): robo.whereami(sys._getframe().f_code.co_name) basetag = model_data["basetag"] model_type = model_data["model_type"] model_dir = model_data["model_dir"] path_to_retrainedgraph = cfg.path_to_trainingsumms + cfg.dd + basetag + cfg.dd + model_dir + cfg.dd + cfg.retrainedgraph_file path_to_labels = cfg.path_to_trainingsumms + cfg.dd + basetag + cfg.dd + model_dir + cfg.dd + cfg.retrainedlabels_file # build a command if model_type == cfg.mobile_model: testimgcommand = "python ../scripts/label_image.py \ --graph='" + path_to_retrainedgraph + "' \ --labels='" + path_to_labels + "' \ --input_height=224 \ --input_width=224 \ --input_mean=128 \ --input_std=128 \ --image=" + testimg elif model_type == cfg.inception_model: testimgcommand = "python ../scripts/label_image.py \ --graph='" + path_to_retrainedgraph + "' \ --labels='" + path_to_labels + "' \ --input_height=299 \ --input_width=299 \ --input_mean=128 \ --input_std=128 \ --input_layer='Mul' \ --image=" + testimg else: robo.goodbye("woops no classification model! Program stopping...") print "image: " + testimg # use the tensorflow label_image script try: imagelabel_raw = subprocess.check_output(testimgcommand, shell=True) except Exception: # just remove file for now (rather than store for later analysis) # because they are broken images, not misclassified os.remove(testimg) imagelabel_raw = False return imagelabel_raw
def getwebfile(webfileurl): robo.whereami(sys._getframe().f_code.co_name) print "API for:", webfileurl imgur_client_id = 'Client-ID ' + os.environ.get('IMGURAPI_ID') req = Request(webfileurl) req.add_header('Authorization', imgur_client_id) try: webfile = urlopen(req) return webfile except: print cfg.color.magenta print "doh, probably 'urllib2.HTTPError: HTTP Error 500: Internal Server Error'" print "(something wrong with imgur API. happens all the time. try again in a min.)" print cfg.color.white robo.goodbye() sys.exit(1) #shouldnt get here, but for safety
def imgurapi_clientid_confirm(): robo.whereami(sys._getframe().f_code.co_name) try: imgur_client_id = 'Client-ID ' + os.environ.get('IMGURAPI_ID') return imgur_client_id except: print cfg.color.magenta + ''' Whelp! no Imgur API Client-ID found in environment variables. (and thus, no ability to download images from imgur.com...) ''' print cfg.color.white + ''' SOLUTIONS: 1. Change 'scrapesite_default' in config file, or 2. Set yourself up an API key at: https://apidocs.imgur.com/ then take 10 seconds to add it to your environment (on mac) at: http://osxdaily.com/2015/07/28/set-enviornment-variables-mac-os-x/''' robo.goodbye() sys.exit(1) # for safety
def main(model_data): robo.whereami(sys._getframe().f_code.co_name) basetag = model_data["basetag"] model_type = model_data["model_type"] path_to_sortedimgs_basetag = cfg.path_to_testimgs + cfg.dd + basetag + cfg.dd + cfg.sorted_dirname if makesortedlabels_dirs(model_data) == False: robo.goodbye( "Dirs for final output NOT created or available. Check your permissions. Stopping program..." ) #get images images_list = robo.getimagelist_fromdir(cfg.path_to_testimgs + cfg.dd + basetag) #call the labeling function testimages_dict = {} for testimage in images_list: timestart = time.strftime("%H%M%S") ##### call tensorflow label_image script imagelabel_raw = classify_image(testimage, model_data) ####################################### # process the label if imagelabel_raw: imagelabel_processed = process_imagelabel_for_final(imagelabel_raw) testimage_clean = testimage.replace( cfg.path_to_testimgs + cfg.dd + basetag + cfg.dd, "") #process the classifiedimages proc_classed_images_list = {} proc_result = processclassifiedimages(testimage_clean, imagelabel_processed, basetag) # save to dict for later analysis timeend = time.strftime("%H%M%S") timespent = float(timeend) - float(timestart) testimages_dict[proc_result[0]] = (proc_result[1], proc_result[2], timespent) #make a CLASSIFICATION log file filetitle = cfg.imagelog_prefix + model_type + "_" + logtime + cfg.imagelog_suffix path_to_thisfile = path_to_sortedimgs_basetag + cfg.dd + filetitle robo.createfilefromdict(path_to_thisfile, testimages_dict) #make a MODEL INFO log file thatfile = cfg.imagelog_prefix + cfg.modeldatalog_name + logtime + cfg.imagelog_suffix path_to_thatfile = path_to_sortedimgs_basetag + cfg.dd + thatfile robo.createfilefromdict(path_to_thatfile, model_data) #AFTER -- send a message if cfg.twilio_active == True: num_of_testimages = len(testimages_dict) sms_msg = "roboclassified and moved " + str( num_of_testimages ) + " imgs! now, do a manual QA sortcheck (and/or re-sort) for best longterm success. boop boop." sms_status = robo.sendsms(sms_msg) print "exiting robo_classify..." return sys.exit(1) #shouldnt get here, but just in case...
#AFTER -- send a message if cfg.twilio_active == True: num_of_testimages = len(testimages_dict) sms_msg = "roboclassified and moved " + str( num_of_testimages ) + " imgs! now, do a manual QA sortcheck (and/or re-sort) for best longterm success. boop boop." sms_status = robo.sendsms(sms_msg) print "exiting robo_classify..." return sys.exit(1) #shouldnt get here, but just in case... ################################# ################################# # boilerplate kicker offer (yes thats a tech term!) if __name__ == '__main__': try: model_data except: print print "This script not callable directly, as it needs data from " + cfg.download_script + " passed to it." print "maybe it's time read the help? copy/paste this:" print "\tpython roboflow.py --help" robo.goodbye() #go get 'er done main(model_data)