def index(): f = request.files['image'] result = dict( image=f.filename, classification=classify_image(f), ) return jsonify(result)
def download_image(url, image_url): c.execute("SELECT * FROM urls WHERE url=?", (url, )) databse_url = c.fetchone() if databse_url is None: # Download image print("Downloading url: {}".format(url)) my_folder = args.save_dir if not os.path.exists(my_folder): os.makedirs(my_folder) parsed_url = urlparse(image_url) image_name = os.path.basename(parsed_url.path) img_data = requests.get(image_url).content with open(my_folder + image_name, 'wb') as handler: handler.write(img_data) time.sleep(0.2) # Insert image into sqlite3, so we do not download it again c.execute("INSERT INTO urls ('url', 'image_name') VALUES ('{}', '{}')". format(url, image_name)) # Classify image classified_image = classify_image(my_folder + image_name) if classified_image: c.execute( "INSERT INTO classified ('url_id', 'gender', 'gender_conf', 'age', 'age_conf') VALUES ({}, {}, '{}', {}, '{}')" .format(c.lastrowid, classified_image['gender'], classified_image['genderprediction'], classified_image['age'], classified_image['ageprediction'])) conn.commit() return True else: return False
def process_block_queue(lock, block_queue, dst_queue, full_image_name, assess_quality, stretch_params, white_balance, tds, im_metadata): ''' Function run by each process. Will process blocks placed in the block_queue until the 'STOP' command is reached. ''' # Parse input arguments lower, upper, wb_reference, bp_reference = stretch_params wb_reference = np.array(wb_reference, dtype=np.float) bp_reference = np.array(bp_reference, dtype=np.float) image_type = im_metadata[0] for block_indices in iter(block_queue.get, 'STOP'): x, y, read_size_x, read_size_y = block_indices # Load block data with gdal (offset and block size) lock.acquire() src_ds = gdal.Open(full_image_name, gdal.GA_ReadOnly) image_data = src_ds.ReadAsArray(x, y, read_size_x, read_size_y) src_ds = None lock.release() # Restructure raster for panchromatic images: if image_data.ndim == 2: image_data = np.reshape(image_data, (1, read_size_y, read_size_x)) # Calculate the quality score on an arbitrary band if assess_quality: quality_score = pp.calc_q_score(image_data[0]) else: quality_score = 1. # Apply correction to block based on earlier histogram analysis (if applying correction) # Converts image to 8 bit by rescaling lower -> 1 and upper -> 255 image_data = pp.rescale_band(image_data, lower, upper) if white_balance: # Applies a white balance correction image_data = pp.white_balance(image_data, wb_reference, np.amax(wb_reference)) # Segment image segmented_blocks = segment_image(image_data, image_type=image_type) # Classify image classified_block = classify_image(image_data, segmented_blocks, tds, im_metadata, wb_reference, bp_reference) # Add the pixel counts from this classified split to the # running total. pixel_counts_block = utils.count_features(classified_block) # Pass the data back to the main thread for writing dst_queue.put( (quality_score, pixel_counts_block, x, y, classified_block)) dst_queue.put(None)
def upload(): # Get the name of the uploaded user_file user_file = request.files['file'] # Check if the user_file is one of the allowed types/extensions if user_file and allowed_file(user_file.filename): # Make the filename safe, remove unsupported chars filename = user_file.filename # Move the user_file form the temporal folder to # the upload folder we setup file_path = os.path.join(webapp.config['UPLOAD_FOLDER'], filename) user_file.save(file_path) prediction_list = classify_image(file_path) print prediction_list return render_template('classify.html', prediction=prediction_list, filename=filename) else: return render_template('index.html')
def predict(): labels = [] if 'file' not in request.files: flash('No file part') return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected file') return redirect(request.url) if file and allowed_file(file.filename): filename = secure_filename(file.filename) filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(filepath) labels = classify.classify_image(filepath) return str(labels)
def main(args): if not args.text: print("No text to display.") return img_gen = camera.rpi.generate_capture() image = next(img_gen) img_gen.close() import classify classification = classify.classify_image(image) char_width = int(args.width / 6) lines = textwrap.wrap(classification, char_width) virtual = viewport(args.device, width=args.device.width, height=args.height) with canvas(virtual) as draw: for i, line in enumerate(lines): draw.text((0, i * 12), text=line, fill="white") input("Please Enter to close")
classify_func=get_classify_func() print 'generating probabilty model...' pro_model = generate_probability_model(relation_pairs_path,class_int_dict) print 'combining these two models' test_path='test.jpg' test_name=test_path.split('.')[0] images=get_object_images(test_path,ishape) last_object=None for i in xrange(len(images)): img = numpy.asarray(images[i], dtype='float64') / 256. # print img.shape if(img.shape != (ishape[0],ishape[1],3)): continue img = img.swapaxes(0,2).swapaxes(1,2).reshape(1,3,ishape[0],ishape[1]) y_pred,p_y_given_x=classify_image(image=img,classify_func=classify_func) y_pred=y_pred[0] p_y_given_x=p_y_given_x[0] if(p_y_given_x[y_pred]<threshold_p_y_given_x): continue print 'deep learning model implies this is:\n',image_classes[y_pred] if(last_object==None): images[i].save(test_name+'_output_object_'+str(i)+'_'+image_classes[y_pred]+'.jpg') last_object=y_pred else: final_p_y_given_x=p_y_given_x + model_factor * pro_model[last_object] p_dict={final_p_y_given_x[i]:i for i in xrange(len(final_p_y_given_x))} max_value=max(final_p_y_given_x) if(max_value<threshold_p_y_given_x): continue final_y_pred=p_dict[max_value]
# OSSP Process
elif args.function == 3: # 使用大类训练好的模型对每个大类中的小类进行分类器的训练 # 比赛数据训练集路径(按小类划分) evaluation_train_path = args.train_path # 比赛数据测试集路径(按小类划分) evaluation_test_path = args.test_path # 获取训练集和测试集文件列表 train_kind_lists = get_file_list(evaluation_train_path) test_kind_lists = get_file_list(evaluation_test_path) # 训练模型 train_subclass(train_kind_lists, test_kind_lists, IMAGE_SIZE, EPOCHS) if args.classify == 1: # 对图片进行批量分类 print('[INFO] start...') time_start = time.time() # 顶层类型模型的地址 top_model_path = 'models/evaluation_top_model.h5' # 读取用于顶层分类的模型 top_model = load_model(top_model_path) # 需分类的图片路径 classify_image_path = args.test_path image_lists = load_test_image(classify_image_path, IMAGE_SIZE) # 开始送入模型分类 classify_image(top_model, image_lists, KIND_LISTS) time_end = time.time() print('运行用时:{}'.format(time_end - time_start)) elif args.classify == 2: # 对单张图片进行分类 classify_one_image_path = args.test_path classify_one_image(classify_one_image_path, IMAGE_SIZE, KIND_LISTS)
def classify_and_fingerprint_dresses(path_to_image_file): #pdb.set_trace() print( "in fingerPrint2:classify_and_fingerprint_dresses:path_to_image_file: " + str(path_to_image_file)) REMOTE_FILE = False item_found = False fingerprint = "" classification_dict = {} #read arg from command line if none given to function if path_to_image_file is not None: PATH_TO_FILE = path_to_image_file #else: #wtf is supposed to happen here - who is calling this from command line?? #we want to be able to read URL as well as local file path if "://" in path_to_image_file: FILENAME = path_to_image_file.split('/')[-1].split('#')[0].split( '?')[0] res = urllib.urlretrieve(PATH_TO_FILE, FILENAME) PATH_TO_FILE = FILENAME REMOTE_FILE = True #pdb.set_trace() #main prog starts here img = cv2.imread(PATH_TO_FILE) roi = [] classification_dict = classify.classify_image(img) BB_coordinates = classification_dict[ "/home/www-data/web2py/applications/fingerPrint/modules/dressClassifier001.xml"] if len(BB_coordinates) > 0: if BB_coordinates[0] is not None: item_found = True roi = crop_image_to_BB(img, BB_coordinates[0]) else: roi = img BB_coordinates = [[0, 0, 0, 0]] print( "in fingerPrint2.py: len(BB_coordinates)>0 but BB[0] is None -- REALLY WEIRD:" ) print(BB_coordinates) else: # roi = None #if no BB was formed , don't return an answer!!!! print( "in fingerPrint2.py:bad roi (could not crop?)-using entire img - len(BB)!>0" ) roi = img BB_coordinates = [[0, 0, 0, 0]] if roi is not None: fingerprint = fp(roi) else: print( "in fingerPrint2.py:bad roi (could not crop?) - using entire img (again)" ) fingerprint_length = 56 fingerprint = [0 for x in range(fingerprint_length)] fingerprint[0] = -1 print("in fingerPrint2.py:fp=" + str(fingerprint)) if REMOTE_FILE: os.remove(PATH_TO_FILE) # right now, we're only doing shirts, so it's binary # 0 - nothing found, 1 - at least one shirt found. # even within this simplified case, still need to figure out # how to deal with multiple shirts in an image classification_list = [] if item_found: classification_list.append(2) else: classification_list.append(0) print('in fingerPrint2.py:classify_and_fingerprint_dresses:fingerprint=' + str(fingerprint)) print('in fingerPrint2.py:classify_and_fingerprint_dresses:BB=' + str(BB_coordinates)) return classification_list, fingerprint, BB_coordinates
def main(): # Set Up Arguments parser = argparse.ArgumentParser() parser.add_argument("input_dir", help='''directory path containing date directories of images to be processed''') parser.add_argument("image_type", type=str, choices=["srgb", "wv02_ms", "pan"], help="image type: 'srgb', 'wv02_ms', 'pan'") parser.add_argument("training_dataset", help="training data file") parser.add_argument("--training_label", type=str, default=None, help="name of training classification list") parser.add_argument("-o", "--output_dir", type=str, default=None, help="directory to place output results.") parser.add_argument( "-s", "--splits", metavar='int', type=int, default=1, help="number of subdividing splits to preform on raw image") parser.add_argument("-p", "--parallel", metavar='int', type=int, default=1, help='''number of processing threads to create.''') parser.add_argument("-v", "--verbose", action="store_true", help="display text information and progress") parser.add_argument("-e", "--extended_output", action="store_true", help='''Save additional data: 1) classified image (png) 2) classified results (csv) ''') parser.add_argument( "-c", "--nostretch", action="store_false", help="Do not apply a histogram stretch image correction to input.") # Parse Arguments args = parser.parse_args() # System filepath that contains the directories or files for batch processing user_input = args.input_dir if os.path.isdir(user_input): src_dir = user_input src_file = '' elif os.path.isfile(user_input): src_dir, src_file = os.path.split(user_input) else: raise IOError('Invalid input') # Image type, choices are 'srgb', 'pan', or 'wv02_ms' image_type = args.image_type # File with the training data tds_file = args.training_dataset # Default tds label is the image type if args.training_label is None: tds_label = image_type else: tds_label = args.training_label # Default output directory (if not provided) if args.output_dir is None: dst_dir = os.path.join(src_dir, 'classified') else: dst_dir = args.output_dir if not os.path.isdir(dst_dir): os.makedirs(dst_dir) num_splits = args.splits num_threads = args.parallel verbose = args.verbose extended_output = args.extended_output stretch = args.nostretch # For Ames OIB Processing: assess_quality = True # Set a default quality score until this value is calculated quality_score = 1. # Directory where temporary files are saved if num_splits > 1: working_dir = os.path.join(src_dir, 'splits') else: working_dir = None # Prepare a list of images to be processed based on the user input # list of task objects based on the files in the input directory. # Each task is an image to process, and has a subtask for each split # of that image. task_list = utils.create_task_list(os.path.join(src_dir, src_file), dst_dir, num_splits) # Load Training Data tds = utils.load_tds(tds_file, tds_label) for task in task_list: # ASP: Restrict processing to the frame range # try: # frameNum = getFrameNumberFromFilename(file) # except Exception, e: # continue # if (frameNum < args.min_frame) or (frameNum > args.max_frame): # continue # Skip this task if it is already marked as complete if task.is_complete(): continue # If the image has not yet been split or if no splitting was requested, # proceed to the preprocessing step. image_name = task.get_id() if not task.is_split() or num_splits == 1: image_data, im_info = prepare_image(src_dir, image_name, image_type, output_path=working_dir, number_of_splits=num_splits, apply_correction=stretch, verbose=verbose) if assess_quality: if verbose: print("Calculating image quality score...") # Calculate the quality score for this image: quality_score = utils.calc_q_score(image_data[1]) block_dims = im_info[0] image_date = im_info[1] pixel_counts = [0, 0, 0, 0, 0] classified_image = [] # Loop until all subtasks are complete. # Breaks when task.get_next_subtask() returns None (all subtasks complete) # or if the task is complete. while True: if task.is_complete(): break elif task.has_subtask(): subtask = task.get_next_subtask() if subtask is None: break # If there is a subtask, the image data is stored in a split on the # drive. Subtask == {} when there are no subtasks. image_data = os.path.join(working_dir, subtask) + '.h5' with h5py.File(image_data, 'r') as f: block_dims = f.attrs.get("Block Dimensions") image_date = f.attrs.get("Image Date") else: subtask = task.get_id() # Segment image seg_time = time.clock() if verbose: print("Segmenting image: %s" % subtask) image_data, segmented_blocks = segment_image(image_data, image_type=image_type, threads=num_threads, verbose=verbose) if verbose: print("Segment finished: %s: %f" % (subtask, time.clock() - seg_time)) # Classify image class_time = time.clock() if verbose: print("Classifying image: %s" % subtask) classified_blocks = classify_image(image_data, segmented_blocks, tds, [image_type, image_date], threads=num_threads, verbose=verbose) if verbose: print("Classification finished: %s: %f" % (subtask, time.clock() - class_time)) # Hold onto the output of this subtask clsf_split = utils.compile_subimages(classified_blocks, block_dims[0], block_dims[1]) # Save the results to the temp folder if there is more than 1 split if num_splits > 1: with h5py.File( os.path.join(working_dir, subtask + '_classified.h5'), 'w') as f: f.create_dataset('classified', data=clsf_split, compression='gzip', compression_opts=3) # Add the pixel counts from this classified split to the # running total. pixel_counts_split = utils.count_features(clsf_split) for i in range(len(pixel_counts)): pixel_counts[i] += pixel_counts_split[i] # Mark this subtask as complete. This sets task.complete to True # if there are no subtasks. task.update_subtask(subtask) # Writing the results to a sqlite database. (Only works for # a specific database structure that has already been created) # db_name = 'ImageDatabase.db' # db_dir = '/media/sequoia/DigitalGlobe/' # image_name = task.get_id() # image_name = os.path.splitext(image_name)[0] # image_id = image_name.split('_')[2] # part = image_name.split('_')[5] # utils.write_to_database(db_name, db_dir, image_id, part, pixel_counts) # Create a sorted list of the tasks. Then create the correct filename # for each split saved on the drive. # Compile the split images back into a single image if num_splits > 1: if verbose: print("Recompiling: %s" % task.get_id()) clsf_splits = [] task_list = task.get_tasklist() task_list.sort() for task_id in task_list: cname = os.path.join(working_dir, task_id + "_classified.h5") clsf_splits.append(cname) classified_image = utils.stitch(clsf_splits) else: classified_image = clsf_split # Open input file to read metadata/projection src_ds = gdal.Open(os.path.join(src_dir, image_name)) input_xsize = src_ds.RasterXSize input_ysize = src_ds.RasterYSize # Trim output image to correct size classified_image = classified_image[:input_ysize, :input_xsize] # Save the classified image output as a geotiff fileformat = "GTiff" image_name = os.path.splitext(image_name)[0] dst_filename = os.path.join(dst_dir, image_name + '_classified.tif') driver = gdal.GetDriverByName(fileformat) dst_ds = driver.Create(dst_filename, xsize=input_xsize, ysize=input_ysize, bands=1, eType=gdal.GDT_Byte, options=["TILED=YES", "COMPRESS=LZW"]) # Transfer the metadata from input image # dst_ds.SetMetadata(src_ds.GetMetadata()) # Transfer the input projection dst_ds.SetGeoTransform( src_ds.GetGeoTransform()) ##sets same geotransform as input dst_ds.SetProjection( src_ds.GetProjection()) ##sets same projection as input # Write information to output dst_ds.GetRasterBand(1).WriteArray(classified_image) # Close dataset and write to disk dst_ds = None src_ds = None # Write extra data (total pixel counts and quality score to the database (or csv) output_csv = os.path.join(dst_dir, image_name + '_md.csv') with open(output_csv, "wb") as csvfile: writer = csv.writer(csvfile) writer.writerow([ "Quality Score", "White Ice", "Gray Ice", "Melt Ponds", "Open Water" ]) writer.writerow([ quality_score, pixel_counts[0], pixel_counts[1], pixel_counts[2], pixel_counts[3] ]) # Save color image for viewing if extended_output: utils.save_color(classified_image, os.path.join(dst_dir, image_name + '.png')) # Remove temp folders if working_dir is not None: if os.path.isdir(working_dir): shutil.rmtree(working_dir) if verbose: print("Done")