def classify_upload(): try: # We will save the file to disk for possible data collection. imagefile = flask.request.files['imagefile'] filename_ = str(datetime.datetime.now()).replace(' ', '_') + \ werkzeug.secure_filename(imagefile.filename) filename = os.path.join(UPLOAD_FOLDER, filename_) imagefile.save(filename) path, extension = os.path.splitext(filename) if extension == '.png': im = Image.open(filename) filename = "%s.jpg" % path im.save(filename) logging.info('Saving to %s.', filename) image = exifutil.open_oriented_im(filename) except Exception as err: logging.info('Uploaded image open error: %s', err) return flask.render_template('classify.html', has_result=True, result=(False, 'Cannot open uploaded image.')) names, time_cost, probs = app.clf.classify_image(filename) return flask.render_template( 'classify.html', has_result=True, result=[True, zip(names, probs), '%.3f' % time_cost], imagesrc=embed_image_html(image))
def classify_url(): imageurl = flask.request.args.get('imageurl', '') localfile = '/tmp/samplefile.png' try: bytes = request.urlopen(imageurl).read() #save to tmp,just for temp use for my classify api. tmpfile = open(localfile, 'wb') tmpfile.write(bytes) tmpfile.close() if sys.version_info.major == 2: string_buffer = StringIO.StringIO(bytes) else: string_buffer = BytesIO(bytes) image = exifutil.open_oriented_im(string_buffer) except Exception as err: # For any exception we encounter in reading the image, we will just # not continue. logging.info('URL Image open error: %s', err) return flask.render_template('detection.html', has_result=True, result=(False, 'Cannot open image from URL.')) app.logger.info('Image: %s', imageurl) image_np, time_cost = app.clf.detect_image(localfile) os.remove(localfile) return flask.render_template('detection.html', has_result=True, result=[True, '%.3f' % time_cost], imagesrc=embed_image_html(image))
def detection_url(): imageurl = flask.request.args.get('image_url', '') filename_ = str(datetime.datetime.now()).replace(' ', '_') + 'samplefile.png' localfile = os.path.join(UPLOAD_FOLDER, filename_) try: bytes = request.urlopen(imageurl).read() # save to tmp,just for temp use for my classify api. tmpfile = open(localfile, 'wb') tmpfile.write(bytes) tmpfile.close() if sys.version_info.major == 2: string_buffer = StringIO.StringIO(bytes) else: string_buffer = BytesIO(bytes) image = exifutil.open_oriented_im(localfile) except Exception as err: # For any exception we encounter in reading the image, we will just # not continue. logging.info('URL Image open error: %s', err) return flask.render_template('detection.html', imagesrc="", has_result=False, jsonstr='Cannot open image from URL.') app.logger.info('Image: %s', imageurl) # names, time_cost, probs = app.clf.classify_image(localfile) chn_idx = my_detector.detect_image(localfile) json_file = "/tmp/" + str(chn_idx) + ".json" image_shape = image.shape ids, cls, bboxs, scores, json_str = osd.parse_json_file( json_file, image_shape, label) osd.draw_labels(image, ids, cls, bboxs, scores, colors) os.remove(localfile) return flask.render_template('detection.html', imagesrc=embed_image_html(image), has_result=True, jsonstr=json_str)
def classify_upload(): try: # We will save the file to disk for possible data collection. imagefile = flask.request.files['image_file'] filename_ = str(datetime.datetime.now()).replace(' ', '_') + \ werkzeug.secure_filename(imagefile.filename) filename = os.path.join(UPLOAD_FOLDER, filename_) imagefile.save(filename) path, extension = os.path.splitext(filename) if extension == '.png': im = Image.open(filename) im = im.convert("RGB") os.remove(filename) filename = "%s.jpg" % path im.save(filename) logging.info('Saving to %s.', filename) image = exifutil.open_oriented_im(filename) except Exception as err: logging.info('Uploaded image open error: %s', err) return flask.render_template('detection.html', imagesrc="", has_result=False, jsonstr='Cannot open uploaded image.') # names,time_cost, probs = app.clf.classify_image(filename) chn_idx = my_detector.detect_image(filename) json_file = "/tmp/" + str(chn_idx) + ".json" image_shape = image.shape ids, cls, bboxs, scores, json_str = osd.parse_json_file( json_file, image_shape, label) osd.draw_labels(image, ids, cls, bboxs, scores, colors) os.remove(filename) return flask.render_template('detection.html', imagesrc=embed_image_html(image), has_result=True, jsonstr=json_str)
def detection_url(): imageurl = flask.request.args.get('image_url', '') filename_ = str(datetime.datetime.now()).replace(' ', '_') + 'samplefile.png' localfile = os.path.join(UPLOAD_FOLDER, filename_) try: bytes = request.urlopen(imageurl).read() # save to tmp,just for temp use for my classify api. tmpfile = open(localfile, 'wb') tmpfile.write(bytes) tmpfile.close(); if sys.version_info.major == 2: string_buffer = StringIO.StringIO(bytes) else: string_buffer = BytesIO(bytes) image = exifutil.open_oriented_im(localfile) except Exception as err: # For any exception we encounter in reading the image, we will just # not continue. logging.info('URL Image open error: %s', err) return flask.render_template( 'detection.html', imagesrc="", has_result=False, jsonstr='Cannot open image from URL.' ) app.logger.info('Image: %s', imageurl) if flask.request.args.get('web_type') == 'detection': my_detector = detector.Detector() config_path = "./detection_config.json" my_detector.build_pipeline_by_JSONFile(config_path) label = osd.parse_label(config_path) colors = osd.generate_colors(len(label)); chn_idx = my_detector.detect_image(localfile) json_file = "/tmp/" + str(chn_idx) + ".json"; image_shape = image.shape ids, cls, bboxs, scores, json_str = osd.parse_json_file(json_file, image_shape, label) osd.draw_labels(image, ids, cls, bboxs, scores, colors) os.remove(localfile) temp = flask.request.args.get('web_type') return flask.render_template( 'detection.html', imagesrc=embed_image_html(image, temp), has_result=True, jsonstr=json_str ) elif flask.request.args.get('web_type') == 'dehaze': my_detector = detector.Detector() config_path = "./dehaze_config.json" my_detector.build_pipeline_by_JSONFile(config_path) cchn_idx = my_detector.detect_image(localfile) dehaze_path = os.path.abspath( os.path.dirname(os.path.abspath(__file__)) + "/end.jpg") dehaze_image = cv.imread(dehaze_path) de_image = cv.cvtColor(dehaze_image,cv.COLOR_BGR2RGB) image_shape = image.shape temp = flask.request.args.get('web_type') return flask.render_template( 'detection.html', imagesrc=embed_image_html(de_image, temp), has_result=True, jsonstr="" ) elif flask.request.args.get('web_type') == 'style_transfer': my_detector = detector.Detector() config_path = "./style_config.json" my_detector.build_pipeline_by_JSONFile(config_path) cchn_idx = my_detector.detect_image(localfile) style_path = os.path.abspath( os.path.dirname(os.path.abspath(__file__)) + "/style.jpg") style_image = cv.imread(style_path) st_image = cv.cvtColor(style_image,cv.COLOR_BGR2RGB) image_shape = image.shape temp = flask.request.args.get('web_type') return flask.render_template( 'detection.html', imagesrc=embed_image_html(st_image, temp), has_result=True, jsonstr="" ) elif flask.request.args.get('web_type') == "SuperResolution": my_detector = detector.Detector() config_path = "./SuperResolution.json" my_detector.build_pipeline_by_JSONFile(config_path) cchn_idx = my_detector.detect_image(localfile) image_shape = image.shape result_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))+ "/output/result.png") image = exifutil.open_oriented_im(result_path) os.remove(localfile) temp = flask.request.args.get('web_type') return flask.render_template( 'detection.html', imagesrc=embed_image_html(image,temp), has_result=True, jsonstr="" )
def classify_upload(): try: # We will save the file to disk for possible data collection. imagefile = flask.request.files['image_file'] filename_ = str(datetime.datetime.now()).replace(' ', '_') + \ werkzeug.secure_filename(imagefile.filename) filename = os.path.join(UPLOAD_FOLDER, filename_) imagefile.save(filename) path, extension = os.path.splitext(filename) if extension == '.png': im = Image.open(filename) im = im.convert("RGB") os.remove(filename) filename = "%s.jpg" % path im.save(filename) logging.info('Saving to %s.', filename) image = exifutil.open_oriented_im(filename) except Exception as err: logging.info('Uploaded image open error: %s', err) return flask.render_template( 'detection.html', imagesrc="", has_result=False, jsonstr='Cannot open uploaded image.' ) if flask.request.form["web_type"] == 'dehaze': my_detector = detector.Detector() config_path = "./dehaze_config.json" my_detector.build_pipeline_by_JSONFile(config_path) chn_idx = my_detector.detect_image(filename) dehaze_path = os.path.abspath( os.path.dirname(os.path.abspath(__file__)) + "/end.jpg") dehaze_image = cv.imread(dehaze_path) de_image = cv.cvtColor(dehaze_image,cv.COLOR_BGR2RGB) image_shape = image.shape os.remove(filename) temp = flask.request.form["web_type"] return flask.render_template( 'detection.html', imagesrc=embed_image_html(de_image, temp), has_result=True, jsonstr="" ) elif flask.request.form["web_type"] == 'style_transfer': my_detector = detector.Detector() config_path = "./style_config.json" my_detector.build_pipeline_by_JSONFile(config_path) chn_idx = my_detector.detect_image(filename) style_path = os.path.abspath( os.path.dirname(os.path.abspath(__file__)) + "/style.jpg") style_image = cv.imread(style_path) st_image = cv.cvtColor(style_image,cv.COLOR_BGR2RGB) image_shape = image.shape os.remove(filename) temp = flask.request.form["web_type"] return flask.render_template( 'detection.html', imagesrc=embed_image_html(st_image, temp), has_result=True, jsonstr="" ) elif flask.request.args.get('web_type') == "SuperResolution": my_detector = detector.Detector() config_path = "./SuperResolution.json" my_detector.build_pipeline_by_JSONFile(config_path) cchn_idx = my_detector.detect_image(localfile) image_shape = image.shape result_path = os.path.abspath(os.path.dirname(os.path.abspath(__file__))+ "/output/result.png") image = exifutil.open_oriented_im(result_path) os.remove(filename) temp = flask.request.form["web_type"] return flask.render_template( 'detection.html', imagesrc=embed_image_html(image, temp), has_result=True, jsonstr="" ) elif flask.request.form["web_type"] == 'detection': my_detector = detector.Detector() config_path = "./detection_config.json" my_detector.build_pipeline_by_JSONFile(config_path) label = osd.parse_label(config_path) colors = osd.generate_colors(len(label)); chn_idx = my_detector.detect_image(filename) json_file = "/tmp/" + str(chn_idx) + ".json"; image_shape = image.shape ids, cls, bboxs, scores, json_str = osd.parse_json_file(json_file, image_shape, label) osd.draw_labels(image, ids, cls, bboxs, scores, colors) os.remove(filename) temp = flask.request.form["web_type"] return flask.render_template( 'detection.html', imagesrc=embed_image_html(image, temp), has_result=True, jsonstr=json_str )