def __init__( self, sketch_folder_path, sketch_index_file, \ image_gallery_folder_path, mapping_file_path, sub_sample=None \ ): self.sketches = [] self.images = [] self.sketch_class = [] self.image_class = [] self.random_flag = [] class_map = get_class_map(sketch_folder_path + "/" + mapping_file_path) structured_images = load_images(image_gallery_folder_path) index_file_path = sketch_folder_path + "/" + sketch_index_file with open(index_file_path, "r") as sketch_file: # Proceded to reduce the size of the source datasets to sub_sample due to hardware limitations if sub_sample is not None: sketch_lines = random.sample(sketch_file.readlines(), sub_sample) else: sketch_lines = sketch_file.readlines() for line in sketch_lines: sketch_path, sketch_idx = line.split() sketch_path = sketch_folder_path + "/" + sketch_path sketch_class = class_map[sketch_idx] self.sketches.append(get_processed_img(sketch_path)) self.sketch_class.append(int(sketch_idx)) random_flag = random.random() <= 0.5 if random_flag: random_image = sample(structured_images[sketch_class], 1)[0] image_path = f"{image_gallery_folder_path}/{sketch_class}/{random_image}" self.images.append(get_processed_img(image_path)) self.image_class.append(int(sketch_idx)) self.random_flag.append(1) else: while True: random_class_idx = str(random.randint(0, 249)) if random_class_idx != sketch_idx: random_class = class_map[random_class_idx] random_image = sample( structured_images[random_class], 1)[0] image_path = f"{image_gallery_folder_path}/{random_class}/{random_image}" self.images.append(get_processed_img(image_path)) self.image_class.append(int(random_class_idx)) self.random_flag.append(0) break
def __init__( self, sketch_folder_path, sketch_index_file, \ image_gallery_folder_path, mapping_file_path, use_triplets = False, sub_sample=None \ ): self.use_triplets = use_triplets self.sketches = [] self.classes = [] self.negative_classes = [] self.positive_images = [] if use_triplets: self.negative_images = [] class_map = get_class_map(sketch_folder_path + "/" + mapping_file_path) structured_images = load_images(image_gallery_folder_path) index_file_path = sketch_folder_path + "/" + sketch_index_file with open(index_file_path, "r") as sketch_file: # Proceded to reduce the size of the source datasets to sub_sample due to hardware limitations if sub_sample is not None: sketch_lines = random.sample(sketch_file.readlines(), sub_sample) else: sketch_lines = sketch_file.readlines() for line in sketch_lines: sketch_path, sketch_idx = line.split() sketch_path = sketch_folder_path + "/" + sketch_path sketch_class = class_map[sketch_idx] positive_random_image = sample(structured_images[sketch_class], 1)[0] positive_image_path = f"{image_gallery_folder_path}/{sketch_class}/{positive_random_image}" self.sketches.append(get_processed_img(sketch_path)) self.positive_images.append( get_processed_img(positive_image_path)) self.classes.append(int(sketch_idx)) if use_triplets: while True: negative_random_class_idx = str(random.randint(0, 249)) if negative_random_class_idx != sketch_idx: negative_class = class_map[ negative_random_class_idx] negative_random_image = sample( structured_images[negative_class], 1)[0] negative_image_path = f"{image_gallery_folder_path}/{negative_class}/{negative_random_image}" self.negative_classes.append( int(negative_random_class_idx)) self.negative_images.append( get_processed_img(negative_image_path)) break
import numpy as np import os import utils import config detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(config.mask_model_infer_path, mode='rb') as graph_file: serialized_graph = graph_file.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def) class_map = utils.get_class_map(config.class_map_file) sess = tf.Session(graph=detection_graph) input_ = sess.graph.get_tensor_by_name("import/image_tensor:0") boxes = sess.graph.get_tensor_by_name("import/detection_boxes:0") scores = sess.graph.get_tensor_by_name("import/detection_scores:0") classes = sess.graph.get_tensor_by_name("import/detection_classes:0") masks = sess.graph.get_tensor_by_name("import/detection_masks:0") count = 0 app = Flask(__name__) @app.route("/", methods=['POST'])
def index(): print('Request-form', list(request.form.keys()), file=sys.stderr) #print('Request-form-name',request.form['name'],file=sys.stderr) # print('Request-form-image',request.form['image'],file=sys.stderr) #image_name = request.form['name'] image_string = request.form['image'] #image_bytes = bytes(image_string,'utf-8') #image_decoded = base64.decodestring(image_string) image = Image.open(BytesIO(base64.b64decode(image_string))) # rotated_image = image.rotate(270,expand=True) # input_array = np.array(rotated_image) input_array = np.array(image) input_array = np.expand_dims(input_array, axis=0) detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(config.mask_model_infer_path, mode='rb') as graph_file: serialized_graph = graph_file.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def) class_map = utils.get_class_map(config.class_map_file) with tf.Session(graph=detection_graph) as sess: input_ = sess.graph.get_tensor_by_name("import/image_tensor:0") boxes = sess.graph.get_tensor_by_name("import/detection_boxes:0") scores = sess.graph.get_tensor_by_name("import/detection_scores:0") classes = sess.graph.get_tensor_by_name("import/detection_classes:0") masks = sess.graph.get_tensor_by_name("import/detection_masks:0") #result_array = detect.run(input_array) (_boxes, _scores, _classes, _masks) = sess.run([boxes, scores, classes, masks], feed_dict={input_: input_array}) _boxes = np.squeeze(_boxes, axis=0) _scores = np.squeeze(_scores, axis=0) _classes = np.squeeze(_classes, axis=0) _masks = np.squeeze(_masks, axis=0) input_array = np.squeeze(input_array, axis=0) detections = utils.get_detections(_scores, config.threshold_score) utils.draw_bounding_box(input_array, detections, _boxes, _classes, class_map, _masks) result_image = Image.fromarray(input_array) #print('rotated_image.shape = ',input_array.shape) #result_image.save('output.jpg',format='JPEG') #convert image back to string.. buffered = BytesIO() result_image.save(buffered, format="JPEG") final_img_str = base64.b64encode(buffered.getvalue()) # print('Request-files:',request.files,file=sys.stderr) # print('Requestfiletype:',type(request.files),file=sys.stderr) # data = request.files.to_dict() # print('data',data,file=sys.stderr) # #to-do Input file validation... (ensure input file is valid jpg or png) # file = data['upload'] # print('File name:',file.filename,file=sys.stderr) # file_path = os.path.join("Images",file.filename) # file.save(file_path) # print('File saved with name:',file.filename,file=sys.stderr) #Deserialize the image.. # with open(image_name,'wb') as image_file: # image_file.write(image) response = final_img_str # print("Returning Image Response...",file=sys.stderr) # tf.reset_defualt_graph(); return response
ap.add_argument("-c", "--confidence", type=float, default=0.25, help="minimum probability to filter weak detections") ap.add_argument("-t", "--threshold", type=float, default=0.25, help="threshold when applying non-maxima suppression") args = vars(ap.parse_args()) size = 1.5 image_size = (1280, 720) map_class = get_class_map() class_map = {} for key in map_class.keys(): class_map[map_class[key]] = key # Load models using opencv dnn module yoloVehicle = YoloModel(args["vehicle_model"], confidenceTresh=args['confidence'], nmsTresh=args['threshold']) yoloPlate = YoloModel(args["plate_model"], confidenceTresh=args['confidence'], nmsTresh=args['threshold']) yoloChar = YoloModel(args["char_model"], confidenceTresh=args['confidence'], nmsTresh=args['threshold'])